前言
用C语言实现扫雷游戏,标记功能,取消标记功能,自动拓展功能,标记只是雷,并且数量等于雷的数量,自动获胜。
一、扫雷完整代码
// test.c 源文件
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()
{
printf("********************\n");
printf("***** 1. Play *****\n");
printf("***** 0. Quit *****\n");
printf("********************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
int choose = 0;
int win = 0; // 记录标记的雷的个数
int sign = 0; // 记录标记的个数
// 初始化棋盘 一个是布置雷的信息 一个是排查雷的信息
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
// 设置雷
SetMine(mine, ROW, COL);
// 打印初始化棋盘
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
while (win < ROW * COL - EASY_COUNT)
{
printf("********************\n");
printf("***** 1. 标记 *****\n");
printf("***** 2. 排雷 *****\n");
printf("********************\n");
printf("请选择:>");
scanf("%d", &choose);
switch (choose)
{
case 1:
// 标记
win = SignShow(show,mine,ROW,COL);
break;
case 2:
// 排雷
win = FindMine(mine, show, ROW, COL, win);
break;
default :
printf("选择错误,请重新选择\n");
break;
}
if (win == -1)
{
break;
}
}
if (win == ROW * COL - EASY_COUNT)
{
printf("恭喜你,排雷成功!\n");
DisplayBoard(mine, ROW, COL);
}
// 排雷
//FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
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);
return 0;
}
// game.h 头文件
#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 ret);
// 打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);
// 设置雷
void SetMine(char board[ROWS][COLS], int row, int col);
// 排雷
int FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int win);
// 标记函数
int SignShow(char show[ROWS][COLS], char mine[ROWS][COLS], int row, int col);
// 拓展函数
void Extend(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
// game.c 源文件
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
// 初始化函数声明
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = ret;
}
}
}
// 打印函数定义
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("----------扫雷游戏-----------\n");
int i = 0;
for (i = 0; i <= row; i++)
{
printf("%2d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%2d ", i);
for (j = 1; j <= col; j++)
{
// 打印数据
printf("%2c ", board[i][j]);
}
printf("\n");
}
printf("----------扫雷游戏-----------\n");
}
// 设置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
// 标记雷
int SignShow(char board[ROWS][COLS],char mine[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int flag = 0; // 判断标记还是取消标记
static int count = 0; // 标记的个数
static int sign = 0;
while(1)
{
printf("*********************\n");
printf("***** 1.标记 ********\n");
printf("***** 0.取消标记 ****\n");
printf("*********************\n");
printf("请选择操作:>");
scanf("%d", &flag);
if (1 == flag)
{
printf("请输入标记坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x][y] == '*')
{
sign++;
board[x][y] = '$';
if ('1' == mine[x][y])
{
count++;
}
DisplayBoard(board, ROW, COL);
break;
}
else
{
printf("已经排查过了,无法标记!\n");
DisplayBoard(board, ROW, COL);
break;
}
}
else
{
printf("超出棋盘范围,请重新输入\n");
DisplayBoard(board, ROW, COL);
break;
}
}
else if(0 == flag)
{
printf("请输入标记坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x][y] == '$')
{
sign--;
board[x][y] = '*';
if ('1' == mine[x][y])
{
count--;
}
DisplayBoard(board, ROW, COL);
break;
}
else
{
printf("此处未被标记,请选择其他位置\n");
DisplayBoard(board, ROW, COL);
break;
}
}
else
{
printf("超出棋盘范围,请重试\n");
DisplayBoard(board, ROW, COL);
break;
}
}
else
{
printf("输入错误,请重新输入\n");
DisplayBoard(board, ROW, COL);
}
}
if (sign == count && count == EASY_COUNT)
{
sign = 0;
count = 0;
return (ROW * COL - EASY_COUNT);
}
}
// 计算雷的个数
int Sum_mine(char board[ROWS][COLS], int x, int y)
{
int i = 0;
int sum = 0;
for (i = -1; i <= 1; i++)
{
int j = 0;
for (j = -1; j <= 1; j++)
{
//printf("%c", board[x + i][y + i]);
sum = sum + (board[x + i][y + j] - '0');
//printf("%d", sum);
}
}
return sum;
//return (mine[x - 1][y - 1] +
// mine[x - 1][y] +
// mine[x - 1][y + 1] +
// mine[x][y - 1] +
// mine[x][y + 1] +
// mine[x + 1][y - 1] +
// mine[x + 1][y] +
// mine[x + 1][y + 1] - 8 * '0');
}
// 排雷
int FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int win)
{
int x = 0;
int y = 0;
int wins = win;
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");
DisplayBoard(mine, ROW, COL);
wins = -1;
return wins;
}
else
{
wins++;
int count = Sum_mine(mine, x, y);
printf("%d", count);
if (count == 0)
{
//printf("开始执行拓展函数\n");
Extend(mine, show,x,y);
DisplayBoard(show, ROW, COL);
}
else
{
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
}
return wins;
}
}
else
{
printf("已经排查过了,请勿重复排查\n");
}
}
else
{
printf("超出棋盘范围,请重新输入\n");
}
}
// 拓展函数
void Extend(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
//DisplayBoard(mine, ROW, COL);
//printf("%d", mine[x][y]);
int count = Sum_mine(mine, x, y);
if (0 == count)
{
show[x][y] = ' ';
int i = 0;
for (i = -1; i <= 1; i++)
{
int j = 0;
for (j = -1; j <= 1; j++)
{
if (i == j && 0 == j)
{
continue;
}
else if (show[x + i][y + j] == ' ')
{
continue;
}
else
{
Extend(mine, show, x + i, y + j);
}
}
}
}
else
{
show[x][y] = count + '0';
}
}
总结
用C语言实现扫雷游戏,标记功能,取消标记功能,自动拓展功能,标记只是雷,并且数量等于雷的数量,自动获胜。自动拓展到有雷的地方显示周围雷的个数。