C++ to achieve the push box

  • 2020-11-25 07:25:40
  • OfStack

Example of this article for you to share C++ implementation of the box push game specific code, for your reference, the specific content is as follows


// 1.cpp :  This file contains  "main"  Function. This is where program execution begins and ends. 
//

#include <iostream>
#include <stdio.h>// s 
#include <cstdlib>
#include <conio.h>
using std::cout;
const int row = 12;
const int col = 13;
int x = 1;
int y = 11;
int map[row][col] = {
 {2,2,2,2,2,2,2,2,2,2,2,2,2},
 {2,0,0,0,0,0,0,0,0,0,0,5,2},
 {2,0,0,0,0,0,0,0,0,0,3,0,2},
 {2,0,0,0,0,0,0,0,0,0,0,0,2},
 {2,0,0,0,0,0,0,0,1,0,0,0,2},
 {2,0,0,0,0,0,0,0,0,0,0,0,2},
 {2,0,0,0,0,0,0,0,0,0,0,0,2},
 {2,0,0,0,0,0,0,0,0,0,0,0,2},
 {2,0,0,0,0,0,0,0,0,0,0,0,2},
 {2,0,0,0,0,0,0,0,0,0,0,0,2},
 {2,0,0,0,0,0,0,0,0,0,0,0,2},
 {2,2,2,2,2,2,2,2,2,2,2,2,2},
};

void draw() {
 int i;
 for (i = 0; i < 12; i++)
 {
  for (int j = 0; j < 13; j++)
  {
   switch (map[i][j])
   {
   case 0:
   {
    cout << " ";//0 Stands for space that can be walked 
    break;
   }
   case 1:
   {
    cout << " u ";//1 P is for box, I'll just replace it with  u  
    break;
   }
   case 2:
   {
    cout << " s ";//2 It represents a wall that cannot be crossed 
    break;
   }
   case 3:
   {
    cout << " Do things ";
    break;
   }

   case 5:
   {
    cout << " ♀ ";//5 It's a person. It's mobile 
    break;
   }
   }
  }
  cout << "\n";
 }
}

void move(int _x, int _y) {
 if (map[x + _x][y + _y] == 2 || map[x + _x][y + _y] == 3 ||
  map[x + _x][y + _y] == 5) {// Judge the boundary man can't walk out of the wall 
  return;
 }
 if (map[x + _x][y + _y] == 1) {
  if (map[x + 2 * _x][y + 2 * _y] == 2) {
   return;
  }
  map[x + _x][y + _y] = 0;
  map[x + 2 * _x][y + 2 * _y] = 1;
  move(_x, _y);
  return;
 }
 map[x][y] = 0;
 x += _x;
 y += _y;
 map[x][y] = 5;
 draw();

}
void step(char o) {
 switch (o) {
 case 'w': {// Just like cf . cs In that way, wsad It's up, down, left, right 
  
  move(-1, 0);
  break;
 }
 case 's': {
  move(1, 0);
  break;
 }
 case 'd': {
  move(0, 1);
  break;
 }
 case 'a': {
  move(0, -1);
  break;
 }
 }
}

int main()
{
 while (true) {  
  draw();// According to the 2 Dimension array values to draw 
  step(_getch());// Get screen input directly 
  system("cls");// Clear the screen for animation 
 }
}

Again for everyone to share 1 implementation code: thank the original author to share

Pushing box is a classic c + + title, use w a, s, d move, the effect from


