c语言编写的扫雷游戏

c语言编写的扫雷游戏

运行环境MSVS2017
在创建的控制台项目中运行
粘贴过去代码之后可能由于VS版本原因会有报错,
但仍可以运行,代码写得不是很好,欢迎各位大佬批评指正.

操作方式

asdw控制方向

o:打开

f:插旗
在这里插入图片描述

// 扫雷.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
#include<stdio.h>
#include<time.h>
#include<Windows.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<math.h>
//#include<graphics.h>尽量不用
using namespace std;
#define MaxWid 8
#define MaxHei 8
#define BombNum 5
#define Cube "■"
#define Flag  "▲"
#define Close "×"
#define Right "√"
#define Boom "¤"


//全局变量
struct Cell
{
	int num;
	int isboom;
	int isseen;
	int isclose;
	
	/*Cell(int a,int b,int c,int d)
	{
		num = a;
		isboom = b;
		isseen = c;
		isclose = d;
	}*/
}cell[MaxHei][MaxWid];
//□●▲■特殊符号╳¤✔①②③④⑤⑥⑦⑧
struct 
{
	char cube[3];
	char flag[3];
	char close[3];
	char right[3];
	char boom[3];

}Icon = 
{
	Cube,
	Flag ,
	Close,
	Right,
	Boom
};
struct 
{
	int x;
	int y;
}bomb[BombNum];
char number[9][3] = {
	"□","①","②", "③", "④","⑤", "⑥", "⑦", "⑧"
};
enum 
{
	black, blue, green, lakeBlue, red, purple, yellow, white, gray,
	lightBlue, lightGreen, lightSimpleGreen, lightRed, lightPurple, lightYellow, brightWhite
}Color;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD coord;
int isover = 0;
int key;
int lx;
int ly;
Cell *thiscell=&cell[0][0];
int px=0,py=0;

//函数
//光标移动
void gotoxy(int x,int y)
{
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(handle, coord);
}
//0特殊情况
void open(int i, int j)
{
	

		for (int is = 0; is < MaxHei; is++)
		{
			for (int js = 0; js < MaxWid; js++)
			{
				if (  (abs(is - i) == 0 && abs(js - j) == 1) || (abs(is - i) == 1 && abs(js - j) == 0))
				{
					if (cell[is][js].isseen == 0 && cell[is][js].num == 0&& cell[is][js].isboom==0&& cell[is][js].isclose==0)
					{
						cell[is][js].isseen = 1;
						open(is, js);
					}
				}
			}
		}


	
}
//绘制地图
void paintmap()
{
	for (int i = 0; i < MaxHei; i++)
	{
		for (int j = 0; j < MaxWid; j++)
		{
			gotoxy(j*2,i);
			//■
			if (cell[i][j].isseen==0&& cell[i][j].isclose==0&&isover==0)
			{
				cout << Icon.cube;
			}
			//╳
			if (isover == 1 && cell[i][j].isboom == 0 && cell[i][j].isclose == 1&&cell[i][j].isseen==0)
			{
				cout << Icon.close;
			}
			//✔
			if (isover == 1 && cell[i][j].isboom == 1 && cell[i][j].isclose == 1 && cell[i][j].isseen == 0)
			{
				cout << Icon.right;
			}
			//"□","①","②", "③", "④","⑤", "⑥", "⑦", "⑧"
			if ((cell[i][j].isseen == 1 && cell[i][j].isboom == 0)||( isover==1&&cell[i][j].isboom == 0))
			{							
					cout <<number[cell[i][j].num];
					if (cell[i][j].num == 0)
					{
						open(i, j);
						for (int i = 0; i < MaxHei; i++)
						{
							for (int j = 0; j < MaxWid; j++)
							{
								if (cell[i][j].num == 0&&cell[i][j].isseen==1)
								{
									gotoxy(j * 2, i);
									cout << number[0];
								}
							}
						}
						//paintmap();
						gotoxy(px, py);
					}
			}
			//¤
			if ((cell[i][j].isboom == 1 && cell[i][j].isseen == 1)||(isover==1&& cell[i][j].isboom == 1&&cell[i][j].isclose==0))
			{
				
				cout << Icon.boom;
				//SetConsoleTextAttribute(handle, gray);
				if(isover == 1)
				continue;
				isover =1;
				paintmap();
			}
			//▲
			if (cell[i][j].isclose == 1&&isover==0&&cell[i][j].isseen==0)
			{
				cout << Icon.flag;
			}
			
		}
	}
	if (isover == 1)
	{
		gotoxy(10, 8);
		SetConsoleTextAttribute(handle, red);
		cout << "you lose";
		getchar();
		exit(0);
	}
}
//生成炸弹?
void creatboom()
{
	int x[10];
	int y[10];
	int i = 0;
	int iscreat = 1;
	srand((unsigned)time(NULL));

	do
	{
		x[i] = rand() % (MaxHei );
		y[i] = ((rand() % MaxWid)*2);
		if(i!=0)
		for (int k = 0; k < i; k++)
		{
			if (x[k]==x[i]&&y[k]==y[i])
			{
				iscreat = 0;
				break;
			}
			iscreat = 1;
		}
		if (iscreat == 1)
		{
			bomb[i].x = x[i];
			bomb[i].y = y[i];
			i++;
		}
	} while (i<MaxHei);
}
void makenum()
{
	for(int i=0;i<MaxHei;i++)
		for (int j = 0; j < MaxWid; j++)

		{
			int bm = 0;
			for (int is = 0; is < MaxHei; is++)
			{
				for (int js = 0; js < MaxWid; js++)
				{
					if ((abs(is - i) == 1 && abs(js - j) == 1) || (abs(is - i) == 0 && abs(js - j) == 1) || (abs(is - i) == 1 && abs(js - j) == 0))
					{
						if (cell[is][js].isboom == 1)
						{
							bm++;
						}
					}
				}
			}
			cell[i][j].num = bm;
		}
}
//方块属性初始化
void inigame()
{
	for (int i = 0; i < MaxHei; i++)
	{
		for (int j = 0; j < MaxWid; j++)
		{
			cell[i][j] = {0,0,0,0 };

			for (int k = 0; k< BombNum; k++)
			{
				if (i == bomb[k].x&&(j * 2 == bomb[k].y))
				{
					cell[i][j].isboom = 1;
				}
			}
		}
	}	
}

