首先建立游戏主题
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("************************************\n");
printf("****************1.开始**************\n");
printf("****************0.结束**************\n");
printf("************************************\n");
}
void game()
{
//mine数组中存放雷
char mine[rows][cols] = { '0' };//数组全部初始化
//show数组中存放排查出的雷的信息
char show[rows][cols] = { '*' };//数组全部初始化
//初始化棋盘
initboard(mine,rows,cols,'0');
initboard(show, rows, cols, '*');
//布雷
//在9*9的棋盘上随机布置10个雷
setmine(mine,rows, cols);
//打印棋盘
display(show, row, col);
//排查雷
find(mine, show, row, col);
}
void test()
{
int input = 0;
do
{
srand((unsigned int)time(NULL));
menu();
printf("请选择:\n");
scanf("%d", &input);
switch (input)
{
case 0:
printf("退出游戏\n");
break;
case 1:
printf("开始游戏\n");
game();
break;
default:
printf("选择错误,请重新选择:\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define row 10
#define col 10
#define rows row+2
#define cols col+2
#define monster 10
//声明函数
//棋盘初始化的函数
void initboard(char arr[rows][cols], int Rows, int Cols,char ch);
void display(char arr[rows][cols], int Rows, int Cols);
void setmine(char arr[rows][cols], int Rows, int Cols);
void find(char mine[rows][cols], char show[rows][cols], int Rows, int Cols);
接下来再具体实现主体中的各个功能:
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void initboard(char arr[rows][cols], int Rows, int Cols,char ch)
{
int i = 0;
for (i = 0;i < rows;i++)
{
for (int j = 0;j < cols;j++)
{
arr[i][j] = ch;
}
}
}
void display(char arr[rows][cols], int Row, int Col)
{
//打印横向分割线
for (int i = 1;i <= Col + 2;i++)
{
printf("--", i);
}
printf("-\n");
//打印标题
for (int i = 0;i < Col + 2-4;i++)
{
printf("-");
}
printf("扫雷游戏");
for (int i = 0;i < Col + 2 - 4;i++)
{
printf("-");
}
printf("-\n");
//打印横向分割线
for (int i = 1;i <= Col+2;i++)
{
printf("--", i);
}
printf("-\n");
//打印横向坐标
printf("0 | ");
for (int i = 1;i <= Col;i++)
{
printf("%d ", i);
}
printf("\n");
//打印横向分割线
for (int i = 1;i <= Col + 2;i++)
{
printf("--", i);
}
printf("-\n");
//打印主体
for (int i = 1;i <= Row;i++)
{
printf("%-2d| ", i);
for (int j = 1;j <= Col;j++)
{
printf("%-2c", arr[i][j]);
}
printf("|\n");
}
//打印横向分割线
for (int i = 1;i <= Col + 2;i++)
{
printf("--", i);
}
printf("-\n");
}
void setmine(char arr[rows][cols], int Rows, int Cols)
{
int count = monster;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}
void find(char mine[rows][cols], char show[rows][cols], int Rows, int Cols)
{
int x;
int y;
int flag = 0;
while (flag<row*col-monster)
{
printf("请输入要排查的坐标:\n");
scanf("%d%d", &x, &y);
//判断坐标的有效性
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遗憾,你踩到雷了!\n");
display(mine, row, col);
break;
}
else
{
//查询周围有几个雷
//然后赋给对应坐标
if (show[x][y] == '*')
{
flag++;
}
show[x][y] = '0' + search(mine, x, y);
display(show, row, col);
}
}
else
{
printf("坐标非法,请重新选择:\n");
}
}
if (flag == row * col - monster)
{
printf("恭喜你,赢了!\n");
}
}
static int search(char mine[rows][cols], int x, int y)
{
return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0');
}