<>C Language to achieve the complete code of push box game


I made it on my own. , Maybe some of the code is not neat enough , Or some small problems , But the basic operation of the game can be achieved

<> Code effect

The code is divided into 8 Three parts ,4 Two controls move up, down, left and right ,2 It's a matter of judgment ,1 Number of Statistics , A drawing .

Set up map manually

use ’0’ Represents a space ,“1” Represents a wall ,“2” Denotes a box ,“3” Signifier ,“4” Indicates the end point

This can improve the portability of the code

If you need to input the map manually, you can define a two-dimensional array directly , Just assign a value to him
int screen[9][11]={ {0,1,1,1,1,1,1,1,1,0,0}, {0,1,0,0,0,1,0,0,0,1,0}, {0,1,0,2,
2,2,2,2,0,1,0}, {0,1,0,2,0,2,0,2,0,1,1}, {0,1,0,0,0,3,0,0,2,0,1}, {1,1,0,1,1,1,1
,0,2,0,1}, {1,0,4,4,4,4,4,1,0,0,1}, {1,0,4,4,4,4,4,0,0,1,1}, {1,1,1,1,1,1,1,1,1,
1,0} };// Defined as a global variable ( Map ) i Express Line ,j Represents a column
Calculate the number of endpoints in the map

This step is mainly for judging whether the game will win or lose
int cum(){ int i,j,k=0; for(i=0;i<9;i++){ for(j=0;j<11;j++){ if(screen[i][j]==2
){ k++; } } }// Traverses the entire two-dimensional array return k; }// How many endpoints are there in the map
Print map function

adopt switch Function to visualize the values in a two-dimensional array , That is to draw a map

be careful : It's also defined here 6 and 7, It's calculated by overlapping relationships , It's the box at the end , This position has boxes and ends 2 Logo , So let's add up the two values , Easy to understand , It is also convenient for later calculation
void print(){ int i,j; printf(" Please use it wsad Representatives play the game up and down, left and right \n"); for(i=0;i<9;i++){ for(j=0
;j<11;j++){ switch(screen[i][j]){ case 0: printf(" ");// empty break; case 1: printf(
"■");// wall break; case 2: printf("★");// case break; case 3: printf("♀");// people break;
case 4: printf("○");// End break; case 6: printf("★"); break;// Box and destination case 7://
People and terminal display people printf("♀"); break; } } printf("\n"); } }
Judge the game

I wrote here 2 Functions , A judge wins , A judge loses , And return the value , Then in the last side of the main function, the game is determined by judging the return value

