该程序使用一个二维字符数组来表示游戏面板,其中每个位置的字符可以是' * '、'1'或'0'-'9'。' * '表示未被揭开的未知格子,'1'表示地雷,'0'-'9'表示相邻地雷的数量。程序通过随机放置地雷,并根据玩家输入的坐标逐步揭开格子,直到所有非地雷格子被揭开或玩家触雷为止。
game.h
头文件,包含了 C 函数声明和宏定义,
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define _CRT_SECURE_NO_WARNINGS
#define EASY_COUNT 10
//定义ROW(行)为9
//定义COL(列)为9
#define ROW 9 //9行
#define COL 9 //9列
#define ROWS ROW+2
#define COLS COL+2
//声明自定功能函数
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void zhankai(char board[ROWS][COLS], int row, int col);
game.cpp
游戏的功能主体
初始化棋盘
#include"game.h"
//初始化棋盘
void InitBoard(char board[ROWS][COLS],int rows,int cols,char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
显示棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
printf("--------扫雷--------\n");
for(i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
int j = 0;
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("--------扫雷--------\n");
}
布置地雷
布置10颗地雷
生成随机坐标,布置雷
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 GetMineCount(char mine[ROWS][COLS], int x, int y)
{
return(mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x - 1][y] + mine[x + 1][y] + mine[x - 1][y + 1] + mine[x][y + 1] + mine[x + 1][y + 1] - 8 * '0');
}
显示空格
展开周围非地雷的格子
void zhankai(char show[ROWS][COLS],char mine[ROWS][COLS], int x, int y)
{
int h = 0;
for (int h = x - 1; h <= x + 1; h++)
{
int l = 0;
for (int l = y - 1; l <= y; l++)
{
if (mine[h][l] == '0')
{
int n = GetMineCount(mine, h, l);
show[h][l] = n + '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_s("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
system("cls");
printf("碰到地雷,游戏结束!\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
show[x][y] = count + '0';
zhankai(show, mine,x,y);
system("cls");
DisplayBoard(show, ROW, COL);
win++;
}
}
else
{
printf("输入错误,重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("排雷完毕,游戏胜利!\n");
DisplayBoard(mine, ROW, COL);
}
}
test.cpp
实现游戏的逻辑流程
#include"game.h"
//菜单
void menu()
{
printf("********************\n");
printf("***-----扫雷-----***\n");
printf("*** ***\n");
printf("*** 1.play ***\n");
printf("*** 0.exit ***\n");
printf("*** ***\n");
printf("********************\n");
}
//游戏流程逻辑
void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
DisplayBoard(show, ROW, COL);
SetMine(mine, ROW, COL);
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//生成随机数种子
do
{
menu();
printf("请选择:>");
scanf_s("%d", &input);
switch (input)
{
case 0:
break;
case 1:
system("cls");
game();
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
感谢浏览