C语言康复训练-4
head.h
#pragma once
#define ROWS 11
#define COLS 11
#define ROW 9
#define COL 9
#define number 10
void menu();
void game();
void initboard(char board[ROWS][COLS], int rows, int cols, char ret);
void displayboard(char board[ROWS][COLS], int row, int col);
void mailei(char board[ROWS][COLS], int row, int col);
void pailei(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int tongji(char board[ROWS][COLS], int row, int col);
void jifa(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y);
void biaoji(char show[ROWS][COLS], int row, int col);
head.c
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include"head.h"
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请开始你的选择:\n");
scanf("%d", &input);
switch (input)
{
case 1: printf("游戏开始:\n");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
project.c
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include"head.h"
void menu()
{
printf("*************************\n");
printf("*************************\n");
printf("*******1.开始游戏********\n");
printf("*******0.退出游戏********\n");
printf("*************************\n");
printf("*************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
initboard(mine, ROWS, COLS, '0');
initboard(show, ROWS, COLS, '*');
mailei(mine, ROW, COL);
displayboard(show, ROW, COL);
pailei(mine, show, ROW, COL);
}
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;
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");
}
}
void mailei(char board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = number;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
void pailei(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
char ch = 0;
while (win < row * col - number)
{
printf("请输入雷区坐标:\n");
scanf("%d%d", &x, &y);
if (x >= 1 && y >= 1 && x <= row && y <= col)
{
if (mine[x][y] == '1')
{
printf("你输了\n");
displayboard(mine, ROW, COL);
break;
}
else
{
jifa(mine, show, row, col, x, y);
displayboard(show, row, col);
printf("是否标记:Y,不需要标记:N\n");
while ((ch = getchar()) != '\n');
scanf("%c", &ch);
switch (ch)
{
case 'Y':
biaoji(show, row, col);
break;
default:
break;
}
}
}
else
{
printf("字符非法,请重新输入:");
}
}
if (win >= row * col - number)
{
printf("扫雷成功");
}
else
{
printf("扫雷失败\n");
}
}
int tongji(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y] +
mine[x - 1][y - 1] +
mine[x - 1][y + 1] +
mine[x][y - 1] +
mine[x - 1][y + 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] - 8 * '0';
}
void jifa(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
{
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
int z = tongji(mine, x, y);
if (z == 0)
{
show[x][y] = ' ';
int i = 0;
for (i = x - 1; i <= x + 1; i++)
{
int j = 0;
for (j = y - 1; j <= y + 1; j++)
{
if (show[i][j] == '*')
{
jifa(mine, show, row, col, i, j);
}
}
}
}
else
{
show[x][y] = z + '0';
}
}
}
void biaoji(char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入要标记的坐标:");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
show[x][y] = '!';
break;
}
else
{
printf("输入非法,请重新输入:\n");
}
}
else
{
printf("输入非法,请重新输入:\n");
}
}
displayboard(show, row, col);
}
- 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
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220