Judge to win
int win(){ int i,j,k=0; int t=0; for(i=0;i<9;i++){ for(j=0;j<11;j++){ if(screen
[i][j]==6){ k++; } } }// Traverses the entire two-dimensional array , Calculate the number of boxes at the end if(k==cum()){ t=1; }
// If the number of endpoints is equal to the number of endpoints calculated previously , It means that all the terminals are packed with boxes , Explain the victory of the game return t; } // Judge to win
Judge lose
int lose(){ int i,j; int k=0; for(i=0;i<9;i++){ for(j=0;j<11;j++){ if(i>0 && j>
0 ){ if(screen[i][j] == 2 || screen[i][j] == 6){ if(((screen[i-1][j] == 1 ||
screen[i-1][j] == 2 || screen[i-1][j] == 6) && (screen[i][j-1] == 1 || screen[i]
[j-1] == 2 || screen[i][j-1] == 6)) || ((screen[i][j-1] == 1 || screen[i][j-1]
== 2 || screen[i][j-1] == 6) && (screen[i+1][j] == 1 || screen[i+1][j] == 2 ||
screen[i+1][j] == 6)) || ((screen[i+1][j] == 1 || screen[i+1][j] == 2 || screen[
i+1][j] == 6) && (screen[i][j+1] == 1 || screen[i][j+1] == 2 || screen[i][j+1]
== 6)) || ((screen[i][j+1] == 1 || screen[i][j+1] == 2 || screen[i][j+1] == 6)
&& (screen[i-1][j] == 1 || screen[i-1][j] == 2 || screen[i-1][j] == 6))){ k++; }
} } }/* This is also traversing the entire array , Judge the situation of all boxes in four directions , If three directions are blocked, the box can't move , It also indicates that the box is invalid ,
use k To record the number of failures , When all fail, the game fails ( This is how the game is played , In fact, if one is blocked, it is impossible to win )*/ } if(k==cum()){ k=1; }
return k;// return 1 The game failed }
Next are the four most important control functions

Move up

Through the change of the number to control the change of the two-dimensional array , And then control the updating of the map

It's very important to understand : plus 1, plus 2, plus 3 reduce 3 What do you mean

plus 1: What is the value of the box 2, What is the value of people 3, So the position of the box becomes that people need to add 1 To achieve

plus 2: What is the value of open space 0, What is the value of the box 2, What is the value of the box with the destination 6, So when you push the box , The value will be increased after the box is placed in the front space or at the end 2

plus 3 reduce 3: What is the value of people 3, If people want to move , It will be in the original grid because the person left, resulting in a numerical reduction 3, Go to the grid will be because of the stand and add people 3

If this is understood , The code is very simple
void movew(){ if(x>0){ if(screen[x-1][y]==1){ return ;/* If there's a wall on top of the box , The map will not change , because
I can't push it */ }else if(screen[x-1][y]==0){ screen[x-1][y]+=3; screen[x][y]-=3; x--;
/* If there's an open space ahead , You need to move one space forward , That's where the original person was Become a clearing , The open space ahead becomes human , Open space (0) Become human (3) Need to add 3, People need to be reduced when they become empty space 3*/ }else
if(screen[x-1][y]==4){ screen[x-1][y]+=3; screen[x][y]-=3; x--; }// same else if(
screen[x-1][y]==2||screen[x-1][y]==6){ if(screen[x-2][y]==0){ screen[x-2][y]+=2;
// The grid in front of the box becomes a box (2) screen[x-1][y]+=1;// The position of the box becomes human (3) screen[x][y]-=3;
/* If there's an open space ahead , You need to move forward One frame , In other words, the original box lattice becomes a person , The position of people becomes empty land , The original space became a box , case (2) Become human (3) Need to reduce 3, Open space becomes human */
x--; }else if(screen[x-2][y]==1){ return ; }else if(screen[x-2][y]==2){ return;
// If the front of the box is a wall or other box , The box can't be pushed }else if(screen[x-2][y]==4){ screen[x-2][y]+=2; screen
[x-1][y]+=1; screen[x][y]-=3; x--; }// Don't leave out this situation } } }
The code ideas of the other three directions are the same as this one

Move down
void moves(){ if(x<9){ if(screen[x+1][y]==1){ return ; }else if(screen[x+1][y]
==0){ screen[x+1][y]+=3; screen[x][y]-=3; x++; }else if(screen[x+1][y]==4){
screen[x+1][y]+=3; screen[x][y]-=3; x++; } else if(screen[x+1][y]==2||screen[x+1
][y]==6){ if(screen[x+2][y]==1){ return; }else if(screen[x+2][y]==0){ screen[x+2
][y]+=2; screen[x+1][y]+=1; screen[x][y]-=3; x++; }else if(screen[x+2][y]==2){
return ; }else if(screen[x+2][y]==4){ screen[x+2][y]+=2; screen[x+1][y]+=1;
screen[x][y]-=3; x++; } } } }
Move left
void movea(){ if(y>0){ if(screen[x][y-1]==1){ return; }else if(screen[x][y-1]==
4){ screen[x][y-1]+=3; screen[x][y]-=3; y--; } else if(screen[x][y-1]==0){
screen[x][y-1]+=3; screen[x][y]-=3; y--; }else if(screen[x][y-1]==2||screen[x][y
-1]==6){ if(screen[x][y-2]==0){ screen[x][y-2]+=2; screen[x][y-1]+=1; screen[x][
y]-=3; y--; }else if(screen[x][y-2]==1){ return; }else if(screen[x][y-2]==2){
return; }else if(screen[x][y-2]=4){ screen[x][y-2]+=2; screen[x][y-1]+=1; screen
[x][y]-=3; y--; } } } }
Move right
void moved(){ if(y<9){ if(screen[x][y+1]==1){ return; }else if(screen[x][y+1]==
4){ screen[x][y+1]+=3; screen[x][y]-=3; y++; } else if(screen[x][y+1]==0){
screen[x][y+1]+=3; screen[x][y]-=3; y++; }else if(screen[x][y+1]==2||screen[x][y
+1]==6){ if(screen[x][y+2]==0){ screen[x][y+2]+=2; screen[x][y+1]+=1; screen[x][
y]-=3; y++; }else if(screen[x][y+2]==4){ screen[x][y+2]+=2; screen[x][y+1]+=1;
screen[x][y]-=3; y++; }else if(screen[x][y+2]==2){ return; }else if(screen[x][y+
2]==1){ return; } } } }
Principal function

The main function is a bit messy , Just look at the notes
int main(){ int n,t; int j,k; int b=1; here: system("cls");// printf(
" To start the game, press 1\n To exit the game, press 2\n"); scanf("%d",&j); if(j==1){ printf(" Please use it wsad Representatives play the game up and down, left and right \n");
// This leads to the game while(1){ system("cls");/* Clear the previous map after each move , Or you'll go every day Generate a graph in one step */ print();
// Print the map first scanf("%c",&n);// Read in user actions switch(n){ case 'w': movew(); break; case 's':
moves(); break; case 'a': movea(); break; case 'd': moved(); break; } // Controller Mobile t=
win(); if(t==1){ goto there; }// After each operation, judge whether the game is successful or not , If you win, jump to the end of the function if(b == lose()){
system("cls"); print(); printf(" The game failed "); return 0; } // Game failure tips } }else { system(
"cls"); printf(" Are you sure you want to quit the game \n Press to confirm exit 1\t To return to the previous level, press 2\n"); scanf("%d",&k); if(k==1){ printf
(" You are out of the game , Looking forward to your coming again , thank you "); return 0; }else { goto here; } }
// This is where the first user enters the game , If the user chooses to quit the game, perform the action there: printf(" Congratulations on passing the game !"); return 0; }// Principal function
That's all the code , If you need a complete code, you can leave a message .