今天用C语言写一个扫雷的代码
扫雷其实和我们之前写的三子棋有点相似,首先是打印菜单,进行选择
我们还是像之前一样有三个文件,一个是game.h game.c test.c
test.c实现我们的扫雷逻辑
gam.c实现我们的游戏需要的函数
game.h来声明
那么我们首先应该有个菜单,并进行选择
c
void menu()
{
printf("************************\n");
printf("******* 1. Play ******\n");
printf("******* 0. Exit ******\n");
printf("************************\n");
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("扫雷游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input); //进入循环的条件是input为真
return 0;
}
下一步就是如何实现这个逻辑,那我们需要一个game函数里写下我们的创建过程
第一步来初始化棋盘
c
//初始化棋盘
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;
}
}
}
第二部我们应该打印我们的棋盘
c
//打印棋盘
void PrintfBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("****** 扫雷 ******\n");
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");
}
}
打印棋盘之后我们需要的是布置雷
c
//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = EASY_COUNT;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
布置雷之后,就是应该想我们输入坐标来排雷
c
//统计雷的个数
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 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 (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,游戏失败\n");
PrintfBoard(mine, ROW, COL);
break;
}
else
{
//统计雷的个数
int ret = GetMineCount(mine, x, y);
show[x][y] = ret + '0';
PrintfBoard(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已被排查,请重新输入:\n");
}
}
else
{
printf("输入错误,请重新输入:\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功!\n");
PrintfBoard(mine, ROW, COL);
}
}
完整代码
game.h
c
#pragma once
#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 rows, int cols, char set);
//打印棋盘
void PrintfBoard(char board[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//统计雷的个数
int GetMineCount(char mine[ROWS][COLS], int x, int y);
game.c
c
#define _CRT_SECURE_NO_WARNINGS 1
//源文件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 PrintfBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("****** 扫雷 ******\n");
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 SetMine(char mine[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = EASY_COUNT;
while (count)
{
x = rand() % row + 1;
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 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 (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,游戏失败\n");
PrintfBoard(mine, ROW, COL);
break;
}
else
{
//统计雷的个数
int ret = GetMineCount(mine, x, y);
show[x][y] = ret + '0';
PrintfBoard(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已被排查,请重新输入:\n");
}
}
else
{
printf("输入错误,请重新输入:\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功!\n");
PrintfBoard(mine, ROW, COL);
}
}
test.c
c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void game()
{
//创建一个二维数组用来放置我们排雷时所看到的棋盘
//再创建一个二维数组用来布置雷
char mine[ROWS][COLS] = { 0 }; //用来显示雷的位置
char show[ROWS][COLS] = { 0 }; //用来排雷
//初始化棋盘
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印棋盘
PrintfBoard(show, ROW, COL);
//PrintfBoard(mine, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
PrintfBoard(mine, ROW, COL);
//排雷
FindMine(mine, show, ROW, COL);
}
void menu()
{
printf("************************\n");
printf("******* 1. Play ******\n");
printf("******* 0. Exit ******\n");
printf("************************\n");
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("扫雷游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input); //进入循环的条件是input为真
return 0;
}
主要是分享一个扫雷的代码,讲解并不是特别多,谢谢大家