You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

498 lines
10 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<iomanip>
#include<cstring>
#include<conio.h>
//#include "Rand&Print.h"
//#include "do.h"
using namespace std;
using std::cout;
//using namespace xzb;
//using namespace wwh;
const int MapSize = 4;
int MAP[MapSize][MapSize];
int score{ 0 };
void Initialize(int(&MAP)[MapSize][MapSize]);//定义函数Initialize初始化MAP
int Rand_Odd_Even();//定义函数Rand_Odd_Even,在1到100之间取随机数给下面RandAddition函数备用如果取到奇数后面就添加数字2偶数就添加数字4
int RandBlank(int start, int end);//定义函数RandBlank生成start和end之间的随机整数
void RandAddition(int(&MAP)[MapSize][MapSize]);//定义函数RandAddition在空格中随机放入2或4
void PrintRule(int counter);//定义函数PrintRule,明确十六个元素的输出法则
void PrintMap(int(&MAP)[MapSize][MapSize]);//打印
int Updo(int(&MAP)[MapSize][MapSize]);
bool Upable(int(&MAP)[MapSize][MapSize]);
int Downdo(int(&MAP)[MapSize][MapSize]);
bool Downable(int(&MAP)[MapSize][MapSize]);
int Leftdo(int(&MAP)[MapSize][MapSize]);
bool Leftable(int(&MAP)[MapSize][MapSize]);
int Rightdo(int(&MAP)[MapSize][MapSize]);
bool Rightable(int(&MAP)[MapSize][MapSize]);
int mymax(int(&MAP)[MapSize][MapSize]);
int main()
{
srand(static_cast<unsigned int>(time(0)));
std::cout << "点击上下左右进行操作" << endl;
cout << "当上下左右都尝试过并且无法操作时,游戏结束" << endl;
Initialize(MAP);
for (int i{ 1 }; i <= 15; ++i)
{
RandAddition(MAP);
}
PrintMap(MAP);//void 函数,打印表格
bool up = true;//上滑能否操作
bool left = true;
bool down = true;
bool right = true;
bool able = true;//游戏结束标志
while (able)//判断能不能操作一次
{
able = up || down || left || right;
char c=_getch();//输入
switch (c)
{ case 72:
{
if (Upable(MAP))
{
up = true;
down = true;
left = true;
right = true;
score = score;
RandAddition(MAP);
cout << "得分:" << score << endl;
}
else up = false, cout << "无法上滑操作" << endl;
}//int 上滑并计算分数
PrintMap(MAP);
break;
case 75:
{
if (Leftable(MAP))
{
up = true;
down = true;
left = true;
right = true;
score = score;
RandAddition(MAP);
cout << "得分:" << score << endl;
}
else left = false, cout << "无法左滑操作" << endl;
}//int 左滑
PrintMap(MAP);
break;
case 80:
{
if (Downable(MAP))
{
up = true;
down = true;
left = true;
right = true;
score = score;
RandAddition(MAP);
cout << "得分:" << score << endl;
}
else down = false, cout << "无法下滑操作" << endl;
}//int 下滑
PrintMap(MAP);
break;
case 77 :
{
if (Rightable(MAP))
{
up = true;
down = true;
left = true;
right = true;
score = score;
RandAddition(MAP);
cout << "得分:" << score << endl;
}
else right = false, cout << "无法右滑操作" << endl;
}//int 右滑
PrintMap(MAP);
break;
default:
break;
}
}
cout << "您本次游戏获得的分数是:" << score << endl;
cout << "您本次游戏合成最大的数为:" << mymax(MAP) << endl;//查找最大的数
}
void Initialize(int(&MAP)[MapSize][MapSize])//定义函数Initialize初始化MAP
{
for (int i = 0; i < MapSize; i++)
{
for (int j = 0; j < MapSize; j++)
{
MAP[i][j] = 0;
}
}
}
int Rand_Odd_Even()//定义函数Rand_Odd_Even,在1到100之间取随机数给下面RandAddition函数备用如果取到奇数后面就添加数字2偶数就添加数字4
{
int a = 1, b = 100;
return (rand() % (b - a + 1)) + a;
}
int RandBlank(int start, int end)//定义函数RandBlank生成start和end之间的随机整数
{
return (rand() % (end - start + 1)) + start;
}
void RandAddition(int(&MAP)[MapSize][MapSize])//定义函数RandAddition在空格中随机放入2或4
{
int additionDiamonds{};//添加的数字是2还是4
if (Rand_Odd_Even() % 2 == 0)
additionDiamonds = 4;
else additionDiamonds = 2;
int emptyPlaid[MapSize * MapSize][2] = { 0 };//定义数组emptyPlaid储存空的格子对应的编号
int emptyPlaidLine{ 0 };//定义整型emptyPlaidLine作为数组emptyPlaid的行编号
for (int i = 0; i < MapSize; i++)
{
for (int j = 0; j < MapSize; j++)
{
if (MAP[i][j] == 0)
{
emptyPlaid[emptyPlaidLine][0] = i;
emptyPlaid[emptyPlaidLine][1] = j;
++emptyPlaidLine;
}
}
}
int lock = RandBlank(0, emptyPlaidLine - 1);//调用RandBlank锁定要填数字的空格子
int i = emptyPlaid[lock][0];
int j = emptyPlaid[lock][1];
MAP[i][j] = additionDiamonds;
}
void PrintRule(int counter)//定义函数PrintRule,明确十六个元素的输出法则
{
int t = MAP[counter / 4][counter % 4];
if (t != 0)
{
cout << left << setw(6) << setfill(' ') << t;
}
else
{
cout << left << setw(6) << setfill(' ') << "-";
}
}
void PrintMap(int(&MAP)[MapSize][MapSize])
{
int counter{};
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
PrintRule(counter++);
}
cout << endl << endl;
}
}
int Updo(int(&MAP)[MapSize][MapSize])
{
int col{ 0 };
int row{ 0 };
for (col = 0; col <= 3; ++col)//列
{
for (row = 0; row <= 2; ++row)
{
if (MAP[row][col] == 0)//判断0的存在与否
{
for (int i{ row }; i <= 2; ++i)
{
MAP[i][col] = MAP[i + 1][col];
}
MAP[3][col] = 0;
}
}
for (row = 0; row <= 2; ++row)
{
if (MAP[row][col] == 0)//判断0的存在与否
{
for (int i{ row }; i <= 2; ++i)
{
MAP[i][col] = MAP[i + 1][col];
}
MAP[3][col] = 0;
}
}
for (row = 0; row <= 2; ++row)
{
if (MAP[row][col] == 0)//判断0的存在与否
{
for (int i{ row }; i <= 2; ++i)
{
MAP[i][col] = MAP[i + 1][col];
}
MAP[3][col] = 0;
}
}
row = 0;
if (MAP[0][col] == MAP[1][col])//不用考虑0一样操作
{
score += MAP[0][col];
MAP[0][col] *= 2;
if (MAP[2][col] == MAP[3][col])//不能一次性合成两次
{
score += MAP[1][col];
MAP[1][col] = 2 * MAP[2][col];
MAP[2][col] = 0, MAP[3][col] = 0;
}
else MAP[1][col] = MAP[2][col], MAP[2][col] = MAP[3][col], MAP[3][col] = 0;
}
else if (MAP[1][col] == MAP[2][col])
{
score += MAP[1][col];
MAP[1][col] *= 2;
MAP[2][col] = MAP[3][col];
MAP[3][col] = 0;
}
else if (MAP[2][col] == MAP[3][col])
{
score += MAP[2][col];
MAP[2][col] *= 2;
MAP[3][col] = 0;
}
else;
}
return score;
}
bool Upable(int(&MAP)[MapSize][MapSize])
{
int is{ 0 };
int MAP2[MapSize][MapSize];
for (int row{ 0 }; row <= 3; ++row)
{
for (int col{ 0 }; col <= 3; ++col)
{
MAP2[row][col] = MAP[row][col];
}
}
Updo(MAP);
for (int row{ 0 }; row <= 3; ++row)
{
for (int col{ 0 }; col <= 3; ++col)
{
if (MAP2[row][col] != MAP[row][col])
is += 1;
}
}
return is > 0 ? true : false;
}
int Downdo(int(&MAP)[MapSize][MapSize])
{
int col{ 0 };
int row{ 0 };
for (col = 0; col <= 3; ++col)//列
{
for (row = 3; row >= 1; --row)
{
if (MAP[row][col] == 0)
{
for (int i{ row }; i >= 1; --i)
{
MAP[i][col] = MAP[i - 1][col];
}
MAP[0][col] = 0;
}
}
for (row = 3; row >= 1; --row)
{
if (MAP[row][col] == 0)
{
for (int i{ row }; i >= 1; --i)
{
MAP[i][col] = MAP[i - 1][col];
}
MAP[0][col] = 0;
}
}
for (row = 3; row >= 1; --row)
{
if (MAP[row][col] == 0)
{
for (int i{ row }; i >= 1; --i)
{
MAP[i][col] = MAP[i - 1][col];
}
MAP[0][col] = 0;
}
}
row = 0;
if (MAP[3][col] == MAP[2][col])//不用考虑0一样操作
{
score += MAP[3][col];
MAP[3][col] *= 2;
if (MAP[1][col] == MAP[0][col])
{
score += MAP[1][col];
MAP[2][col] = 2 * MAP[1][col];
MAP[1][col] = 0, MAP[0][col] = 0;
}
else MAP[2][col] = MAP[1][col], MAP[1][col] = MAP[0][col], MAP[0][col] = 0;
}
else if (MAP[1][col] == MAP[2][col])
{
score += MAP[1][col];
MAP[2][col] *= 2;
MAP[1][col] = MAP[0][col];
MAP[0][col] = 0;
}
else if (MAP[1][col] == MAP[0][col])
{
score += MAP[1][col];
MAP[1][col] *= 2;
MAP[0][col] = 0;
}
else;
}
return score;
}
bool Downable(int(&MAP)[MapSize][MapSize])
{
int is{ 0 };
int MAP2[MapSize][MapSize];
for (int row{ 0 }; row <= 3; ++row)
{
for (int col{ 0 }; col <= 3; ++col)
{
MAP2[row][col] = MAP[row][col];
}
}
Downdo(MAP);
for (int row{ 0 }; row <= 3; ++row)
{
for (int col{ 0 }; col <= 3; ++col)
{
if (MAP2[row][col] != MAP[row][col])
is += 1;
}
}
return is > 0 ? true : false;
}
int Leftdo(int(&MAP)[MapSize][MapSize])//相当于交换row col
{
int row{ 0 };
int col{ 0 };
int a[MapSize][MapSize];
for (int row = 0; row <= 3; ++row)
{
for (int col = 0; col <= 3; ++col)
{
a[col][row] = MAP[row][col];
}
}
Updo(a);
for (int row = 0; row <= 3; ++row)
{
for (int col = 0; col <= 3; ++col)
{
MAP[col][row] = a[row][col];
}
}
return score;
}
bool Leftable(int(&MAP)[MapSize][MapSize])
{
int is{ 0 };
int MAP2[MapSize][MapSize];
for (int row{ 0 }; row <= 3; ++row)
{
for (int col{ 0 }; col <= 3; ++col)
{
MAP2[row][col] = MAP[row][col];
}
}
Leftdo(MAP);
for (int row{ 0 }; row <= 3; ++row)
{
for (int col{ 0 }; col <= 3; ++col)
{
if (MAP2[row][col] != MAP[row][col])
is += 1;
}
}
return is > 0 ? true : false;
}
int Rightdo(int(&MAP)[MapSize][MapSize])//相当于交换row col
{
int row{ 0 };
int col{ 0 };
int a[MapSize][MapSize];
for (int row = 0; row <= 3; ++row)
{
for (int col = 0; col <= 3; ++col)
{
a[col][row] = MAP[row][col];
}
}
Downdo(a);
for (int row = 0; row <= 3; ++row)
{
for (int col = 0; col <= 3; ++col)
{
MAP[col][row] = a[row][col];
}
}
return score;
}
bool Rightable(int(&MAP)[MapSize][MapSize])
{
int is{ 0 };
int MAP2[MapSize][MapSize];
for (int row{ 0 }; row <= 3; ++row)
{
for (int col{ 0 }; col <= 3; ++col)
{
MAP2[row][col] = MAP[row][col];
}
}
Rightdo(MAP);
for (int row{ 0 }; row <= 3; ++row)
{
for (int col{ 0 }; col <= 3; ++col)
{
if (MAP2[row][col] != MAP[row][col])
is += 1;
}
}
return is > 0 ? true : false;
}
int mymax(int(&MAP)[MapSize][MapSize])
{
int a{ 0 };
for (int row{ 0 }; row <= 3; ++row)
{
for (int col{ 0 }; col <= 3; ++col)
{
a = a >= MAP[row][col] ? a : MAP[row][col];
}
}
return a;
}