数字拼图游戏

本文介绍了一个使用C++实现的数字拼图游戏,包括游戏界面、颜色设置、移动逻辑和游戏状态管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# include <iostream>
# include <windows.h>
# include <conio.h>
# define ABS(x) (((x)>0) ? (x) : (0-(x)))
using namespace std;

class Console
{
public:
	Console(void)
	{
		hOut = GetStdHandle(STD_OUTPUT_HANDLE);

		CONSOLE_CURSOR_INFO cursor; 
		cursor.dwSize   =   10;
		cursor.bVisible = false;

		SetConsoleCursorInfo(hOut, &cursor); 

		COORD bSize = {80, 25};
		SetConsoleScreenBufferSize(hOut, bSize);
		SMALL_RECT wSize = {0,0, 80-1, 25-1};
		SetConsoleWindowInfo(hOut, true, &wSize);

		SetConsoleTitle("数字拼图游戏");
	}
protected:
	HANDLE hOut;

	void SetColor(int code)
	{
		switch (code)
		{
		case 0:
			{
				SetConsoleTextAttribute(hOut, 0x07);
				break;
			}
		case 1:
			{
				SetConsoleTextAttribute(hOut, 0x70);
				break;
			}
		case 2:
			{
				SetConsoleTextAttribute(hOut, 0x80);
				break;
			}
		}
	}
	void GotoXy(int x, int y)
	{
		COORD pos = {x, y};

		SetConsoleCursorPosition(hOut, pos);
	}
	void GotoPos(int x, int y)
	{
		Console::GotoXy(y*6+2, x*2+1);
	}
};

class Game : private Console
{
public:
	Game(void)
	{
		char *tableline[] = {
			{"┏━━┳━━┳━━┓"},
			{"┃    ┃    ┃    ┃"},
			{"┣━━╋━━╋━━┫"},
			{"┗━━┻━━┻━━┛"}
		};
		Console::SetColor(0);

		for (int count = 0; count < 7; count++)
		{
			switch (count)
			{
			case 0:
				{ 
					cout<<tableline[0]<<endl;
					break;
				}
			case 6:
				{
					cout<<tableline[3]<<endl;
					break;
				}
			default:
				{
					if (count % 2) cout<<tableline[1]<<endl; 
					else cout<<tableline[2]<<endl; 
				}
			}
		}
	}
	void New()
	{
		int count_1, count_2;
		int temp = 8;

		for (count_1 = 0; count_1 < 3; count_1++)
		{
			for (count_2 = 0; count_2 < 3; count_2++)
			{
				if (count_1 == 1 && count_2 == 1)
				{
					sz[count_1][count_2] = 0;
					continue;
				}
				else sz[count_1][count_2] = temp--;
			}
		}
	}
	bool Draw()
	{
		int count_1, count_2;
		int count = 1;
		bool pd = true;

		for (count_1 = 0; count_1 < 3; count_1++)
		{
			for (count_2 = 0; count_2 < 3; count_2++)
			{
				if ((sz[count_1][count_2] == count++ || sz[count_1][count_2] == 0) && pd)
				{
					if (sz[count_1][count_2] == 0) count--;
					Game::DrawPos(count_1, count_2, 2);
				}
				else 
				{
					Game::DrawPos(count_1, count_2, 1);
					pd = false;
				}
			}
		}
		if (pd == true) return true;
		else return false;

		Console::SetColor(0);
	}
	void swap(int number, int null = 0)
	{
		int nx, ny, ux, uy;
		int a = find(number), b = find(null);

		nx = a/10-1; ny = a%10-1;
		ux = b/10-1; uy = b%10-1;

		if (nx == ux && ny != uy || ny == uy && nx != ux)
		{
			if (ABS(nx - ux) == 1 || ABS(ny - uy) == 1)
			{
				sz[nx][ny] ^= sz[ux][uy];
				sz[ux][uy] ^= sz[nx][ny];
				sz[nx][ny] ^= sz[ux][uy];

				Game::Draw();
			}
		}
	}
	int zh(int code)
	{
		switch (code)
		{
		case 7:	return sz[0][0];
		case 8:	return sz[0][1];
		case 9:	return sz[0][2];
		case 4:	return sz[1][0];
		case 5:	return sz[1][1];
		case 6:	return sz[1][2];
		case 1: return sz[2][0];
		case 2:	return sz[2][1];
		case 3:	return sz[2][2];
		}
		return -1;
	}
private:
	int sz[3][3];

	void DrawPos(int x, int y, int code)
	{
		switch (sz[x][y])
		{
		case 0:
			{
				Console::GotoPos(x, y);
				Console::SetColor(0);

				cout<<"    ";
				break;
			}
		default:
			{
				Console::SetColor(code);
				Console::GotoPos(x, y);

				cout<<"  "<<sz[x][y]<<" ";
				break;
			}
		}
	}
	int find(int number)
	{
		int count_1, count_2;

		for (count_1 = 1; count_1 < 4; count_1++)
		{
			for (count_2 = 1; count_2 < 4; count_2++)
			{
				if (sz[count_1-1][count_2-1] == number) return count_1*10 + count_2;
			}
		}
		return -1;
	}
};

int main(void)
{
	Game game;
	int number;

	while (1)
	{
		game.New();

		while (!game.Draw())
		{
			number = getch()-48;
			if (number > 0 && number <= 9)
			{
				game.swap(game.zh(number));
			}
		}
		getch();
	}

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值