C++ 简单的扫雷实现

C++ 简单的扫雷实现

这里是一个刚接触编程一年的新手!
所以程序有一些地方我做不到还原原版扫雷(比如……可能会格子上会出现数字0,周围的格子需要一个一个手动扫过去……)
ps:地雷密度可能稍微有点大?不管了你们加油(手动滑稽)

#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <Windows.h>
#include <conio.h>
using namespace std;

int chess[9][9];
int endgame;

void ismine(void)		//判断是否为雷
{
	int a=3,b=3;
	do
	{
		while (a>=3 && a<=5 && b>=3 && b<=5)
		{
			a=rand()%9;
			b=rand()%9;
		}
		if (chess[a][b]==100)
		{
			a=3;b=3;
		}else
		{
			chess[a][b]=100;
			break;
		}
	}while (1);
}

int count(int x,int y)	//数地雷
{
	int a,b;
	int num=0;
	for (int n=-1;n<=1;n++)
	{
		for (int i=-1;i<=1;i++)
		{
			a=x+n;b=y+i;
			if (a>8 || a<0 || b>8 || b<0)
				continue;
			if (chess[a][b]==100)
				num++;
		}
	}
	return num;
}

void printchess(void)	//打印棋盘
{
	if (endgame==0)
	{
		printf("  1 2 3 4 5 6 7 8 9\n");
		for (int n=0;n<=8;n++)
		{
			printf("%d ",n+1);
			for (int i=0;i<=8;i++)
			{
				if (i>=3 && i<=5 && n>=3 && n<=5)
					printf("%d ",count(i,n));
				else if (chess[i][n]!=100 && chess[i][n]!=50)
				{
					printf("%d ",chess[i][n]);
				}
				else printf("■");
			}
			printf("\n");
		}
		printf("作者:奇古\n");
		printf("请输入要扫除区域:x y\n");
	}else 
	{
		printf("  1 2 3 4 5 6 7 8 9\n");
		for (int n=0;n<=8;n++)
		{
			printf("%d ",n+1);
			for (int i=0;i<=8;i++)
			{
				if (i>=3 && i<=5 && n>=3 && n<=5)
					printf("%d ",count(i,n));
				else if (chess[i][n]!=100 && chess[i][n]!=50)
					printf("%d ",chess[i][n]);
				else if (chess[i][n]==100)
					printf("* ");
				else printf("■");
			}
			printf("\n");
		}
	}
}

void fail_end(int y,int x)
{
	char beh;
	endgame=1;
	system("cls");
	printchess();
	Sleep(3000);
	system("cls");
	printf("游戏结束,按1重来,按2结束\n");
enter:beh=_getch();
	if (beh=='2')
		exit(0);
	else if (beh=='1')
		fflush(stdin);
	else goto enter;
	system("cls");
}

void win_end(void)
{
	char beh;
	endgame=1;
	system("cls");
	printchess();
	Sleep(3000);
	system("cls");
	printf("恭喜你,胜利了!\n按1重来,按2结束\n");
enter:beh=_getch();
	if (beh=='2')
		exit(0);
	else if (beh=='1')
		fflush(stdin);
	else goto enter;
	system("cls");
}



int _tmain(int argc, _TCHAR* argv[])
{
	
	while(endgame==0)
	{
		endgame=0;
		int n=0,x=-1,y=-1;
		for (x=0;x<=8;x++)		//棋盘初始化
		{
			for (y=0;y<=8;y++)
			{
				chess[x][y]=50;
			}
		}

		srand((unsigned)time(0));	//随机生成地雷
		for (;n<=15;n++)
			ismine();

		printchess();
		n=1;
		while (n<=56)
		{
			scanf("%d %d",&y,&x);
			y--;x--;
			while (x<0 || x>8 || y<0 || y>8 || chess[y][x]!=100 && chess[y][x]!=50)
			{
				printf("输入错误,请重新输入\n");
				fflush(stdin);
				scanf("%d %d",&y,&x);
				y--;x--;
			}
			if (chess[y][x]==100)
			{
				fail_end(y,x);
				endgame=0;
				break;
			}
			else
				chess[y][x]=count(y,x);
			system("cls");
			printchess();
			n++;
		}
		if (n==57)		//是否将地雷全部扫除  56  57
		{
			win_end();
			endgame=0;
		}
	}
	return 0;
}
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值