1>第一次下子,不炸死。
2>坐标周围没雷,可以实现展开。
game.h
#ifndef __GAME_H__
#define __GAME_H__
#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 70
//void InitMine(char mine[ROWS][COLS],int row,int col);
//void InitShow(char show[ROWS][COLS],int row,int col);
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 mine[ROWS][COLS], int row, int col);
char GetMineCount(char mine[ROWS][COLS], int x, int y);
void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS]);
void RmoveMine(char mine[ROWS][COLS], int x, int y);
void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
#endif
game.c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void InitMine(char mine[ROWS][COLS],int row,int col)
{
int i = 0;
int j = 0;
/*char set;
scanf("%c",&set);*/
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
mine[i][j]='0';
}
}
}
void InitShow(char show[ROWS][COLS],int row,int col)
{
int i = 0;
int j = 0;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
show[i][j]='*';
}
}
}
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) //初始化
{
int i = 0;
int j = 0;
for (i = 0; i<rows; i++)
{
for (j = 0; j<cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col) //显示棋盘
{
int i = 0;
int j = 0;
printf(" ");
for (i = 1; i<row - 1; i++)
{
printf(" %d", i);
}
printf("\n");
for (i = 1; i<row - 1; i++)
{
printf("%2d ", i);
for (j = 1; j<col - 1; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char mine[ROWS][COLS], int row, int col) //随机布置雷
{
int x = 0;
int y = 0;
int minecount = EASY_COUNT;
srand((unsigned)time(NULL));
while (minecount)
{
x = rand() % (ROW)+1;
y = rand() % (COL)+1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
minecount--;
}
}
}
char GetMineCount(char mine[ROWS][COLS], int x, int y) //统计周围雷的个数函数
{
int minecount = 0;
if (mine[x - 1][y] == '1')
minecount++;
if (mine[x - 1][y + 1] == '1')
minecount++;
if (mine[x - 1][y + 1] == '1')
minecount++;
if (mine[x][y + 1] == '1')
minecount++;
if (mine[x + 1][y + 1] == '1')
minecount++;
if (mine[x + 1][y] == '1')
minecount++;
if (mine[x + 1][y - 1] == '1')
minecount++;
if (mine[x][y - 1] == '1')
minecount++;
if (mine[x - 1][y - 1] == '1')
minecount++;
return minecount;
}
void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS]) //排雷函数
{
int x = 0;
int y = 0;
int clear = 0; //已经排雷个数
int minecount = 0; //周围雷数
while (clear < (ROW)*(COL)-EASY_COUNT)
{
printf("开始排雷,请输入坐标:\n");
scanf("%d%d", &x, &y);
if (mine[x][y] == '1')
{
if (clear == 0) //第一次踩雷移走
{
RmoveMine(mine, x, y);
DisplayBoard(mine, ROWS, COLS);
printf("\n");
minecount = GetMineCount(mine, x, y);
if (minecount == 0)
{
show[x][y] = ' ';
clear++;
Expand(mine, show, x, y);
DisplayBoard(show, ROWS, COLS);
}
else
{
show[x][y] = minecount + '0';
DisplayBoard(show, ROWS, COLS);
}
}
else
{
printf("很遗憾,你被炸死了!\n");
DisplayBoard(mine, ROWS, COLS);
break;
}
}
else if (mine[x][y] != '1')
{
minecount = GetMineCount(mine, x, y);
if (minecount == 0)
{
show[x][y] = ' ';
}
else
{
show[x][y] = minecount + '0';
}
clear++;
Expand(mine, show, x, y);
DisplayBoard(show, ROWS, COLS);
}
if (clear == (ROW)*(COL)-EASY_COUNT)
{
printf("扫雷成功\n");
}
}
}
void RmoveMine(char mine[ROWS][COLS], int x, int y)//第一次踩雷 移走
{
mine[x][y] = '0';
GetMineCount(mine, x, y);
while (1)
{
int x1 = rand() % (ROW)+1;
int y1 = rand() % (COL)+1;
if (mine[x1][y1] != '1' && ((x1 != x) && (y1 != y)))
{
mine[x1][y1] = '1';
break;
}
}
}
void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)//展开函数
{
if ((x >= 1) && (y >= 1) && (x <= ROW) && (y <= COL))
{
if (GetMineCount(mine, x, y) == 0) //周围没有雷
{
show[x][y] = ' '; //置为空白
if (show[x - 1][y - 1] == '*')
{
Expand(mine, show, x - 1, y - 1);
}
if (show[x - 1][y] == '*')
{
Expand(mine, show, x - 1, y);
}
if (show[x - 1][y + 1] == '*')
{
Expand(mine, show, x - 1, y + 1);
}
if (show[x][y + 1] == '*')
{
Expand(mine, show, x, y + 1);
}
if (show[x + 1][y + 1] == '*')
{
Expand(mine, show, x + 1, y + 1);
}
if (show[x + 1][y] == '*')
{
Expand(mine, show, x + 1, y);
}
if (show[x + 1][y - 1] == '*')
{
Expand(mine, show, x + 1, y - 1);
}
if (show[x][y - 1] == '*')
{
Expand(mine, show, x, y - 1);
}
}
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu() //打印菜单
{
printf(" \n");
printf(" 欢迎进入扫雷界面 \n");
printf(" \n");
printf(" 1 play 0 exit \n");
}
void game()
{
int x = 0;
int y = 0;
int count = 0;
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
InitBoard(show, ROWS, COLS, '*');
DisplayBoard(show, ROWS, COLS);
printf("\n");
InitBoard(mine, ROWS, COLS, '0');
/*DisplayBoard(mine,ROWS,COLS);*/
SetMine(mine, ROW, COL);
/*DisplayBoard(mine,ROWS,COLS);*/
printf("\n");
SweepMine(mine, show);
}
void test()
{
int input = 0;
do
{
menu();
printf("请选择>\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入不正确 ,请重新选择!\n");
break;
}
} while (input);
}
int main()
{
test();
system("pause");
return 0;
}