//按键控制
//1.游戏操作
//2.光标移动
void play()
{
	
	if (_kbhit())
	{
		key = _getch();
		fflush(stdin);
		GetConsoleScreenBufferInfo(handle, &csbi);
		lx = csbi.dwCursorPosition.X;
		ly = csbi.dwCursorPosition.Y;
		switch (key)
		{
		   case 'w':
			   if (coord.Y != 0)
			   {
				   coord.Y--;
			  }
			   px = coord.X;
			   py = coord.Y;
			break;
		   case 'd':
			   if (coord.X != (MaxWid - 1)*2)
			   {
				   coord.X+=2;
			   }
			   px = coord.X;
			   py = coord.Y;
			   break;
		   case 's':
			   if (coord.Y != MaxHei - 1)
			   {
				   coord.Y++;
			   }
			   px = coord.X;
			   py = coord.Y;
			   break;
		   case 'a':
			   if (coord.X != 0)
			   {
				   coord.X-=2;
			   }
			   px = coord.X;
			   py = coord.Y;
			   break;
		   case 'f':
			   if (cell[coord.Y][coord.X / 2].isclose == 1)
			   {
				   //cout << Icon.cube;
				   //cell[coord.Y][coord.X / 2].isclose = 0;
				   thiscell->isclose = 0;
			   }
			   else
			   {
				   //cout << Icon.flag;
				  // cell[coord.Y][coord.X / 2].isclose = 1;
				   thiscell->isclose = 1;
			   }
			   paintmap();
			   gotoxy(px,py);
			   break;
		   case 'o':
			   thiscell->isseen = 1;
			   paintmap();
			   gotoxy(px, py);
			   break;
		}		
		thiscell = &(cell[coord.Y][coord.X / 2]);
		gotoxy(coord.X, coord.Y);
	}
}
void judgegame()
{
	int n=0;
	int m = 0;
	for (int is = 0; is < MaxHei; is++)
	{
		for (int js = 0; js < MaxWid; js++)
		{
			if (cell[is][js].isseen==1)
			{
				n++;
			}
			/*if (cell[is][js].isboom&&cell[is][js].isclose == 0)
			{
				m++;
			}*/
		}
	}
	if (n == 59)
	{
		isover = 1;		
		//paintmap();
		gotoxy(10, 8);
		SetConsoleTextAttribute(handle, red);
		cout << "you win";
		getchar();
		exit(0);
	}	
}
int main()
{
	//system("color f1");	
	//SetConsoleTextAttribute(handle, 8);
	
	creatboom();
	inigame();	
	makenum();
	paintmap();
	gotoxy(0, 0);
	
	while (1)
	{		
		play();
		judgegame();
		
	}
	
	system("pause");
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值