game.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define East_Mode 10
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char mine[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = ret;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("———扫雷游戏———\n");
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
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");
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = East_Mode;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')//0代表不是雷
{
mine[x][y] = '1';//1代表是雷
count--;
}
}
}
//查找所选坐标周围一圈雷的个数并返回
static int get_mine(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 board(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
//判断坐标是否越界
if (x == 0 || y == 0 || x == ROW + 1 || y == COL + 1)
return;
//判断是否已经被排除
if (show[x][y] != '*')
{
return;
}
int count = get_mine(mine, x, y);
if (count > 0)
{
show[x][y] = count + '0';
return;
}
//递归拓展地图
else if (count == 0)
{
show[x][y] = ' ';
board(mine, show, x-1, y-1);
board(mine, show, x-1, y);
board(mine, show, x-1, y+1);
board(mine, show, x, y-1);
board(mine, show, x, y+1);
board(mine, show, x+1, y-1);
board(mine, show, x+1, y);
board(mine, show, x+1, y+1);
}
}
//查找剩余未排查的‘*’的个数,如果和雷输一样,证明扫雷完成
int Travel(char show[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
int count = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j < col; j++)
{
if (show[i][j] == '*')
count++;
}
}
return count;
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
//1.输入排查的坐标并判断坐标的合法性
//2.检查坐标处是不是雷
//(1)是雷 ——很遗憾炸死了 - 游戏结束
//(2)不是雷 ——统计坐标周围有几个雷 - 存储排查到的雷的个数到show数组,游戏继续
int x = 0;
int y = 0;
int win = 0;
while (1)
{
printf("请输入想要排查位置得坐标:>");
scanf("%d %d", &x, &y);
//判断坐标合法性
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else//不是雷
{
//show[x][y] = get_mine(mine, x, y);
board(mine, show, x, y);
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("坐标非法,请重新输入。\n");
}
win = Travel(show, row, col);
if (win == East_Mode)
break;
}
if (win == East_Mode)
{
printf("恭喜你,排雷成功!\n");
DisplayBoard(mine, ROW, COL);
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#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 };//存放布置好的雷
//初始化雷
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印show棋盘
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
//DisplayBoard(mine, 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:
printf("输入错误,请重新输入。\n");
break;
}
} while (input);
return 0;
}