关闭

2048C++源代码

标签: 2048c++优化
316人阅读 评论(0) 收藏 举报
分类:

根据ChenReason的代码,原作者之前写的代码有很多问题,自己优化了他之前的好多不足的地方,达到了实际2048上线APP的功能,不过还是要感谢原作者的代码。附上原文链接地址http://developer.51cto.com/art/201404/434925.htm

/*By Mengxiaolong*/

#include<iostream>
#include <iomanip>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include <conio.h> //为了读取方向键
#include<windows.h>

using namespace std;

int pane[4][4]; //棋盘
int N = 1; //2的n次方

void showpane() //显示棋盘
{
cout << setw(44) << "2048 by 孟小龙" << endl;
cout << setw(50) << " *************************" << endl;

for (int i = 0; i <= 3; i++)
{
cout << setw(24) << "";
for (int j = 0; j <= 3; j++)
{
//SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED);
if (pane[i][j] == 0)
cout << setw(2) << "*" << setw(4) << " ";
else
cout << setw(2) << "*" << setw(4) << pane[i][j];

if (j == 3)
{
cout << setw(2) << "*" << endl;
cout << setw(50) << " *************************" << endl;
}
}
}
}

void newgame() //开始游戏
{
N = 1;
for (int i = 0; i <= 3; i++) //初始化棋盘
for (int j = 0; j <= 3; j++)
pane[i][j] = 0;

srand((unsigned)time(NULL));
int m = rand() % 4;
int n = rand() % 4;
int p = rand() % 4;
int q = rand() % 4;

pane[m][n] = pane[p][q] = 2;
showpane();
}

int if2n(int x) //判断x是否是2的n次方
{
int flag = 0;
for (int n = 1; n <= 11; n++)
{
if (x == pow(2, n))
{
flag = 1;
if (n>N)
N = n;
return flag;
}
}
return flag;
}

int upmove() //上移
{
int flag = 0;

{//所有数字都移动到最上边
int flag1 = 0;
int k;
for (int j = 0; j <= 3; j++)
for (int i = 0; i < 3; i++)
{
k = i;
flag1 = 0;
while (pane[k][j] == 0)
{
k++;
flag1 = 1;
if (k == 3)
break;
}
if (flag1)
{
pane[i][j] = pane[k][j];
pane[k][j] = 0;
}
}
}

for (int j = 0; j <= 3; j++)
for (int i = 0; i<3; i++)
{

if (if2n(pane[i][j] + pane[i + 1][j]) == 1)
{
pane[i][j] = pane[i][j] + pane[i + 1][j];
pane[i + 1][j] = 0;
flag = 1;
}
}
return flag;
}

int downmove() //下移
{
int flag = 0;

{//所有数字都移动到最下边
int flag1 = 0;
int k;
for (int j = 0; j <= 3; j++)
for (int i = 3; i > 0; i--)
{
k = i;
flag1 = 0;
while (pane[k][j] == 0)
{
k--;
flag1 = 1;
if (k == 0)
break;
}
if (flag1)
{
pane[i][j] = pane[k][j];
pane[k][j] = 0;
}
}
}

for (int j = 0; j <= 3; j++)
for (int i = 3; i>0; i--)
{
if (if2n(pane[i][j] + pane[i - 1][j]) == 1)
{
pane[i][j] = pane[i][j] + pane[i - 1][j];
pane[i - 1][j] = 0;
flag = 1;
}
}
return flag;
}

int leftmove() //左移
{
int flag = 0;

{//所有数字都移动到最左边
int flag1 = 0;
int k;
for (int i = 0; i <= 3; i++)
for (int j = 0; j < 3; j++)
{
k = j;
flag1 = 0;
while (pane[i][k] == 0)
{
k++;
flag1 = 1;
if (k == 3)
break;
}
if (flag1)
{
pane[i][j] = pane[i][k];
pane[i][k] = 0;
}
}
}

for (int i = 0; i <= 3; i++)
for (int j = 0; j<3; j++)
{
if (if2n(pane[i][j] + pane[i][j + 1]) == 1)
{
pane[i][j] = pane[i][j] + pane[i][j + 1];
pane[i][j + 1] = 0;
flag = 1;
}
}
return flag;
}

int rightmove() //右移
{
int flag = 0;

{//所有数字都移动到最右边
int flag1 = 0;
int k;
for (int i = 0; i <= 3; i++)
for (int j = 3; j > 0; j--)
{
k = j;
flag1 = 0;
while (pane[i][k] == 0)
{
k--;
flag1 = 1;
if (k == 0)
break;
}
if (flag1)
{
pane[i][j] = pane[i][k];
pane[i][k] = 0;
}
}
}

for (int i = 0; i <= 3; i++)//将相同的数加到一块
for (int j = 3; j>0; j--)
{
if (if2n(pane[i][j] + pane[i][j - 1]) == 1)
{
pane[i][j] = pane[i][j] + pane[i][j - 1];
pane[i][j - 1] = 0;
flag = 1;
}
}
return flag;
}

