前言
如何利用代码实现一个扫雷游戏呢?在我们玩过的扫雷游戏里,打开游戏后看到的是一个nxn的棋盘,我们需要点击棋盘的某一位置,在最短的时间内根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输。
在实际创建扫雷游戏时,我们需要构造两个棋盘,一个棋盘是隐形的,它储存的是雷的信息,另一个棋盘是面向玩家的,它显示的是最新的棋盘信息
我们点击棋盘的某一位置时,该位置会显示以它为中心的九宫格的类的数目,考虑到边缘的格子没有九宫格,我们在设置好的棋盘应在加上2行2列,但这2行2列是隐形的
一、游戏界面实现
1.游戏选择界面
void meau()
{
printf(" 扫雷游戏 \n");
printf(" 1.开始游戏 \n");
printf(" 0.退出游戏 \n");
}
2.游戏控制开关
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do{
meau();
printf("请选择\n");
scanf("%d", &input);
switch (input)
{
case 1:game();
break;
case 0:printf("游戏结束\n");
break;
default:printf("输入错误,请重新选择\n");
break;
}
} while (input);
return 0;
}
二、游戏功能实现
1.初始化棋盘
void Initboard(char board[ROWS][COLS], int rows, int cols, char set)
//扫雷游戏存在两个棋盘,因此加入参数ret对不同棋盘进行不同初始化
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
2.显示棋盘内容
void Display(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= row; 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");
}
}
3.在隐形的棋盘里布置雷
void Setthunder(char mineboard[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = COUNTOFTHUNDER;//宏定义雷的个数赋值给count
while (count)//雷全部布置完成跳出循环
{
x = rand() % ROW + 1;
y = rand() % COL + 1;
if (mineboard[x][y] == '0')
{
mineboard[x][y] = '1';
count--;
}
}
}
4.在棋盘寻找雷
void Findthunder(char mineboard[ROWS][COLS], char thunderboard[ROWS][COLS], int row, int col)
//mineboard棋盘为隐藏棋盘,thunderboard为玩家看见的棋盘
{
int x = 0;
int y = 0;
int win = 0; //已被排查出来的不是雷的位置数目
int count = 0;
while (win <ROW*COL - COUNTOFTHUNDER)
//除雷外其余位置全被排除完后跳出循环
{
printf("请输入排雷坐标\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
//使得雷的坐标合法化
{
if (mineboard[x][y] == '1')
{
printf("gameover\n");
break;
}
else
{
count=Showthunder(mineboard, x, y);
thunderboard[x][y] = count + '0';
printf("%d\n", count);
Display(thunderboard, ROW, COL);
win++;
}
}
else
{
printf("范围非法,请重新输入\n");
}
}
if (win == ROW*COL - COUNTOFTHUNDER)
{
printf("you win!\n");
}
}
5.显示雷的信息
int Showthunder(char mineboard[ROWS][COLS], int x, int y)
//棋盘内容为字符,函数返回类型为int
//'3'-'0'=3
//mineboard为隐藏棋盘,如果是雷,该位置的隐藏信息为1
{
return mineboard[x - 1][y] +
mineboard[x - 1][y - 1] +
mineboard[x - 1][y + 1] +
mineboard[x][y - 1] +
mineboard[x][y + 1] +
mineboard[x + 1][y - 1] +
mineboard[x + 1][y] +
mineboard[x + 1][y + 1] - 8 * '0';
}
三、代码实现
1.game.h
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define COUNTOFTHUNDER 10
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void Initboard(char board[ROWS][COLS], int rows, int cols, char set);
void Display(char board[ROWS][COLS], int row, int col);
void Setthunder(char mineboard[ROWS][COLS], int row, int col);
void Findthunder(char mineboard[ROWS][COLS], char thunderboard[ROWS][COLS], int row, int col);
int Showthunder(char Showthunder[ROWS][COLS], int row, int col);
2.game.c
#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 Display(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= row; 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 Setthunder(char mineboard[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = COUNTOFTHUNDER;
while (count)
{
x = rand() % ROW + 1;
y = rand() % COL + 1;
if (mineboard[x][y] == '0')
{
mineboard[x][y] = '1';
count--;
}
}
}
void Findthunder(char mineboard[ROWS][COLS], char thunderboard[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
int count = 0;
while (win <ROW*COL - COUNTOFTHUNDER)
{
printf("请输入排雷坐标\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
{
if (mineboard[x][y] == '1')
{
printf("gameover\n");
break;
}
else
{
count=Showthunder(mineboard, x, y);
thunderboard[x][y] = count + '0';
printf("%d\n", count);
Display(thunderboard, ROW, COL);
win++;
}
}
else
{
printf("范围非法,请重新输入\n");
}
}
if (win == ROW*COL - COUNTOFTHUNDER)
{
printf("you win!\n");
}
}
int Showthunder(char mineboard[ROWS][COLS], int x, int y)
{
return mineboard[x - 1][y] +
mineboard[x - 1][y - 1] +
mineboard[x - 1][y + 1] +
mineboard[x][y - 1] +
mineboard[x][y + 1] +
mineboard[x + 1][y - 1] +
mineboard[x + 1][y] +
mineboard[x + 1][y + 1] - 8 * '0';
}
3.test.c
#include"game.h"标签:ROWS,游戏,int,COLS,C语言,char,mineboard,扫雷,printf From: https://blog.51cto.com/u_15466618/6020810
void meau()
{
printf(" 扫雷游戏 \n");
printf(" 1.开始游戏 \n");
printf(" 0.退出游戏 \n");
}
void game()
{
char mineboard[ROWS][COLS] = { 0 };
char thunderboard[ROWS][COLS] = { 0 };
Initboard(mineboard, ROWS, COLS, '0');
Initboard(thunderboard, ROWS, COLS, '*');
Display(mineboard, ROW, COL);
Display(thunderboard, ROW, COL);
Setthunder(mineboard, ROW, COL);
Findthunder(mineboard, thunderboard, ROW, COL);
Showthunder(thunderboard, ROWS, COLS);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do{
meau();
printf("请选择\n");
scanf("%d", &input);
switch (input)
{
case 1:game();
break;
case 0:printf("游戏结束\n");
break;
default:printf("输入错误,请重新选择\n");
break;
}
} while (input);
return 0;
}