扫雷游戏C++

cpp 复制代码
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#define NUM 250
int num = NUM;
int cursorx = 1;
int cursory = 1;
int pressx = 0;
int pressy = 0;
char background[42][42];
char frontground[42][42];
void color(int c) {
	SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void xy(int x, int y) {
	COORD coord = {(short)x, (short)y};
	SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void pzkissb(int x, int y) {
	if (x < 1 || y < 1 || x > 40 || y > 40) return;
	if (background[y][x] != '0' || frontground[y][x] == '0') return;
	frontground[y][x] = background[y][x]; 
	for (int dx = x - 1; dx <= x + 1; dx++)
	for (int dy = y - 1; dy <= y + 1; dy++)
	if(background[dy][dx] != '0') 
		frontground[dy][dx] = background[dy][dx];
	for (int dx = x - 1; dx <= x + 1; dx++)
	for (int dy = y - 1; dy <= y + 1; dy++)
		pzkissb(dx, dy);
}
void input() {
	int c;
	do {
		c = getch();
	} while(kbhit() != 0);
	switch(c) {
		case 'W': {
			if(cursory > 1) cursory--;
			break;
		}
		case 'D': {
			if(cursorx < 40) cursorx++;
			break;
		}
		case 'S': {
			if(cursory < 40) cursory++;
			break;
		}
		case 'A': {
			if (cursorx > 1) cursorx--;
			break;
		}
		case 'J': {
			pressx = cursorx;
			pressy = cursory;
			if (background[pressy][pressx] != '0')
				frontground[pressy][pressx] = background[pressy][pressx];
			if (background[pressy][pressx] == '*' && frontground[pressy][pressx] != 'F') {
				system("color 0C");
				xy(0 ,40);
				printf("Game over\n");
				printf("%d ", num);
				while(true);
			}
			break;
		}		
		case 'K': {
			if (frontground[cursory][cursorx] != '#') break; 
			if (background[cursory][cursorx] != '*') {
				xy(0, 40);
				system("color 0C");
				printf("Game over\n");
				printf("%d ", num);
				while(true);
			} else {
				num--;
				frontground[cursory][cursorx] = 'F';
			}
			break;
		}
		case 'L': {
			CONSOLE_CURSOR_INFO INFO = {1, 0};
			SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &INFO); 
			break;
		}
		case ' ': {
			num++;
			if (background[cursory][cursorx] != '*')
				frontground[cursory][cursorx] = background[cursory][cursorx];
			break;
		} 
	}
}
void output() {
	xy(0, 0);
	for (int i = 1; i <= 40; i++) {
		for (int j = 1; j <= 40; j++) {
			switch(frontground[i][j]) {
				case '#': {
					color(9);
					break;
				}
				case 'F': {
					color(12);
					break;
				}
				case '0': {
					color(0);
					break;
				}
				case '1': {
					color(10);
					break;
				}
				case '2': {
					color(6);
					break;
				}
				case '3': {
					color(13);
					break;
				}
				case '4': {
					color(11);
					break;
				}
				case '5': {
					color(2);
					break;
				}
				case '6': {
					color(14);
					break;
				}
				case '7': {
					color(4);
					break;
				}
				case '8': {
					color(3);
					break;
				}
			}
			printf(" %c", frontground[i][j]);
		}
		printf(" \n");
	}
	color(15);
	xy(cursorx * 2 - 2, cursory - 1);
	printf("(");
	xy(cursorx * 2, cursory - 1);
	printf(")");
	color(14);
	xy(0, 40);
	if (num == 0) {
		system("color 0E");
		printf ("Win");
		while(true);
	}
	printf("%d ", num);
}
int main() {
	srand(time(NULL));
	system("mode con lines=42 cols=82");
	CONSOLE_CURSOR_INFO INFO = {1, 0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &INFO); 
	for (int i = 0; i < 42; i++)
	for (int j = 0; j < 42; j++) {
		background[i][j] = '0';
		frontground[i][j] = '#';
	}
	while(pressx == 0 || pressy == 0) {
		output();		
		input();
	}
	while(num > 0) {
		int tmpx = rand() % 40 + 1;
		int tmpy = rand() % 40 + 1;
		int dis = abs(tmpx - pressx) + abs(tmpy - pressy);
		if (dis >= 4 && background[tmpy][tmpx] != '*') {
			background[tmpy][tmpx] = '*';
			num--;
		}
	}
	num = NUM;
	for (int i = 1; i <= 40; i++)
	for (int j = 1; j <= 40; j++)
	if (background[i][j] != '*') {
		for (int dx = j - 1; dx <= j + 1; dx++)
		for (int dy = i - 1; dy <= i + 1; dy++)
		if (background[dy][dx] == '*')
			background[i][j]++;
	}
	while(true) {
		pzkissb(pressx, pressy);
		output();
		input();
	}
}
相关推荐
许怀楠6 分钟前
【主页介绍】
linux·c++·贪心算法·visual studio
Wave8452 小时前
STM32--时间戳,BKB,RTC
stm32·单片机·嵌入式硬件
木心爱编程2 小时前
C++链表实战:STL与手动实现详解
开发语言·c++·链表
离越词2 小时前
QTday1作业
c++·qt
m0_555762902 小时前
项目1——单片机程序审查,控制系统流程图和时序图
单片机·mongodb·流程图
HAH-HAH4 小时前
【蓝桥杯 2024 国 Java A】粉刷匠小蓝
c++·学习·数学·算法·职场和发展·蓝桥杯·组合数学
我是是是是是西红柿4 小时前
游戏中的展销系统使用的数据结构
数据结构·游戏
小吴同学·5 小时前
OPC Client第10讲:实现主界面;获取初始界面传来的所有配置信息config【C++读写Excel:xlnx;ODBC;缓冲区】
c++·wxwidgets
边疆.5 小时前
【C++】继承详解
开发语言·c++·继承
hweiyu005 小时前
C++设计模式,高级开发,算法原理实战,系统设计与实战(视频教程)
c++·算法·设计模式