用c语言简单实现小游戏——扫雷
扫雷是大家小时候大家在电脑上都玩过的单机游戏,它点开一个可能会展开一大片,你要通过这个数字判断他周围的雷个数。
首先扫雷要写三个文件:
头文件:game.h //包含函数各种声明,宏定义
源文件:test.c //主要负责测试整个代码
源文件:game.c //包含各种函数的定义
整体思路
1.玩游戏之前,首先我们需要一个菜单,来确定你是不是要玩游戏。
void menu()
{
printf("***********************************************\n");
printf("******* 1.play 0.exit ******\n");
printf("***********************************************\n");
}
2.我们需要利用二维数组来构建雷盘,
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
并且使用宏定义来定义雷盘,这样方便修改雷盘的大小
# define ROW 9 //雷盘行数
# define COL 9
#define ROWS ROW+2 //雷盘的实际行数
#define COLS COL+2
3.定义完之后,我们初始化雷盘,利用InitBoard函数来实现。
void InitBoard(char board[ROWS][COLS], int row, int col, char set)//初始化雷盘
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = set;
}
}
}
4.初始化雷盘之后,我们需要在雷盘上布置雷,利用SetMine函数来实现。这里的rand需要调用<time.h>来实现。利用这个函数srand((unsigned int)(time(NULL)));来生成随机数。
void SetMine(char mine[ROWS][COLS], int row, int col)//布置雷
{
int count = EASY_COUNT;
while (count)
{
int x;
int y;
x = rand() % row + 1;//保证行坐标在1到9之间
y = rand() % col + 1;
if (mine[x][y] = '0')//判断该位置是否布置过雷
{
mine[x][y] = '1';
count--;
}
}
}
5.布置完雷之后,我们需要把雷盘打印出来,这样才能使我们更方面的开始玩游戏。
void DisplayBoard(char board[ROWS][COLS], int row, int col)//打印雷盘
{
system("CLS");//每次打印雷盘之前清屏一次
int i = 0;
int j = 0;
for (i = 0; i <= row; i++)
printf("%d ", i);//打印列坐标1 2 3 4 5 6 7 8 9
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);//打印行坐标1 2 3 4 5 6 7 8 9
for (j = 1; j <= row; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
6.我们需要开始扫雷,在扫雷之前我们需要保证第一次点开的不是雷,因为刚开始如果点开就是雷,那这个游戏还要怎么玩下去。
void FirstMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x;
int y;
int ret = 0;
int count = 0;
printf("请输入要排查的坐标:");
scanf("%d,%d", &x, &y);
if (mine[x][y] == '1')
{
mine[x][y] = '0';
count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
while (ret)//随机生成一个雷
{
x = rand() % ROW + 1;
y = rand() % COL + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
}
ret--;
}
}
OpenMine(mine, show, row, col, x, y);
DisplayBoard(show, row,col);
}
7.我们还需要做到判断点开一个数之后的周围是否有雷。
int GetMineCount(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';//将该位置周围八个位置的情况(是否有雷)计数
}
8.我们还需要在点开一个点之后,能够展开一片,这样更方面我们玩游戏。
void OpenMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)//展开一片
{
int ret = 0;
ret = GetMineCount(mine, x, y);
if (ret == 0)
{
show[x][y] = ' ';
if (x > 0 && y + 1 <= col && show[x][y + 1] == '*')
OpenMine(mine, show, row, col, x, y + 1);
if (x - 1 > 0 && y + 1 <= col && show[x - 1][y + 1] == '*')
OpenMine(mine, show, row, col, x - 1, y + 1);
if (x - 1 > 0 && y > 0 && show[x - 1][y] == '*')
OpenMine(mine, show, row, col, x - 1, y);
if (x - 1>0 && y - 1 > 0 && show[x - 1][y - 1] == '*')
OpenMine(mine, show, row, col, x - 1, y - 1);
if (x >0 && y - 1 > 0 && show[x][y - 1] == '*')
OpenMine(mine, show, row, col, x, y - 1);
if (x + 1 <= row && y - 1 > 0 && show[x + 1][y - 1] == '*')
OpenMine(mine, show, row, col, x + 1, y - 1);
if (x + 1 <= row && y > 0 && show[x + 1][y] == '*')
OpenMine(mine, show, row, col, x + 1, y);
if (x + 1 <= row && y + 1 <= col&& show[x + 1][y + 1] == '*')
OpenMine(mine, show, row, col, x + 1, y + 1);
}
else
{
show[x][y] = GetMineCount(mine, x, y) + '0';
}
}
9.我们现在只需要在做最后一步开始扫雷。
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 count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
OpenMine(mine, show, row, col, x, y);
DisplayBoard(show, row, col);
win++;
}
}
else
printf("坐标非法!");
}
if (win == row*col - EASY_COUNT)
{
printf("恭喜,排雷成功!");
DisplayBoard(mine, row, col);
}
}
这就是扫雷的整体思路,我觉得扫雷这个小程序对我来说是有一定困难,很多东西都是在网上学习别人的,因为自己能力不够。但是跟三子棋相比,是又一定的相似度,只要我们能够努力学习,也可以做到。
源代码
头文件:game.h
#ifndef __GAME_H__
#define __GAME_H__
# define _CRT_SECURE_NO_WARNINGS 1
# include <stdio.h>
# include <stdlib.h>
# include <time.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 row, int col, char set);//初始化雷盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);//打印雷盘
void SetMine(char mine[ROWS][COLS], int row, int col);//布置雷
int GetMineCount(char mine[ROWS][COLS], int x, int y);//统计雷数
void OpenMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y);//展开一片
void FirstMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);// 确定第一次点开不是雷
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row, int col);//排雷
#endif
源文件:test.c
# define _CRT_SECURE_NO_WARNINGS 1
# include <stdio.h>
# include <stdlib.h>
# include "game.h"
void menu()
{
printf("***********************************************\n");
printf("******* 1.play 0.exit ******\n");
printf("***********************************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
InitBoard(mine, ROWS, COLS, '0');//初始化'0',存雷的数字
InitBoard(show, ROWS, COLS, '*');//初始化'*',
SetMine(mine, ROW, COL);//布置雷
DisplayBoard(show, ROW, COL);//打印雷盘
FirstMine(mine, 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();
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
}
int main()
{
test();
system("pause");
return 0;
}
源文件:game.c
# define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROWS][COLS], int row, int col, char set)//初始化雷盘
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)//打印雷盘
{
system("CLS");//每次打印雷盘之前清屏一次
int i = 0;
int j = 0;
for (i = 0; i <= row; i++)
printf("%d ", i);//打印列坐标1 2 3 4 5 6 7 8 9
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);//打印行坐标1 2 3 4 5 6 7 8 9
for (j = 1; j <= row; 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;
int y;
x = rand() % row + 1;//保证行坐标在1到9之间
y = rand() % col + 1;
if (mine[x][y] = '0')//判断该位置是否布置过雷
{
mine[x][y] = '1';
count--;
}
}
}
int GetMineCount(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';//将该位置周围八个位置的情况(是否有雷)计数
}
void OpenMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)//展开一片
{
int ret = 0;
ret = GetMineCount(mine, x, y);
if (ret == 0)
{
show[x][y] = ' ';
if (x > 0 && y + 1 <= col && show[x][y + 1] == '*')
OpenMine(mine, show, row, col, x, y + 1);
if (x - 1 > 0 && y + 1 <= col && show[x - 1][y + 1] == '*')
OpenMine(mine, show, row, col, x - 1, y + 1);
if (x - 1 > 0 && y > 0 && show[x - 1][y] == '*')
OpenMine(mine, show, row, col, x - 1, y);
if (x - 1>0 && y - 1 > 0 && show[x - 1][y - 1] == '*')
OpenMine(mine, show, row, col, x - 1, y - 1);
if (x >0 && y - 1 > 0 && show[x][y - 1] == '*')
OpenMine(mine, show, row, col, x, y - 1);
if (x + 1 <= row && y - 1 > 0 && show[x + 1][y - 1] == '*')
OpenMine(mine, show, row, col, x + 1, y - 1);
if (x + 1 <= row && y > 0 && show[x + 1][y] == '*')
OpenMine(mine, show, row, col, x + 1, y);
if (x + 1 <= row && y + 1 <= col&& show[x + 1][y + 1] == '*')
OpenMine(mine, show, row, col, x + 1, y + 1);
}
else
{
show[x][y] = GetMineCount(mine, x, y) + '0';
}
}
void FirstMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x;
int y;
int ret = 0;
int count = 0;
printf("请输入要排查的坐标:");
scanf("%d,%d", &x, &y);
if (mine[x][y] == '1')
{
mine[x][y] = '0';
count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
while (ret)//随机生成一个雷
{
x = rand() % ROW + 1;
y = rand() % COL + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
}
ret--;
}
}
OpenMine(mine, show, row, col, x, y);
DisplayBoard(show, row,col);
}
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 count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
OpenMine(mine, show, row, col, x, y);
DisplayBoard(show, row, col);
win++;
}
}
else
printf("坐标非法!");
}
if (win == row*col - EASY_COUNT)
{
printf("恭喜,排雷成功!");
DisplayBoard(mine, row, col);
}
}