1.分三个部分test.c / game.h /game.c
2.test.c 游戏执行的流程部分
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("***************************n");
printf("********** 扫雷游戏 *******n");
printf("********** 0.exit *******n");
printf("********** 1.play *******n");
printf("********** 2.rule *******n");
printf("***************************n");
}
void rule()
{
printf("操作注意:n");
printf("当你要输入扫雷的坐标是,请以 x y 形式输入(x横坐标,y纵坐标)n");
printf("当你要标记某位置为雷时,请输入 'YES' .n");//必须是大写的YES,小写不行.
//不想要标记可以任意输入都行,但是要标记必须大写YES
printf("如果不标记雷的位置,请输入NO.n");
printf("当你把所有的非雷位置都扫掉以后,游戏将获得胜利。n");
printf("祝您游戏愉快!n");
}
void game()
{
//雷的信息存储
//1.布置好的雷的信息
char mine[ROWS][COLS] = { 0 };//11*11
//2.排查出的雷的信息
char show[ROWS][COLS] = { 0 };
//初始化棋盘
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印棋盘
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);
//排雷
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 0:
printf("退出游戏n");
break;
case 1:
system("cls");//清屏
game();
break;
case 2:
system("cls");//清屏
rule();
break;
default:
printf("选择错误,请重新选择!n");
break;
}
} while (input);
return 0;
}
3. game.h 头文件和函数声明部分
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include
#include
#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 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);
//求周围雷的个数
int GetMineCount(char mine[ROWS][COLS], int x, int y);
//标记雷
void Mark(char show[ROWS][COLS], int row, int col);
//展开周围
void OpenNeighbor(char show[ROWS][COLS], char mine[ROWS][COLS],int row, int col, int x, int y, int state[ROWS][COLS]);
//展开自己
void OpenOwn(char show[ROWS][COLS], char mine[ROWS][COLS],int row, int col, int x, int y, int state[ROWS][COLS]);
4.game.c 游戏执行部分
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("-----------扫雷游戏--------------n");
for (int i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);
for (int j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("n");
}
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;//生成的余数范围(0--8)+1 = 1--9
int y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
int sum = 0;
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (mine[i][j] == '1')
{
sum++;
}
}
}
return sum;
}
void OpenOwn(char show[ROWS][COLS], char mine[ROWS][COLS],int row, int col, int x, int y, int state[ROWS][COLS])
{
if (state[x][y] == 0)
{
state[x][y] = 1;
int count = GetMineCount(mine, x, y);
show[x][y] = '0' + count;
}
}
void OpenNeighbor(char show[ROWS][COLS], char mine[ROWS][COLS],int row, int col, int x, int y, int state[ROWS][COLS])
{
if (state[x][y] == 0)
{
state[x][y] = 1;
int count1 = GetMineCount(mine, x, y);
show[x][y] = '0' + count1;
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (i >= 1 && i <= ROW && j >= 1 && j <= COL)
{
if (state[i][j] == 0)
{
int count = GetMineCount(mine, i, j);
if (count != 0)
OpenOwn(show, mine, row, col, i, j, state);
else
OpenNeighbor(show, mine, row, col, i, j, state);
}
}
}
}
}
else
{
return;
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y;
int win = 0;
int state[ROWS][COLS] = { 0 };
//一共有COL*ROW个格子,当我们排了COL*ROW-EASY_COUNT个格子的时候,扫雷就成功了
while (win < COL * ROW - EASY_COUNT)
{
char whethermark[10] = { 0 };
printf("请输入扫雷的坐标->");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
if (count != 0)
{
win += 1;
OpenOwn(show, mine, ROW, COL, x, y, state);
system("cls");
DisplayBoard(show, ROW, COL);
Sleep(2000);//睡眠2s
}
else
{
OpenNeighbor(show, mine, row, col, x, y, state);
int nums = 0;
for (int i = 1; i <= ROW; i++)
{
for (int j = 1; j <= COL; j++)
{
if (state[i][j] == 1)
{
nums++;
}
}
}
win = nums;
system("cls");
DisplayBoard(show, ROW, COL);
Sleep(2000);//睡眠2s
}
while (1)
{
printf("是否要标记雷?YES OR NOn");
scanf("%s", whethermark);
if (strcmp(whethermark, "YES") == 0)//两个字符串比较用strcmp
{
Mark(show, ROW, COL);
}
else
{
break;
}
}
}
}
else
{
printf("输入错误请重新输入n");
}
}
if (win == COL * ROW - EASY_COUNT)
{
printf("恭喜你扫雷成功!n");
printf("再来一把?n");
}
}
void Mark(char show[ROWS][COLS], int row, int col)
{
int x, y;
again:
printf("请输入要标记为雷的坐标->");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
system("cls");
show[x][y] = '#';
DisplayBoard(show, ROW, COL);
return;
}
else
{
printf("输入非法坐标,请重新输入n");
goto again;
}
}
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include#include #include #include #include #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 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); //求周围雷的个数 int GetMineCount(char mine[ROWS][COLS], int x, int y); //标记雷 void Mark(char show[ROWS][COLS], int row, int col); //展开周围 void OpenNeighbor(char show[ROWS][COLS], char mine[ROWS][COLS],int row, int col, int x, int y, int state[ROWS][COLS]); //展开自己 void OpenOwn(char show[ROWS][COLS], char mine[ROWS][COLS],int row, int col, int x, int y, int state[ROWS][COLS]);
4.game.c 游戏执行部分
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("-----------扫雷游戏--------------n");
for (int i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);
for (int j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("n");
}
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;//生成的余数范围(0--8)+1 = 1--9
int y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
int sum = 0;
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (mine[i][j] == '1')
{
sum++;
}
}
}
return sum;
}
void OpenOwn(char show[ROWS][COLS], char mine[ROWS][COLS],int row, int col, int x, int y, int state[ROWS][COLS])
{
if (state[x][y] == 0)
{
state[x][y] = 1;
int count = GetMineCount(mine, x, y);
show[x][y] = '0' + count;
}
}
void OpenNeighbor(char show[ROWS][COLS], char mine[ROWS][COLS],int row, int col, int x, int y, int state[ROWS][COLS])
{
if (state[x][y] == 0)
{
state[x][y] = 1;
int count1 = GetMineCount(mine, x, y);
show[x][y] = '0' + count1;
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (i >= 1 && i <= ROW && j >= 1 && j <= COL)
{
if (state[i][j] == 0)
{
int count = GetMineCount(mine, i, j);
if (count != 0)
OpenOwn(show, mine, row, col, i, j, state);
else
OpenNeighbor(show, mine, row, col, i, j, state);
}
}
}
}
}
else
{
return;
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y;
int win = 0;
int state[ROWS][COLS] = { 0 };
//一共有COL*ROW个格子,当我们排了COL*ROW-EASY_COUNT个格子的时候,扫雷就成功了
while (win < COL * ROW - EASY_COUNT)
{
char whethermark[10] = { 0 };
printf("请输入扫雷的坐标->");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
if (count != 0)
{
win += 1;
OpenOwn(show, mine, ROW, COL, x, y, state);
system("cls");
DisplayBoard(show, ROW, COL);
Sleep(2000);//睡眠2s
}
else
{
OpenNeighbor(show, mine, row, col, x, y, state);
int nums = 0;
for (int i = 1; i <= ROW; i++)
{
for (int j = 1; j <= COL; j++)
{
if (state[i][j] == 1)
{
nums++;
}
}
}
win = nums;
system("cls");
DisplayBoard(show, ROW, COL);
Sleep(2000);//睡眠2s
}
while (1)
{
printf("是否要标记雷?YES OR NOn");
scanf("%s", whethermark);
if (strcmp(whethermark, "YES") == 0)//两个字符串比较用strcmp
{
Mark(show, ROW, COL);
}
else
{
break;
}
}
}
}
else
{
printf("输入错误请重新输入n");
}
}
if (win == COL * ROW - EASY_COUNT)
{
printf("恭喜你扫雷成功!n");
printf("再来一把?n");
}
}
void Mark(char show[ROWS][COLS], int row, int col)
{
int x, y;
again:
printf("请输入要标记为雷的坐标->");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
system("cls");
show[x][y] = '#';
DisplayBoard(show, ROW, COL);
return;
}
else
{
printf("输入非法坐标,请重新输入n");
goto again;
}
}
说明:这有2个数组,一个带*号的是用来遮盖雷的如图一;另一个是来表示雷的,雷用1表示,0表示无雷,如图二;用#号来标记雷如图三;
图一
图二
图三
5.可以更改游戏规模(1)不止可以玩9*9的游戏,可以变成12*12等规模的游戏
只需要更改game.h里面的定义常量
这样游戏规模就变成12*12的。
(2)可以改变雷的个数,如20个。



