【2048小游戏】
By
永远喜欢胡桃
at 2025-03-22 • 0人收藏 • 28人看过
#include <iostream> #include <random> #include <cstdlib> #include <conio.h> using namespace std; const int Boardlength=4; int board[Boardlength][Boardlength]={2,4,16,2,4,32,64,8,4,16,128,8,2,4,32,2}; random_device rd; void Rand() { uniform_int_distribution<int> dist(0,Boardlength*Boardlength-1); int randcol,randrow; while(true){ int randboard=dist(rd); randcol=randboard%Boardlength; randrow=randboard/Boardlength; if(board[randrow][randcol]==0) break; } uniform_int_distribution<int> dist2(1, 2); int randnumber=dist2(rd)*2; board[randrow][randcol]=randnumber; } void Clockwise() { int rotaboard[Boardlength][Boardlength]; for(int i=0;i<Boardlength;i++){ for(int j=0;j<Boardlength;j++){ rotaboard[j][Boardlength-1-i]=board[i][j]; } } for(int i=0;i<Boardlength;i++){ for(int j=0;j<Boardlength;j++){ board[i][j]=rotaboard[i][j]; } } } void MoveUp() { bool merged[Boardlength][Boardlength] = {}; // 追踪已合并的格子 for (int i = 0; i < Boardlength; i++) { for (int j = 0; j < Boardlength; j++) { if (board[j][i] != 0) { int k = j; while (k > 0 && board[k - 1][i] == 0) { board[k - 1][i] = board[k][i]; // 向上移动 board[k][i] = 0; k--; } if (k > 0 && board[k - 1][i] == board[k][i] && !merged[k - 1][i]) { board[k - 1][i] *= 2; // 合并 board[k][i] = 0; merged[k - 1][i] = true; // 标记已合并 } } } } Rand(); // 合并后生成新数字 } void MoveDown() { Clockwise(); Clockwise(); MoveUp(); Clockwise(); Clockwise(); } void MoveRight() { Clockwise(); Clockwise(); Clockwise(); MoveUp(); Clockwise(); } void MoveLeft() { Clockwise(); MoveUp(); Clockwise(); Clockwise(); Clockwise(); } void DisplayBoard() { system("cls"); // 清屏 cout<<"通过上下左右方向键来控制,按q退出游戏,按a显示简介"<<endl; for (int i=0;i<Boardlength;i++){ for (int j=0;j<Boardlength;j++){ cout<<board[i][j]<<"\t"; } cout<<endl; } } bool Isfull() { for(int i=0;i<Boardlength;i++){ for(int j=0;j<Boardlength;j++){ if(board[i][j]==0) return false; } } return true; } bool IsfullbutCanMoveCol() { if(Isfull()){ for(int i=0;i<Boardlength;i++){ for(int j=0;j<Boardlength;j++){ if(j<Boardlength-1 && board[i][j]==board[i][j+1]) return true; } } return false; } else return false; } bool IsfullbutCanMoveRow() { if(Isfull()){ for(int i=0;i<Boardlength;i++){ for(int j=0;j<Boardlength;j++){ if(i<Boardlength-1 && board[i][j]==board[i+1][j]) return true; } } return false; } else return false; } int main(){ char a; cout<<"2048小游戏"<<endl; cout<<"通过上下左右方向键来控制,按q退出游戏"<<endl; cout<<"点击任意键开始游戏"<<endl; a=getch(); while(true){ Rand(); Rand(); DisplayBoard(); while(true){ a=getch(); if(Isfull){cout<<"ok"<<endl;} if(Isfull()){ if(IsfullbutCanMoveCol&&!IsfullbutCanMoveRow){ if(a==75 || a==77){ cout<<"No doing this step"<<endl; cout<<"点击任意键继续游戏"<<endl; a=getch(); goto there; } } if(IsfullbutCanMoveRow&&!IsfullbutCanMoveCol){ if(a==72 || a==80){ cout<<"No doing this step"<<endl; cout<<"点击任意键继续游戏"<<endl; a=getch(); goto there; } } if(!IsfullbutCanMoveCol()&&!IsfullbutCanMoveRow()){ cout<<"Game Over"<<endl; break; } } //上:72 下:80 左:75 右:77 if(a==72) MoveUp(); if(a==80) MoveDown(); if(a==75) MoveLeft(); if(a==77) MoveRight(); if(a=='q') break; there: DisplayBoard(); } cout<<"如果要再玩一次游戏,请输入123"<<endl; string response; cin>>response; if(response!="123") break; fill(&board[0][0],&board[0][0]+sizeof(board)/sizeof(int),0); } return 0; }
登录后方可回帖