目录
一、菜单模块
菜单与井字棋类似都是先打印菜单然后用一个do……while循环嵌套switch选择玩游戏或着退出游戏,如果选择错误则提示输入错误,让用户重新选择
#include"game.h"
void menu()
{
printf("**************************************\n");
printf("************** 1. play ************\n");
printf("**************************************\n");
printf("************** 0. exit ************\n");
printf("**************************************\n");
}
void test()
{
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1 :
game();
break;
case 0 :
exit(0);
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
二、game函数
1、函数声明及行数列数雷数
#include<stdio.h>
#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 rows, int cols);//打印棋盘
void SetMine(char board[ROWS][COLS], int row, int col);//布置雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//排查雷
为后期方便修改游戏行列数直接在宏定义出行列数
2、初始化雷阵
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;
}
}
}
3、打印雷阵
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0, 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");
}
}
为方便输入要排雷的坐标在第一行打印列数,第一列打印行数
4、布置雷阵
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
用count记录雷数当布置够则跳出循环,结束函数
x,y在行列数以内随机取值,如果没有雷就布置雷
为正确生成随机数在test函数中加入
srand((unsigned int)time(NULL));
5、计算雷数
int get_mine_count(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] - 8 * '0'
+ mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1];
}
如果输入的坐标正确,计算周围八个坐标中有几个雷
因为初始化时在有雷的地方,写为字符一,没雷的地方为字符零,所以加起来减去八个字符零,就是雷的个数
6、排雷
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 (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, row, col);
break;
}
else
{
int n = get_mine_count(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("输入错误,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("你赢了,排雷成功!\n");
}
}
若输入的坐标x,y超出行与列的范围则输入错误,让玩家重新输入
若输入的坐标x,y位置处为字符一,则踩雷输掉游戏,再打印出雷阵让玩家“瞑目”
若输入的坐标x,y位置处为字符零,预先准备一个计数器,计算排雷的数量,当数量等于所有的格子减雷的数量则排雷完成
7、game函数
void game()
{
char mine[ROWS][COLS] = { 0 };//存放布雷位置
char show[ROWS][COLS] = { 0 };//存放排雷信息
InitBoard(mine, ROWS, COLS, '0');//初始化mine为0
InitBoard(show, ROWS, COLS, '*');//初始化show为0
SetMine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
首先准备两个数组,一个存放雷的位置,一个存放排雷的信息
将mine数组全部初始化为零,show全部初始化为*
先将棋盘打印一下,然后开始排雷
三、全部代码
1、main.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("**************************************\n");
printf("************** 1. play ************\n");
printf("**************************************\n");
printf("************** 0. exit ************\n");
printf("**************************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };//存放布雷位置
char show[ROWS][COLS] = { 0 };//存放排雷信息
InitBoard(mine, ROWS, COLS, '0');//初始化mine为0
InitBoard(show, ROWS, COLS, '*');//初始化show为0
SetMine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
void test()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1 :
game();
break;
case 0 :
exit(0);
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
2、game.h
#pragma once
#include<stdio.h>
#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 rows, int cols);//打印棋盘
void SetMine(char board[ROWS][COLS], int row, int col);//布置雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//排查雷
3、game.c
#define _CRT_SECURE_NO_WARNINGS 1
#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)
{
int i = 0, 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 board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int get_mine_count(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] - 8 * '0'
+ mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1];
}
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 (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, row, col);
break;
}
else
{
int n = get_mine_count(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("输入错误,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("你赢了,排雷成功!\n");
}
}