“扫雷”C语言实现(感染效果)
game.h
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
enum Option
{
//棋盘大小
rows = 9,
cols = 9,
Rows = rows + 2,
Cols = cols + 2,
//难度,相当于布置几个雷
Difficulty = 10
};
void menu();
void InitBoard(char Board[Rows][Cols],int Row,int Col, char a);
void SetMine(char Board[Rows][Cols],int Row,int Col);
void Display(char Board[Rows][Cols], int Row, int Col);
void Playgame(char Board[Rows][Cols], char ShowBoard[Rows][Cols]);
int Countmine(char Board[Rows][Cols], char ShowBoard[Rows][Cols], int Row, int Col);
void Openmine(char Board[Rows][Cols], char ShowBoard[Rows][Cols], int Row, int Col);
test.c
#include"game.h"
void game()
{
//创建游戏棋盘
char MineBoard[Rows][Cols] = { 0 };
//这个是给玩家看的棋盘
//给玩家看的棋盘肯定与原棋盘不一样
//什么能看、什么不能看
char ShowBoard[Rows][Cols] = { 0 };
//初始化棋盘
InitBoard(MineBoard, Rows, Cols,'0');
InitBoard(ShowBoard, Rows, Cols, '*');/
//展示棋盘
//Display(MineBoard, Rows, Cols);//这个不能展示给玩家哦
Display(ShowBoard, Rows, Cols);
printf("\n");
//随机放置炸弹
SetMine(MineBoard, Rows, Cols);
//printf("\n");
//Display(MineBoard, Rows, Cols);//查看是否设置成功
//准备工作完成,玩家正式开始游戏
Playgame(MineBoard, ShowBoard);
}
void test()
{
menu();
int input;
printf("请输入:>");
scanf_s("%d", &input);
switch (input-1)
{
case 0:
game();
break;
case 1:
printf("正在退出游戏");
break;
default:
printf("输入错误,请输入1或2");
break;
}
}
int main()
{
srand((unsigned)time(NULL));
test();
return 0;
}
game.c
#include"game.h"
void menu()
{
printf("*************************\n");
printf("*******1.play************\n");
printf("*******2.exit************\n");
printf("*************************\n");
}
void InitBoard(char Board[Rows][Cols], int Row, int Col,char a) //
{
int i , j ;
for (i = 0;i < Row; i++)
{
for (j = 0;j < Col; j++)
{
Board[i][j] = a;
}
}
}
void SetMine(char Board[Rows][Cols], int Row, int Col)//
{
int flag = 0;
while (flag< Difficulty)
{
int x = rand() % 9+1;
int y = rand() % 9+1;
if (Board[x][y] == '0')
{
Board[x][y] = '1';
flag++;
}
}
}
void Display(char Board[Rows][Cols], int Row, int Col)
{
int i = 0, j = 0;
for (i = 0; i < Rows-1; i++)
{
printf("\n");
for (j = 0; j < Cols-1; j++)
{
if (i == 0 && j != 0)
{
printf("%d", j);
}
else if (j == 0 && i != 0)
{
printf("%d", i);
}
else if (j == 0 && i == 0)
{
printf("0");
}
else if (0<j< Rows-1 && 0 < i < Cols - 1)
printf("%c", Board[i][j]);
}
}
printf("\n");
}
void Openmine(char Board[Rows][Cols], char ShowBoard[Rows][Cols], int Row, int Col)
{
int c = 0;
c =Countmine(Board, ShowBoard, Row, Col);
if (c == 0)
{
ShowBoard[Row][Col] = '0';
if (Row - 1 > 0 && ShowBoard[Row-1][Col] == '*')
{
Openmine(Board, ShowBoard, Row - 1, Col);
}
if (Row + 1 < Rows - 1 && ShowBoard[Row+1][Col] == '*')
{
Openmine(Board, ShowBoard, Row + 1, Col);
}
if (Col - 1 > 0 && ShowBoard[Row][Col-1] == '*')
{
Openmine(Board, ShowBoard, Row, Col - 1);
}
if (Col + 1 < Cols - 1 && ShowBoard[Row][Col+1] == '*')
{
Openmine(Board, ShowBoard, Row, Col + 1);
}
if (Row - 1 > 0 && Col - 1 > 0 && ShowBoard[Row-1][Col-1] == '*')
{
Openmine(Board, ShowBoard, Row - 1, Col - 1);
}
if (Row - 1 > 0 && Col + 1 < Cols - 1 && ShowBoard[Row-1][Col+1] == '*')
{
Openmine(Board, ShowBoard, Row - 1, Col+ 1);
}
if (Col - 1 > 0 && Row + 1 > 0 && ShowBoard[Row+1][Col-1] == '*')
{
Openmine(Board, ShowBoard, Row + 1, Col - 1);
}
if (Col + 1 <Cols-1 && Row + 1 < Rows-1 && ShowBoard[Row+1][Col+1] == '*')
{
Openmine(Board, ShowBoard, Row+1, Col + 1);
}
}
else//
{
ShowBoard[Row][Col] = c + '0';
}
}
int Countmine(char Board[Rows][Cols], char ShowBoard[Rows][Cols], int Row, int Col )//
{
int c = Board[Row - 1][Col - 1] + Board[Row - 1][Col + 1]
+ Board[Row - 1][Col]
+ Board[Row][Col - 1]
+ Board[Row][Col + 1]
+ Board[Row + 1][Col - 1]
+ Board[Row + 1][Col]
+ Board[Row + 1][Col + 1] - 8 * '0';
return c;
}
void Playgame(char Board[Rows][Cols], char ShowBoard[Rows][Cols])
{
int x = 0, y = 0;
while (1)
{
printf("开始游戏!请输入您选择的坐标位置:>\n");
scanf_s("%d%d", &x, &y);
if (x > 0 && x <= 9 && y <= 9 && y > 0)
{
if (Board[x][y] == '0')
{
Openmine(Board, ShowBoard, x, y);//利用递归实现炸开效果
Display(ShowBoard, Rows, Cols);
//检查有没有胜利
int flag = 0;
for (int i = 1; i < Rows - 1; i++)
{
for (int j = 1; j < Cols - 1; j++)
{
if (ShowBoard[i][j] == '*')
{
++flag;
}
}
}
if (flag == Difficulty)
{
printf("你已找出所有炸弹,游戏胜利!");
break;
}
}
else
{
printf("炸弹bomb!游戏结束\n");
Display(Board, Rows, Cols);
break;
}
}
else
{
printf("输入错误,请重新输入1~9的数字");
}
}
}