扫雷游戏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();
	}
}
相关推荐
伴杯猫23 分钟前
【ESP32-IDF】高级外设开发3:I2S
c语言·单片机·嵌入式硬件·mcu·物联网·esp32·esp-idf
Mark127725 分钟前
Trie 树(字典树)
c++·mark1277
Jiezcode1 小时前
Unreal Engine ClassName Rule
c++·游戏·图形渲染·虚幻引擎
你也向往长安城吗1 小时前
基于 navmesh 的路径搜索技术剖析 (游戏、三维方向必看!!!)
javascript·游戏·游戏开发
long_run1 小时前
C++之静态库 vs 动态库
c++
淘晶驰AK4 小时前
51单片机与stm32单片机,先学习哪一个?
stm32·单片机·51单片机
Moonnnn.5 小时前
【51单片机学习】AT24C02(I2C)、DS18B20(单总线)、LCD1602(液晶显示屏)
笔记·单片机·学习·51单片机
wind_one16 小时前
项目:电动车报警器
stm32·单片机·嵌入式硬件
risc-v@cn6 小时前
【在ubuntu下使用vscode打开c++的make项目及编译调试】
c++·vscode·ubuntu
让我们一起加油好吗7 小时前
【C++】多态(详解)
c++·visualstudio·多态·虚函数