首页 > 代码库 > C语言版推箱子

C语言版推箱子

推箱子源代码初步:

 

  1 #include<stdio.h>  2 #include<conio.h>  3 #include<stdlib.h>  4 #define boolean int  5 #define true 1  6 #define false 0  7   8 #define ROWS 10  9 //之所以定义为11,是因为字符串的尾部的\0 10 #define COLUMNS 11 11  12 //根据程序定义或者文件读入来构建地图,然后把他绘制到屏幕上 13 void DrawMap(char map[][COLUMNS]); 14 void SwapXY(char map[][COLUMNS], int *x1, int *y1, int *x2, int *y2); 15 boolean IsLose(char map[][COLUMNS], int boxPositionX, int boxPositionY, char condition); 16 //读取文件中的地图和人物、箱子的位置 17 void LoadMap(char Map[][COLUMNS], int *boxPositionX, int *boxPositionY, int *manPositionX, int *manPositionY); 18 //在推箱子的过程中接收用户的输入,然后做出相应的处理 19 void PushBox(int *boxPositionX, int *boxPositionY, int *manPositionX, int *manPositionY); 20  21 int main() 22 { 23     char wall = #; 24     char box = X; 25     char street =  ; 26     int boxPositionX,boxPositionY; 27     int manPositionX,manPositionY; 28  29     //定义地图的出口,也就是箱子到达此处就赢了 30     int winPositionX = ROWS -2; 31     int winPositionY = COLUMNS - 2; 32  33     boolean IsWin = false; 34  35     //int manNextPositionX,manNextPositionY; 36  37     char Map[ROWS][COLUMNS] =  38     { 39         "##########", 40         "#0 ####  #", 41         "# X####  #", 42         "#        #", 43         "######   #", 44         "#  ####  #", 45         "#        #", 46         "#   ######", 47         "#         ", 48         "##########" 49     }; 50     DrawMap(Map); 51     LoadMap(Map, &boxPositionX, &boxPositionY, &manPositionX, &manPositionY); 52  53     while(!IsWin) 54     { 55         int boxNextPositionX = boxPositionX; 56         int boxNextPositionY = boxPositionY; 57         int manNextPositionX = manPositionX; 58         int manNextPositionY = manPositionY; 59         //获取用户的输入 60         PushBox(&boxNextPositionX, &boxNextPositionY, &manNextPositionX, &manNextPositionY); 61         // 62         system("CLS"); 63  64         //这里处理走之后的效果 TODO: 65         if(Map[boxNextPositionX][boxNextPositionY] == street && Map[manNextPositionX][manNextPositionY] == box) 66         { 67             SwapXY(Map,&boxPositionX, &boxPositionY, &boxNextPositionX, &boxNextPositionY); 68             SwapXY(Map,&manPositionX, &manPositionY, &manNextPositionX, &manNextPositionY); 69  70         } 71         if(Map[manNextPositionX][manNextPositionY] == street) 72         { 73             SwapXY(Map,&manPositionX, &manPositionY, &manNextPositionX, &manNextPositionY); 74          75         } 76  77         DrawMap(Map); 78  79         if(boxPositionX == winPositionX && boxPositionY == winPositionY) 80         { 81             printf("You Are Win!\n"); 82             IsWin = true; 83              84         } 85         if(IsLose(Map,boxPositionX, boxPositionY, wall)) 86         { 87             printf("You Are Lose!\n"); 88             break; 89         } 90     } 91     system("Pause"); 92     return 0; 93  94 } 95  96 void DrawMap(char map[][COLUMNS]) 97 { 98     for(int i = 0; i < ROWS; i++) 99     {100         printf("%s\n", map[i]);101     }102 }103 void SwapXY(char map[][COLUMNS], int *x1, int *y1, int *x2, int *y2)104 {105     char temp = map[*x1][*y1];106     map[*x1][*y1] = map[*x2][*y2];107     map[*x2][*y2] = temp;108 109     *x1 = *x2;110     *y1 = *y2;    111 }112 boolean IsLose(char map[][COLUMNS], int boxPositionX, int boxPositionY, char condition)113 {114     if((map[boxPositionX + 1][boxPositionY] == condition && map[boxPositionX][boxPositionY - 1] == condition)115         ||(map[boxPositionX - 1][boxPositionY] == condition && map[boxPositionX][boxPositionY - 1] == condition)116         ||(map[boxPositionX - 1][boxPositionY] == condition && map[boxPositionX][boxPositionY + 1] == condition)117         ||(map[boxPositionX + 1][boxPositionY] == condition && map[boxPositionX][boxPositionY + 1] == condition)118         )119     {120         return true;121     }122     return false;123 }124 void LoadMap(char Map[][COLUMNS], int *boxPositionX, int *boxPositionY, int *manPositionX, int *manPositionY)125 {126     *boxPositionX = 2;127     *boxPositionY = 2;128     *manPositionX = 1;129     *manPositionY = 1;130 }131 void PushBox(int *boxPositionX, int *boxPositionY, int *manPositionX, int *manPositionY)132 {133     char direction;134     printf("请输入一个方向 [w,a,s,d]分别代表[上下左右]\n\n");135     direction = getch();136     switch(direction)137     {138     case W:139     case w:140         (*manPositionX)--;141         (*boxPositionX)--;142         break;143     case A:144     case a:145         (*manPositionY)--;146         (*boxPositionY)--;147         break;148     case S:149     case s:150         (*manPositionX)++;151         (*boxPositionX)++;152         break;153     case D:154     case d:155         (*manPositionY)++;156         (*boxPositionY)++;157         break;158     default:159         break;160         //DO NOTHING:161     }162 }

 

C语言版推箱子