int testup() //能否上移测试
{
int flag = 0;
for (int j = 0; j <= 3; j++)
for (int i = 0; i<3; i++)
{
if ((if2n(pane[i][j] + pane[i + 1][j]) == 1) && pane[i + 1][j] || (pane[i][j] == 0 && pane[i + 1][j] == 1))
{
flag = 1;
}

/*if (pane[i][j] == 0 && pane[i+1][j]==1)
{
flag = 1;
}*/

}
return flag;
}

int testdown() //测试能否下移
{
int flag = 0;
for (int j = 0; j <= 3; j++)
for (int i = 3; i>0; i--)
{
if ((if2n(pane[i][j] + pane[i - 1][j]) == 1) && pane[i - 1][j] || (pane[i][j] == 0 && pane[i - 1][j] == 1))
{
flag = 1;
}
}
return flag;
}

int testleft() //测试能否左移
{
int flag = 0;
for (int i = 0; i <= 3; i++)
for (int j = 0; j<3; j++)
{
if ((if2n(pane[i][j] + pane[i][j + 1]) == 1) && pane[i][j + 1] || (pane[i][j] == 0 && pane[i][j+1] == 1))
{
flag = 1;
}
}
return flag;
}

int testright() //测试能否右移
{
int flag = 0;
for (int i = 0; i <= 3; i++)
for (int j = 3; j>0; j--)
{
if ((if2n(pane[i][j] + pane[i][j - 1]) == 1) && pane[i][j - 1] || (pane[i][j] == 0 && pane[i][j - 1] == 1))
{
flag = 1;
}
}
return flag;
}


int panemax() //棋盘最大数
{
int max = pane[0][0];
for (int i = 0; i <= 3; i++)
for (int j = 0; j <= 3; j++)
if (pane[i][j]>max)
max = pane[i][j];
return max;
}

int ifwin() //判断是否胜利
{
int flag = 0;
if (panemax() == 2048)
{
cout << setw(45) << "You Win!" << endl;
flag = 1;
}
return flag;
}

int ifGameOver() //判断是否游戏结束
{
int flag = 0;

if (testup() + testdown() + testleft() + testright() == 0)
{
cout << setw(43) << "Game Over!" << endl;
flag = 1;
}
return flag;
}
void addnewnumberup() //上移后添加新数
{
srand((unsigned)time(NULL));
int n;
if (1 != rand() % 4)
n = 1;
else
n = (rand() % 2+1);
int newnumber = pow(2, n);

int i = rand() % 4;
int j = rand() % 4;
while (pane[i][j]!=0)
{
i = rand() % 4;
j = rand() % 4;
}
pane[i][j] = newnumber;
}

void addnewnumberdown() //下移后添加新数
{
srand((unsigned)time(NULL));
int n;
if (1 != rand() % 4)
n = 1;
else
n = (rand() % 2 + 1);
int newnumber = pow(2, n);

int i = rand() % 4;
int j = rand() % 4;
while (pane[i][j] != 0)
{
i = rand() % 4;
j = rand() % 4;
}
pane[i][j] = newnumber;
}

void addnewnumberleft() //左移后添加新数
{
srand((unsigned)time(NULL));
int n;
if (1 != rand() % 4)
n = 1;
else
n = (rand() % 2 + 1);
int newnumber = pow(2, n);

int i = rand() % 4;
int j = rand() % 4;
while (pane[i][j] != 0)
{
i = rand() % 4;
j = rand() % 4;
}
pane[i][j] = newnumber;
}

void addnewnumberright() //右移后添加新数
{
srand((unsigned)time(NULL));
int n;
if (1 != rand() % 4)
n = 1;
else
n = (rand() % 2 + 1);
int newnumber = pow(2, n);
int i = rand() % 4;
int j = rand() % 4;
while (pane[i][j] != 0)
{
i = rand() % 4;
j = rand() % 4;
}
pane[i][j] = newnumber;
}

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() //主函数
{
system("color 2e");//system可以调用dos下的命令
int makesure = 1;
while (makesure)
{
system("cls");//清屏
newgame();
while (ifwin() + ifGameOver() == 0)
{
int c = GetDirection();
switch (c)
{
case 2:
if (testup() == 1)
{
upmove();
addnewnumberup();
system("cls");
showpane();
}
break;
case 4:
if (testdown() == 1)
{
downmove();
addnewnumberdown();
system("cls");
showpane();
}
break;
case 1:
if (testleft() == 1)
{
leftmove();
addnewnumberleft();
system("cls");
showpane();
}
break;
case 3:
if (testright() == 1)
{
rightmove();
addnewnumberright();
system("cls");
showpane();
}
break;
default:
break;
}
}
cout << setw(44) << "您最后的成绩为:" << panemax() << endl;
cout << setw(60) << "若要重新开始游戏请输入1,若要结束请输入0。" << endl;
cin >> makesure;
while (makesure != 1 && makesure != 0)
{
cout << "输入不正确,请重新输入!" << endl;
cin >> makesure;
}
}
cout << "再见!" << endl;
system("pause");
return 0;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:795次
    • 积分:39
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档