c语言----<项目>_小游戏<2048>
程序员文章站
2022-06-30 23:50:11
2048 小游戏 主要是针对逻辑思维的一个训练. 主要学习方面:1.随机数产生的概率.2.行与列在进行移动的时候几种情况.3.MessageBox的使用 include include include include using namespace std; int board[4][4] = {0 ......
2048 小游戏 主要是针对逻辑思维的一个训练.
主要学习方面:1.随机数产生的概率.2.行与列在进行移动的时候几种情况.3.messagebox的使用
#include <iostream> #include <time.h> #include <conio.h> #include <windows.h> using namespace std; int board[4][4] = {0}; //二维数组 int if_need_rand; //是否生成随机数 int if_game_over; //游戏结束 void showgame() { //打印边框 system("cls"); //刷新屏幕 printf("┏━━━┳━━━┳━━━┳━━━┓\n"); for (int i = 0; i < 4; i++) { printf("┃"); for (int j = 0; j < 4;j++) { if (board[i][j]==0) { printf(" ┃"); } else { printf("%3d┃",board[i][j]); } } if (i<3) { printf("\n┣━━━╋━━━╋━━━╋━━━┫\n"); } else { printf("\n┗━━━┻━━━┻━━━┻━━━┛\n"); } } } //设计随机数 void addrand() { //随机数种子 srand((unsigned)time(null)); int i, j;//行列 while (1) { i = rand() % 4; j = rand() % 4; if (board[i][j]==0) { //三目运算符,几率时2分之一 board[i][j] = (rand() % 3 ? 2 : 4); break; } else { continue; } } } //初始化 void initgame() { if_need_rand = 1; if_game_over = 0; int i, j; i = rand() % 4; j = rand() % 4; board[i][j] = 2; //将数组内值,赋值为0;重新初始化 for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; j++) { board[i][j] = 0; } } addrand(); showgame(); } //方向分为上下左右 //左移 void moveleft() { for (int i = 0; i < 4;i++) { for (int j = 1, k = 0; j < 4;++j) { //先找到k项后面第一个不为0的项 if (board[i][j]!=0) { //分为三个情况.相等,k项是0,不等 //相等 if (board[i][k]==board[i][j]) { board[i][k++] <<= 1; //k++先赋值给k而后k++到下一个位置,进行判断 board[i][j] = 0; if_need_rand = 1; }else //k项是0 if (board[i][k]==0) { board[i][k] = board[i][j]; board[i][j] = 0; if_need_rand = 1; } //k项与j项不等,有两种情况,j项就在k项后边, //所以不能将j项赋值为0 else { board[i][++k] = board[i][j]; if (j!=k) { board[i][j] = 0; if_need_rand = 1; } } } } } } //右移 void moveringht() { for (int i = 0; i < 4; i++) { for (int j = 2, k = 3; j >= 0; j--) { //先找到k项后面第一个不为0的项 if (board[i][j] != 0) { //分为三个情况.相等,k项是0,不等 //相等 if (board[i][k] == board[i][j]) { board[i][k--] <<= 1; board[i][j] = 0; if_need_rand = 1; } else //k项是0 if (board[i][k] == 0) { board[i][k] = board[i][j]; board[i][j] = 0; if_need_rand = 1; } //k项与j项不等,有两种情况,j项就在k项后边, //所以不能将j项赋值为0 else { board[i][--k] = board[i][j]; if (j != k) { board[i][j] = 0; if_need_rand = 1; } } } } } } //上移 void moveup() { for (int i = 0; i < 4; i++) { for (int j = 1, k = 0; j < 4; ++j) { //先找到k项后面第一个不为0的项 if (board[j][i] != 0) { //分为三个情况.相等,k项是0,不等 //相等 if (board[k][i] == board[j][i]) { board[k++][i] <<= 1; board[j][i] = 0; if_need_rand = 1; } else //k项是0 if (board[k][i] == 0) { board[k][i] = board[j][i]; board[j][i] = 0; if_need_rand = 1; } //k项与j项不等,有两种情况,j项就在k项后边, //所以不能将j项赋值为0 else { board[++k][i] = board[j][i]; if (j != k) { board[j][i] = 0; if_need_rand = 1; } } } } } } //下移 void movedown() { for (int i = 0; i < 4; i++) { for (int j = 2, k = 3; j >=0; --j) { //先找到k项后面第一个不为0的项 if (board[j][i] != 0) { //分为三个情况.相等,k项是0,不等 //相等 if (board[k][i] == board[j][i]) { board[k--][i] <<= 1; board[j][i] = 0; if_need_rand = 1; } else //k项是0 if (board[k][i] == 0) { board[k][i] = board[j][i]; board[j][i] = 0; if_need_rand = 1; } //k项与j项不等,有两种情况,j项就在k项后边, //所以不能将j项赋值为0 else { board[--k][i] = board[j][i]; if (j != k) { board[j][i] = 0; if_need_rand = 1; } } } } } } //查看有没有空着的格子 int getnull() { int n = 0; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { if (board[i][j] == 0) { n++; } } } return n; } //比较相邻的两个数,是否相等 void gameover() { for (int i = 0; i < 4; ++i) { for (int j = 0; j < 3; ++j) { if (board[i][j] == board[i][j + 1] || board[j][i] == board[j + 1][i]) { if_game_over = 0; return; } } } if_game_over = 1; } //执行函数 void startgame() { on: initgame(); while (1) { if_need_rand = 0;//首先将随机值设为0. switch (_getch()) { case 'a': case 'a': case 75: moveleft(); break; case 'd': case 'd': case 77: moveringht(); break; case 'w': case 'w': case 72: moveup(); break; case 's': case 's': case 80: movedown(); default: break; } if (if_need_rand) { addrand(); showgame(); } //游戏判断 if (getnull()==0) { gameover(); if (if_game_over) { if (messagebox(null,l"是否重来一局!",l"游戏结束",mb_yesno)==idyes) { goto on; } else { return; } } } } } int main() { startgame(); return 0; }
上一篇: 最新防雾霾脸罩