c语言实现简单的扫雷小游戏

a.输入坐标来排雷。

b.雷排完之后,胜利游戏结束。

c.踩中雷之后,失败游戏结束。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<math.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define MINE_SUM 10

void gameBegin() {
	printf("+---------------------------------------+\n");
	printf("|***************************************|\n");
	printf("|****** 1.开始游戏 *** 2.游戏结束 ******|\n");
	printf("|***************************************|\n");
	printf("+---------------------------------------+\n");
}

void initBoard(char str[ROWS][COLS], char temp_) {
	for (int i = 0; i < ROWS; i++) {
		for (int j = 0; j < COLS; j++) {
			str[i][j] = temp_;
		}
	}
}

void inputBoard(char str[ROWS][COLS]) {
	for (int i = 0; i < ROWS; i++) {
		for (int j = 0; j < COLS; j++) {
			printf("%c ", str[i][j]);
		}
		printf("\n");
	}
	printf("-----------------------------------------\n");
}

void setMine(char str[ROWS][COLS]) {
	int temp_1 = 0;
	while (1) {
		int rand_x_ = rand() % 9 + 1;
		int rand_y_ = rand() % 9 + 1;
		if(str[rand_x_][rand_y_]=='0'){
			str[rand_x_][rand_y_] = '1';
			temp_1++;
		}
		if (temp_1 == 10) {
			break;
		}
	}
}

void showPlayer(char str[ROWS][COLS]) {
	printf("+--+--+--+--+--+--+--+--+--+--+\n");
	printf("| ");
	for (int i = 0; i < 10; i++) {
		printf("%d| ", i);
	}
	printf("\n");
	printf("+--+--+--+--+--+--+--+--+--+--+\n");
	for (int j = 1; j <= 9; j++) {
		printf("| ");
		printf("%d| ", j);
		for (int i = 1; i <= 9; i++) {
			printf("%c| ", str[j][i]);
		}
		printf("\n");
		printf("+--+--+--+--+--+--+--+--+--+--+\n");
	}
}

// 11  12  13
// 21  22  23
// 31  32  33

char tempMine(char str1[ROWS][COLS], int x, int y) {
	int temp = 0;
	if (str1[x - 1][y - 1] == '1') {
		temp = temp + 1;
	}
	else
	{
		temp = temp + 0;
	}
	if (str1[x - 1][y] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	if (str1[x - 1][y + 1] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	if (str1[x][y + 1] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	if (str1[x + 1][y + 1] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	if (str1[x + 1][y] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	if (str1[x + 1][y - 1] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	if (str1[x][y - 1] == '1') {
		temp = temp + 1;
	}
	else {
		temp = temp + 0;
	}
	return temp;
}

void gameIng(char str1[ROWS][COLS], char str2[ROWS][COLS]) {
	int x, y;
	int temp = 0;
	int mine_temp = 0;
	setMine(str1);//设置雷
	while (1) {
		//inputBoard(str1);
		showPlayer(str2);
		printf("请玩家输入要排除的棋盘坐标(x,y)(两个数字之间用逗号隔开):\n");
		scanf("%d,%d", &x, &y);
		if (str1[x][y] == '1') {
			printf("你被炸死了,游戏结束!\n");
			break;
		}
		if (str1[x][y] == '0') {

			str2[x][y] = tempMine(str1, x, y) + '0';
			mine_temp++;
		}
		if (mine_temp == (ROW * COL - MINE_SUM)) {
			printf("你胜利了,游戏结束!\n");
			break;
		}
		//完成游戏
		system("cls");
	}
	inputBoard(str1);
}

int main() {
	int choose_ = 0;
	char temp_1 = '0';
	char temp_2 = '?';
	srand((unsigned int)time(0));//定义随机数
	//3.定义棋盘
	//a.存储棋盘
	char mine_board_1[ROWS][COLS];
	//b.展示棋盘
	char mine_board_2[ROWS][COLS];
	//a.初始化存储棋盘
	initBoard(mine_board_1, temp_1);
	//b.初始化展示棋盘
	initBoard(mine_board_2, temp_2);
	//设置雷
	//setMine(mine_board_1);
	//打印棋盘
	//inputBoard(mine_board_1);
	//inputBoard(mine_board_2);

	while (choose_ != 2) {

		//1.定义游戏界面
		gameBegin();
		//2.玩家选择
		printf("请输入你的选择:");
		scanf("%d", &choose_);
		system("cls");
		switch (choose_)
		{
		case 1://4.开始游戏
			printf("游戏开始!\n");
			initBoard(mine_board_1, temp_1);
			initBoard(mine_board_2, temp_2);
			gameIng(mine_board_1, mine_board_2, &choose_);
			break;
		case 2:
			printf("游戏结束!\n");
			break;
		default:
			printf("输入有误请重新输入!\n");
			break;
		}

	}

}

上一篇:C/C++产生随机数


下一篇:C语言猜数字小游戏及如何生产随机数rand()