(六年级)一个全是bug的C++扫雷小游戏(欢迎挑刺)

Mint工作室出品
我不想说废话了,直接放代码吧

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define MAXN 55
#define BACKGROUND_GRAY 120
#define FOREGROUND_BLACK 8
char a[MAXN][MAXN], show[MAXN][MAXN];
bool vis[MAXN][MAXN];
int n, cnt, cntn, cntp;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
void Display() {
	SetConsoleTextAttribute(handle, BACKGROUND_GRAY | FOREGROUND_BLUE);
	for (int i = 1; i <= n; i ++) {
		for (int j = 1; j <= n; j ++) {
			if (a[i][j] == '*') {
    			SetConsoleTextAttribute(handle, BACKGROUND_GRAY | FOREGROUND_RED);
				putchar('*');
    			SetConsoleTextAttribute(handle, BACKGROUND_GRAY | FOREGROUND_BLUE);
			}
			else    putchar(a[i][j]);
			putchar(' ');
		}
		putchar('\n');
	}
}
void Lose_Show() {
	system("cls");
	system("color 74");
	printf("BOOOOOOOOOOM!!!\n");
	Display();
	SetConsoleTextAttribute(handle, BACKGROUND_GRAY | FOREGROUND_RED);
	Sleep(1000);
	printf("Game ");
	Sleep(1000);
	printf("over\n");
}
void Win_Show() {
    system("cls");
    Display();
    SetConsoleTextAttribute(handle, BACKGROUND_GRAY | FOREGROUND_RED);
	Sleep(1000);
	printf("YOU ");
	Sleep(1000);
	printf("WIN!");
}
void Check() {
    int sumn = 0, sumb = 0;
    for (int l = 1; l <= n; l ++) {
        for (int k = 1; k <= n; k ++) {
            if (show[l][k] == 'P' and a[l][k] == '*') {
                sumb ++;
                continue;
            }
            if (show[l][k] != '#')  sumn ++;
        }
    }
    // printf("\n%d %d\n%d %d\n", sumn, sumb, cntn, cnt);
    if (sumn == cntn or sumb == cnt) {
        Win_Show();
        exit(0);
    }
}
void Mark() {
	int Bumb = n * n / 6;
	cnt = cntp = Bumb + 1;
	cntn = n * n - Bumb - 1;
	int l = rand() % n + 1, r = rand() % n + 1;
	for (int sum = 0; sum <= Bumb; l = rand() % n + 1, r = rand() % n + 1)
		if (! a[l][r] and l > 2 and r > 2)
			a[l][r] = '*', sum ++;
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= n; j ++)
			if (! (a[i][j] == '*')) {
				int sum = 0;
				if (a[i + 1][j] == '*')	sum ++;
				if (a[i + 1][j - 1] == '*')	sum ++;
				if (a[i + 1][j + 1] == '*')	sum ++;
				if (a[i][j - 1] == '*')	sum ++;
				if (a[i][j + 1] == '*')	sum ++;
				if (a[i - 1][j + 1] == '*')	sum ++;
				if (a[i - 1][j] == '*')	sum ++;
				if (a[i - 1][j - 1] == '*')	sum ++;
				a[i][j] = sum + '0';
			}
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= n; j ++)
			show[i][j] = '#';
}
void Print(int y, int x) {
    for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j ++) {
			if (i == y and j == x) {
			    SetConsoleTextAttribute(handle, BACKGROUND_GRAY | FOREGROUND_BLACK);
			    putchar('+');
			    SetConsoleTextAttribute(handle, BACKGROUND_GRAY | FOREGROUND_BLUE);
			}
			else if (show[i][j] == 'P') {
			    SetConsoleTextAttribute(handle, BACKGROUND_GRAY | FOREGROUND_RED);
			    putchar('P');
			    SetConsoleTextAttribute(handle, BACKGROUND_GRAY | FOREGROUND_BLUE);
			}
			else if (show[i][j] == '0') {
			    SetConsoleTextAttribute(handle, BACKGROUND_GRAY | FOREGROUND_GREEN);
			    putchar('0');
			    SetConsoleTextAttribute(handle, BACKGROUND_GRAY | FOREGROUND_BLUE);
			}
			else putchar(show[i][j]);
			putchar(' ');
		}
		putchar('\n');
	}
}
void Search(int l, int r) {
    vis[l][r] = 1;
    for (int i = l - 1; i <= l + 1; i ++)
        for (int j = r - 1; j <= r + 1; j ++)
            if (! vis[i][j]) {
                vis[i][j] = 1, show[i][j] = a[i][j];
                if (a[i][j] == '0') {
                    Search(i, j);
                }
            }
}
void Start_Game() {
    int y = 1, x = 1;
	char c;
	for (; ; c = getch()) {
		system("cls");
		printf("还剩%d根旗子\n", cntp);
		switch(c) {
			case 'a' : {
				if (x > 1)	x --;
				break;
			}
			case 'w' : {
				if (y > 1)	y --;
				break;
			}
			case 's' : {
				if (y < n)	y ++;
				break;
			}
			case 'd' : {
				if (x < n)	x ++;
				break;
			}
			case 'e' : {
				if (a[y][x] == '*') {
					Lose_Show();
					exit(0);
				}
				else {
					if (show[y][x] != a[y][x]) {
						show[y][x] = a[y][x];
					}
				}
				if (a[y][x] == '0') {
					Search(y, x);
				}
				break;
			}
			case 'f' : {
				if (show[y][x] == 'P') {
					cntp ++;
					show[y][x] = '#';
				}
				else if (cntp > 0 and show[y][x] == '#') {
					cntp --;
					show[y][x] = 'P';
				}
				break;
			}
			case 'r' : {
				int sump = 0;
				for (int i = y - 1; i <= y + 1; i ++)
					for (int j = x - 1; j <= x + 1; j ++)
						if (i != y or j != x)
							if (show[i][j] == 'P')
								sump ++;
				if (sump == show[y][x] - '0')
					for (int i = y - 1; i <= y + 1; i ++)
						for (int j = x - 1; j <= x + 1; j ++) {
							if (show[i][j] == 'P')	continue;
							if (a[i][j] == '*') {
								Lose_Show();
								exit(0);
							}
							else {
								show[i][j] = a[i][j];
							}
						}
				break;
			}
		}
		Print(y, x);
		Check();
	}
	Win_Show();
	exit(0);
}
void Tell_Rules() {
    system("color 70");
    printf("C++小游戏之:扫雷\n");
    Sleep(1500);
    printf("使用wasd控制移动\n");
    Sleep(1000);
    printf("点击f可在格子上插旗\n");
    Sleep(1500);
    printf("点击r(如果周围已插足够数量的旗)可翻开周围的格子\n");
    Sleep(1500);
    printf("对于闪屏,我无能为力...(只有大小输小点喽)\n");
    Sleep(1500);
    printf("温馨提示:作者已友情帮助你清空左上角的雷,因此你不会一开始就死掉\n");
    Sleep(1000);
    SetConsoleTextAttribute(handle, BACKGROUND_GRAY | FOREGROUND_RED);
    printf("Mint工作室出品(不得侵权,违者必究)\n");
    Sleep(2500);
    system("cls");
    system("color 70");
    printf("请输入大小:(必须大于3且小于26)");
}
void Judge() {
    cout << "What do you mean?";
    exit(0);
}
int main() {
	Tell_Rules();
	SetConsoleTextAttribute(handle, BACKGROUND_GRAY | FOREGROUND_BLUE);
	scanf("%d", &n);
	srand(time(NULL));
	if (n < 4 or n > 25)   Judge();
	Mark();
	Start_Game();
	return 0;
}

还有很多漏洞,都可以在下面评论区留言

写代码不易,点个赞再走吧!

你可能感兴趣的:(c#,游戏)