【C语言】扫雷
看这篇博客之前,可已先看一下我前面的一篇三子棋的博客,两个都是二维数组的小应用,我在这里就不讲了直接上代码,因为打字讲的话很难讲通透,真的想学会的话可以到B站上看鹏哥的视频。
代码运行起来是这样的:
还是三个文件:
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void game()
{
//GameTime();
char mine[ROWS][COLS];
char show[ROWS][COLS];
//初始化
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//生成雷
SetMine(mine, ROW, COL);
while (1)
{
system("Cls");
DisplayBoard(show, ROW, COL);
//选位置
int ret = find(mine, show, ROW, COL);
if (ret == -1)
{
system("Cls");
printf("你被炸死了,地雷如下(1为雷)\n");
DisplayBoard(mine, ROW, COL);
break;
}
else if(ret == NUM_OF_MINE)
{
DisplayBoard(show, ROW, COL);
printf("玩家胜利\n");
break;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("\t\t\t\t\t请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
//菜单
void menu()
{
printf("\n\n\n\n\n\n\n\n");
printf("\t\t\t\t\t********************\n");
printf("\t\t\t\t\t****** 1.play ******\n");
printf("\t\t\t\t\t****** 0.exit ******\n");
printf("\t\t\t\t\t********************\n");
}
//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = ch;
}
}
}
//打印
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("\n\n\n\n\n");
printf("\t\t\t\t\t");
for (int i = 0; i <= row; i++)
{
printf("%-2d ", i);
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("\t\t\t\t\t%-2d ", i);
for (int j = 1; j <= col; j++)
{
printf("%-2c ", board[i][j]);
}
printf("\n");
}
printf("-----------------------------------------------------扫雷---------------------------------------------------------------\n");
}
//选位置排雷
int find(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
printf("\t\t\t\t\t请选择坐标:");
scanf("%d %d", &x, &y);
//坐标合法
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//该坐标在展示面板处是否被排查过
if (show[x][y] == '*')
{
//该处是否有雷
if (mine[x][y] == '1')
{
//炸死了
return -1;
}
else
{
ShowMove(mine, show, x, y);
}
}
else
{
printf("该位置已经排查过了\n");
}
}
else
{
printf("坐标非法,重新输入。\n");
}
return IsWin(show, row, col);
}
//生成雷
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = NUM_OF_MINE;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
//周围雷数
int SurroundMine(char mine[ROWS][COLS], int x, int y)
{
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';
}
//递归排雷
void ShowMove(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
if (SurroundMine(mine, x, y) == 0)
{//没有雷就进去
show[x][y] = ' ';
//在边上就不递归
if ((x == 1 && y >= 1 && y <= COL) ||
(x == ROW && y >= 1 && y <= COL) ||
(y == 1 && x >= 1 && x <= ROW) ||
(y == COL && x >= 1 && x <= ROW))
{
//上方
if (x == 1 && y >= 1 && y <= COL)
{
if (y == 1)
{
ShowMove(mine, show, x, y + 1);
ShowMove(mine, show, x + 1, y);
ShowMove(mine, show, x + 1, y + 1);
}
else if (y == COL)
{
ShowMove(mine, show, x, y + 1);
ShowMove(mine, show, x - 1, y);
ShowMove(mine, show, x - 1, y + 1);
}
else
{
ShowMove(mine, show, x, y + 1);
ShowMove(mine, show, x + 1, y);
ShowMove(mine, show, x + 1, y + 1);
ShowMove(mine, show, x - 1, y);
ShowMove(mine, show, x - 1, y + 1);
}
}
//下方
else if (x == ROW && y >= 1 && y <= COL)
{
if (y == 1)
{
ShowMove(mine, show, x, y - 1);
ShowMove(mine, show, x + 1, y);
ShowMove(mine, show, x + 1, y - 1);
}
else if (y == COL)
{
ShowMove(mine, show, x - 1, y - 1);
ShowMove(mine, show, x - 1, y);
ShowMove(mine, show, x, y - 1);
}
else
{
ShowMove(mine, show, x + 1, y);
ShowMove(mine, show, x + 1, y - 1);
ShowMove(mine, show, x - 1, y - 1);
ShowMove(mine, show, x - 1, y);
ShowMove(mine, show, x, y - 1);
}
}
//左方
else if (y == 1 && x >= 1 && x <= ROW)
{
if (x == 1)
{
ShowMove(mine, show, x + 1, y);
ShowMove(mine, show, x + 1, y + 1);
ShowMove(mine, show, x, y + 1);
}
else if (x == ROW)
{
ShowMove(mine, show, x + 1, y);
ShowMove(mine, show, x + 1, y - 1);
ShowMove(mine, show, x, y - 1);
}
else
{
ShowMove(mine, show, x + 1, y);
ShowMove(mine, show, x + 1, y + 1);
ShowMove(mine, show, x + 1, y - 1);
ShowMove(mine, show, x, y + 1);
ShowMove(mine, show, x, y - 1);
}
}
//右方
else if (y == COL && x >= 1 && x <= ROW)
{
if (x == 1)
{
ShowMove(mine, show, x - 1, y);
ShowMove(mine, show, x - 1, y + 1);
ShowMove(mine, show, x, y + 1);
}
else if (x == ROW)
{
ShowMove(mine, show, x - 1, y - 1);
ShowMove(mine, show, x - 1, y);
ShowMove(mine, show, x, y - 1);
}
else
{
ShowMove(mine, show, x - 1, y);
ShowMove(mine, show, x - 1, y + 1);
ShowMove(mine, show, x - 1, y - 1);
ShowMove(mine, show, x, y + 1);
ShowMove(mine, show, x, y - 1);
}
}
}
else
{
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
if (show[x + i][y + j] == '*')
{
ShowMove(mine, show, x + i, y + j);
}
}
}
}
}
else
{
show[x][y] = '0' + SurroundMine(mine, x, y);
}
}
//判断是否排完雷
int IsWin(char show[ROWS][COLS], int row, int col)
{
int count = 0;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if (show[i][j] == '*')
{
count++;
}
}
}
return count;
}
void GameTime()
{
int hour = 0, min = 0, sec = 0;
int cnt = 0;
while (1) //任意键退出循环(结束计时)
{
hour = cnt / 3600; //获取计时小时数
min = cnt / 60; //获取计时分钟数
sec = cnt % 60; //获取计时秒数
printf(" %02d:%02d:%02d\r", hour, min, sec);
Sleep(1000); //1s延时
cnt++;
}
}
game.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <Windows.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define NUM_OF_MINE 10
//菜单
void menu();
//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ch);
//打印
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//选位置排雷
int find(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//生成雷
void SetMine(char board[ROWS][COLS], int row, int col);
//周围雷数
int SurroundMine(char board[ROWS][COLS], int row, int col);
//递归排雷
void ShowMove(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
//判断是否排完雷
int IsWin(char board[ROWS][COLS], int row, int col);
//时间
void GameTime();
到此结束。