排雷小游戏
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了排雷小游戏相关的知识,希望对你有一定的参考价值。
这次写了一个排雷小游戏,源代码及流程如下。
1,分别运用头文件(game.h)以及源代码(main.c)(game.c)的形式让代码更加清晰;
2,难点:
初始界面的布局以及微调让其更加美化。
扫描一个格子周围8个格子雷的个数以及棋盘周围判断时的溢出问题。
3,优化
为了界面更加清新简单使用系统cls清屏小技巧。
game.h
#include "game.h"
void menu()
{
printf("###############################
");
printf("### 1 PLAY ######### 2 EXIT ###
");
printf("###############################
");
printf("请输入你的选项:");
}
void show()
{
for (int i = 1; i <= 39; i++) {
printf("-");
}
}
void show_board(char board[][12], int row, int col)
{
printf(" ");
for (int i = 1; i <= 10; i++) {
printf(" %d ",i);
}
printf("
");
for (int i = 1; i < row - 1; i++)
{
printf(" ");
show();
printf("
");
printf("%2d ", i);
for (int j = 1; j < col - 1; j++)
{
printf("| %c ", board[i][j]);
}
printf("
");
}
}
void set_mine(char mine_board[][12], int row, int col)
{
int i = 0;
while (i < 99)
{
int x = rand() % 10 + 1;
int y = rand() % 10 + 1;
if (mine_board[x][y] == ‘0‘)
{
mine_board[x][y] = ‘1‘;
i++;
}
}
}
int get_mines(char mine_board[][12], int row, int col, int x, int y)
{
return mine_board[x - 1][y - 1] + mine_board[x - 1][y] + mine_board[x - 1][y + 1] + mine_board[x][y - 1] + mine_board[x][y + 1] + mine_board[x + 1][y - 1] + mine_board[x + 1][y] + mine_board[x + 1][y + 1] - 8 * ‘0‘;
}
void move(char my_board[][12], char mine_board[][12], int row, int col)
{
int count = 80;
int x = 0;
int y = 0;
do {
show_board(my_board, ROW, COL);
printf("请输入坐标:");
scanf_s("%d%d", &x, &y);
if (x < 1 || x > 10 || y < 1 || y > 10)
{
printf("输入坐标错误");
continue;
}
if (my_board[x][y] != ‘*‘)
{
printf("输入坐标重复");
continue;
}
system("cls");
if (mine_board[x][y] == ‘1‘)
{
break;
}
int num = get_mines(mine_board, ROW, COL, x, y);
my_board[x][y] = num + ‘0‘;
count--;
} while (count > 0);
if (count > 0)
{
printf("你被炸死了!
");
}
else
{
printf("你赢了!
");
}
printf("下面是雷区的排布!
");
show_board(mine_board, ROW, COL);
}
void GAME()
{
srand((unsigned long)time(NULL));
char my_board[ROW][COL];
char mine_board[ROW][COL];
memset(my_board, ‘*‘, sizeof(my_board));
memset(mine_board, ‘0‘, sizeof(mine_board));
set_mine(mine_board, ROW, COL);
move(my_board, mine_board, ROW, COL);
}
game.c
#include "game.h"
void menu()
{
printf("###############################
");
printf("### 1 PLAY ######### 2 EXIT ###
");
printf("###############################
");
printf("请输入你的选项:");
}
void show()
{
for (int i = 1; i <= 39; i++) {
printf("-");
}
}
void show_board(char board[][12], int row, int col)
{
printf(" ");
for (int i = 1; i <= 10; i++) {
printf(" %d ",i);
}
printf("
");
for (int i = 1; i < row - 1; i++)
{
printf(" ");
show();
printf("
");
printf("%2d ", i);
for (int j = 1; j < col - 1; j++)
{
printf("| %c ", board[i][j]);
}
printf("
");
}
}
void set_mine(char mine_board[][12], int row, int col)
{
int i = 0;
while (i < 99)
{
int x = rand() % 10 + 1;
int y = rand() % 10 + 1;
if (mine_board[x][y] == ‘0‘)
{
mine_board[x][y] = ‘1‘;
i++;
}
}
}
int get_mines(char mine_board[][12], int row, int col, int x, int y)
{
return mine_board[x - 1][y - 1] + mine_board[x - 1][y] + mine_board[x - 1][y + 1] + mine_board[x][y - 1] + mine_board[x][y + 1] + mine_board[x + 1][y - 1] + mine_board[x + 1][y] + mine_board[x + 1][y + 1] - 8 * ‘0‘;
}
void move(char my_board[][12], char mine_board[][12], int row, int col)
{
int count = 80;
int x = 0;
int y = 0;
do {
show_board(my_board, ROW, COL);
printf("请输入坐标:");
scanf_s("%d%d", &x, &y);
if (x < 1 || x > 10 || y < 1 || y > 10)
{
printf("输入坐标错误");
continue;
}
if (my_board[x][y] != ‘*‘)
{
printf("输入坐标重复");
continue;
}
system("cls");
if (mine_board[x][y] == ‘1‘)
{
break;
}
int num = get_mines(mine_board, ROW, COL, x, y);
my_board[x][y] = num + ‘0‘;
count--;
} while (count > 0);
if (count > 0)
{
printf("你被炸死了!
");
}
else
{
printf("你赢了!
");
}
printf("下面是雷区的排布!
");
show_board(mine_board, ROW, COL);
}
void GAME()
{
srand((unsigned long)time(NULL));
char my_board[ROW][COL];
char mine_board[ROW][COL];
memset(my_board, ‘*‘, sizeof(my_board));
memset(mine_board, ‘0‘, sizeof(mine_board));
set_mine(mine_board, ROW, COL);
move(my_board, mine_board, ROW, COL);
}
main.c
#include "game.h"
int main()
{
int select = 0;
int quit = 1;
while (quit)
{
menu();
scanf_s("%d", &select);
switch (select)
{
case 1:
GAME();
break;
case 2:
quit = 0;
break;
default:
printf("输入有误
");
break;
}
}
printf("拜拜
");
return 0;
}
1,初始话游戏进入界面如下
2,初始化棋盘设置如下
3,游戏开始及过程展示如下
以上是关于排雷小游戏的主要内容,如果未能解决你的问题,请参考以下文章