扫雷游戏C++

发布于:2025-07-31 ⋅ 阅读:(19) ⋅ 点赞:(0)

 

#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();
	}
}


网站公告

今日签到

点亮在社区的每一天
去签到