今天给大家分享我们最适合初学者学习研究的经典扫雷游戏。
先看看代码跑起来后的效果
来看看源代码:
我们为了方便,同样分成了三个源文件,分别是test.c,game.c,game.h
直接上源代码
test.c
#define _CRT_SECURE_NO_WARNINGS 1
/*
* 程序名:扫雷
* 作者:Aron
* 时间:2024/8/14
*/
#include <windows.h>
#include "game.h"
#include <time.h>
void menu(void);
void game(void);
int main()
{
int choice = 1;
srand((unsigned int)time(NULL));
do
{
menu();//菜单
printf("please enter your choice(0/1):");
if (scanf("%d", &choice) != 1)
{
printf("please enter the correct format!\n");
while (getchar() != '\n');//清空缓冲区
Sleep(500);
system("cls");
continue;
}
switch (choice)
{
case 1:
system("cls");
game();
break;
case 0:
printf("\n-----------------exit!---------------\n");
exit(0);
break;
default:
printf("please choose again!\n");
break;
}
} while (choice);
system("pause");
return 0;
}
void menu(void)
{
printf("--------------------------------------\n");
printf("------------ ---------\n");
printf("------------ 1.play 0.exit ---------\n");
printf("------------ ---------\n");
printf("--------------------------------------\n\n\n");
}
void GameMenu(void)
{
printf("\t\t\t *************************************************\n");
printf("\t\t\t *************************************************\n");
printf("\t\t\t ************** 扫 雷 游 戏 **************\n");
printf("\t\t\t *************************************************\n");
printf("\t\t\t *************************************************\n\n\n");
}
void game(void)
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
GameMenu();//游戏菜单
Sleep(900);
InitBoard(mine, '0');//初始化mine棋盘
InitBoard(show, '*');//初始化show棋盘
SetMine(mine, '0', '1');//设置雷
DisplayBoard(show);//打印棋盘
FindMine(mine, show, '*', ' ', '1');//排查雷
system("pause");
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
int win = 0;
/* 棋盘初始化 */
void InitBoard(char board[ROWS][COLS], char ch)
{
int i, j;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
board[i][j] = ch;
}
}
}
/* 打印棋盘 */
void DisplayBoard(char board[ROWS][COLS])
{
int i, j;
printf("\t\t\t\t ");
for (i = 1; i <= ROW; i++)
{
printf("%3d", i);
printf(" ");
}
printf("----------->x\t\t<扫雷游戏>");
printf("\n");
for (i = 0; i <= ROW; i++)
{
printf("\t\t\t\t ");
for (j = 0; j <= COL; j++)
{
printf("|");
if (j < ROW)
printf("---");
}
printf("\n");
if (i < ROW)
{
printf("\t\t\t\t%2d", i + 1);
for (j = 0; j <= COL; j++)
{
printf("|");
if (j < COL)
printf(" %c ", board[i + 1][j + 1]);
}
printf("\n");
}
}
printf("\t\t\t\t |\n\t\t\t\t ↓\n\t\t\t\t y\n");
}
/* 棋盘设置雷 */
void SetMine(char board[ROWS][COLS], char ch1, char ch2)
{
int x, y;
int number = MINE;
while (number)
{
int x = rand() % ROW + 1;
int y = rand() % COL + 1;
if (board[x][y] == ch1)
{
board[x][y] = ch2;
number--;
}
else
continue;
}
}
/* 计算九宫格数量 */
static int CalMine(char mine[ROWS][COLS], int x, int y)
{
return mine[y - 1][x - 1] + mine[y - 1][x] + mine[y - 1][x + 1]
+ mine[y][x - 1] + mine[y][x + 1]
+ mine[y + 1][x - 1] + mine[y + 1][x] + mine[y + 1][x + 1] - 8 * '0';
}
/* 扩展无雷区域 */
static void ExpandBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, char space, char secret)
{
int num = CalMine(mine, x, y);
if (num && (show[y][x] == secret))
{
show[y][x] = num + '0';
win++;
}
else if (show[y][x] == secret)
{
int i, j;
show[y][x] = space;
win++;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (i >= 1 && i <= ROW && j >= 1 && j <= COL)
ExpandBoard(mine, show, i, j, space, secret);
}
}
}
}
/* 排查雷 */
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], char secret, char space, char ch)
{
int x = 0;//横坐标
int y = 0;//纵坐标
while (1)
{
printf("win = %d\t\ttarget:%d\n", win, ROW * COL - MINE);
printf("please enter the coordinate(space apart>(x y)):");
if (scanf("%d %d", &x, &y) != 2)
{
printf("format error!\n");
while (getchar() != '\n');
continue;
}
else if (x < 1 || x > 9 || y < 1 || y > 9)
{
printf("Coordinates out of range!\n");
continue;
}
else if (show[y][x] != secret)
{
printf("The coordinates have been checked!\n");
continue;
}
else
{
if (mine[y][x] == ch)
{
printf("\n\t\t\t--------------------- failed! ---------------------\n");
DisplayBoard(mine);
win = 0;
break;
}
else
{
ExpandBoard(mine, show, x, y, space, secret);
system("cls");
DisplayBoard(show);
if (win == ROW * COL - MINE)
{
printf("\n\t\t\t--------------------- You win! ---------------------\n");
win = 0;
break;
}
}
}
}
}
game.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#define ROW 9
#define COL 9
#define ROWS 11
#define COLS 11
#define MINE 10
/* 棋盘初始化 */
void InitBoard(char board[ROW][COL], char ch);
/* 打印棋盘 */
void DisplayBoard(char board[ROWS][COLS]);
/* 棋盘设置雷 */
void SetMine(char board[ROW][COL], char ch1, char ch2);
/* 排查雷 */
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], char secret, char space, char ch);
有不懂的地方请评论留言