#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 9//申明全局变量
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);//初始化棋盘格
void DisplayBoard(char board[ROWS][COLS], int row, int col);//打印棋盘
void SetMine(char board[ROWS][COLS], int row, int col);//埋雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//扫雷
//当没有雷的时候展开一片,这里用到递归
void extend_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
#include <stdio.h>
#include "扫雷.h"
//初始化棋盘,遍历二维数组,将每个元素修改为set字符
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set) {
?? ?int i = 0;
?? ?int j = 0;
?? ?for (i = 0; i < rows; i++) {
?? ??? ?for (j = 0; j < cols; j++) {
?? ??? ??? ?board[i][j] = set;
?? ??? ?}
?? ?}
}
//打印棋盘,运用简单的二维数组的遍历即可实现
void DisplayBoard(char board[ROWS][COLS], int row, int col) {
?? ?int i = 0;
?? ?int j = 0;
?? ?printf("-----------扫雷游戏----------\n");
?? ?for (j = 0; j <= col; j++) {
?? ??? ?if(j!=0)
?? ??? ? ? ?printf("%d ", j);
?? ??? ?else
?? ??? ?{
?? ??? ??? ?printf(" ?");
?? ??? ?}
?? ?}
?? ?printf("\n");
?? ?for (i = 1; i <= row; i++) {
?? ??? ?printf("%d ", i);
?? ??? ?for (j = 1; j <= col; j++) {
?? ??? ??? ?printf("%c ", board[i][j]);
?? ??? ?}
?? ??? ?printf("\n");
?? ?}
?? ?printf("-----------------------------\n");
}
//在mine这个二维数组随机埋雷,埋雷的数量由全局变量EASY_COUNT决定
void SetMine(char board[ROWS][COLS], int row, int col) {
?? ?int count = EASY_COUNT;
?? ?//1-9
?? ?while (count)
?? ?{
?? ??? ?//生成雷的随机坐标
?? ??? ?int x = rand() % row + 1;
?? ??? ?int y = rand() % col + 1;
?? ??? ?//布置雷
?? ??? ?if (board[x][y]=='0')
?? ??? ?{
?? ??? ??? ?board[x][y] = '1';
?? ??? ??? ?count--;
?? ??? ?}
?? ?}
}
//用于计算目前排查雷周边雷的数量
int get_mine_count(char board[ROWS][COLS], int x, int y) {
?? ?return (board[x - 1][y] + board[x - 1][y - 1] + board[x][y - 1]
?? ??? ?+ board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1]
?? ??? ?+ board[x][y + 1] + board[x - 1][y + 1] - 8 * '0');
}
//玩家进行找雷的代码,当被雷炸死和将雷全部找到游戏结束
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
?? ?int x = 0;
?? ?int y = 0;
?? ?int win = 0;
?? ?while (win<row*col-EASY_COUNT)
?? ?{
?? ??? ?printf("请输入要排查的坐标:>\n");
?? ??? ?scanf("%d %d", &x, &y);
?? ??? ?if (x >= 1 && x <= row && y >= 1 && y <= col) {
?? ??? ??? ?if (show[x][y] != '*') {
?? ??? ??? ??? ?printf("该坐标被排查过了,请重新输入\n");
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ?{
?? ??? ??? ??? ?if (mine[x][y] == '1') {
?? ??? ??? ??? ??? ?//如果是雷,游戏结束
?? ??? ??? ??? ??? ?printf("很遗憾,你被炸死了\n");
?? ??? ??? ??? ??? ?DisplayBoard(mine, ROW, COL);
?? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?win++;
?? ??? ??? ??? ??? ?//如果不是雷,就要统计周边雷的个数
?? ??? ??? ??? ??? ?int count = get_mine_count(mine, x, y);
?? ??? ??? ??? ??? ?show[x][y] = count + '0';
?? ??? ??? ??? ??? ?//展开一片
?? ??? ??? ??? ??? ?if (count == 0) {
?? ??? ??? ??? ??? ??? ?extend_mine(mine,show,x,y);
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?DisplayBoard(show, ROW, COL);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?printf("输入的坐标非法,请重新输入\n");
?? ??? ?}
?? ?}
?? ?if (win == row * col - EASY_COUNT)
?? ?{
?? ??? ?printf("恭喜你,排雷成功\n");
?? ??? ?DisplayBoard(mine, ROW, COL);
?? ?}
}
//用于找雷时快速将无雷区展开,提高玩家找雷速度
void extend_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)//这里的形参对应坐标上的数字为0
{
?? ?int x1 = x - 1;
?? ?int y1 = y - 1;
?? ?if(show[x1][y1]=='*')
?? ?{
?? ??? ?show[x1][y1] = get_mine_count(mine, x1, y1) + '0';
?? ??? ?if (get_mine_count(mine, x1, y1) == 0) {
?? ??? ??? ?extend_mine(mine, show, x1, y1);
?? ??? ?}
?? ?}
?? ?int x2 = x - 1;
?? ?int y2 = y;
?? ?if (show[x2][y2] == '*')
?? ?{
?? ??? ?show[x2][y2] = get_mine_count(mine, x2, y2) + '0';
?? ??? ?if (get_mine_count(mine, x2, y2) == 0) {
?? ??? ??? ?extend_mine(mine, show, x2, y2);
?? ??? ?}
?? ?}
?? ?int x3 = x - 1;
?? ?int y3 = y + 1;
?? ?if (show[x3][y3] == '*')
?? ?{
?? ??? ?show[x3][y3] = get_mine_count(mine, x3, y3) + '0';
?? ??? ?if (get_mine_count(mine, x3, y3) == 0) {
?? ??? ??? ?extend_mine(mine, show, x3, y3);
?? ??? ?}
?? ?}
?? ?int x4 = x;
?? ?int y4 = y - 1;
?? ?if (show[x4][y4] == '*')
?? ?{
?? ??? ?show[x4][y4] = get_mine_count(mine, x4, y4) + '0';
?? ??? ?if (get_mine_count(mine, x4, y4) == 0) {
?? ??? ??? ?extend_mine(mine, show, x4, y4);
?? ??? ?}
?? ?}
?? ?int x5 = x;
?? ?int y5 = y + 1;
?? ?if (show[x5][y5] == '*')
?? ?{
?? ??? ?show[x5][y5] = get_mine_count(mine, x5, y5) + '0';
?? ??? ?if (get_mine_count(mine, x5, y5) == 0) {
?? ??? ??? ?extend_mine(mine, show, x5, y5);
?? ??? ?}
?? ?}
?? ?int x6 = x + 1;
?? ?int y6 = y - 1;
?? ?if (show[x6][y6] == '*')
?? ?{
?? ??? ?show[x6][y6] = get_mine_count(mine, x6, y6) + '0';
?? ??? ?if (get_mine_count(mine, x6, y6) == 0) {
?? ??? ??? ?extend_mine(mine, show, x6, y6);
?? ??? ?}
?? ?}
?? ?int x7 = x + 1;
?? ?int y7 = y;
?? ?if (show[x7][y7] == '*')
?? ?{
?? ??? ?show[x7][y7] = get_mine_count(mine, x7, y7) + '0';
?? ??? ?if (get_mine_count(mine, x7, y7) == 0) {
?? ??? ??? ?extend_mine(mine, show, x7, y7);
?? ??? ?}
?? ?}
?? ?int x8 = x + 1;
?? ?int y8 = y + 1;
?? ?if (show[x8][y8] == '*')
?? ?{
?? ??? ?show[x8][y8] = get_mine_count(mine, x8, y8) + '0';
?? ??? ?if (get_mine_count(mine, x8, y8) == 0) {
?? ??? ??? ?extend_mine(mine, show, x8, y8);
?? ??? ?}
?? ?}
}
#include <stdio.h>
#include "扫雷.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 };//存放排查出雷的信息
?? ?//初始化数组的内容为指定的内容
?? ?//mine数组在没有布置雷的时候,都是'0'
?? ?//show数组在没有排查雷的时候,都是'*'
?? ?InitBoard(mine, ROWS, COLS,'0');
?? ?InitBoard(show, ROWS, COLS, '*');
?? ?SetMine(mine, ROW, COL);
?? ?DisplayBoard(mine, ROW, COL);
?? ?DisplayBoard(show, ROW, COL);
?? ?//排查雷
?? ?FindMine(mine, show, ROW, COL);
}
int main()
{
?? ?//设置随机数的生成
?? ?srand((unsigned int)time(NULL));
?? ?int input = 0;
?? ?do {
?? ??? ?menu();
?? ??? ?printf("请选择:>");
?? ??? ?scanf("%d", &input);
?? ??? ?switch (input)
?? ??? ?{
?? ??? ?case 1:
?? ??? ??? ?game();
?? ??? ?
?? ??? ??? ?break;
?? ??? ?case 0:
?? ??? ??? ?printf("退出游戏\n");
?? ??? ??? ?break;
?? ??? ?default:
?? ??? ??? ?break;
?? ??? ?}
?? ?} while (input);
?? ?return 0;
}