c语言----<项目>_小游戏<2048>
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;
}
计算机学科是一门科学,出现的所有问题都可以用实践来证明!