今天我展示的是c语言版的
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 15
#define COL 15
#define ROWS ROW+2
#define COLS COL+2
#define easy_count 20
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);
int getmine(char mine[ROWS][COLS], int x, int y);
void OpenMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y);
int iswine(char show[ROWS][COLS], int row, int col);
void menu()
{
printf("*********************************\n");
printf("*************1.play**************\n");
printf("*************0.exit**************\n");
printf("*********************************\n");
}
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;
for (i = 1; i <= row; i++)
{
int j = 0;
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
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--;
}
}
}
void FindMINE(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
while (iswine(show,row,col)!=easy_count)
{
printf("请输入一个坐标");
int x, y;
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遗憾\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
int count = getmine(mine, x, y);
show[x][y] = '0'+count;
OpenMine(mine, show, row, col,x,y);
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("请入坐标有误\n");
}
}
if (iswine(show,row,col)==easy_count)
{
printf("恭喜你,排雷成功\n");
}
}
int getmine(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 OpenMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y) //展开雷阵
{
int ret = 0;
ret = getmine(mine, x, y);
if (ret == 0)
{
show[x][y] = ' ';
if (x - 1 > 0 && y > 0&&x-1<=row&&y<=col && show[x - 1][y] == '*')
OpenMine(mine, show, row, col, x - 1, y);
if (x - 1 > 0 && y + 1 <= col && show[x - 1][y + 1] == '*')
OpenMine(mine, show, row, col, x - 1, y + 1);
if (x > 0 && y + 1 <= col && show[x][y + 1] == '*')
OpenMine(mine, show, row, col, x, y + 1);
if (x + 1 <= row && y + 1 <= col && show[x + 1][y + 1] == '*')
OpenMine(mine, show, row, col, x + 1, y + 1);
if (x + 1 <= row && y > 0 && show[x + 1][y] == '*')
OpenMine(mine, show, row, col, x + 1, y);
if (x + 1 <= row && y - 1 > 0 && show[x + 1][y - 1] == '*')
OpenMine(mine, show, row, col, x + 1, y - 1);
if (x > 0 && y - 1 > 0 && show[x][y - 1] == '*')
OpenMine(mine, show, row, col, x, y - 1);
if (x - 1 > 0 && y - 1 > 0 && show[x - 1][y - 1] == '*')
OpenMine(mine, show, row, col, x - 1, y - 1);
}
else
{
show[x][y] = getmine(mine, x, y) + '0';
}
}
int iswine(char show[ROWS][COLS], int row, int col)
{
int count = 0;
int i = 0;
for (i = 1; i <= row; i++)
{
int j;
for (j = 1; j <= col; j++)
{
if (show[i][j] == '*')
count++;
}
}
return count;
}
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(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//排雷
FindMINE(mine,show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出系统\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
}