C语言-扫雷

test.c文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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 }; // 放排查雷的信息
// 初始化 mine: 0
InitBoard(mine, ROWS, COLS, '0');
// 初始化 show: *
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:
printf("输入错误\n");
break;
}

} while (input);

return 0;
}

game.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"


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 (i = 0; i <= col; 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 board[ROWS][COLS], int row, int col)
{
int count = EASY_CONUT; // 雷的个数
while (count)
{
int x = rand() % row + 1; // 1 - 9
int y = rand() % row + 1; // 1 - 9
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}

int CountMine(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_CONUT)
{
printf("请输入要排查的坐标:");
scanf("%d %d", &x, &y);

if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf("您刚输入的位置,已经排查过了");
}
else if (mine[x][y] == '1') // 踩雷了
{
printf("你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
win++;
// 统计这个坐标周围有几个雷
int count = CountMine(mine, x, y);
show[x][y] = count + '0'; // show 是字符数组,要将count转为字符,也就是+'0'
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("坐标输入错误,请重新输入\n");
}
}

if (win == row * col - EASY_CONUT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}

game.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#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_CONUT 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);