热门游戏 2048 C++ 源代码 再版 分享

来源:互联网 发布:淘宝小类目让我发财了 编辑:程序博客网 时间:2024/06/15 16:12

参考:http://developer.51cto.com/art/201404/434925.htm

看到一个前辈写的2048源码,很受启发,学习了,有一些问题,那位前辈懒的改了,我稍微修改了一下,跟 2048 的基本功能没有区别了。
下面上源码。

/*By Reason*/#include<iostream>#include <iomanip>#include<math.h>#include<stdlib.h>#include<time.h>#include <conio.h> //为了读取方向键#include<windows.h>using namespace std;//srand( (unsigned)time( NULL ) ); //随机数种子 不能用在这里int pane[4][4]; //棋盘int N = 11; //2的n次方void showpane() //显示棋盘{cout << setw(46) << "X2048 by Reason" << endl;cout << setw(50) << " |-----------------------|" << endl;for (int i = 0; i <= 3; i++){cout << setw(24) << "";for (int j = 0; j <= 3; j++){//SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED);cout << setw(2) << "|" << setw(4) << pane[i][j];if (j == 3){cout << setw(2) << "|" << endl;cout << setw(50) << " |-----------------------|" << endl;}}}}void newgame() //开始游戏{for (int i = 0; i <= 3; i++) //初始化棋盘for (int j = 0; j <= 3; j++)pane[i][j] = 0;//mem by fzpsrand((unsigned)time(NULL));int m = rand() % 4;int n = rand() % 4;pane[m][n] = 2;showpane();}bool if2n(int x) //判断x是否是2的n次方{bool flag = false;for (int n = 1; n <= N; n++){if (x == pow(2, n)){flag = true;}}return flag;}int upmove() //上移{int flag = 0;for (int j = 0; j <= 3; j++)for (int i = 0; i < 3; i++){int count = 0;for (int k = 0; k < 4; ++k){if (pane[k][j] != 0){pane[count][j] = pane[k][j];if (k != count)pane[k][j] = 0;count++;}}if (if2n(pane[i][j] + pane[i + 1][j]) == 1){pane[i][j] = pane[i][j] + pane[i + 1][j];pane[i + 1][j] = 0;flag = 1;}}return flag;}int downmove() //下移{int flag = 0;for (int j = 0; j <= 3; j++)for (int i = 3; i > 0; i--){int count = 3;for (int k = 3; k >= 0; --k){if (pane[k][j] != 0){pane[count][j] = pane[k][j];if (k != count)pane[k][j] = 0;count--;}}if (if2n(pane[i][j] + pane[i - 1][j]) == 1){pane[i][j] = pane[i][j] + pane[i - 1][j];pane[i - 1][j] = 0;flag = 1;}}return flag;}int leftmove() //左移{int flag = 0;for (int i = 0; i <= 3; i++)for (int j = 0; j < 3; j++){int count = 0;for (int k = 0; k < 4; ++k){if (pane[i][k] != 0){pane[i][count] = pane[i][k];if (k != count)pane[i][k] = 0;count++;}}if (if2n(pane[i][j] + pane[i][j + 1]) == 1){pane[i][j] = pane[i][j] + pane[i][j + 1];pane[i][j + 1] = 0;flag = 1;}}return flag;}int rightmove() //右移{int flag = 0;for (int i = 0; i <= 3; i++)for (int j = 3; j > 0; j--){int count = 3;for (int k = 3; k >= 0; --k){if (pane[i][k] != 0){pane[i][count] = pane[i][k];if (k != count)pane[i][k] = 0;count--;}}if (if2n(pane[i][j] + pane[i][j - 1]) == 1){pane[i][j] = pane[i][j] + pane[i][j - 1];pane[i][j - 1] = 0;flag = 1;}}return flag;}int testup() //能否上移测试{int flag = 0;for (int j = 0; j <= 3; j++)for (int i = 0; i < 3; i++){if (if2n(pane[i][j] + pane[i + 1][j]) == 1 && pane[i + 1][j] != 0){flag = 1;}}return flag;}int testdown() //测试能否下移{int flag = 0;for (int j = 0; j <= 3; j++)for (int i = 3; i > 0; i--){if (if2n(pane[i][j] + pane[i - 1][j]) == 1){flag = 1;}}return flag;}int testleft() //测试能否左移{int flag = 0;for (int i = 0; i <= 3; i++)for (int j = 0; j < 3; j++){if (if2n(pane[i][j] + pane[i][j + 1]) == 1){flag = 1;}}return flag;}int testright() //测试能否右移{int flag = 0;for (int i = 0; i <= 3; i++)for (int j = 3; j > 0; j--){if (if2n(pane[i][j] + pane[i][j - 1]) == 1){flag = 1;}}return flag;}int panemax() //棋盘最大数{int max = pane[0][0];for (int i = 0; i <= 3; i++)for (int j = 0; j <= 3; j++)if (pane[i][j] > max)max = pane[i][j];return max;}bool ifwin() //判断是否胜利{if (panemax() == pow(2, N)){cout << setw(45) << "You Win!" << endl;return true;}else{return false;}}bool ifGameOver() //判断是否游戏结束{if (testup() + testdown() + testleft() + testright() == 0){cout << setw(43) << "Game Over!" << endl;return true;}else{return false;}}bool isPaneFull(){bool flag = true;for (int i = 0; i < 4; ++i)for (int j = 0; j < 4; ++j){if (pane[i][j] == 0)//flag = false;}return flag;}void addNewNumber() // add new number {if (isPaneFull())return;unsigned m, n;srand(unsigned(time(0)));do{m = rand() % 4;n = rand() % 4;} while (pane[m][n] != 0);if (rand() % 4 == 3)pane[m][n] = 4;//随机出2,和4 但是2的概率要大一点。elsepane[m][n] = 2;}int GetDirection() //读取方向{int ret = 0;do{int ch = _getch();if (isascii(ch))continue;ch = _getch();switch (ch){case 72:ret = 2; // upbreak;case 75:ret = 1; // leftbreak;case 77:ret = 3; // rightbreak;case 80:ret = 4; // downbreak;default:break;}} while (ret == 0);return ret;}void main() //主函数{system("color f9");int makesure = 1;while (makesure){system("cls");newgame();while (ifwin() + ifGameOver() == 0){int c = GetDirection();switch (c){case 2:upmove();break;case 4:downmove();break;case 1:leftmove();break;case 3:rightmove();break;default:break;}system("cls");addNewNumber();showpane();}cout << setw(43) << "你的最后成绩为:" << panemax() << endl;cout << setw(60) << "若要重新开始游戏请输入1,若要结束请输入0。" << endl;cin >> makesure;while (makesure != 1 && makesure != 0){cout << "输入不正确,请重新输入!" << endl;cin >> makesure;}}cout << "再见!" << endl;system("pause");}

界面还是比较简陋,以后会考虑再完善一下。



0 0