此文章将展示扫雷游戏的一种实现逻辑,分别从总体实现逻辑和各函数细节解释两个维度,对此代码进行深入剖析。
2.用户显示界面(初始化为全'空格')
#pragma once
#define ROW 9
#define COL 9
#define ROWS ROW+2//加边界
#define COLS COL+2
#define EASY_COUNT 10//雷的数目
#include
#include
#include
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col, char set);
//打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col);
//放雷
void SetMine(char mine[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], int row, int col);
#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, ' ');
//打印棋盘
//DisplayBoard(mine, ROW, COL);
//布雷
SetMine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//排雷
FindMine(mine,show, ROW, COL);
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选项错误\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
//1~9
int i = 0;
int j = 0;
//列号的打印
for (i = 0; i <= col; 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 SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count > 0)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
int dx[8] = { -1,-1,0,1,1,1,0,-1 };
int dy[8] = { 0,-1,-1,-1,0,1,1,1 };
static int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
/*return mine[x - 1][y] +
mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] - 8 * '0';*/
int c = 0;
for (int i = 0; i < 8; i++)
{
c += (mine[x + dx[i]][y + dy[i]] - '0');
}
return c;
}
int win = 0;
void open(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
for (int i = 0; i < 8; i++)
{
int tox = x + dx[i];
int toy = y + dy[i];
int g = get_mine_count(mine, tox, toy);
if (tox >= 1 && tox <= ROW && toy >= 1 && toy <= COL && mine[tox][toy] == '0' && show[tox][toy] == ' ' && g == 0)
{
show[tox][toy] = '0';
win++;
//DisplayBoard(show, ROW, COL);
//DisplayBoard(mine, ROW, COL);
open(mine, show, tox, toy);
}
else if (tox >= 1 && tox <= ROW && toy >= 1 && toy <= COL && show[tox][toy] == ' ')
{
show[tox][toy] = g + '0';
win++;
}
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while (win < row*col-EASY_COUNT)
{
printf("请输入要排查的坐标:>");
scanf("%d %d", &x, &y);
if (1 <= x && x <= row && 1 <= y && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遗憾你被炸死了\n");
DisplayBoard(mine, row, col);
break;
}
else
{
//计算x,y周围有几颗雷
int n = get_mine_count(mine, x, y);
if (0 == n)
{
show[x][y] = '0';
open(mine, show, x, y);//递归地打开与空相邻的方块
}
show[x][y] = n + '0';
DisplayBoard(show, row, col);
}
}
else
{
printf("输入坐标非法,无法排雷,请重新输入");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, row, col);
}
}
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
用两个for循环嵌套而实现对二维数组"board" 的初始化,全部置为某个字符。
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
//1~9
int i = 0;
int j = 0;
//列号的打印
for (i = 0; i <= col; 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 SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
static int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y] +
mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] - 8 * '0';
}
int open(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int win = 0;
for (int i = 0; i < 8; i++)
{
int tox = x + dx[i];
int toy = y + dy[i];
int g = get_mine_count(mine, tox, toy);
if (tox >= 1 && tox <= ROW && toy >= 1 && toy <= COL && mine[tox][toy] == '0' && show[tox][toy] == ' ' && g == 0)
{
show[tox][toy] = '0';
win++;
//DisplayBoard(show, ROW, COL);
//DisplayBoard(mine, ROW, COL);
open(mine, show, tox, toy);
}
else if (tox >= 1 && tox <= ROW && toy >= 1 && toy <= COL && show[tox][toy] == ' ')
{
show[tox][toy] = g + '0';
win++;
}
}
return win;
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row*col-EASY_COUNT)
{
printf("请输入要排查的坐标:>");
scanf("%d %d", &x, &y);
if (1 <= x && x <= row && 1 <= y && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遗憾你被炸死了\n");
DisplayBoard(mine, row, col);
break;
}
else
{
//计算x,y周围有几颗雷
int n = get_mine_count(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("输入坐标非法,无法排雷,请重新输入");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, row, col);
}
}
次程序大致实现了扫雷的基本功能,但可能仍有一些问题,还希望大神指出。