文章目录
前言
通过学习C语言的二维数组,决定做一个扫雷小游戏对所学知识进行验证
一、扫雷小游戏的实现步骤
1.建立游戏菜单
2.定义二维数组进行雷的存储
3.定义二维数组进行对雷的数量的统计
4.初始化以上两个数组
5.放雷
6.开始扫雷
7.标记雷
8.判断是否胜利
二、具体实现过程
1.建立游戏菜单
void menu()
{
printf("############################\n");
printf("###1.play 0.exit###\n");
printf("############################\n");
}
void menu1()
{
printf("##############################\n");
printf("#######1.选择非雷区域 #######\n");
printf("#######2.标记雷的位置#######\n");
printf("#######3.取消雷的标记 #######\n");
printf("##############################\n");
}
2.初始化数组
代码如下(示例):
void Initboard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0, j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
3.设置雷
代码如下(示例):
void SetMine(char board[ROWS][COLS], int row, int col)
{
int x = 0, y = 0;
int count = EASY_COUNT;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
4.设置雷
代码如下(示例):
void SetMine(char board[ROWS][COLS], int row, int col)
{
int x = 0, y = 0;
int count = EASY_COUNT;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
5.防止第一次直接扫到雷(将该坐标强制变为非雷,在其它地方重新定义一个雷,不能是该坐标)
代码如下(示例):
void ChangePlace(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
{
printf("第一次就踩雷了,已进行规避\n");
mine[x][y] = '0';
while (1)
{
int _x = rand() % row + 1;
int _y = rand() % col + 1;
if (_x != x || _y != y)
{
if (mine[_x][_y] != '1')
{
mine[_x][_y] = '1';
break;
}
}
}
broad(mine, show, x, y);
DisplayBoard(show, row, col);
}
6.标记雷和取消标记雷
代码如下(示例):
int Flagmine(char show[ROWS][COLS], int row, int col, size_t flag)
{
int x = 0, y = 0;
if (flag == EASY_COUNT)
{
printf("标记的雷数和实际存在的雷数相等,无法再标记\n");
return;
}
printf("请输入你要标记位置的坐标:>\n");
scanf("%d%d", &x, &y);
//判断坐标是否合法
if (x > 0 && x <= row && y > 0 && y <= col)
{
//判断该坐标是否已经被确认不为雷
if (show[x][y] == '*')
{
show[x][y] = '!';
flag++;
}
else
{
printf("该位置不可能是雷,请重新输入\n");
}
}
else
{
printf("该坐标不合法,请重新输入:>\n");
}
return flag;
}
int Cancelflag(char show[ROWS][COLS], int row, int col, int flag_count)
{
int x = 0;
int y = 0;
scanf("%d%d", &x, &y);
//判断坐标是否合法
if (x > 0 && x <= row && y > 0 && y <= col)
{
//判断该位置是否被标记过
if (show[x][y] == '!')
{
show[x][y] = '*';
flag_count--;
}
else
printf("该位置未被标记过,无需取消标记\n");
}
else
{
printf("该坐标不合法,请重新输入:>\n");
}
return flag_count;
}
7.棋盘的大范围拓展(递归思想,需注意的是加判断,防止出现死递归)
代码如下(示例):
void broad(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
if (x == 0 || y == 0 || x == ROWS - 1 || y == COLS - 1)
return;
if (show[x][y] != '*')
{
return;
}
int count = get_mine(mine, x, y);
if (count > 0)
{
show[x][y] = count + '0';
return;
}
else if (count == 0)
{
show[x][y] = ' ';
broad(mine, show, x - 1, y);
broad(mine, show, x - 1, y - 1);
broad(mine, show, x, y - 1);
broad(mine, show, x + 1, y - 1);
broad(mine, show, x + 1, y);
broad(mine, show, x + 1, y + 1);
broad(mine, show, x, y + 1);
broad(mine, show, x - 1, y + 1);
}
}
8.统计标记(!)和未扫描的雷(*)
代码如下(示例):
int Travel(char show[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
int win = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show[i][j] == '*' || show[i][j] == '!')
{
win++;
}
}
}
return win;
}
9.执行扫雷
代码如下(示例):
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int ch = 0;
size_t flag_count = 0;
int win = 0;
int fch = 1;//标记玩家是否为第一次排雷
//需要循环的条件:需改进
while (1)//循环条件存在问题
{
menu1();
scanf("%d", &ch);
if (ch == 1)
{
int x = 0, y = 0;
printf("请开始排雷:>");
scanf("%d%d", &x, &y);
//判断坐标合法
if (x <= row && x > 0 && y > 0 && y <= col)
{
//判断玩家是否是第一次排雷
if (fch == 1 && mine[x][y] == '1')
{
ChangePlace(mine, show, row, col, x, y);
fch++;
}
else
{
if (mine[x][y] == '1')
{
printf("游戏结束\n");
printf("恭喜你,踩到雷了\n");
DisplayBoard(mine, row, col);
break;
}
else
{
broad(mine, show, x, y);
DisplayBoard(show, row, col);
//遍历show地图(改进)
win = Travel(show, row, col);
if (win == EASY_COUNT)
break;
}
fch++;
}
}
else
{
printf("输入坐标不合法,请重新输入\n");
}
}
else if (ch == 2)
{
printf("请开始标记雷:>\n");
flag_count = Flagmine(show, row, col, flag_count);
DisplayBoard(show, row, col);
//遍历show地图(改进)
win = Travel(show, row, col);
if (win == EASY_COUNT)
break;
}
else if (ch == 3)
{
printf("请选择要取消标记的位置:>\n");
flag_count = Cancelflag(show, row, col, flag_count);
DisplayBoard(show, row, col);
}
}
//判断游戏胜利的标准(改进)
if (win == EASY_COUNT)
{
printf("恭喜你,游戏胜利\n");
}
}
10.game.h文件
代码如下(示例):
#pragma once
#include <stdio.h>
#include <string.h>
#include <time.h>
#define ROW 9
#define COL 9
#define EASY_COUNT 10
#define ROWS ROW+2
#define COLS COL+2
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);
void broad(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
11.test.C文件
代码如下(示例):
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()
{
printf("############################\n");
printf("###1.play 0.exit###\n");
printf("############################\n");
}
void game()
{
//雷的信息存储
//1布置雷的信息
char mine[ROWS][COLS] = { 0 };
//2.排查雷的信息
char show[ROWS][COLS] = { 0 };
//初始化
Initboard(mine, ROWS, COLS, '0');
Initboard(show, ROWS, COLS, '*');
//布置雷
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:
printf("游戏开始\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入:>");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
总结
以上就是五子棋的各个函数介绍和总体代码,若有问题,希望大家指正。
Gitee地址:https://gitee.com/feng-jie-0917/c-language/tree/master/2020-5-5