C語言推箱子游戲?qū)崿F(xiàn)代碼
推箱子游戲的運行規(guī)則:在街道上上小人推動箱子移動,直到把箱子移動到目的地。
思路分析:
小人及箱子的移動就是小人或者箱子和路的交換;
1 定義二維字符數(shù)組,存儲地圖
2 顯示地圖,提示游戲玩法
3 記錄小人及箱子位置,并定義字符變量接收用戶輸入方向
4 循環(huán)判斷語句
1).小人的下一步是否為路,如果為路,則移動并記錄小人新位置信息
2).小人的下一步如果不是路,在判斷是否為箱子,如果是箱子,在判斷箱子的下一個位置是否是路,如果是路,則移動箱子和小人
3). 刷新地圖
4) .判斷箱子的位置,如果在指定位置,則游戲結(jié)束;
下面是實現(xiàn)代碼:
#include <stdio.h> //交換字符數(shù)組元素 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; } //打印數(shù)組 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[]) { //定義數(shù)組 char ch[10][11] = { {'#','#','#','#','#','#','#','#','#','#','\0'}, {'#','#','#',' ',' ','#','#','#','#','#','\0'}, {'#','0','X',' ',' ','#','#','#','#','#','\0'}, {'#','#','#','#',' ','#','#','#','#','#','\0'}, {'#','#','#',' ',' ',' ',' ','#','#','#','\0'}, {'#','#','#',' ',' ',' ',' ',' ','#','#','\0'}, {'#','#','#','#','#',' ',' ',' ','#','#','\0'}, {'#','#','#','#','#',' ',' ',' ',' ',' ','\0'}, {'#','#','#','#','#','#','#','#',' ',' ','\0'}, {'#','#','#','#','#','#','#','#','#','#','\0'} }; //打印數(shù)組 printArray(ch); //記錄小人及箱子位置 //定義路的標志變量street int personX = 2,personY = 1,boxX = 2, boxY = 2; char street = ' '; //提示用戶輸入移動方向 printf("請輸入小人移動方向:(w-上,s-下,a-左,d-右)\n"); //定義記錄用戶輸入的方向 char direction; //根據(jù)方向定義循環(huán)語句 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); //定義結(jié)束標志 if (boxY == 9) { printf("恭喜你,游戲過關(guān)!\n"); return 0; } } return 0; }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解C++編程中多級派生時的構(gòu)造函數(shù)和訪問屬性
這篇文章主要介紹了詳解C++編程中多級派生時的構(gòu)造函數(shù)和訪問屬性,是C++入門學習中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09C++數(shù)據(jù)結(jié)構(gòu)之哈希表的實現(xiàn)
哈希表,即散列表,可以快速地存儲和查詢記錄。這篇文章主要為大家詳細介紹了C++數(shù)據(jù)結(jié)構(gòu)中哈希表的實現(xiàn),感興趣的小伙伴可以了解一下2023-03-03詳解C語言gets()函數(shù)與它的替代者fgets()函數(shù)
這篇文章主要介紹了詳解C語言gets()函數(shù)與它的替代者fgets()函數(shù)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-10-10