# 2048 控制台版

```#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include <conio.h>   //为了读取方向键
#include <iomanip>	//设置控制台填充字符等
using namespace std;
int score = 0;
int map[4][4];    //棋盘

void showMap()    //显示棋盘
{
cout << setw(46) << "2048 by DoubleCake" << endl;
cout << setw(50) << " |-----------------------|" << endl;
for (int i = 0; i <= 3; i++)
{
cout << setw(24) << "";
for (int j = 0; j <= 3; j++)
{
if (map[i][j] == 0)
cout << setw(2) << "|" << setw(4) << " ";
else
cout << setw(2) << "|" << setw(4) << map[i][j];

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

void randNum()	//从随机位置产生数
{
int m = rand() % 4;
int n = rand() % 4;
while (map[m][n]){
m = rand() % 4;
n = rand() % 4;
}
map[m][n] = 2;
}
void startGame() //开始游戏
{
memset(map, 0, sizeof(map));
randNum();
showMap();
}
int moveUp()	//上移
{
int i, j;
int res = 0;//用来记录是否发生移动
for (j = 0; j < 4; j++){
//先补一次空位
for (i = 1; i < 4; i++){
if (map[i][j] && !map[i - 1][j]){
map[i - 1][j] = map[i][j];
map[i][j] = 0;
//把空位补到底
if (i > 1)
i -= 2;
res = 1;
}
}
//从根部开始，相同的合并
for (i = 1; i < 4; i++){
if (map[i][j] && map[i][j] == map[i - 1][j]){
map[i - 1][j] *= 2;
score += map[i][j];
map[i][j] = 0;
res = 1;
}
}
//再补一次空位
for (i = 1; i < 4; i++){
if (map[i][j] && !map[i - 1][j]){
map[i - 1][j] = map[i][j];
map[i][j] = 0;
//把空位补到底
if (i > 1)
i -= 2;
}
}
}
return res;
}

int moveDown()	//下移
{
int i, j;
int res = 0;
for (j = 0; j < 4; j++){
for (i = 2; i >= 0; i--){
if (map[i][j] && !map[i + 1][j]){
map[i + 1][j] = map[i][j];
map[i][j] = 0;
if (i < 2)
i += 2;
res = 1;
}
}
for (i = 2; i >= 0; i--){
if (map[i][j] && map[i][j] == map[i + 1][j]){
map[i + 1][j] *= 2;
score += map[i][j];
map[i][j] = 0;
res = 1;
}
}
for (i = 2; i >= 0; i--){
if (map[i][j] && !map[i + 1][j]){
map[i + 1][j] = map[i][j];
map[i][j] = 0;
if (i < 2)
i += 2;
}
}
}
return res;
}

int moveLeft()
{
int i, j;
int res = 0;
for (i = 0; i < 4; i++){
for (j = 1; j < 4; j++){
if (map[i][j] && !map[i][j - 1]){
map[i][j - 1] = map[i][j];
map[i][j] = 0;
if (j > 1)
j -= 2;
res = 1;
}
}
for (j = 1; j < 4; j++){
if (map[i][j] && map[i][j] == map[i][j - 1]){
map[i][j - 1] *= 2;
score += map[i][j];
map[i][j] = 0;
res = 1;
}
}
for (j = 1; j < 4; j++){
if (map[i][j] && !map[i][j - 1]){
map[i][j - 1] = map[i][j];
map[i][j] = 0;
if (j > 1)
j -= 2;
}
}
}
return res;
}

int moveRight()
{
int i, j;
int res = 0;
for (i = 0; i < 4; i++){
for (j = 2; j >= 0; j--){
if (map[i][j] && !map[i][j + 1]){
map[i][j + 1] = map[i][j];
map[i][j] = 0;
if (j < 2)
j += 2;
res = 1;
}
}
for (j = 2; j >= 0; j--){
if (map[i][j] && map[i][j] == map[i][j + 1]){
map[i][j + 1] *= 2;
score += map[i][j];
map[i][j] = 0;
res = 1;
}
}
for (j = 2; j >= 0; j--){
if (map[i][j] && !map[i][j + 1]){
map[i][j + 1] = map[i][j];
map[i][j] = 0;
if (j < 2)
j += 2;
}
}
}
return res;
}

int maxNum()  //棋盘最大数
{
int max = map[0][0];
for (int i = 0; i <= 3; i++)
for (int j = 0; j <= 3; j++)
if (map[i][j]>max)
max = map[i][j];
return max;
}
int Win()    //判断是否胜利
{
int flag = 0;
if (maxNum() == 2048)
{
cout << setw(45) << "You Win!" << endl;
flag = 1;
}
return flag;
}
int GameOver()       //判断是否游戏结束
{
int flag = 1;
int i, j;
//如果有空位就可以继续
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (!map[i][j])
flag = 0;
//如果没有空位但是有相邻相同的数也可以继续
if (flag == 1){
for (i = 0; i < 4; i++){
for (j = 0; j < 4; j++){
if (i != 0 && map[i][j] == map[i - 1][j])
flag = 0;
if (i != 3 && map[i][j] == map[i + 1][j])
flag = 0;
if (j != 0 && map[i][j] == map[i][j - 1])
flag = 0;
if (j != 3 && map[i][j] == map[i][j + 1])
flag = 0;
}
}
}
if (flag == 1)
cout << setw(43) << "Game Over!" << endl;
return flag;
}
void keydown() //读取方向
{
int ch = _getch();
srand((unsigned)time(NULL));
switch (ch)
{
case 72:	// top
if (moveUp()){
randNum();
system("cls");
showMap();
}
break;
case 75:	 // left
if (moveLeft()){
randNum();
system("cls");
showMap();
}
break;
case 77:	// right
if (moveRight()){
randNum();
system("cls");
showMap();
}
break;
case 80:	// down
if (moveDown()){
randNum();
system("cls");
showMap();
}
break;
default:
break;
}
}
int main() //主函数
{
system("color f9");
int makesure = 1;	//游戏结束后是否继续游戏
while (makesure)
{
system("cls");
startGame();
while (Win() + GameOver() == 0)
{
keydown();
}
cout << setw(43) << "你的最后成绩为：" << score << endl;
cout << setw(60) << "若要重新开始游戏请输入1，若要结束请输入0。" << endl;
cin >> makesure;
while (makesure != 1 && makesure != 0)
{
cout << "输入不正确，请重新输入!" << endl;
cin >> makesure;
}
}
cout << "再见！" << endl;
system("pause");
return 0;
}```

## C++实现简单的扫雷游戏（控制台版）

C++新手的代码,请各位多包涵. 用C++写的一个简单的控制台版扫雷游戏.玩家通过输入方块的坐标来翻开方块. 只是一个雏形,能够让玩家执行翻开方块的操作并且判断输赢,还未添加标记方块.游戏菜单.记录游戏时间.重新开一局等等的功能. 玩家输入坐标的方式来翻开方块只适用于小型的“雷区”,若“雷区”大了,用坐标会变得很不方便. 代码片段扫雷V1.1 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

## 2048电脑版

2048 PC版 开发环境:VS2012 + WIN8-x64 框架:MFC,主要GDI绘图 时间:1天 缺点:部分代码冗余,不知如何合并简化-- 源码及可执行程序 下载链接 http://download.csdn.net/detail/npu_wy/7369611 不足之处,多多指教! 2048电脑版

## 2048网页版源代码

<!DOCTYPE html> <html> <head> <title>2048</title> <meta charset="utf-8"/> <style> #gridPanel{ width:480px; height:480px; margin:0 auto; background-color:#bbada0; border-radius:10px; position:relative; }