C language push box game implementation code

  • 2020-06-12 10:02:36
  • OfStack

The rules of the game: the little man on the street pushes the box until it reaches its destination.

Thinking analysis:

The movement of the man and the box is the exchange of the man or the box and the road;

1 Defines a 2-dimensional character array to store the map

2 Display the map and prompt the gameplay

3. Record the positions of SIMS and boxes, and define character variables to receive user input direction

4 loop judgment statement

1). Whether the next step of the villain is a road, if it is, move and record the new position information of the villain

2). If the next step of the person is not a road, judge whether it is a box; if it is a box, judge whether the next position of the box is a road; if it is a road, move the box and the person

3) refresh the map

4). Determine the position of the box. If it is in the specified position, the game will end;

Here is the implementation code:


#include <stdio.h>
// Swap character array elements 
void swapPosition(char ch[][11],int oldX,int oldY,int newX,int newY)
{
 char temp;
 temp = *(*(ch + oldX) + oldY);
 *(*(ch + oldX) + oldY) = *(*(ch + newX) + newY);
 *(*(ch + newX) + newY) = temp;
 
}
// Print the array 
void printArray(char (*ch)[11])
{
 for(int i = 0;i < 10;i++)
 {
  for(int j = 0;j < 10;j++)
  {
   printf("%c\t",*(*(ch + i) + j));
  }
  printf("\n");
 }
}
int main(int argc, char* argv[])
{
 // Define an array 
 char ch[10][11] = {
  {'#','#','#','#','#','#','#','#','#','#','\0'},
  {'#','#','#',' ',' ','#','#','#','#','#','\0'},
  {'#','0','X',' ',' ','#','#','#','#','#','\0'},
  {'#','#','#','#',' ','#','#','#','#','#','\0'},
  {'#','#','#',' ',' ',' ',' ','#','#','#','\0'},
  {'#','#','#',' ',' ',' ',' ',' ','#','#','\0'},
  {'#','#','#','#','#',' ',' ',' ','#','#','\0'},
  {'#','#','#','#','#',' ',' ',' ',' ',' ','\0'},
  {'#','#','#','#','#','#','#','#',' ',' ','\0'},
  {'#','#','#','#','#','#','#','#','#','#','\0'}
 };
 // Print the array 
 printArray(ch);
 // Record the position of the person and the box 
 // Defines the flag variable for the path street
 int personX = 2,personY = 1,boxX = 2, boxY = 2;
 char street = ' ';
 // Prompt the user for direction of movement 
 printf(" Please enter the movement direction of the villain :( w- On, s- Next, a- Left, d- Right) \n");
 // Define the direction in which user input is recorded 
 char direction;
 // Define the loop statement by direction 
 while(1)
 {
  scanf("%c",&direction);
  getchar();// Receives keyboard return characters 
  switch(direction)
  {
   case 'w':
   case 'W':
    if(*(*(ch+personX-1)+personY) == street)
    {
     swapPosition(ch,personX,personY,personX - 1,personY );
     personX--;
     
    }
    else if(*(*(ch+personX-1)+personY) == *(*(ch+boxX)+boxY))
    {
     if(*(*(ch+boxX-1)+boxY) == street)
     {
      swapPosition(ch,boxX,boxY,boxX - 1,boxY);
      boxX--;
      swapPosition(ch,personX,personY,personX - 1,personY);
      personX--;
     }
    }
  
    break;
   case 's':
   case 'S':
    if(*(*(ch+personX + 1) + personY) == street)
    {
     swapPosition(ch,personX,personY,personX + 1,personY );
     personX++;
     
    }
    else if(*(*(ch+personX+1)+personY) == *(*(ch+boxX)+boxY))
    {
     if(*(*(ch+boxX+1)+boxY) == street)
     {
      swapPosition(ch,boxX,boxY,boxX + 1,boxY);
      boxX++;
      swapPosition(ch,personX,personY,personX + 1,personY);
      personX++;
     }
    }
    break;
   case 'a':
   case 'A':
    if(*(*(ch+personX)+personY - 1) == street)
    {
     swapPosition(ch,personX,personY,personX ,personY - 1 );
     personY--;
     
    }
    else if(*(*(ch+personX)+personY - 1) == *(*(ch+boxX)+boxY))
    {
     if(*(*(ch+boxX)+boxY - 1) == street)
     {
      swapPosition(ch,boxX,boxY,boxX,boxY - 1);
      boxY--;
      swapPosition(ch,personX,personY,personX,personY - 1);
      personY--;
     }
    }
    break;
   case 'd':
   case 'D':
    
    if(*(*(ch+personX)+personY + 1) == street)
    {
     swapPosition(ch,personX,personY,personX ,personY + 1 );
     personY++;
     
    }
    else if(*(*(ch+personX)+personY + 1) == *(*(ch+boxX)+boxY))
    {
     if(*(*(ch+boxX)+boxY + 1) == street)
     {
      swapPosition(ch,boxX,boxY,boxX,boxY + 1);
      boxY++;
      swapPosition(ch,personX,personY,personX,personY + 1);
      personY++;
     }
    }
    break;
    case 'q':
    case 'Q':
    return 0;
   
  }
  printArray(ch);
  // End of definition flag 
  if (boxY == 9) {
   printf(" Congratulations, game pass! \n");
   return 0;
  }
 }
 
 return 0;
}

Related articles: