扫雷游戏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();
	}
}
相关推荐
渡我白衣21 小时前
list 与 forward_list:一场 STL 中的“链表哲学”之争
数据结构·c++·list
许商1 天前
【stm32】【edgetx】解析链接脚本文件(ld)
stm32·单片机·嵌入式硬件
charlie1145141911 天前
理解C++20的革命特性——协程支持2:编写简单的协程调度器
c++·学习·算法·设计模式·c++20·协程·调度器
云山工作室1 天前
基于单片机的三相逆变电源设计
单片机·嵌入式硬件·毕业设计·毕设
国科安芯1 天前
ASP3605电源芯片关键指标测试说明
功能测试·单片机·嵌入式硬件·pcb工艺
liulilittle1 天前
网络编程基础算法剖析:从字节序转换到CIDR掩码计算
开发语言·网络·c++·算法·通信
SundayBear1 天前
LVGL 开发指南:从入门到精通的嵌入式 GUI 实战心法
单片机·嵌入式·lvgl
刀客1231 天前
C++ 面试总结
开发语言·c++·面试
greentea_20131 天前
Codeforces Round 65 B. Progress Bar(71)
c++·算法
序属秋秋秋1 天前
《C++进阶之C++11》【智能指针】(下)
c++·笔记·学习·面试·c++11·智能指针·新特性