简介
游戏通过game.h 、game.c 和test.c 三个文件协同实现,因为最近时间比较紧张,暂时先把代码贴出来,希望能帮到有需要的小伙伴,同时代码中也存在不足的地方,还请各位大佬批评指正。
game.h 文件
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9
#define ROWS COL+2
#define COLS ROW+2
#define EASY 10
#define MEDIUM 30
#define HARD 50
#define EXTREME 80
extern void InitBoard(char board[ROWS][COLS],int rows,int cols, char set);
extern void ShowBoard(char board[ROWS][COLS], int row, int col);
extern void SetMine(char mine[ROWS][COLS], int row, int col, int game_mode);
extern void SweepMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row, int col);
extern int CountMineNum(char mine[ROWS][COLS], int x, int y);
extern void OpenNull(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col);
extern int isWin(char show[ROWS][COLS], int row, int col);
game.c 文件
#include "game.h"
void InitBoard(char board[ROWS][COLS],int rows,int cols, char set)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void ShowBoard(char board[ROWS][COLS], int row, int col)
{
int i, j;
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 count)
{
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] != '1')
{
mine[x][y] = '1';
count--;
}
}
}
void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0, y = 0,num = 0,count = 0,win = 1;
while (win)
{
ShowBoard(show, ROW, COL);
printf("请输入坐标:>");
scanf("%d%d", &x, &y);
if (x < 1 || x > row || y < 1 || y > col)
{
printf("输入有误,请重新输入!\n");
}
else if (mine[x][y] == '1')
{
printf("扫雷失败,再接再厉!\n");
ShowBoard(mine, ROW, COL);
return;
}
else {
OpenNull(mine,show,x,y,row,col);
win = isWin(show, row, col);
}
}
printf("扫雷成功,恭喜!!\n");
}
int CountMineNum(char mine[ROWS][COLS],int x, int y)
{
int i = 0, j = 0, count = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (mine[i][j] == '1')
count++;
}
}
return count;
}
void OpenNull(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col)
{
int i = 0, j = 0,count = 0;
int num = CountMineNum(mine, x, y);
if (!num && 1 <= x && x <= row && 1 <= y && y <= col )
{
show[x][y] = ' ';
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (show[i][j] != ' ')
{
OpenNull(mine, show, i, j, row, col);
}
}
}
}
else {
show[x][y] = num + '0';
}
}
int isWin(char show[ROWS][COLS], int row, int col)
{
int i = 0, j = 0,count = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show[i][j] == '*')
{
count++;
}
}
}
if (count == EASY)
return 0;
return 1;
}
test.c 文件
#include "game.h"
void menu()
{
printf("**********************************************************\n");
printf("******************* 1. Play *********************\n");
printf("******************* 0. Exit *********************\n");
printf("**********************************************************\n");
printf("请输入选择:>");
}
void game()
{
printf("欢迎进入扫雷游戏!\n");
srand((unsigned int)time(NULL));
char mine[ROWS][COLS];
char show[ROWS][COLS];
InitBoard(mine,ROWS,COLS, '0');
InitBoard(show, ROWS, COLS, '*');
SetMine(mine, ROW, COL, EASY);
SweepMine(mine, show, ROW, COL);
}
int main()
{
int input = 1;
while (input)
{
menu();
scanf("%d", &input);
while (getchar() != '\n') {
;
}
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出成功!\n");
break;
default:
printf("输入有误请重新输入!\n");
break;
}
}
return 0;
}
游戏效果

|