扫雷大致思路
1.打印期盼.
2.藏雷。
3.扫雷
具体步骤参考代码
text.c:大的步骤
#include "game.h"
void menu()
{
printf("***********************************\n");
printf("*********** 1.play ***********\n");
printf("*********** 1.exit ***********\n");
printf("***********************************\n");
}
int main()
{
srand((unsigned)time(NULL)); //时间戳,用于后续rand函数取随机值
int a = 0;
char Board1[Rows][Cols];
char Board2[Rows][Cols];
do
{
menu(); //打印选择菜单
printf("请输入:>");
scanf("%d",&a);
if (a == 1)
{
printf("-------游戏开始-------\n");
InitBoard1(Board1, Rows, Cols); //初始化藏雷棋盘
InitBoard2(Board2, Rows, Cols); //初始化显示期盼
DisplayBoard(Board2, Row, Col); //打印棋盘
/*DisplayBoard(Board1, Row, Col);*/
DepositRadar(Board1, Row, Col); //埋雷
SweepRadar(Board1, Board2, Rows, Cols); //扫雷
}
} while (a);
return 0;
game.c 具体实现
#include "game.h"
void InitBoard1(char board[Rows][Cols], int row, int col)
{
char board1 = '0'; //将埋雷棋盘初始化为全0
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = board1;
}
}
}
void InitBoard2(char board[Rows][Cols], int row, int col)
{
char board1 = '*'; //将显示棋盘初始化为全*
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = board1;
}
}
}
void DisplayBoard(char board[Rows][Cols], int row, int col) //打印显示棋盘
{
for (int i = 1; i<10; i++)
{
for (int j = 1; j<10; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void DepositRadar(char board[Row][Col], int row, int col) //埋雷
{
int count = 10;
int x,y;
while (count)
{
x = rand() % 9; //产生随即坐标x
y = rand() % 9; //产生随即坐标y
if (board[x][y] == '0') //检查此坐标是否没有雷,有的话重新埋
{
board[x][y] = '1';
count--;
}
}
}
void SweepRadar(char Board1[Rows][Cols], char Board2[Rows][Cols], int row, int col)
{
int x, y;
int count = 71; //总共71个不是雷的地方
while (count)
{
printf("请输入您要扫的坐标:> ");
scanf("%d,%d", &x, &y);
if (Board1[x][y] == '1') //判断是否被炸死
{
printf("很遗憾你被炸死了\n");
DisplayBoard(Board2, Row, Col); //让你死得瞑目
DisplayBoard(Board1, Row, Col);
break;
}
else
{
//没有被炸死,显示周围雷的数量
int ret = Board1[x + 1][y - 1] + Board1[x][y - 1] + Board1[x - 1][y - 1] + Board1[x + 1][y] + Board1[x - 1][y] + Board1[x + 1][y + 1] + Board1[x][y + 1] + Board1[x - 1][y + 1] - 8 * '0' ;
Board2[x][y] = ret + '0';
count--;
DisplayBoard(Board2, Row, Col);
}
}
if (count == 0) //扫完雷胜利
{
printf("恭喜你赢得比赛\n");
}
}
game.h 函数所需头文件
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#define Rows 11
#define Cols 11
#define Row 9
#define Col 9
#include<stdio.h>
#include<stdlib.h>
void InitBoard1(char board1[Rows][Cols], int row, int col);
void InitBoard2(char board[Rows][Cols], int row, int col);
void DisplayBoard(char board[Rows][Cols], int row, int col);
void DepositRadar(char board[Row][Col], int row, int col);
void SweepRadar(char Board1[Rows][Cols], char Board2[Rows][Cols], int row, int col);