2048小游戏(变态版哦)
生活随笔
收集整理的這篇文章主要介紹了
2048小游戏(变态版哦)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
近日,由于博主同學暑假有個作業是寫個2048小游戲,我一聽挺好玩的。。然后就開始了。。
首先,2048在移動過程中的規則其實也沒有特別難,但是感覺也不是一句話就能說完的。(不過玩的多……感覺總是有的)
廢話不多說,其實博主同學提供了pdf描述了2048的算法。
各位筒子入坑前請先過幾眼這個規則,以及其算法(當然我覺得算法第二點有點問題,后述)
那么在游戲的編寫前,可以先對細枝末節做一些準備。
1.出現數字2/4的概率
2.移動后新數字產生的位置(當然也是隨機數了)
int getRandAddr()//返回一個0-15的數 {return rand() % 16; }bool isSafeAddr(int addr)//由上一個函數產生的位置不一定安全,判斷一下 {if (a[addr / 4][addr % 4] == 0)//當這個位置是0,也就是空才安全return true;return false;//都沒安全當然不安全拉 }3.判斷2048游戲是否結束(就是你game over拉)
bool isEnd() {for (int i = 0; i < 4; i++)//第一步,只要有空位,那就還沒輸{for (int j = 0; j < 4; j++){if (a[i][j] == 0)return false;}}//第二步,當你滿了之后,要判斷上下左右相鄰的是否還能合并for (int i = 0; i < 4; i++)//行{for (int j = 0; j < 3; j++){if (a[i][j] == a[i][j + 1])return false;}}for (int i = 0; i < 4; i++)//列{for (int j = 0; j < 3; j++){if (a[j][i] == a[j+1][i])return false;}}return true; }對了,頂上預編譯指令和全局變量如下
#include<iostream> #include<string> #include<ctime>//給srand()預備 #include<conio.h> using namespace std;int a[4][4];//2048的數組 int b[4][4];//提供給它撤銷功能(不過就提供了一步,感覺要很多步要用棧吧。 //PS:真正的2048哪有這個功能啊) int score=0;//記錄積分 int score_ex = 0;//為撤銷而存在好了,下面進入正文。
W/A/S/D的移動是2048的核心,我只展示一下W吧,反正其他類似,敲起來累死(方向變來變去,路癡的我要神經衰弱了)
當當當,完成四個移動的函數還改了好幾遍真是頭昏腦漲啊。
接下來制定游戲規則部分,不過先初始化一下吧
void init() {cout << "rule:\n\t1.w/s/a/d for direction\n\t2.r to restart \n\t3.u to 返回一步(不能連續用)\n\t4.e to exit\n";for (int i = 0; i < 4; i++){for (int j = 0; j < 4; j++){a[i][j] = 0;b[i][j] = 0;}}score = 0;score_ex = 0;srand(time(NULL));//如果隨機數種子不改一下,每次玩的都一樣……int addr = getRandAddr();a[addr / 4][addr % 4] = getRand();}那么開始GAME吧
void game() {string choose;cout << "choose level: 1-normal 2-hard 3-BT:";//選擇難度吧,騷年~while (cin>>choose){if (choose[0] == '1'){init();break;}if (choose[0] == '2'){init2();break;}if (choose[0] == '3'){init3();//哈哈變態版,添加了四個障礙break;}}while (1){display();if (isEnd())//判斷你是否還能移動{string end;cout << "\t\tGame over !\n";cout << "\t\tYour Score is:" << score << endl;cout << "input 'r' to restart other to exit.";cin >> end;if (end[0] == 'r'){system("cls");game();}else{exit(0);}}//cin >> choose;//原來用這個,每次輸入w/a/s/d都要按下Enterchoose[0] = _getch();//這個按了w就會直接移動了,詳情百度conio.h _getch();if (choose[0] == 'u'){reWrite();//just one step ,就是將備份的數組寫回進來system("cls");continue;}copyRight();//既然你都不撤回了,我就先將現在的保存一下switch (choose[0]){case 'w':isW(); break;//上下左右case 'a':isA(); break;case 's':isS(); break;case 'd':isD(); break;case 'e':exit(0);case 'r':{system("cls"); game(); break; }//一般人不會一直玩到棧溢出吧。。。default:{system("cls");cout << "Error input,Try angin.\n";continue;}}if (isSame()){//如果相同就不產生新數字了,算法里面沒描述,玩過都知道~system("cls");//因為每次都會display()還是應該清除一下continue;}int addr = getRandAddr();while (!isSafeAddr(addr))//一直找到安全位置為止{addr = getRandAddr();}a[addr / 4][addr % 4] = getRand();//產生新數了。system("cls");} }下面這個函數都是在里面出現過,不是很重要而且沒代碼的
void init2()//這個只是一個障礙,想要4障礙自己寫一下吧 {cout << "rule:\n\t1.w/s/a/d for direction\n\t2.r to restart \n\t3.u to 返回一步(不能連續用)\n\t4.e to exit\n";for (int i = 0; i < 4; i++){for (int j = 0; j < 4; j++){a[i][j] = 0;b[i][j] = 0;}}score = 0;score_ex = 0;srand(time(NULL));int addr = getRandAddr();a[addr / 4][addr % 4] = getRand();addr = getRandAddr();while (!isSafeAddr(addr)){addr = getRandAddr();}a[addr / 4][addr % 4] = -1; }void display()//打印一下 {char D = char(127);//障礙物的樣子,我也不知道用啥好for (int i = 0; i < 4; i++){cout << "\t";for (int j = 0; j < 4; j++){if (a[i][j] == 0)//每次都是0眼都花了{cout << ".\t";continue;}if (a[i][j] == -1 || a[i][j] == -2 || a[i][j] == -3 || a[i][j] == -4)//遇到障礙物{cout << D<<"\t";continue;}cout << a[i][j] << "\t";}cout << "\n\n";}cout << "score: " << score << "\n\n";cout << "\t" << "Next:"; }這兒可能代碼還不全,待會上傳下載的,會在下面補上。
附上一張變態版的皂片吧(^__^) 嘻嘻……
總結
以上是生活随笔為你收集整理的2048小游戏(变态版哦)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle官网下载plsql,Orac
- 下一篇: 印刷汉字识别方法综述