扫雷游戏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();
	}
}
相关推荐
sakabu16 分钟前
ESP32 外设驱动开发指南 (ESP-IDF框架)——GPIO篇:基础配置、外部中断与PWM(LEDC模块)应用
笔记·单片机·学习·esp32
源代码•宸1 小时前
深入浅出设计模式——创建型模式之单例模式 Singleton
开发语言·c++·经验分享·单例模式·设计模式
是孑然呀2 小时前
【笔记】重学单片机(51)
笔记·单片机·嵌入式硬件
蒋星熠2 小时前
字母异位词分组(每天刷力扣hot100系列)
开发语言·c++·算法·leetcode·职场和发展
Darkwanderor2 小时前
哈希相关的模拟实现
数据结构·c++·算法·哈希算法
封奚泽优3 小时前
使用Python制造扫雷游戏
开发语言·python·游戏·pygame·扫雷·random
Murphy_lx3 小时前
C++多态的原理
java·开发语言·c++
武晓兵4 小时前
51单片机和 STM32 有何区别
单片机
CC呢4 小时前
基于单片机胎压检测/锅炉蒸汽压力/气压检测系统
单片机·嵌入式硬件·胎压检测·空气压力
Tjyuking4 小时前
OS架构整理
运维·c++·缓存·架构·操作系统