C++实现游戏2048

用c++实现2048

#include<iostream>
#include<random>
#include<chrono>
#include<conio.h>
#define SIZE 4
using namespace std;
//菜单
void Menu() 
{
	cout << "********************" << endl;
	cout << "****   2 0 4 8  ****" << endl;
	cout << "**** 1.开始游戏 ****" << endl;
	cout << "**** 2.最高记录 ****" << endl;
	cout << "**** 0.退出游戏 ****" << endl;
	cout << "********************" << endl;
}
//初始化游戏
int board[SIZE][SIZE] = { 0 };
 long long int score = 0;

//随机种子
static std::mt19937& GlobalRng()
{
	static std::mt19937 gen((unsigned)std::chrono::steady_clock::now().time_since_epoch().count());
	return gen;
}


//核心 
//数字生成
int GenerateNum()
{
	std::bernoulli_distribution d(0.9);
	return d(GlobalRng()) ? 2 : 4;
}


int generatenum = GenerateNum();

void Generate()
{
	const int total = SIZE * SIZE;
	uniform_int_distribution<int> dist(0, total - 1);

	for (int t = 0;t < total * 2;++t)
	{
		int idx = dist(GlobalRng());
		int r = idx / SIZE;
		int c = idx % SIZE;
		if (board[r][c] == 0)
		{
			board[r][c] = generatenum;
			return;
		}
	}
	for (int i = 0; i < SIZE;i++) 
	{
		for (int j = 0;j < SIZE;j++)
		{
			if (board[i][j] == 0)
			{
				board[i][j] = generatenum;
				return;
			}
		}
	}

}

// 复制棋盘
void CopyBoard(int dst[SIZE][SIZE], const int src[SIZE][SIZE])
{
	for (int i = 0; i < SIZE; ++i)
		for (int j = 0; j < SIZE; ++j)
			dst[i][j] = src[i][j];
}

// 判断两块棋盘是否相等(true = 相等)
bool BoardsEqual(const int a[SIZE][SIZE], const int b[SIZE][SIZE])
{
	for (int i = 0; i < SIZE; ++i)
		for (int j = 0; j < SIZE; ++j)
			if (a[i][j] != b[i][j]) return false;
	return true;
}
//游戏输赢检测
bool GameOver() 
{
	for (int i = 0;i < SIZE;i++)
	{
		for (int j = 0;j < SIZE;j++)
		{
			if (board[i][j] == 0)
			{
				return false;
			}
		}
	}
	return true;
}

//游戏界面
void GameDisplay()
{
	for (int i = 0;i < SIZE;i++)
	{
		for (int j = 0;j < SIZE;j++)
		{
			if (board[i][j] == 0)
			{
				cout << "\t" << "*";
			}
			else
			{
				cout << "\t" << board[i][j];
			}
		}
		cout << "\n" << "\n"<<"\n";
	}
}
enum Direction
{
	None = 0,
	Up,
	Down,
	Left,
	Right,
	Quit
};

Direction GameControl()
{
	int ch = _getch();//检测按键阻塞
	if (ch == 0 || ch == 0xE0)
	{
		int ch2 = _getch();
		switch (ch2)
		{
		case 72:
			return Up;
		case 80:
			return Down;
		case 75:
			return Left;
		case 77:
			return Right;
		default:
			return None;

		}
	}
	else
	{
		char c = static_cast<char>(ch);
		if (c == 'w' || c == 'W') return Up;
		if (c == 's' || c == 'S') return Down;
		if (c == 'a' || c == 'A') return Left;
		if (c == 'd' || c == 'D') return Right;
		if (c == 27 || c == 'q' || c == 'Q') return Quit;
	}
	return None;

}

void moveUp()
{
	for (int k = 0;k < SIZE -1;++k)
	{
		for (int i = 0;i < SIZE - 1;i++)
		{
			for (int j = 0;j < SIZE;j++)
			{//检测是否为空格
				if (board[i][j] == 0)
				{
					board[i][j] = board[i + 1][j];
					board[i + 1][j] = 0;
				}
				//合并
				else if (board[i][j] == board[i + 1][j])
				{
					board[i][j] = 2 * board[i][j];
					board[i + 1][j] = 0;
					//增加分数
					score += board[i][j];
				}
			}
		}
	}
}

