自己写的2048小游戏的程序,为什么数字多的时候运行的很慢,要等一两秒才能变过来
程序代码:
#include<iostream> #include<conio.h> #include<math.h> #include<Windows.h> #include<time.h> #include<iomanip> using namespace std; int pan[4][4]; void showpan()//显示棋盘 { system("cls"); system("color f9"); cout << setw(50) << "木木的2048" << endl; cout << setw(60) << "--------------------" << endl; for (int i = 0; i < 4; i++) { cout << setw(39) << " " << "|"; for (int j = 0; j < 4; j++) { if (pan[i][j] == 0) cout << setw(4) << " " << "|"; else { cout << setw(4) << pan[i][j]<<"|" ; } } cout << endl; cout << setw(60) << "--------------------" << endl; } } void newgame()//开始一个新游戏 { for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) pan[i][j] = 0; srand((unsigned)time(NULL)); int m = rand() % 4; int n = rand() % 4; pan[m][n] = 2; showpan(); } int moveup()//上移 { int flag = 0; for (int j = 0; j < 4; j++) { for (int i = 0, k = 1; k < 4; k++) { if (pan[k][j] > 0) { if (pan[i][j] == 0) { pan[i][j] = pan[k][j]; pan[k][j] = 0; } else { if (pan[i][j] == pan[k][j]) { pan[i][j] += pan[k][j]; pan[k][j] = 0; } else { pan[i + 1][j] = pan[k][j]; if (k != i + 1) pan[k][j] = 0; i += 1; } } } } } return flag; } int movedown()//下移 { int flag = 0; for (int j = 0; j < 4;j++) { for (int i=3, k = 2; k>=0; k--) { if (pan[k][j] > 0) { if (pan[i][j] == 0) { pan[i][j] = pan[k][j]; pan[k][j] = 0; } else if (pan[i][j] == pan[k][j]) { pan[i][j] += pan[k][j]; pan[k][j] = 0; } else { if (k != (i - 1)) { pan[i - 1][j] = pan[k][j]; pan[k][j] = 0; } i--; } } } }return flag; } int moveleft()//左移 { int flag = 0; for (int i = 0; i< 4; i++) { for (int j=0,k =1; k <= 3; k++) { if (pan[i][k] > 0) { if (pan[i][j] == 0) { pan[i][j] = pan[i][k]; pan[i][k] = 0; } else if (pan[i][j] == pan[i][k]) { pan[i][j] += pan[i][k]; pan[i][k] = 0; } else { if ((j + 1) != k) { pan[i][j + 1] = pan[i][k]; pan[i][k] = 0; }j++; } } } }return flag; } int moveright()//右移 { int flag = 0; for (int i = 0; i< 4; i++) { for (int k =2,j=3; k >= 0; k--) { if (pan[i][k] > 0) { if (pan[i][j] == 0) { pan[i][j] = pan[i][k]; pan[i][k] = 0; } else if (pan[i][j] == pan[i][k]) { pan[i][j] += pan[i][k]; pan[i][k] = 0; } else { if (k != (j - 1)) { pan[i][j - 1] = pan[i][k]; pan[i][k] = 0; }j--; } } } }return flag; } int panmax()//检测最大数 { int max = 0; for (int i = 0; i < 4;i++) { for (int j = 0; j < 4;j++) { if (max<pan[i][j]) { max = pan[i][j]; } } } return max; } int if2n(int x) { int flag = 0; for (int n = 1; n < 12; n++) { if (x == pow(2, n)) flag = 1; }return flag; } int ifwin()//检测是否游戏胜利 { int flag = 0; if (panmax() == 2048) { flag = 1; cout << setw(40) << "可以啊,小伙子,你赢了" << endl; }return flag; } int testup()//检测能否上移 { int flag = 0; for (int j = 0;j< 4;j++) { for (int i = 0; i <3; i++ ) { if (if2n(pan[i][j] + pan[i + 1][j])&& (pan[i+1][j])) flag = 1; } }return flag; } int testdown()//检测能否下移 { int flag = 0; for (int j = 0; j < 4; j++) { for (int i = 3; i > 0; i--) { if (if2n(pan[i][j] + pan[i - 1][j] )&&pan[i-1][j] != 0) flag = 1; } }return flag; } int testleft()//检测能否左移 { int flag = 0; for (int i = 0; i< 4; i++) { for (int j = 0;j < 3; j++) { if ((if2n(pan[i][j] + pan[i][j+1])==1 )&&(pan[i][j+1]!= 0)) flag = 1; } }return flag; } int testright()//检测能否右移 { int flag = 0; for (int i = 0; i < 4; i++) { for (int j = 3 ; j >0; j--) //if (pan[k][i] > 0) { if (if2n(pan[i][j] +pan[i][j-1])&& pan[i][j-1] != 0) flag = 1; ; } }return flag; } int gameover()//判断游戏是否结束 { int flag = 0; if ((testup() + testdown() + testleft() + testright()) == 0) { flag = 1; cout << setw(30) << "不行啊,小伙子,继续努力" << endl; } return flag; } void add() { int flag = 1; while (flag) { srand((unsigned)time(NULL)); int m = rand() % 4; int n = rand() % 4; if (pan[m][n] == 0) { pan[m][n] = 2 ; flag = 0; } } } int GetDirection() //读取方向 { int ret = 0; do { int ch= _getch(); if (isascii(ch)) continue; ch = _getch(); switch (ch) { case 72: ret = 2; // top break; case 75: ret = 1; // left break; case 77: ret = 3; // right break; case 80: ret = 4; // down break; default: break; } } while (ret == 0); return ret; } int main() { int makesure = 1; while (makesure) { system("cls"); system("color f9"); newgame(); while ((ifwin() + gameover())==0) { int c = GetDirection(); switch (c) { case 2: if (testup() == 1) { moveup(); add(); system("cls"); showpan(); } break; case 4: if (testdown() == 1) { movedown(); add(); system("cls"); showpan(); } break; case 1: if (testleft() == 1) { moveleft(); add(); system("cls"); showpan(); } break; case 3: if (testright() == 1) { moveright(); add(); system("cls"); showpan(); } break; default: break; } } int max = panmax(); cout << "小伙子,你才拿了" << max << "分" << endl; cout << "如果要再来一盘就输入1,不玩了就输入0" << endl; cout << "来:" << endl; cin >> makesure; if (makesure != 1 && makesure != 0) { cout << "小伙子,别瞎写,重来:" << endl; cin >> makesure; } } }