Sokoban games

Sokoban games

# include <stdio.h> # include <math.h> # include <string.h> # include <stdlib.h> # include <time.h> int main () { begin: int i,j; char p[ 16 ][ 16 ]; for (i= 0 ; i< 16 ; i++) { for (j = 0 ; j< 16 ; j++) p[i][j]= '' ; } for (i = 0 ; i< 16 ; i++) { p[i][ 15 ] = '#' ; p[i][ 0 ] = '#' ; } for (j = 0 ; j< 16 ; j++) { p[ 0 ][j]= '#' ; p[ 15 ][j]= '#' ; } p[ 5 ][ 3 ] = '#' ; p[ 6 ][ 8 ] = '#' ; p[ 2 ][ 3 ] = '#' ; p[ 8 ][ 12 ] = '#' ; p[ 2 ][ 12 ] = '#' ; p[ 11 ][ 3 ] = '#' ; p[ 10 ][ 9 ] = '#' ; p[ 6 ][ 6 ] = '#' ; p[ 12 ][ 12 ] = '#' ; p[ 12 ][ 11 ] = '#' ; p[ 13 ][ 8 ] = '#' ; p[ 8 ][ 6 ] = '#' ; p[ 8 ][ 5 ] = '#' ; p[ 9 ][ 5 ] = '#' ; p[ 5 ][ 8 ] = '#' ; p[ 2 ][ 0 ] = '#' ; p[ 2 ][ 7 ] = '#' ; p[ 2 ][ 8 ] = '#' ; p[ 3 ][ 8 ] = '#' ; p[ 3 ][ 9 ] = '#' ; p[ 4 ][ 3 ] = '#' ; p[ 5 ][ 14 ] = '#' ; p[ 5 ][ 13 ] = '#' ; p[ 13 ][ 1 ] = '#' ; p[ 13 ][ 2 ] = '#' ; p[ 13 ][ 3 ] = '#' ; p[ 1 ][ 1 ] = '#' ; p[ 1 ][ 2 ] = '#' ; p[ 2 ][ 1 ] = '#' ; p[ 7 ][ 8 ] = '#' ; p[ 6 ][ 6 ] = '#' ; p[ 7 ][ 6 ] = '#' ; p[ 8 ][ 6 ] = '#' ; p[ 14 ][ 1 ] = '#' ; p[ 14 ][ 2 ] = '#' ; p[ 14 ][ 3 ] = '#' ; p[ 14 ][ 4 ] = '#' ; p[ 14 ][ 5 ] = '#' ; p[ 10 ][ 7 ] = '#' ; p[ 10 ][ 8 ] = '#' ; p[ 11 ][ 8 ] = '#' ; p[ 4 ][ 9 ] = '#' ; p[ 7 ][ 1 ] = '#' ; p[ 8 ][ 1 ] = '#' ; p[ 9 ][ 2 ] = '#' ; p[ 14 ][ 14 ] = '#' ; p[ 14 ][ 13 ] = '#' ; p[ 6 ][ 14 ]= '#' ; srand ( time ( NULL )); p[ 4 ][ 5 ] = '!' ; p[ 5 ][ 4 ] = '@' ; int i3,j3; for (i = 0 ; i< 100 ; i++) { i3=( rand ()% 16 ),j3=( rand ()% 16 ); if (p[i3][j3]!= '#' &&p[i3][j3]!= '!' &&p[i3] [j3]! = '@' ) break ; } p[i3][j3]= 'O' ; for (i= 0 ; i< 16 ; i++) { for (j= 0 ; j< 16 ; j++) printf ( "%c" ,p[i][j]); printf ( "\n" ); } printf ( "\n" ); //Print the map printf ( "'@' is the box'!' is the character' #'is the wall and'O' is the end point\n enter uppercase WSAD and press Enter to confirm that they are up, down, left, and right Move\n(input a single uppercase letter once)\n" ); int i1= 4 ,j1= 5 ,i2= 5 ,j2= 4 ; //i1,j1 character coordinates i2,j2 box coordinates char x; int bushu = 0 ; while (~ scanf ( "%c" ,&x)) { char q[ 100 ]; gets (q); if (x== 'S' ) //If you go down { if (i1==i2 -1 &&j1==j2) //If the person is directly above the box { if (p[i2+ 1 ][j2]!= '#' ) //If the box is not blocked by the wall { p[i2+ 1 ][j2]= '@' ; p[i2][j2]= '!' ; p[i2 -1 ][j2]= '' ; i2++; i1++; bushu++; } else //If the box is blocked by the wall { printf ( "" ); } } else if (p[i1+ 1 ][j1]!= '#' ) //If the character is not around the box and the character is not blocked by the box { p[i1+ 1 ][j1]= '!' ; p[i1][j1]= '' ; i1++; bushu++; } } else if (x== 'W' ) { if (i1==i2+ 1 &&j1==j2) //If the person is directly under the box { if (p[i2 -1 ][j2]!= '#' ) //If the box is not blocked by the wall { p[i2 -1 ][j2]= '@' ; p[i2][j2]= '!' ; p[i2+ 1 ][j2]= '' ; i2--; i1--; bushu++; } else //If the box is blocked by the wall { printf ( "" ); } } else if (p[i1 -1 ][j1]!= '#' ) //If the character is not around the box and the character is not blocked by the box { p[i1 -1 ][j1]= '!' ; p[i1][j1]= '' ; i1--; bushu++; } } else if (x== 'A' ) { if (i1==i2&&j1==j2+ 1 ) //If the person is on the left side of the box { if (p[i2][j2 -1 ]!= '#' ) //If the box is not blocked by the wall { p[i2][j2 -1 ] = '@' ; p[i2][j2]= '!' ; p[i2][j2+ 1 ] = '' ; j2--; j1--; bushu++; } else //If the box is blocked by the wall { printf ( "" ); } } else if (p[i1][j1 -1 ]!= '#' ) //If the character is not around the box and the character is not blocked by the box { p[i1][j1 -1 ] = '!' ; p[i1][j1]= '' ; j1--; bushu++; } } else if (x== 'D' ) { if (i1==i2&&j1==j2 -1 ) //If the person is on the left side of the box { if (p[i2][j2+ 1 ]!= '#' ) //If the box is not blocked by the wall { p[i2][j2+ 1 ] = '@' ; p[i2][j2]= '!' ; p[i2][j2 -1 ] = '' ; j2++; j1++; bushu++; } else //If the box is blocked by the wall { printf ( "" ); } } else if (p[i1][j1+ 1 ]!= '#' ) //If the character is not around the box and the character is not blocked by the box { p[i1][j1+ 1 ] = '!' ; p[i1][j1]= '' ; j1++; bushu++; } } system ( "cls" ); for (i= 0 ; i< 16 ; i++) { for (j= 0 ; j< 16 ; j++) printf ( "%c" ,p[i][j]); printf ( "\n" ); } printf ( "\n" ); if (i2==i3&&j2==j3) { printf ( "============================================= =====Mission Succeed======================================== ==========\n" ); printf ( "Press'R' to continue the game\n" ); } else if ((p[i2+ 1 ][j2]== '#' &&p[i2][j2+ 1 ]== '#' )||(p[i2+ 1 ][j2]== '#' &&p[i2 ] [J2 -1 ] == '#' ) || (P [+ I2 . 1 ] [J2] == '#' && P [I2] [J2 -1 ] == '#' ) || (P [I2 - 1 ][j2]== '#' &&p[i2][j2 -1 ]== '#' )||(i1==i3&&j1==j3)) { printf ( "============================================= =====Mission Failure========================================== ==========\n" ); printf ( "Press'R' to restart the game\n" ); } else printf ( "The number of steps currently used: %d\n" ,bushu); if (x== 'R' ) { system ( "cls" ); goto begin; } } char q[ 100 ]; for (i= 0 ; i< 100 ; i++) scanf ( "%c" ,&q[i]); return 0 ; } Copy code