#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctime>
using namespace std;
int main()
{
//定义一个数组并进行初始化全部为0
int GameMap[4][4] = {};//定义一个数组并进行初始化全部为0
int AgainMap[4][4];//定义一个数组存储初始化地图
int grade = 0;//定义一个变量用来存储总分数,并初始化为0;
int n = 0;
memcpy(AgainMap, GameMap, sizeof(GameMap));
srand(time(NULL));
while (true)//游戏无限循环
{
int num1 = 0;//定义1个变量初始化为0,存储2048的数量
int num2 = 0;//定义1个变量初始化为0,用来判断是否可以继续操作
system("CLS");//清屏
cout << "游戏分数 " << grade<<endl;
//随机生成2,每次有且只生成1个
for (int a = 0; a < 100; a++)
{
int x = rand() % 4;
int y = rand() % 4;
if (0 == GameMap[x][y])//如果那个位置上没有数
{
GameMap[x][y] = 2;
break;
}
else
continue;
}
//生成游戏图像
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (GameMap[i][j] < 10)
{
cout << GameMap[i][j]<< " ";
}
else if (GameMap[i][j] < 100)
{
cout << GameMap[i][j]<< " " ;
}
else if (GameMap[i][j] < 1000)
{
cout << GameMap[i][j] << " ";
}
else
{
cout << GameMap[i][j] << " ";
}
}
cout << endl;
}
int Input = getch();//从键盘读取操作
switch (Input)
{
case 's':
case 'S':
for (int i = 3; i >=0; i--)
{
for (int k = 3, j = 2; j>=0; j--)
{
if (GameMap[j][i]>0)
{
if (GameMap[k][i] == GameMap[j][i])//当2个数相等时
{
grade += GameMap[k][i];
GameMap[k][i] *= 2;
GameMap[j][i] = 0;
}
else if (0 == GameMap[k][i])//当GameMap[i][j]的下边有0时
{
GameMap[k][i] = GameMap[j][i];
GameMap[j][i] = 0;
}
else
{
--k;
GameMap[k][i] = GameMap[j][i];
if (k != j)//判断位置是否相同
{
GameMap[j][i] = 0;
}
}
}
}
}
break;
case 'w':
case 'W':
for (int i = 0; i<4; i++)
{
for (int k = 0, j = 1; j<4; j++)
{
if (GameMap[j][i]>0)
{
if (GameMap[k][i] == GameMap[j][i])//当2个数相等时
{
grade += GameMap[k][i];
GameMap[k][i] *= 2;
GameMap[j][i] = 0;
}
else if (0 == GameMap[k][i])//当GameMap[i][j]的左边有0时
{
GameMap[k][i] = GameMap[j][i];
GameMap[j][i] = 0;
}
else
{
++k;
GameMap[k][i] = GameMap[j][i];
if (k != j)//判断位置是否相同
{
GameMap[j][i] = 0;
}
}
}
}
}
break;
case 'a':
case 'A':
for (int i = 0; i < 4; i++)
{
for (int k = 0, j = 1; j < 4; j++)
{
if (GameMap[i][j]>0)
{
if (GameMap[i][k] == GameMap[i][j])//当2个数相等时
{
grade += GameMap[i][k];
GameMap[i][k] *= 2;
GameMap[i][j] = 0;
}
else if (0 == GameMap[i][k])//当GameMap[i][j]的左边有0时
{
GameMap[i][k] = GameMap[i][j];
GameMap[i][j] = 0;
}
else
{
++k;
GameMap[i][k] = GameMap[i][j];
if (k != j)//判断位置是否相同
{
GameMap[i][j] = 0;
}
}
}
}
}
break;
case 'd':
case 'D':
for (int i = 3; i>=0; i--)
{
for (int k = 3, j = 2; j >= 0; j--)
{
if (GameMap[i][j]>0)
{
if (GameMap[i][k] == GameMap[i][j])//当2个数相等时
{
grade += GameMap[i][k];
GameMap[i][k] *= 2;
GameMap[i][j] = 0;
}
else if (0 == GameMap[i][k])//当GameMap[i][j]的左边有0时
{
GameMap[i][k] = GameMap[i][j];
GameMap[i][j] = 0;
}
else
{
--k;
GameMap[i][k] = GameMap[i][j];
if (k != j)//位置是否相同
{
GameMap[i][j] = 0;
}
}
}
}
}
break;
case 'r':
case 'R':
memcpy(GameMap, AgainMap, sizeof(AgainMap));//赋值数组
break;
}
//判断游戏是否通关
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (2048 == GameMap[i][j])
{
++num1;
}
}
}
if (1 == num1)
{
n = 1;
break;
}
else//判断是否已经无法再进行操作
{
num2 = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
if (0 != GameMap[i][j])
{
//前4个(总共4元素)是判断4个角的上下左右总共2个元素是否与它相等
if (0 == i && 0 == j&& (GameMap[i][j + 1] != GameMap[i][j]) && (GameMap[i + 1][j] != GameMap[i][j]))
{
++num2;
}
else if (3 == i && 3 == j && (GameMap[i][j - 1] != GameMap[i][j]) && (GameMap[i - 1][j] != GameMap[i][j]))
{
++num2;
}
else if (0 == i && 3 == j && (GameMap[i][j - 1] != GameMap[i][j]) && (GameMap[i + 1][j] != GameMap[i][j]))
{
++num2;
}
else if (3 == i && 0 == j && (GameMap[i][j + 1] != GameMap[i][j]) && (GameMap[i - 1][j] != GameMap[i][j]))
{
++num2;
}
//后面4个是判断当i或者j为0或者4时时(总共8个元素),上下左右总共3个元素是否与它相等
else if (0 == i && (GameMap[i][j - 1] != GameMap[i][j]) && (GameMap[i][j + 1] != GameMap[i][j]) && (GameMap[i + 1][j] != GameMap[i][j]))
{
++num2;
}
else if (0 == j && (GameMap[i - 1][j] != GameMap[i][j]) && (GameMap[i + 1][j] != GameMap[i][j]) && (GameMap[i][j + 1] != GameMap[i][j]))
{
++num2;
}
else if (3 == i && (GameMap[i][j - 1] != GameMap[i][j]) && (GameMap[i][j + 1] != GameMap[i][j]) && (GameMap[i - 1][j] != GameMap[i][j]))
{
++num2;
}
else if (3 == j && (GameMap[i - 1][j] != GameMap[i][j]) && (GameMap[i + 1][j] != GameMap[i][j]) && (GameMap[i][j - 1] != GameMap[i][j]))
{
++num2;
}
//最后一个(总共4个元素)是判断中间的元素的上下左右4个元素是否与它相等
else if ((GameMap[i - 1][j] != GameMap[i][j]) && (GameMap[i + 1][j] != GameMap[i][j]) && (GameMap[i][j + 1] != GameMap[i][j]) && (GameMap[i][j -1] != GameMap[i][j]))
{
++num2;
}
}
}
}
if (16 == num2)
{
n = 2;
break;
}
}
}
switch (n)
{
case 1:
system("CLS");//清屏
cout << " 您的游戏分数为: " << grade << endl;
cout << "恭喜通关!" << endl;
break;
case 2:
system("CLS");//清屏
cout << "挑战失败!" << endl;
cout << " 您的游戏分数为: " << grade << endl;
break;
}
return 0;
}
c++2048(数组的应用)
最新推荐文章于 2024-03-15 13:04:57 发布