#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
#define ROW 10
#define COL 10
#define mine 10
int mune(){//菜单打印
printf("************\n");
printf(“1.开始游戏\n”);
printf(“2.结束游戏\n”);
printf("************\n");
int choice = 0;
scanf("%d",&choice);
return choice;
}
void init(char a[ROW][COL], char b[ROW][COL]){//初始化地图
for (int i = 0; i < ROW; ++i){//初始化a地图
for (int j = 0; j < COL; ++j){
a[i][j] = ‘*’;
}
}
for (int i = 0; i < ROW; ++i){//初始化b地图
for (int j = 0; j < COL; ++j){
b[i][j] = ‘0’;
}
}
//可插入随机种子
for (int k = 0; k < mine; ++k){//产生 mine 个地雷
int num = rand() % 100;
int num_1;
num_1 = num % 10;
num = num / 10;
if (b[num_1][num] == ‘B’){
continue;
}
b[num_1][num] = ‘B’;
}
}
void print(char a[ROW][COL]){
printf(" |");
for (int i = 0; i < COL; ++i){
printf("%d “,i);
}
printf(”\n");
for (int i = 0; i < COL - 2; ++i){
printf("—");
}
printf("\n");
for (int i = 0; i < ROW; ++i){
printf(" %d|",i);
for (int j = 0; j < COL; ++j){
printf("%c ",a[i][j]);
}
printf("\n");
}
}
void hint_mine_num(int row, int col ,char b[ROW][COL],char a[ROW][COL]){
int count = 0;
if (row + 1 < ROW && col - 1 > 0 && b[row + 1][col - 1] == ‘B’){
++count;
}
if (row + 1 < ROW && b[row + 1][col ] == ‘B’){
++count;
}
if (row + 1 < ROW && col + 1 < COL && b[row + 1][col + 1] == ‘B’){
++count;
}
if ( col - 1 > 0 && b[row ][col - 1] == ‘B’){
++count;
}
if ( col + 1 < COL && b[row ][col + 1] == ‘B’){
++count;
}
if (row - 1 < 0 && col - 1 > 0 && b[row - 1][col - 1] == ‘B’){
++count;
}
if (row - 1 < 0 && b[row - 1][col] == ‘B’){
++count;
}
if (row - 1 < 0 && col + 1 < COL && b[row - 1][col + 1] == ‘B’){
++count;
}
a[row][col] = count + ‘0’;
}
void game(){//执行游戏 循环
char a[ROW][COL];
char b[ROW][COL];
init(a,b);
int sign = 0;
while (1){
system("cls");
print(a);
//打印地图
printf("请输入想要落子的坐标(row col):");
int row = 0;
int col = 0;
scanf("%d %d", &row, &col);
if (row < 0 || row > ROW || col < 0 || col > COL){
printf("输入有误\n");
printf("你新打开的坐标:(%d %d)\n", row, col);
Sleep(2000);
continue;
}
if (a[row][col] != '*'){
printf("该位已被翻开\n");
printf("你新打开的坐标:(%d %d)\n", row, col);
Sleep(2000);
continue;
}
//玩家翻开地图,判断打开点的有效性
//判断胜负
hint_mine_num(row, col, b, a);
if (b[row][col] == 'B'){
a[row][col] = 'B';
system("cls");
print(a);
printf("你新打开的坐标:(%d %d)\n", row, col);
printf("踩雷了!\n");
return;
}
++sign;
//1.踩雷 over
if (sign == ROW*COL - mine){
printf("成功!\n");
return;
}
//2.没踩雷判断是不是胜利
//3.没踩雷也没胜利 告诉玩家当前位置有几个雷
}
}
int main(){
srand((unsigned int)time(0));
while (1){
int choice = mune();
if (choice == 1){
game();
}
else if (choice == 2){
break;
}
else{
printf(“你的输入有误\n”);
}
}
system("pause");
return 0;
}