你可以试试哦2

这篇文章详细介绍了使用C++编写的2048控制台版本游戏,包括游戏规则、矩阵操作(如合并和移动)以及输入处理和游戏状态判断。
摘要由CSDN通过智能技术生成
#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;

int const ROW = 4;
int const COL = 4;
int game[ROW][COL] = {0};

//上下左右
int const UP = 1;
int const DOWN = 2;
int const LEFT = 3;
int const RIGHT = 4;

//游戏所处的状态
int const GAME_OVER = 1;
int const GAME_WIN = 2;
int const GAME_CONTINUE = 3;

enum GameNum {
	Game_2 = 2,
	Game_4 = 4,
	Game_8 = 8,
	Game_16 = 16,
	Game_32 = 32,
	Game_64 = 64,
	Game_128 = 128,
	Game_256 = 256,
	Game_512 = 512,
	Game_1024 = 1024,
	Game_2048 = 2048,
};

//打印所得的数组
void Print() {
	system("cls");
	cout << "*****************  2048 控 制 台 版  ******************" << endl;
	cout << "*****************  By Tanzf (Intern) ******************" << endl << endl;
	for (int i = 0; i < ROW; ++i) {
		cout << "---------------------------------" << endl;
		for (int j = 0; j < COL; ++j) {
			if (game[i][j] == 0) {
				cout << "|    \t";
			} else {
				cout << "|  " << game[i][j] << "\t";
			}
		}
		cout << "|" << endl;
	}
	cout << "---------------------------------" << endl;
}


bool CreateNumber() {
	int x = -1;
	int y = -1;
	int times = 0;
	int maxTimes = ROW * COL;
	int whitch = rand() % 3;
	do {
		x = rand() % ROW;
		y = rand() % COL;
		++times;
	} while (game[x][y] != 0 && times <= maxTimes);

	//说明格子已经满了
	if (times >= maxTimes) {
		return false;
	} else {
		GameNum num;
		if (whitch == 0) {
			num = Game_4;
		} else
			num = Game_2;
		game[x][y] = num;
	}

	return true;
}

void Process(int direction) {
	switch (direction) {
		case UP:
			//最上面一行不动
			for (int row = 1; row < ROW; ++row) {
				for (int crow = row; crow >= 1; --crow) {
					for (int col = 0; col < COL; ++col) {
						//上一个格子为空
						if (game[crow - 1][col] == 0) {
							game[crow - 1][col] = game[crow][col];
							game[crow][col] = 0;
						} else {
							//合并
							if (game[crow - 1][col] == game[crow][col]) {
								game[crow - 1][col] *= 2;
								game[crow][col] = 0;
							}

						}
					}
				}
			}
			break;
		case DOWN:
			//最下面一行不动
			for (int row = ROW - 2; row >= 0; --row) {
				for (int crow = row; crow < ROW - 1; ++crow) {
					for (int col = 0; col < COL; ++col) {
						//上一个格子为空
						if (game[crow + 1][col] == 0) {
							game[crow + 1][col] = game[crow][col];
							game[crow][col] = 0;
						} else {
							//合并
							if (game[crow + 1][col] == game[crow][col]) {
								game[crow + 1][col] *= 2;
								game[crow][col] = 0;
							}

						}
					}
				}
			}
			break;
		case LEFT:
			//最左边一列不动
			for (int  col = 1; col < COL; ++col) {
				for (int ccol = col; ccol >= 1; --ccol) {
					for (int row = 0; row < ROW; ++row) {
						//上一个格子为空
						if (game[row][ccol - 1] == 0) {
							game[row][ccol - 1] = game[row][ccol];
							game[row][ccol] = 0;
						} else {
							//合并
							if (game[row][ccol - 1] == game[row][ccol]) {
								game[row][ccol - 1] *= 2;
								game[row][ccol] = 0;
							}

						}
					}
				}
			}
			break;
		case RIGHT:
			//最右边一列不动
			for (int  col = COL - 2; col >= 0; --col) {
				for (int ccol = col; ccol <= COL - 2; ++ccol) {
					for (int row = 0; row < ROW; ++row) {
						//上一个格子为空
						if (game[row][ccol + 1] == 0) {
							game[row][ccol + 1] = game[row][ccol];
							game[row][ccol] = 0;
						} else {
							//合并
							if (game[row][ccol + 1] == game[row][ccol]) {
								game[row][ccol + 1] *= 2;
								game[row][ccol] = 0;
							}

						}
					}
				}
			}
			break;
	}

}

//处理输入输出,返回上下左右
int Input() {
	//读取上下左右四个方向键
	int upArrow = 0;
	int downArrow = 0;
	int leftArrow = 0;
	int rightArrow = 0;
	int direction = 0;
	while (true) {
		upArrow = GetAsyncKeyState(VK_UP);
		downArrow = GetAsyncKeyState(VK_DOWN);
		leftArrow = GetAsyncKeyState(VK_LEFT);
		rightArrow = GetAsyncKeyState(VK_RIGHT);

		if (upArrow) {
			direction = UP;
			break;
		} else if (downArrow) {
			direction = DOWN;
			break;
		} else if (leftArrow) {
			direction = LEFT;
			break;
		} else if (rightArrow) {
			direction = RIGHT;
			break;
		}

		Sleep(100);
	}

	return direction;
}

//判断游戏状态
int Judge() {
	//赢得游戏
	for (int i = 0; i < ROW; ++i) {
		for (int j = 0; j < COL; ++j) {
			if (game[i][j] == 8192) {
				return GAME_WIN;
				break;
			}
		}
	}

	//横向检查
	for (int i = 0 ; i < ROW; ++i) {
		for (int j = 0; j < COL - 1; ++j) {
			if (!game[i][j] || (game[i][j] == game[i][j + 1])) {
				return GAME_CONTINUE;
				break;
			}
		}
	}
	//纵向检查
	for (int j = 0; j < COL; ++j) {
		for (int i = 0; i < ROW - 1; ++i) {
			if (!game[i][j] || (game[i][j] == game[i + 1][j])) {
				return GAME_CONTINUE;
				break;
			}
		}
	}

	//不符合上述两种状况,游戏结束
	return GAME_OVER;

}

int main() {
	//设置一个随机数种子
	srand((unsigned int)time(0));
	CreateNumber();
	CreateNumber();
	Print();
	int direction = 0;
	int gameState = -1;
	while (true) {
		direction = Input();

		gameState = Judge();
		if (direction && gameState == GAME_CONTINUE) {
			Process(direction);
			CreateNumber();
			Print();
			Sleep(100);
		} else if (gameState == GAME_WIN) {
			Print();
			cout << "You Win!" << endl;
			break;
		} else if (gameState == GAME_OVER) {
			Print();
			cout << "You lose!" << endl;
			break;
		}
	}

	return 0;
}

2048代码!!!

如果你喜欢的话,记得一键三连哦!求求了,这对我很重要!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值