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;
}
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);
}