推箱子游戲的運行規則:在街道上上小人推動箱子移動,直到把箱子移動到目的地。
思路分析:
小人及箱子的移動就是小人或者箱子和路的交換;
1 定義二維字符數組,存儲地圖
2 顯示地圖,提示游戲玩法
3 記錄小人及箱子位置,并定義字符變量接收用戶輸入方向
4 循環判斷語句
1).小人的下一步是否為路,如果為路,則移動并記錄小人新位置信息
2).小人的下一步如果不是路,在判斷是否為箱子,如果是箱子,在判斷箱子的下一個位置是否是路,如果是路,則移動箱子和小人
3). 刷新地圖
4) .判斷箱子的位置,如果在指定位置,則游戲結束;
下面是實現代碼:
#include <stdio.h>//交換字符數組元素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; }//打印數組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[]){ //定義數組 char ch[10][11] = { {'#','#','#','#','#','#','#','#','#','#','/0'}, {'#','#','#',' ',' ','#','#','#','#','#','/0'}, {'#','0','X',' ',' ','#','#','#','#','#','/0'}, {'#','#','#','#',' ','#','#','#','#','#','/0'}, {'#','#','#',' ',' ',' ',' ','#','#','#','/0'}, {'#','#','#',' ',' ',' ',' ',' ','#','#','/0'}, {'#','#','#','#','#',' ',' ',' ','#','#','/0'}, {'#','#','#','#','#',' ',' ',' ',' ',' ','/0'}, {'#','#','#','#','#','#','#','#',' ',' ','/0'}, {'#','#','#','#','#','#','#','#','#','#','/0'} }; //打印數組 printArray(ch); //記錄小人及箱子位置 //定義路的標志變量street int personX = 2,personY = 1,boxX = 2, boxY = 2; char street = ' '; //提示用戶輸入移動方向 printf("請輸入小人移動方向:(w-上,s-下,a-左,d-右)/n"); //定義記錄用戶輸入的方向 char direction; //根據方向定義循環語句 while(1) { scanf("%c",&direction); getchar();//接收鍵盤回車符 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); //定義結束標志 if (boxY == 9) { printf("恭喜你,游戲過關!/n"); return 0; } } return 0;}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答