扫雷游戏大家都不陌生吧,玩了这么多年了,现在学习了C语言,可不得自己整一个,一起和菜菜来看看吧~
目录
主体
先创建个菜单,然后打好基本框架结构,这里很简单,就不赘述了,直接上代码:
void menu()
{
printf("*****************************");
printf("******** 扫雷小游戏 *******");
printf("******** 益智/ 休闲 *******");
printf("******** Play:1 *******");
printf("******** Exit:0 *******");
printf("*****************************");
}
void test()
{
int input = 0;
do
{
menu();
printf("请选择(1/0):");
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;
}
OK,现在主要来攻克game函数:
game函数
思路:创建两个棋盘 mine 和 show。以 9 * 9 (i * i)棋盘为例,为了在统计边缘位置周边8个雷的个数时不越界,需将棋盘规格设置为 11 * 11 ,即(i+2)*(i+2)
mine 棋盘用来布置雷,不打印,是雷放字符 ' 1 ' ,不是雷放字符 ' 0 ' 。先将棋盘全部初始化为字符 ' 0 ' ,再用 time 函数随机布置雷。
show 棋盘用来查找雷,打印,先全部初始化为 ' * ' ,再将查找到周围八个位置雷的个数放进去。
以下所有函数都在 game.h 中声明,在 game.c 中实现。
先来看看 test.c 和 game.h 中的 “ 大纲 ” ,这也是大致思路——
test.c
void game()
{
char mine[ROWS][COLS] = { 0 };//布置雷
char show[ROWS][COLS] = { 0 };//排查雷
//初始化棋盘
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印棋盘
//DisplayBoard(mine, ROW, COL);//自检,不需打印
DisplayBoard(show, ROW, COL);//只要打印中间的棋盘,边缘只是为了防止越界,不用打印
//布置雷
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);//自检,不需打印
//排查雷
FindMine(mine, show, ROW, COL);
}
game.h
注:这么多函数名,可不能随便起,一定一定要取有意义的名字,不然很容易弄混哦!
#pragma once
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY 10
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//初始化棋盘
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 InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;//多设置一个set参数,可以实现两个初始化用同一个函数
}
}
}
打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
//直接遍历数组即可
//打印列号(打印行列游戏时更加方便)
for (j = 1; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);//打印行号
for (j = 1; j < col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
这一步打印 mine 棋盘只是为了自检,看看我们的代码有没有错,玩时只打印 show 棋盘 ,如果 mine 棋盘也打印那就是破解版游戏了,直接开挂起飞哈哈~~
布置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY;//看game.h,EASY等于10
while (count)
{
//生成随机位置
int x = rand() % row + 1;//x属于[1,9]
int y = rand() % col + 1;//y属于[1,9]
//布置雷
if (board[x][y] == '0')//当这个位置还未被占用时,可以布置雷
{
board[x][y] = '1';//是雷放字符'1'
count--;
}
}
}
排查雷
int Count(char board[ROWS][COLS], int x, int y)//统计周围八个坐标雷的个数
{
return (board[x - 1][y] + board[x - 1][y - 1] + board[x][y - 1] + board[x + 1][y - 1]
+ board[x + 1][y] + board[x + 1][y + 1] + board[x][y + 1] + board[x - 1][y + 1] - 8 * '0');
}
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)
{
printf("请输入要排查的坐标:");
scanf("%d%d", &x, &y);
system("cls");//清屏函数,让界面更加简洁
if (x >= 1 && x <= row && y >= 1 && y <= col)//判断坐标是否非法
{
if (show[x][y] != '*')//不是*号说明已经排查
{
printf("该坐标被排查过了\n");
continue;//直接跳到循环开头
}
if (mine[x][y] == '1')//碰到了mine数组中的雷
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);//打印棋盘,让你看看自己怎么si的
break;
}
else//没有碰到雷
{
int n = Count(mine, x, y);//统计周围八个坐标雷的个数
show[x][y] = n + '0';//将返回的数字变成字符放入字符数组中
DisplayBoard(show, ROW, COL);
win++;//注意这里!!
}
}
else
{
printf("坐标非法,请重新输入:");
}
}
if (win == row * col - EASY)//win已经达到上限
{
printf("恭喜你,扫雷成功!!");
}
}
全部代码
需要的自取,这里就没有解析了,在上面已经详细说了。当然最好是自己敲一敲,无数次走马观花不如一次实践操作,懂得都懂 ~
game.h
#pragma once
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY 10
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//初始化棋盘
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);
game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("————扫雷————\n");
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("————扫雷————\n");
}
//布置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
//排查雷
int Count(char board[ROWS][COLS], int x, int y)
{
return (board[x - 1][y] + board[x - 1][y - 1] + board[x][y - 1] + board[x + 1][y - 1]
+ board[x + 1][y] + board[x + 1][y + 1] + board[x][y + 1] + board[x - 1][y + 1] - 8 * '0');
}
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)
{
printf("请输入要排查的坐标:");
scanf("%d%d", &x, &y);
system("cls");
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf("该坐标被排查过了\n");
continue;
}
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int n = Count(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("坐标非法,请重新输入:");
}
}
if (win == row * col - EASY)
{
printf("恭喜你,扫雷成功!!\n");
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menu()
{
printf("*****************************\n");
printf("******** 扫雷小游戏 *******\n");
printf("******** 益智/ 休闲 *******\n");
printf("******** Play:1 *******\n");
printf("******** Exit:0 *******\n");
printf("*****************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化棋盘
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印棋盘
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
void test()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择(1/0):");
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;
}
自己先来一把:
没啦,希望你玩的愉快~