基于C語言實現(xiàn)簡單的走迷宮游戲
更新時間:2016年04月18日 16:07:22 作者:LiaoGlenn
這篇文章主要介紹了基于C語言實現(xiàn)簡單的走迷宮游戲,用到雙向隊列,方便在運行完畢后輸出經(jīng)過的點,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例講述了C語言實現(xiàn)簡單的走迷宮游戲的方法,代碼完整,便于讀者理解。
學(xué)數(shù)據(jù)結(jié)構(gòu)時用“?!睂懙囊粋€走迷宮程序,實際上用到雙向隊列,方便在運行完畢后輸出經(jīng)過的點。
#include <cstdio> #include <deque> #include <windows.h> using namespace std; class node { public: int x,y; int lastOpt; }; deque<node> sta; int x,y; int endx,endy; int mapW,mapH; int steps; int xopt[5]= {0,0,1,0,-1}; int yopt[5]= {0,1,0,-1,0}; int map[100][100]= { }; void init() { x = 1; y = 1; endx = 1; endy = 9; mapH = 10; mapW = 10; for(int i=0; i<=mapH; i++) for(int j=0; j<=mapW; j++) { if(i==0 ||j==0 ||i==mapH||j==mapW) map[i][j]=-1; } steps=0; map[1][2]=-1; map[2][2]=-1; map[3][2]=-1; map[4][2]=-1; map[6][2]=-1; map[7][2]=-1; map[8][2]=-1; map[9][2]=-1; map[9][3]=-1; map[8][3]=-1; map[1][4]=-1; map[3][4]=-1; map[4][4]=-1; map[5][4]=-1; map[6][4]=-1; map[7][4]=-1; map[1][6]=-1; map[2][6]=-1; map[3][6]=-1; map[4][6]=-1; map[5][6]=-1; map[6][6]=-1; map[7][6]=-1; map[8][6]=-1; map[8][7]=-1; map[8][8]=-1; map[7][8]=-1; map[6][8]=-1; map[5][8]=-1; map[4][8]=-1; map[3][8]=-1; map[2][8]=-1; map[1][8]=-1; map[endx][endy]=5; } void dis() { system("cls"); int ori = map[x][y]; map[x][y]=1; for(int i=0; i<=mapH; ++i) { for(int j=0; j<=mapW; ++j) { if(map[i][j]==0) printf(" "); else if(map[i][j]==-1) printf(" #"); else if(map[i][j]==1) printf(" @"); else if(map[i][j]==2) printf(" ."); else if(map[i][j]==5) printf(" !"); } cout<<i<<endl; } for(int j=0; j<=mapW; ++j) cout<<j<<" "; printf("\n\n > steps:%d Exit:(%d,%d)\n",steps,endx,endy); map[x][y] = ori; } int can(int n) { if(map[x+xopt[n]][y+yopt[n]] == 0 || map[x+xopt[n]][y+yopt[n]] == 5) return 1; return 0; } void visit(int n) { map[x][y]=2; x+=xopt[n]; y+=yopt[n]; node tem; tem.x = x; tem.y = y; tem.lastOpt = n; sta.push_back(tem); steps++; } int main() { init(); node tem; while( x != endx || y!=endy) { int cans = 0; for(int i=1; i<=4; i++) { if(can(i)) { cans = 1; visit(i); break; } } if(!cans) { if(!sta.empty()) { tem = sta.back(); map[tem.x][tem.y]=0; sta.pop_back(); } else { map[x][y]=2; x+=xopt[tem.lastOpt]; x+=yopt[tem.lastOpt]; dis(); break; } } dis(); Sleep(500); } if(x==endx && y == endy) cout<<"\n > i am finished....\n"; else cout<<"\n > i am finished...but i can't find the right way\n"; return 0; }
效果圖:
以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)C語言有所幫助。
相關(guān)文章
C語言sizeof和strlen的指針和數(shù)組面試題詳解
strlen是函數(shù),字符串長度,不包括停止符。而sizeof則是內(nèi)存塊的大小,包括停止符。數(shù)組是一種數(shù)據(jù)類型,數(shù)據(jù)類型的本質(zhì)就是固定大小,內(nèi)存塊的別名??梢杂胹izeof()一般都是數(shù)據(jù)類型2022-04-04VS2022連接sqlserver數(shù)據(jù)庫教程
本文主要介紹了VS2022連接sqlserver數(shù)據(jù)庫教程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07C++調(diào)用C函數(shù)報錯無法解析的外部命令/無法解析的外部符號問題
這篇文章主要介紹了C++調(diào)用C函數(shù)報錯無法解析的外部命令/無法解析的外部符號問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08使用UART與PC通信實現(xiàn)msp430g2553單片機超聲波測距示例
這篇文章主要介紹了使用UART與PC通信實現(xiàn)msp430g2553單片機超聲波測距示例,需要的朋友可以參考下2014-05-05