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 }; //存放排查出的雷的信息
//初始化数组内容为指定的内容
//mine 数组在没有布置雷的时候,都是‘0’
InitBoard(mine, ROWS, COLS, '0');
//show 数组在没有排查雷的时候,都是‘*’
InitBoard(show, ROWS, COLS, '*');
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROWS, COLS);
DisplayBoard(show, ROWS, COLS);
FindMine(mine,show, ROW, COL);
Sleep(2500);
printf("************************************\n");
Sleep(500);
printf("************正在清理地面************\n");
Sleep(500);
printf("***************请稍等***************\n");
Sleep(500);
printf("************************************\n");
Sleep(500);
system("cls");
}
int main()
{
srand((unsigned int)time(NULL)); //设置随机数的生成起点
int num = 0;
do
{
Menu();
scanf("%d", &num);
switch (num)
{
case 0:
printf("游戏结束\n");
break;
case 1:
system("cls");
printf("游戏开始\n");
Game();
break;
default:
printf("输入错误,请重新输入。\n");
break;
}
} while (num != 0);
return 0;
}
game.h
#pragma once
#include<stdio.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
//初始化数组
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch);
//展示数组
void DisplayBoard(char board[ROWS][COLS], int rows, int cols);
//雷的布局
void SetMine(char board[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 ch)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = ch;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int rows, int cols)
{
printf("------------扫雷游戏------------\n");
for (int i = 0; i < cols - 1; i++)
{
printf("%d ", i);
}
printf("\n");
for (int i = 1; i < rows - 1; i++)
{
printf("%d ", i);
for (int j = 1; j < cols - 1; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("------------扫雷游戏------------\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count > 0)
{
int i = rand() % row + 1;
int j = rand() % col + 1;
if (board[i][j] != 'x')
{
board[i][j] = 'x';
count--;
}
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y, count, win = 0;
while (1)
{
printf("请输入排查的坐标\n");
scanf("%d %d", &x, &y);
//检查坐标正误
if (x > 0 && x <= row && y > 0 && y <= col && show[x][y] == '*')
{
//如果是雷
if (mine[x][y] == 'x')
{
printf("很遗憾,你被炸死了\n");
Sleep(1000);
DisplayBoard(mine, ROWS, COLS);
break;
}
//如果不是雷
else
{
count = 0;
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
if (mine[x + i][y + j] == 'x')
{
count++;
}
}
}
show[x][y] = count + '0'; //转换成数字字符
DisplayBoard(show, ROWS, COLS);
win++;
if (win == row * col - EASY_COUNT)
{
printf("恭喜你胜利了\n");
Sleep(1500);
DisplayBoard(mine, ROWS, COLS);
break;
}
}
}
else
{
printf("输入坐标错误\n");
}
}
}