game.h
#pragma once
#include<stdio.h>
#include<time.h>
#include<windows.h>
void initMineBoard(char arr[][9],int col,char set);//初始化二维棋盘
void showBoard(char brr[][9],int col);//显示棋盘 (掩盖的棋盘)
void setMine(char arr[][9],char set);//放置雷 使用随机数产生雷
int getMineNum(char arr[][9], int x, int y);//获取一个坐标周围雷的个数
void getMineAround(char arr[][9]);//获取所有坐标周围雷的个数
void openBoard(char putarr[][9],char arr[][9],int x,int y,char set);//打开棋盘
int isWin(char arr[][9],int x,int y);//判断是否获胜
void firstNotMine(char arr[][9],int x,int y,char set);
int setOneMine(char arr[][9],char set);
void play();//功能整合
game.c
#include "game.h"
int count = 0;
void initMineBoard(char arr[][9],int col,char set)
{
for (int i = 0; i < col; i++)
{
for (int j = 0; j < 9; j++)
{
arr[i][j] = set;
}
}
}
void showBoard(char arr[][9], int col)
{
//显示第一行
for (int i = 0; i < col+1; i++)
{
printf("%d ",i);
}
printf("\n");
for (int i = 0; i < col; i++)
{
printf("%d ", i + 1);
for (int j = 0; j < 9; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
//放置雷
void setMine(char arr[][9],char set)
{
int cnt = 12;
while (cnt > 0)
{
if (!setOneMine(arr,set))
{
cnt--;
}
}
}
//放置以个雷
int setOneMine(char arr[][9],char set)
{
int x = rand() % 9;
int y = rand() % 9;
if (arr[x][y] == set)
{
arr[x][y] = '*';
return 0;
}
else
{
return 1;
}
}
//获取一个坐标周围雷的个数
int getMineNum(char arr[][9], int x, int y)
{
int cnt = 0;
if (arr[x][y] == ' ')
{
//左
if (arr[x - 1][y] == '*' && (x - 1) >= 0)
{
cnt++;
}
//左上
if (arr[x - 1][y-1] == '*' && (x - 1) >=0&&(y-1)>=0)
{
cnt++;
}
//上
if (arr[x][y-1] == '*' && (y - 1) >= 0)
{
cnt++;
}
//右上
if (arr[x + 1][y-1] == '*' && (x + 1) <9 &&(y-1)>=0)
{
cnt++;
}
//右
if (arr[x + 1][y] == '*' && (x + 1) <9)
{
cnt++;
}
//右下
if (arr[x + 1][y+1] == '*' && (x + 1) <9&&(y+1)<9)
{
cnt++;
}
//下
if (arr[x][y+1] == '*' && (y + 1) <9 )
{
cnt++;
}
//左下
if (arr[x - 1][y+1] == '*' && (x - 1) >= 0&&(y+1)<9)
{
cnt++;
}
}
return cnt;
}
void getMineAround(char arr[][9])
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (arr[i][j] == ' ')
{
arr[i][j] = getMineNum(arr, i, j) + '0';
}
}
}
}
//当前棋盘为0时继续展开 不是的话不展开
void openBoard(char putarr[][9], char arr[][9], int x, int y,char set)
{
//踩到雷退出
if (arr[x][y] == set && x >= 0 && y >= 0 && putarr[x][y] == '#')
{
putarr[x][y] = arr[x][y];
return;
}
if (arr[x][y] != set && x >= 0 && y >= 0 && putarr[x][y] == '#')
{
putarr[x][y] = arr[x][y];
count++;
}
//左
if (arr[x - 1][y] != set && (x - 1) >= 0&&putarr[x-1][y]=='#')
{
putarr[x-1][y] = arr[x-1][y];
count++;
if (arr[x - 1][y] == '0')
{
openBoard(putarr, arr, x - 1, y, set);
}
}
//左上
if (arr[x - 1][y - 1] !=set && (x - 1) >= 0 && (y - 1) >= 0 && putarr[x-1][y-1] == '#')
{
putarr[x-1][y-1] = arr[x-1][y-1];
count++;
if (arr[x - 1][y - 1] == '0')
{
openBoard(putarr, arr, x - 1, y - 1, set);
}
}
//上
if (arr[x][y - 1] != set && (y - 1) >= 0 && putarr[x][y-1] == '#')
{
putarr[x][y-1] = arr[x][y-1];
count++;
if (arr[x][y - 1] == '0')
{
openBoard(putarr, arr, x, y - 1, set);
}
}
//右上
if (arr[x + 1][y - 1] != set && (x + 1) < 9 && (y - 1) >= 0 && putarr[x+1][y-1] == '#')
{
putarr[x+1][y-1] = arr[x+1][y-1];
count++;
if (arr[x + 1][y - 1] == '0')
{
openBoard(putarr, arr, x + 1, y - 1, set);
}
}
//右
if (arr[x + 1][y] != set && (x + 1) < 9 && putarr[x+1][y] == '#')
{
putarr[x+1][y] = arr[x+1][y];
count++;
if (arr[x + 1][y] == '0')
{
openBoard(putarr, arr, x + 1, y, set);
}
}
//右下
if (arr[x + 1][y + 1] != set && (x + 1) < 9 && (y + 1) < 9 && putarr[x+1][y+1] == '#')
{
putarr[x+1][y+1] = arr[x+1][y+1];
count++;
if (arr[x + 1][y + 1] == '0')
{
openBoard(putarr, arr, x + 1, y + 1, set);
}
}
//下
if (arr[x][y + 1] != set && (y + 1) < 9 && putarr[x][y+1] == '#')
{
putarr[x][y+1] = arr[x][y+1];
count++;
if (arr[x][y + 1] == '0')
{
openBoard(putarr, arr, x, y + 1, set);
}
}
//左下
if (arr[x - 1][y + 1] != set && (x - 1) >= 0 && (y + 1) < 9 && putarr[x-1][y+1] == '#')
{
putarr[x-1][y + 1] = arr[x-1][y + 1];
count++;
if (arr[x - 1][y + 1] == '0')
{
openBoard(putarr, arr, x - 1, y + 1, set);
}
}
}
int isWin(char arr[][9],int x,int y)
{
if (arr[x][y] == '*')
{
return 1;//踩到雷返回0结束
}
if (count == 69)
{
return 1;//成功返回1
}
else
{
return 0;//不成功返回0
}
}
//第一个不是雷
void firstNotMine(char arr[][9], int x, int y,char set)
{
int flag = 1;
if (arr[x][y] == set)//如果第一个是雷的话
{
arr[x][y] = ' ';//将原来位置设置为空雷
while (flag)//设置标记
{
int xx = rand() % 9;//新的横坐标
int yy = rand() % 9;//新的纵坐标
if (arr[xx][yy] != set)
{
arr[xx][yy] = set;//将新位置设置雷
flag = 0;//退出标记
}
}
}
}
//功能整合
void play()
{
srand((unsigned)time(NULL));
int x = 0, y = 0;
char arr[9][9] = { "" };
char putarr[9][9] = { "" };
initMineBoard(putarr, 9, '#');//用#掩盖真的棋盘
initMineBoard(arr, 9, ' ');//用空格初始化真的棋盘
setMine(arr, ' ');//用空格放置雷
printf("请输入坐标:");
scanf("%d%d", &x, &y);
firstNotMine(arr, x, y, '*');//第一个不是雷操作
getMineAround(arr);//获取坐标周围雷的个数
printf("开始游戏\n");
while (1)
{
openBoard(putarr, arr, x, y, '*');
showBoard(putarr, 9);
if (isWin(arr, x, y) == 1)
{
break;
}
else
{
printf("请输入坐标:");
scanf("%d%d", &x, &y);
}
system("cls");
}
}
test.c
#include "game.h"
int main()
{
int choice = 0;
printf("************************\n");
printf("**** 0.play 1.exit ****\n");
printf("************************\n");
printf("请选择:");
scanf("%d", &choice);//选择
switch (choice)
{
case 0:play(); break;
case 1:return 0;
}
system("pause");
return 0;
}