void moveLeft()
{
	for (int k = 0; k < SIZE - 1; ++k)
	{
		for (int i = 0; i < SIZE; ++i) // 每一行
		{
			for (int j = 0; j < SIZE - 1; ++j) // 从左到右检查当前列与右侧列
			{
				if (board[i][j] == 0)
				{
					board[i][j] = board[i][j + 1];
					board[i][j + 1] = 0;
				}
				else if (board[i][j] == board[i][j + 1])
				{
					board[i][j] = 2 * board[i][j];
					board[i][j + 1] = 0;
					score += board[i][j];
				}
			}
		}
	}
}

void moveRight()
{
	for (int k = 0; k < SIZE - 1; ++k)
	{
		for (int i = 0; i < SIZE; ++i) // 每一行
		{
			for (int j = SIZE - 1; j > 0; --j) // 从右到左检查当前列与左侧列
			{
				if (board[i][j] == 0)
				{
					board[i][j] = board[i][j - 1];
					board[i][j - 1] = 0;
				}
				else if (board[i][j] == board[i][j - 1])
				{
					board[i][j] = 2 * board[i][j];
					board[i][j - 1] = 0;
					score += board[i][j];
				}
			}
		}
	}
}

void moveDown()
{
	for (int k = 0; k < SIZE - 1; ++k)
	{
		for (int i = SIZE - 1; i > 0; --i)
		{
			for (int j = 0; j < SIZE; ++j)
			{
				// 检测是否为空格(向下压缩)
				if (board[i][j] == 0)
				{
					board[i][j] = board[i - 1][j];
					board[i - 1][j] = 0;
				}
				// 合并(与上方相同则合并到下方)
				else if (board[i][j] == board[i - 1][j])
				{
					board[i][j] = 2 * board[i][j];
					board[i - 1][j] = 0;
					score += board[i][j];
				}
			}
		}
	}
}

long long int highscore = 0;
void RecordScore(long long int& score)
{
	if (score > highscore)
	{
		highscore = score;
	}
	if (highscore == 0)
	{
		cout << "还没有最高分数哦,快去挑战吧!" << endl;
	}
	else
	{
		cout << "最高分数:" << highscore << endl;
	}
}

//游戏主体
void GameBody() 
{
	score = 0;
	for (int i = 0; i < SIZE; ++i)
		for (int j = 0; j < SIZE; ++j)
			board[i][j] = 0;
	Generate();
	Generate();

	int prevBoard[SIZE][SIZE];

	while (1)
	{
		system("cls");
		cout << "当前分数:" << score << endl;
		GameDisplay();
		cout << "按方向键或 WASD 控制,按 ESC 或 Q 返回菜单" << endl;

		Direction dir = GameControl();

		// 记录移动前棋盘(按你原来逻辑,把拷贝放在移动前)
		CopyBoard(prevBoard, board);

		switch (dir)
		{
		case Up:
			moveUp();
			break;
		case Down:
			moveDown();
			break;
		case Left:
			moveLeft();
			break;
		case Right:
			moveRight();
			break;
		case Quit:
			system("cls");
			return;
		default:
			break;
		}
		
		if (!BoardsEqual(prevBoard, board))
		{
			Generate();
		}
		if (GameOver() == true)
		{
			system("cls");
			cout << "当前分数:" << score << endl;
			GameDisplay();
			cout << "游戏结束!" << endl;
			RecordScore(score);
			system("pause");
			system("cls");
			return;
		}
	}

}

int main() 
{
	while (1) 
	{
		//菜单显示
		Menu();
		int choice = 0;
		cin >> choice;
		switch (choice) 
		{
			case 1:
				GameBody();
				break;
			case 2:
				RecordScore(score);
				system("pause");
				system("cls");
				break;
			case 0:
				return 0;
			default:
				cout << "!!没有此操作!!" << endl;
				system("pause");
				system("cls");
		}

	}
	

	return 0;
}
//作者;KennyHuang
//时间:2025年11月4日
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