#include <stdio.h>  
#include <stdlib.h> 
#include <conio.h>  
int i,j; 
void draw_map(int map[10][12]); // Declares the drawing function 
int main()
{
 system("title  Li Baiheng ");
 char input; 
 int count=0; // Define the score variable 
 int map[10][12] = {{2,2,2,2,2,1,1,1,1,1,2,2},
 {1,1,1,1,2,1,0,0,0,1,1,2},
 {1,0,0,1,1,1,0,1,0,0,1,2},
 {1,0,4,3,3,3,3,3,1,0,1,1},
 {1,0,0,1,1,3,3,3,4,0,0,1},
 {1,0,0,0,0,4,1,1,4,1,0,1},
 {1,0,4,1,4,0,0,0,4,0,0,1},
 {1,1,0,6,0,1,1,1,4,1,0,1},
 {2,1,1,1,1,1,2,1,0,0,0,1},
 {2,2,2,2,2,2,2,1,1,1,1,1}
 };
 while (1) // Loop endlessly, waiting for user commands 
 {
 system("CLS");
 for (i=0;i<12;i++)
 {
 printf("%d",i);
 }
 printf("\n");
 /*for (i=1;i<10;i++)
 {
 printf("%d\n",i);
 }*/
 printf("\n");
 draw_map(map);
 printf(" Current score: %d\n",count);
 // Find the initial position 
 for (i=0;i<10;i++)
 {
 for (j=0;j<12;j++)
 {
 if (map[i][j]==6||map[i][j]==9)
  break;
 }
 if (map[i][j]==6||map[i][j]==9)
 break;
 }
 printf(" Your current coordinates ( %d . %d ) ",i,j);  
 input = getch(); // with getch() The function takes user input without carriage return confirmation and controls the direction of travel. 
 switch (input)
 {
 case 'w':
 // If the person is in front of the open space.  //0 On behalf of the clearing  6 representative  //3 Representative destination 
 if(map[i-1][j]==0)
 {
 map[i-1][j]=6+0; // People walk 1 Step, ID human ID () plus open space ID (). 
 if(map[i][j]==9) // If the current location is the destination, then ID Of or relating to a person ID () plus destination ID ()). 
  map[i][j]=3; // He will go forward 1 After the step is in place ID Change to open space ID (). 
 else
  map[i][j]=0; // Otherwise the in situ ID Change to open space ID  .   
 }
 // If the person in front is the destination. 
 else if((map[i-1][j]==3)||(map[i-1][j]==9))
 {
 map[i-1][j]=6+3; // People walk 1 Step, ID a ID+ destination ID=9 . 
 if(map[i][j]==9) // If the place is also the destination ( ID For). 
  map[i][j]=3; // People leave the place behind ID Modify back to destination ID . 
 else
  map[i][j]=0; // Otherwise the in situ ID Change to open space ID
 }
 // If the person is in front of the box. //4 On behalf of the box  //7 Box entry destination 
 else if(map[i-1][j]==4)
 {
 // If the person in front is a box, and the box in front is an empty space. 
 if (map[i-2][j]==0)
 { 
  map[i-2][j]=4; // The man pushed the box forward 1 Step, put the open space ID Change to box ID (a) 
  // Here is the judgement of the box in situ 
  if(map[i-1][j]==7) // If the box is in place as the destination. 
  map[i-1][j]=9; // People stand where the box is (destination) ID Ought to be human ID+ destination ID=9 . 
  else
  map[i-1][j]=6; // Otherwise, people are standing in the empty space, ID Should be +0=6 . 
  // Here's a look at where people are 
  if(map[i][j]==9) // If it was the destination before. 
  map[i][j]=3; // After the person leaves, modify to return to the destination ID . 
  else
  map[i][j]=0; // Otherwise, it's empty space. 
 }
 // If the front of the person is the box and the front of the box is the destination. 
 else if (map[i-2][j]==3)
 { 
  map[i-2][j]=7; //ID Be the destination of ID (a) + The box ID (a) =7 "Has pushed the box to its destination. 
  count++;
  // The following is the judgment of the original position of the box, ibid. 
  if(map[i-1][j]==7)
  map[i-1][j]=9;
  else
  map[i-1][j]=6;
  // The following is to judge the original position of the person, ditto. 
  if(map[i][j]==9)
  map[i][j]=3;
  else
  map[i][j]=0;
 }
 }
 // If the person is in front of a box that has entered a destination ( ID=7 ). 
 else if(map[i-1][j]==7)
 {
 // If the person is in front of a box that has entered a destination , And the front of the box is empty space. 
 if(map[i-2][j]==0)
 {
  count--;
  map[i-2][j]=4; // Push the box back into the open space, ID= The box ID+ clearing ID=4 . 
  map[i-1][j]=9; // People naturally stand on the original purpose of the ground. 
  // The following is the judgment of the original people, the same as above. 
  if(map[i][j]==9) 
  map[i][j]=3;
  else
  map[i][j]=0;
 }
 // If the person is in front of a box that has entered a destination, and the box is in front of another box 1 The destination. 
 if(map[i-2][j]==3)
 {
  map[i-2][j]=7; // Pushed the box into the other 1 Destination, nature, ID Should also be. 
  map[i-1][j]=9; // Man stood on the spot. 
  // The following is the judgment of the original standing ground, the method is the same as above. 
  if(map[i][j]==9) 
  map[i][j]=3;
  else
  map[i][j]=0;
 }
 }
 break;
 case 's':
 // If the person is in front of the open space. 
 if(map[i+1][j]==0)
 {
 map[i+1][j]=6+0; // People walk 1 Step, ID human ID () plus open space ID (). 
 if(map[i][j]==9) // If the current location is the destination, then ID Of or relating to a person ID () plus destination ID ()). 
  map[i][j]=3; // He will go forward 1 After the step is in place ID Change to open space ID (). 
 else
  map[i][j]=0; // Otherwise the in situ ID Change to open space ID  .   
 }
 // If the person in front is the destination. 
 else if(map[i+1][j]==3)
 {
 map[i+1][j]=6+3; // People walk 1 Step, ID a ID+ destination ID=9 . 
 if(map[i][j]==9) // If the place is also the destination ( ID For). 
  map[i][j]=3; // People leave the place behind ID Modify back to destination ID . 
 else
  map[i][j]=0; // Otherwise the in situ ID Change to open space ID
 }
 // If the person is in front of the box. 
 else if(map[i+1][j]==4)
 {
 // If the person in front is a box, and the box in front is an empty space. 
 if (map[i+2][j]==0)
 { 
  map[i+2][j]=4; // The man pushed the box forward 1 Step, put the open space ID Change to box ID (a) 
  // Here is the judgement of the box in situ 
  if(map[i+1][j]==7) // If the box is in place as the destination. 
  map[i+1][j]=9; // People stand where the box is (destination) ID Ought to be human ID+ destination ID=9 . 
  else
  map[i+1][j]=6; // Otherwise, people are standing in the empty space, ID Should be +0=6 . 
  // Here's a look at where people are 
  if(map[i][j]==9) // If it was the destination before. 
  map[i][j]=3; // After the person leaves, modify to return to the destination ID . 
  else
  map[i][j]=0; // Otherwise, it's empty space. 
 }
 // If the front of the person is the box and the front of the box is the destination. 
 else if (map[i+2][j]==3)
 { 
  map[i-2][j]=7; //ID Be the destination of ID (a) + The box ID (a) =7 "Has pushed the box to its destination. 
  count++;
  // The following is the judgment of the original position of the box, ibid. 
  if(map[i+1][j]==7)
  map[i+1][j]=9;
  else
  map[i+1][j]=6;
  // The following is to judge the original position of the person, ditto. 
  if(map[i][j]==9)
  map[i][j]=3;
  else
  map[i][j]=0;
 }
 }
 // If the person is in front of a box that has entered a destination ( ID=7 ). 
 else if(map[i+1][j]==7)
 {
 // If the person is in front of a box that has entered a destination , And the front of the box is empty space. 
 if(map[i+2][j]==0)
 {
  count--;
  map[i+2][j]=4; // Push the box back into the open space, ID= The box ID+ clearing ID=4 . 
  map[i+1][j]=9; // People naturally stand on the original purpose of the ground. 
  // The following is the judgment of the original people, the same as above. 
  if(map[i][j]==9) 
  map[i][j]=3;
  else
  map[i][j]=0;
 }
 // If the person is in front of a box that has entered a destination, and the box is in front of another box 1 The destination. 
 if(map[i+2][j]==3)
 {
  map[i+2][j]=7; // Pushed the box into the other 1 Destination, nature, ID Should also be. 
  map[i+1][j]=9; // Man stood on the spot. 
  // The following is the judgment of the original standing ground, the method is the same as above. 
  if(map[i][j]==9) 
  map[i][j]=3;
  else
  map[i][j]=0;
 }
 }
 break;
 case 'a':
 // If the person is in front of the open space. 
 if(map[i][j-1]==0)
 {
 map[i][j-1]=6+0; // People walk 1 Step, ID human ID () plus open space ID (). 
 if(map[i][j]==9) // If the current location is the destination, then ID Of or relating to a person ID () plus destination ID ()). 
  map[i][j]=3; // He will go forward 1 After the step is in place ID Change to open space ID (). 
 else
  map[i][j]=0; // Otherwise the in situ ID Change to open space ID  .   
 }
 // If the person in front is the destination. 
 else if(map[i][j-1]==3)
 {
 map[i][j-1]=6+3; // People walk 1 Step, ID a ID+ destination ID=9 . 
 if(map[i][j]==9) // If the place is also the destination ( ID For). 
  map[i][j]=3; // People leave the place behind ID Modify back to destination ID . 
 else
  map[i][j]=0; // Otherwise the in situ ID Change to open space ID
 }
 // If the person is in front of the box. 
 else if(map[i][j-1]==4)
 {
 // If the person in front is a box, and the box in front is an empty space. 
 if (map[i][j-2]==0)
 { 
  map[i][j-2]=4; // The man pushed the box forward 1 Step, put the open space ID Change to box ID (a) 
  // Here is the judgement of the box in situ 
  if(map[i][j-1]==7) // If the box is in place as the destination. 
  map[i][j-1]=9; // People stand where the box is (destination) ID Ought to be human ID+ destination ID=9 . 
  else
  map[i][j-1]=6; // Otherwise, people are standing in the empty space, ID Should be +0=6 . 
  // Here's a look at where people are 
  if(map[i][j]==9) // If it was the destination before. 
  map[i][j]=3; // After the person leaves, modify to return to the destination ID . 
  else
  map[i][j]=0; // Otherwise, it's empty space. 
 }
 // If the front of the person is the box and the front of the box is the destination. 
 else if (map[i][j-2]==3)
 { 
  count++;
  map[i][j-2]=7; //ID Be the destination of ID (a) + The box ID (a) =7 "Has pushed the box to its destination. 
  // The following is the judgment of the original position of the box, ibid. 
  if(map[i][j-1]==7)
  map[i][j-1]=9;
  else
  map[i][j-1]=6;
  // The following is to judge the original position of the person, ditto. 
  if(map[i][j]==9)
  map[i][j]=3;
  else
  map[i][j]=0;
 }
 }
 // If the person is in front of a box that has entered a destination ( ID=7 ). 
 else if(map[i][j-1]==7)
 {
 // If the person is in front of a box that has entered a destination , And the front of the box is empty space. 
 if(map[i][j-2]==0)
 {
  count--;
  map[i][j-2]=4; // Push the box back into the open space, ID= The box ID+ clearing ID=4 . 
  map[i][j-1]=9; // People naturally stand on the original purpose of the ground. 
  // The following is the judgment of the original people, the same as above. 
  if(map[i][j]==9) 
  map[i][j]=3;
  else
  map[i][j]=0;
 }
 // If the person is in front of a box that has entered a destination, and the box is in front of another box 1 The destination. 
 if(map[i][j-2]==3)
 {
  map[i][j-2]=7; // Pushed the box into the other 1 Destination, nature, ID Should also be. 
  map[i][j-1]=9; // Man stood on the spot. 
  // The following is the judgment of the original standing ground, the method is the same as above. 
  if(map[i][j]==9) 
  map[i][j]=3;
  else
  map[i][j]=0;
 }
 }
 break;
 case 'd':
 // If the person is in front of the open space. 
 if(map[i][j+1]==0)
 {
 map[i][j+1]=6+0; // People walk 1 Step, ID human ID () plus open space ID (). 
 if(map[i][j]==9) // If the current location is the destination, then ID Of or relating to a person ID () plus destination ID ()). 
  map[i][j]=3; // He will go forward 1 After the step is in place ID Change to open space ID (). 
 else
  map[i][j]=0; // Otherwise the in situ ID Change to open space ID  .   
 }
 // If the person in front is the destination. 
 else if(map[i][j+1]==3)
 {
 map[i][j+1]=6+3; // People walk 1 Step, ID a ID+ destination ID=9 . 
 if(map[i][j]==9) // If the place is also the destination ( ID For). 
  map[i][j]=3; // People leave the place behind ID Modify back to destination ID . 
 else
  map[i][j]=0; // Otherwise the in situ ID Change to open space ID
 }
 // If the person is in front of the box. 
 else if(map[i][j+1]==4)
 {
 // If the person in front is a box, and the box in front is an empty space. 
 if (map[i][j+2]==0)
 { 
  map[i][j+2]=4; // The man pushed the box forward 1 Step, put the open space ID Change to box ID (a) 
  // Here is the judgement of the box in situ 
  if(map[i][j+1]==7) // If the box is in place as the destination. 
  map[i][j+1]=9; // People stand where the box is (destination) ID Ought to be human ID+ destination ID=9 . 
  else
  map[i][j+1]=6; // Otherwise, people are standing in the empty space, ID Should be +0=6 . 
  // Here's a look at where people are 
  if(map[i][j]==9) // If it was the destination before. 
  map[i][j]=3; // After the person leaves, modify to return to the destination ID . 
  else
  map[i][j]=0; // Otherwise, it's empty space. 
 }
 // If the front of the person is the box and the front of the box is the destination. 
 else if (map[i][j+2]==3)
 { 
  count++;
  map[i][j+2]=7; //ID Be the destination of ID (a) + The box ID (a) =7 "Has pushed the box to its destination. 
  // The following is the judgment of the original position of the box, ibid. 
  if(map[i][j+1]==7)
  map[i][j+1]=9;
  else
  map[i][j+1]=6;
  // The following is to judge the original position of the person, ditto. 
  if(map[i][j]==9)
  map[i][j]=3;
  else
  map[i][j]=0;
 }
 }
 // If the person is in front of a box that has entered a destination ( ID=7 ). 
 else if(map[i][j+1]==7)
 {
 // If the person is in front of a box that has entered a destination , And the front of the box is empty space. 
 if(map[i][j+2]==0)
 {
  count--;
  map[i][j+2]=4; // Push the box back into the open space, ID= The box ID+ clearing ID=4 . 
  map[i][j+1]=9; // People naturally stand on the original purpose of the ground. 
  // The following is the judgment of the original people, the same as above. 
  if(map[i][j]==9) 
  map[i][j]=3;
  else
  map[i][j]=0;
 }
 // If the person is in front of a box that has entered a destination, and the box is in front of another box 1 The destination. 
 if(map[i][j+2]==3)
 {
  map[i][j+2]=7; // Pushed the box into the other 1 Destination, nature, ID Should also be. 
  map[i][j+1]=9; // Man stood on the spot. 
  // The following is the judgment of the original standing ground, the method is the same as above. 
  if(map[i][j]==9) 
  map[i][j]=3;
  else
  map[i][j]=0;
 }
 }
 break;
 }
 if(count==8) // If the score reaches a score 
 {
 system("CLS"); // Clear the screen 
 draw_map(map); 
 break; // Exit loop 
 }
 }
 printf("\n Congratulations, you passed!! \n"); // With prompt 
 return 0;
}
void draw_map(int map[10][12])
{
 
 for(i=0;i<10;i++)
 {
 for(j=0;j<12;j++)
 {
 switch(map[i][j])
 {
 case 0:
 printf(" "); // The number is the road 
 break;
 case 1:
 printf("#"); // The Numbers are the walls 
 break;
 case 2:
 printf(" "); // The Numbers are the margins of the borders of the game 
 break;
 case 3:
 printf("!"); // The number represents the destination 
 break;
 case 4:
 printf("*"); // The number is the box 
 break;
 case 7:
 printf("$"); // The number represents the box entering the destination 
 break;
 case 6:
 printf("@"); // Digital representative 
 break;
 case 9:
 printf("@"); // The number represents the entry destination 
 break;
 }   
 }
 printf("\n"); // The branch output 
 }
}

More interesting classic games to realize the special topic, to share with you:

C++ classic game summary

python classic game summary

python Tetris game collection

JavaScript classic games don't stop playing

java classic game summary

javascript classic game summary


Related articles: