扫雷游戏(C语言版)

扫雷游戏(C语言版)

首先还是先构建框架,写扫雷这个游戏之前,我们首先要明白扫雷是怎么玩的,
扫雷游戏(C语言版)_第1张图片
像图片这样,如果显示一个数字,则说明这个数字旁边的8个空格,有该数字个数雷的数目

#define _CRT_SECURE_NO_WARNINGS

#include "game.h"

void menu() {
	printf("*********************************\n");
	printf("*************1.play**************\n");
	printf("*************0.exit**************\n");
	printf("*********************************\n");
}

void game() {
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	init_board(mine, ROWS, COLS, '0');
	init_board(show, ROWS, COLS, '*');
	display_board(mine, ROW, COL);
	display_board(show, ROW, COL);
}

int main() {
	int input = 0;
	do {
		menu();
		printf("请选择 > \n");
		scanf("%d", &input);
		switch (input) {
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,请重新输入\n");
			break;
		}
	} while (input);
}

正常的扫雷是99个方格,但是我们统计雷的时候,需要将旁边的一圈都要统计,这样列多了两列,行多了两行。所以初始化的时候我们要将所有的地方都初始化,打印的时候只需要打印中间的那个99方格就可以了

这里我们一共有两个棋盘,一个棋盘用来布雷,另一个棋盘使我们可以看到的棋盘。

初始化棋盘

void init_board(char board[ROWS][COLS], int rows, int cols, char set) {
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			board[i][j] = set;
		}
	}
}

打印棋盘

void display_board(char board[ROWS][COLS], int row, int col) {
	for (int i = 1; i <= row; i++) {
		if (i == 1) {
			printf("  %d ", i);
		}
		else {
			printf("%d ", i);
		}
	}
	printf("\n");
	for (int i = 1; i <= row; i++) {
		printf("%d ", i);
		for (int j = 1; j <= col; j++) {
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

布置雷,雷要在内部的棋盘布置

void set_board(char board[ROWS][COLS], int row, int col) {
	int count = EASY_COUNT;
	while (count) {
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0') {
			board[x][y] = '1';
			count--;
		}
	}
}

最后一步就是排雷了
排雷我们要想,如果这个坐标对应的是雷,应该直接让游戏结束,如果不是雷,咋应该显示出该坐标旁边有几个雷

int get(char mine[ROWS][COLS], int x, int y) {
	return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1]
		+ mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1]) - 8 * '0';
}

void play_board(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < col * row - EASY_COUNT) {
		printf("请输入要排雷的坐标\n");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col) {
			if (show[x][y] == '*') {
				if (mine[x][y] == '1') {
					printf("很遗憾,你被炸死了\n");
					break;
				}
				else {
					int ret = get(mine, x, y) + '0';
					show[x][y] = ret;
					display_board(show, ROW, COL);
					win++;
				}
			}
			else {
				printf("该坐标已经被排过了\n");
			}
		}
		else {
			printf("坐标错误,请重新输入\n");
		}
	}
	if (win == col * row - EASY_COUNT) {
		printf("恭喜你,排雷成功!\n");
	}
}

最后综合在一起就是

#define _CRT_SECURE_NO_WARNINGS

#include 
#include 
#include 

#define ROW 9
#define COL 9

#define ROWS ROW + 2
#define COLS COL + 2

#define EASY_COUNT 10

void init_board(char board[ROWS][COLS], int rows, int cols, char set) {
	for (int i = 0; i < rows; i++) {
		for (int j = 0; j < cols; j++) {
			board[i][j] = set;
		}
	}
}
void display_board(char board[ROWS][COLS], int row, int col) {
	for (int i = 1; i <= row; i++) {
		if (i == 1) {
			printf("  %d ", i);
		}
		else {
			printf("%d ", i);
		}
	}
	printf("\n");
	for (int i = 1; i <= row; i++) {
		printf("%d ", i);
		for (int j = 1; j <= col; j++) {
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}

void set_board(char board[ROWS][COLS], int row, int col) {
	int count = EASY_COUNT;
	while (count) {
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (board[x][y] == '0') {
			board[x][y] = '1';
			count--;
		}
	}
}

int get(char mine[ROWS][COLS], int x, int y) {
	return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1]
		+ mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1]) - 8 * '0';
}

void play_board(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < col * row - EASY_COUNT) {
		printf("请输入要排雷的坐标\n");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col) {
			if (show[x][y] == '*') {
				if (mine[x][y] == '1') {
					printf("很遗憾,你被炸死了\n");
					break;
				}
				else {
					int ret = get(mine, x, y) + '0';
					show[x][y] = ret;
					display_board(show, ROW, COL);
					win++;
				}
			}
			else {
				printf("该坐标已经被排过了\n");
			}
		}
		else {
			printf("坐标错误,请重新输入\n");
		}
	}
	if (win == col * row - EASY_COUNT) {
		printf("恭喜你,排雷成功!\n");
	}
}
void menu() {
	printf("*********************************\n");
	printf("*************1.play**************\n");
	printf("*************0.exit**************\n");
	printf("*********************************\n");
}

void game() {
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	init_board(mine, ROWS, COLS, '0');
	init_board(show, ROWS, COLS, '*');
	//display_board(mine, ROW, COL);
	//display_board(show, ROW, COL);
	set_board(mine, ROW, COL);
	display_board(mine, ROW, COL);
	//display_board(show, ROW, COL);
	display_board(show, ROW, COL);
	play_board(mine,show, ROW, COL);
	//display_board(show, ROW, COL);
}

int main() {
	int input = 0;
	srand((unsigned int)time(NULL));
	do {
		menu();
		printf("请选择 > \n");
		scanf("%d", &input);
		switch (input) {
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,请重新输入\n");
			break;
		}
	} while (input);
}

你可能感兴趣的