控制台扫雷游戏

一个在控制台下实现的简易扫雷

鼠标控制代码参照:http://blog.csdn.net/bnb45/article/details/8042819

c++代码:

#include <windows.h>  
#include <stdio.h>
#include <ctime>
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO bInfo;
INPUT_RECORD	mouseRec;
DWORD			res;
COORD			crPos, crHome = { 0, 0 };
int s[51][51], fa = 0, sum = 81;//fa 为失败标志,sum为计数器
int t = 1;
clock_t t1, t2;
void f3(int, int);
void f4(int, int);
void f1()//重新设置雷盘
{
	int n = 10, ti;
	int a, b;
	fa = 0;
	sum = 81;
	t = 1;
	for (int i = 0; i <= 50; i++)
	{
		for (int j = 0; j <= 50; j++)
		{
			s[i][j] = -1;
		}
	}
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = 0;
	pos.Y = 13;
	SetConsoleCursorPosition(hOut, pos);
	for (int i = 1; i <= 64; i++)
	{
		printf(" ");
	}
	pos.X = 0;
	pos.Y = 2;
	SetConsoleCursorPosition(hOut, pos);
	for (int i = 0; i <= 10; i++)
	{
		printf("# ");
	}
	printf("\n");
	for (int i = 1; i < 10; i++)
	{
		printf("# ");
		for (int j = 1; j < 10; j++)
		{
			printf("O ");
		}
		printf("# \n");
	}
	for (int i = 0; i <= 10; i++)
	{
		printf("# ");
	}
	printf("\n");
	srand(time(NULL));
	srand(time(NULL));//设置随机函数种子
	while (n > 0)
	{
		a = rand() % 10;
		b = rand() % 10;
		if ((s[a][b] != 9) && (a != 0) && (b != 0))
		{
			s[a][b] = 9;
			n--;
		}
	}
	for (int i = 1; i <= 9; i++)//设置整个棋盘的数字标号
	{
		for (int j = 1; j <= 9; j++)
		{
			if (s[i][j] == -1)
			{
				ti = 0;
				if (s[i - 1][j - 1] == 9)
				{
					ti++;
				}
				if (s[i - 1][j] == 9)
				{
					ti++;
				}
				if (s[i - 1][j + 1] == 9)
				{
					ti++;
				}
				if (s[i][j - 1] == 9)
				{
					ti++;
				}
				if (s[i][j + 1] == 9)
				{
					ti++;
				}
				if (s[i + 1][j - 1] == 9)
				{
					ti++;
				}
				if (s[i + 1][j] == 9)
				{
					ti++;
				}
				if (s[i + 1][j + 1] == 9)
				{
					ti++;
				}
				s[i][j] = ti;
			}
		}
	}
}
void f2(int x, int y)//对于点击操作执行响应
{
	if (s[x][y] == 9)
	{
		printf("您输了,点击重新开局再试一次吧!  花费时间:%3d秒", (t2 - t1) / CLOCKS_PER_SEC);
		fa = 1;
		t = 1;
	}
	if (s[x][y] == 0)
	{
		f3(x, y);
	}
	else
	{
		if (s[x][y] == 9)
		{
			FillConsoleOutputCharacter(hOut, '*', 1, crPos, &res);
		}
		else
		{
			if (s[x][y] == 1)
			{
				FillConsoleOutputCharacter(hOut, '1', 1, crPos, &res);
				sum--;
			}
			if (s[x][y] == 2)
			{
				FillConsoleOutputCharacter(hOut, '2', 1, crPos, &res);
				sum--;
			}
			if (s[x][y] == 3)
			{
				FillConsoleOutputCharacter(hOut, '3', 1, crPos, &res);
				sum--;
			}
			if (s[x][y] == 4)
			{
				FillConsoleOutputCharacter(hOut, '4', 1, crPos, &res);
				sum--;
			}
			if (s[x][y] == 5)
			{
				FillConsoleOutputCharacter(hOut, '5', 1, crPos, &res);
				sum--;
			}
			if (s[x][y] == 6)
			{
				FillConsoleOutputCharacter(hOut, '6', 1, crPos, &res);
				sum--;
			}
			if (s[x][y] == 7)
			{
				FillConsoleOutputCharacter(hOut, '7', 1, crPos, &res);
				sum--;
			}
			if (s[x][y] == 8)
			{
				FillConsoleOutputCharacter(hOut, '8', 1, crPos, &res);
				sum--;
			}
			s[x][y] = -1;
		}
	}
}
void f3(int x, int y)
{
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = 2 * x;
	pos.Y = y + 2;
	FillConsoleOutputCharacter(hOut, ' ', 1, pos, &res);
	s[x][y] = -1;
	sum--;
	for (int i = x - 1; i <= x + 1; i++)
	{
		for (int j = y - 1; j <= y + 1; j++)
		{
			if (x != i || y != j)
			{
				if (s[i][j] == 0)
				{
					f3(i, j);//若点到空白处,以递归方式翻开所有空白的地方;
				}
				else
				{
					f4(i, j);//若不是空白,则调用f4()显示其数字
				}
			}
		}
	}
}
void f4(int x, int y)
{
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = 2 * x;
	pos.Y = y + 2;
	if (s[x][y] == 1)
	{
		FillConsoleOutputCharacter(hOut, '1', 1, pos, &res);
		sum--;
	}
	if (s[x][y] == 2)
	{
		FillConsoleOutputCharacter(hOut, '2', 1, pos, &res);
		sum--;
	}
	if (s[x][y] == 3)
	{
		FillConsoleOutputCharacter(hOut, '3', 1, pos, &res);
		sum--;
	}
	if (s[x][y] == 4)
	{
		FillConsoleOutputCharacter(hOut, '4', 1, pos, &res);
		sum--;
	}
	if (s[x][y] == 5)
	{
		FillConsoleOutputCharacter(hOut, '5', 1, pos, &res);
		sum--;
	}
	if (s[x][y] == 6)
	{
		FillConsoleOutputCharacter(hOut, '6', 1, pos, &res);
		sum--;
	}
	if (s[x][y] == 7)
	{
		FillConsoleOutputCharacter(hOut, '7', 1, pos, &res);
		sum--;
	}
	if (s[x][y] == 8)
	{
		FillConsoleOutputCharacter(hOut, '8', 1, pos, &res);
		sum--;
	}
	s[x][y] = -1;
}
int main(void)
{
	printf("[扫雷][作者:huplion][光标位置] X: %2lu  Y: %2lu\n", 0, 0);
	printf("重新开局  ");
	printf("退出\n");
	f1();
	int p = 1;
	while (1)
	{
		ReadConsoleInput(hIn, &mouseRec, 1, &res);
		if (mouseRec.EventType == MOUSE_EVENT)
		{
			crPos = mouseRec.Event.MouseEvent.dwMousePosition;
			GetConsoleScreenBufferInfo(hOut, &bInfo);
			SetConsoleCursorPosition(hOut, crHome);
			if (t == 0)
			{
				t2 = clock();
			}
			printf("[扫雷][作者:huplion][光标位置] X: %2lu  Y: %2lu   时间:%3d秒", crPos.X, crPos.Y, (t2 - t1) / CLOCKS_PER_SEC);
			SetConsoleCursorPosition(hOut, bInfo.dwCursorPosition);
			if (sum == 10)
			{
				printf("恭喜您,获得了胜利!  花费时间:%3d秒", (t2 - t1) / CLOCKS_PER_SEC);
				fa = 1;
				t = 1;
				sum--;
			}
			switch (mouseRec.Event.MouseEvent.dwButtonState)
			{
			case FROM_LEFT_1ST_BUTTON_PRESSED:
			{
				if ((crPos.X >= 1 && crPos.X <= 7) && (crPos.Y == 1))
				{
					f1();
				}
				if ((crPos.X >= 10 && crPos.X <= 13) && (crPos.Y == 1))
				{
					return 1;
				}
				if (crPos.X >= 2 && crPos.Y >= 3 && crPos.X <= 18 && crPos.Y <= 11 && crPos.X % 2 == 0 && fa == 0)
				{
					if (t == 1)
					{
						t1 = clock();
						t = 0;
					}
					f2((crPos.X) / 2, crPos.Y - 2);
				}
			}break;
			case RIGHTMOST_BUTTON_PRESSED:
			{

				if (s[crPos.X / 2][crPos.Y - 2] != -1 && crPos.X % 2 == 0)
				{
					if (p > 0)
					{
						FillConsoleOutputCharacter(hOut, '?', 1, crPos, &res);
						p = -p;
					}
					else
					{
						FillConsoleOutputCharacter(hOut, 'O', 1, crPos, &res);
						p = -p;
					}
				}
			}
			break;
			default:
				break;
			}
		}
	}
	CloseHandle(hOut);
	CloseHandle(hIn);
	return 0;
}


注意:1,在VS2010,VS2013下测试可用,VC6.0无法运行;

2,只能实现鼠标左键右键操作,无法使用双击,无法实时刷新时间;


转载请标明出处。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值