前言
扫雷想必大家都玩过,实现起来其实并不困难,上图
设计
![在这里插入图片描述](https://img-blog.csdnimg.cn/055d6800d2a24d439a0b900ea524f26d.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2V2b2tlX2M=,size_16,color_FFFFFF,t_70)
代码展示
扫雷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 set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col);
void PlayGame(char mine[ROWS][COLS], char show[ROWS][COLS]);
void Expand(char mine[ROWS][COLS], int x, int y, char show[ROWS][COLS], int* p);
void ResetMine(char mine[ROWS][COLS], int row, int col, int x, int y, int count);
扫雷game.c
#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 <= row; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", 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] != '1')
{
board[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 ResetMine(char mine[ROWS][COLS], int row, int col, int x, int y, int count)
{
mine[x][y] = '0';
while (count)
{
int i = rand() % row + 1;
int j = rand() % col + 1;
if ((mine[i][j] != '1') && i != x && j != y)
{
mine[i][j] = '1';
count--;
}
}
}
void Expand(char mine[ROWS][COLS], int x, int y, char show[ROWS][COLS], int* p)
{
int i = -1;
int j = -1;
for (i = -1; i < 2; i++)
{
for (j = -1; j < 2; j++)
{
if (i != 0 || j != 0)
{
if (x + i >= 1 && x + i <= ROW && y + j >= 1 && y + j <= COL)
{
if (show[x + i][y + j] == '*' && mine[x + i][y + j] != '1')
{
int count = GetMineCount(mine, x + i, y + j);
if (count != 0)
{
show[x + i][y + j] = count + '0';
(*p)++;
}
else
{
show[x + i][y + j] = ' ';
(*p)++;
Expand(mine, x + i, y + j, show, p);
}
}
}
}
}
}
}
void PlayGame(char mine[ROWS][COLS], char show[ROWS][COLS])
{
int x = 0;
int y = 0;
int win = 0;
int count = 0;
while (win < ROW * COL - count)
{
printf("请输入坐标:>");
scanf_s("%d %d", &x, &y);
if (show[x][y] == count + '0')
{
printf("已经排过雷\n");
}
if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
{
if (mine[x][y] == '1')
{
if (0 == win)
{
ResetMine(mine, ROW, COL, x, y, 1);
count = GetMineCount(mine, x, y);
if (count == 0)
{
show[x][y] = ' ';
win++;
Expand(mine, x, y, show, &win);
DisplayBoard(show, ROW, COL);
}
else
{
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
}
else
{
count = GetMineCount(mine, x, y);
if (count == 0)
{
show[x][y] = ' ';
}
else
{
show[x][y] = count + '0';
}
win++;
Expand(mine, x, y, show, &win);
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("输入坐标不合法\n");
}
}
if (win == ROW * COL - count)
{
printf("排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
扫雷test.c
#include"扫雷game.h"
void menu()
{
printf("******************\n");
printf("***** 1.Play *****\n");
printf("***** 0.Exit *****\n");
printf("******************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
SetMine(mine,ROW,COL);
DisplayBoard(show, ROW, COL);
PlayGame(mine, show);
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf_s("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择!\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
实操演示
差一点就赢了,555~~~ ![在这里插入图片描述](https://img-blog.csdnimg.cn/8cfe0efee35b47e5bb3dd5e0fad97dc6.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2V2b2tlX2M=,size_16,color_FFFFFF,t_70)
壁纸+1
![在这里插入图片描述](https://img-blog.csdnimg.cn/949c00d8369e4de98e052d2909f1f8c4.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2V2b2tlX2M=,size_16,color_FFFFFF,t_70)
|