扫雷的实现

570 words

主文件

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
#include"game.h"
//基本思路
//游戏菜单
//初始化
//打印
//放置雷
//记录

void menu()
{
printf("*******************************\n");
printf("**********1. 开始游戏**********\n");
printf("**********0. 退出游戏**********\n");
printf("*******************************\n");

}

void game()
{
char Lay[ROWS][COLS];
char Find[ROWS][COLS];

Init(Lay, ROWS, COLS, '0');
Init(Find, ROWS, COLS, '*');
print(Find, ROW, COL);
Lay_thun(Lay, ROW, COL);
check(Lay, Find, ROW, COL);
}

int main()
{
int input;
srand((unsigned)time(NULL));
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;
}

头文件

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
#pragma once

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<Windows.h>
#include<time.h>
#include<string.h>

#define ROW 9
#define COL 9

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

#define Thunder 20


void Init(char board[ROWS][COLS], int rows, int cols, char set);
//初始化棋盘,设置行数列数,设置初始字符
void print(char board[ROWS][COLS], int row, int col);
//打印棋盘
void Lay_thun(char Find[ROWS][COLS], int row, int col);
//放置雷
void check(char Lay[ROWS][COLS], char Find[ROWS][COLS], int row, int col);
//排雷
static int Statis(char Lay[ROWS][COLS], int x, int y);
//数x,y周围的雷的个数

源文件

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
106
107
108
#include"game.h"


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

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

}
printf("\n");
}
}

void Lay_thun(char Find[ROWS][COLS], int row, int col)
{

int count = Thunder;
while(count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (Find[x][y] == '0')
{
Find[x][y] = '1';
count--;
}
}

}

void check(char Lay[ROWS][COLS], char Find[ROWS][COLS], int row, int col)
{
int x, y;
int count = 0;
while (count < row * col - Thunder)
{
printf("请输入你要排查的坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (Lay[x][y] == '1')
{
printf("运气有点菜哦 TAT\n");
printf("\n\n");
print(Lay,ROW,COL);
break;
}
if (Find[x][y] == '0' || Find[x][y] == '1')
{
printf("这里你已经扫过啦\n");
}
else
{
int digit = Statis(Lay, x, y);
Find[x][y] = digit + '0';
print(Find, ROW, COL);
count++;
}

}
else
{
printf("输入的坐标范围不对哦,换一个试试\n");
}
if (count == row * col - Thunder)
{
printf("我去,你竟然有耐心做完,太厉害了吧\n");
print(Find, ROW, COL);
}
}
}

static int Statis(char Lay[ROWS][COLS], int x, int y)
{
return Lay[x - 1][y - 1] +
Lay[x][y - 1] +
Lay[x + 1][y - 1] +
Lay[x - 1][y] +
Lay[x + 1][y] +
Lay[x - 1][y + 1] +
Lay[x][y + 1] +
Lay[x + 1][y + 1] - 8 * '0';

}
Comments