2048C++源代码

原创 2015年07月09日 22:00:49

根据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;
}

热门游戏 2048 C++ 源代码 再版 分享

看到一个前辈写的2048源码,很受

2048源代码~~小游戏。C++实现

每一个学习软件工程的人,都很想自己写出游戏的代码,我也不例外,但是,能写出一些小游戏的代码,并不能说明你很厉害,而只能说明你对于这门语言你比较熟悉了。 2048这个游戏,是一两年前很火的游戏,不过现在...

2048的c++源代码

  • 2016年04月29日 00:42
  • 12KB
  • 下载

C++ 2048小游戏源代码

  • 2016年12月10日 16:58
  • 4KB
  • 下载

Unity NGUI实现2048(一)源代码

Unity NGUI实现2048 Manager.cs using UnityEngine; using System.Collections; using System.Collections...

2048 c++源代码

  • 2014年07月06日 09:31
  • 8KB
  • 下载

下载量已超越千万的爆红游戏2048(同名小3传奇、1024)源代码解密和下载(第一篇)

笔者历时一个晚上,终于完成了cocos2d-js开源版本,编码虽易,创意不易,且行且珍惜;故事是这样的,小3传奇的两位开发者花了近一年半时间才开发出这个游戏的核心玩法,却在其上架appstore后仅2...

javascript实现html网页版的2048游戏源代码

原文:javascript实现html网页版的2048游戏源代码 源代码下载地址:http://www.zuidaima.com/share/1841833892269056.htm ...

2048小游戏(Java)源码解析及源代码打包

数据结构课程设计写的2048小游戏,答辩完了就开源了,因为这次的技术文档任性地写成了傻瓜式教程了,就干脆也放出来了,供参考,源代码打包在最后面会附上。 一 实现方案 二 具体代码及程序框图分析 三 参...

2048 源代码

2048 源代码
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:2048C++源代码
举报原因:
原因补充:

(最多只允许输入30个字)