#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define max_row 9
#define max_col 9
//菜单
int menu(){
int choice;
printf("**************************\n");
printf(" 1:开始游戏\n");
printf(" 0:表示并不想玩\n");
printf("***********************\n");
scanf("%d", &choice);
return choice;
}
// 玩家地图
void init(char show_map[max_row][max_col], char mine_map[max_row][max_col], int row, int col){
for (row = 0; row < max_row; ++row){
for (col = 0; col < max_col; ++col){
show_map[row][col] = '*';
}
}
printf("----------------------------\n");
for (row = 0; row < max_row; ++row){
for (col = 0; col < max_col; ++col){
printf("| %c", show_map[row][col]);
}
printf("\n");
printf("----------------------------\n");
}
}
//地雷地图 mine_map 1表示地雷
void minea_map(char show_map[max_row][max_col], char mine_map[max_row][max_col], int row, int col){
int i = 0;
int j = 0;
for (row = 0; row < max_row; ++row){
for (col = 0; col < max_col; ++col){
mine_map[row][col] = '*';
}
}
while (i<10){
row = rand() % 9;
col = rand() % 9;
if (mine_map[row][col] == '*'){
mine_map[row][col] = '1';
i++;
}
}
//可选择是否打印地雷地图
int mine;
printf("是否显示地雷地图\n");
printf("1:显示! 0:不显示\n");
scanf("%d", &mine);
if (mine == 1){
printf("----------------------------\n");
for (row = 0; row < max_row; ++row){
for (col = 0; col < max_col; ++col){
printf("| %c", mine_map[row][col]);
}
printf("\n");
printf("----------------------------\n");
}
}
}
//判断输入坐标处周围无雷时,自动显示空白(两处判断出采用递归)
int number(char show_map[max_row][max_col], char mine_map[max_row][max_col], int row, int col){
if (mine_map[row - 1][col - 1] == '*' && (row - 1) >= 0 && (col - 1) >= 0){
playermove(show_map, mine_map, row - 1, col - 1);
while (show_map[row - 1][col - 1] == '0'){
show_map[row - 1][col - 1] = '0';
break;
}
}
if (mine_map[row - 1][col] == '*' && (row - 1) >= 0){
playermove(show_map, mine_map, row - 1, col);
if (show_map[row - 1][col] == '0'){
show_map[row - 1][col] = '0';
return number(show_map, mine_map, row-1, col);
}
}
if (mine_map[row - 1][col + 1] == '*' && (row - 1) >= 0 && (col + 1) < (max_col)){
playermove(show_map, mine_map, row - 1, col +1);
while (show_map[row - 1][col + 1] == '0'){
show_map[row - 1][col + 1] = '0';
break;
}
}
if (mine_map[row][col - 1] == '*' && (col - 1) >= 0){
playermove(show_map, mine_map, row , col - 1);
while (show_map[row][col - 1] == '0'){
show_map[row][col - 1] = '0';
break;
}
}
if (mine_map[row][col + 1] == '*' && (col + 1) <(max_col)){
playermove(show_map, mine_map, row , col + 1);
if (show_map[row][col + 1] == '0'){
show_map[row][col + 1] = '0';
return number(show_map, mine_map, row , col+1);
}
}
if (mine_map[row + 1][col - 1] == '*' && (row + 1) < (max_col) && (col - 1) >= 0){
playermove(show_map, mine_map, row +1, col - 1);
while (show_map[row + 1][col - 1] == '0'){
show_map[row + 1][col - 1] = '0';
break;
}
}
if (mine_map[row + 1][col] == '*' && (row + 1) <(max_col)){
playermove(show_map, mine_map, row +1, col );
while (show_map[row + 1][col] == '0'){
show_map[row + 1][col] = '0';
break;
}
}
if (mine_map[row + 1][col + 1] == '*' && (row + 1) <(max_col) && (col+1) <(max_col)){
playermove(show_map, mine_map, row +1, col + 1);
while (show_map[row + 1][col + 1] == '0'){
show_map[row + 1][col + 1] = '0';
break;
}
}
}
int playermove(char show_map[max_row][max_col], char mine_map[max_row][max_col], int row, int col){
if (mine_map[row][col] == '*'){// 不是,先判断是否找出所有的地雷
//给当前位置生成一个数字有几个雷
int n = 0;
if (mine_map[row - 1][col - 1] == '1' && (row - 1) >= 0 && (col - 1) >= 0){
n++;
}
if (mine_map[row - 1][col] == '1' && (row - 1) >= 0){
n++;
}
if (mine_map[row - 1][col + 1] == '1' && (row - 1) >= 0 && (col + 1) < (max_col)){
n++;
}
if (mine_map[row][col - 1] == '1' && (col - 1) >= 0){
n++;
}
if (mine_map[row][col + 1] == '1' && (col + 1) < (max_col)){
n++;
}
if (mine_map[row + 1][col - 1] == '1' && (row + 1) < (max_col) && (col - 1) >= 0){
n++;
}
if (mine_map[row + 1][col] == '1' && (row + 1) < (max_col)){
n++;
}
if (mine_map[row + 1][col + 1] == '1' && (row + 1) < (max_col) && (col+1) < (max_col)){
n++;
}
show_map[row][col] = '0' + n;
}
}
void game(char show_map[max_row][max_col], char mine_map[max_row][max_col], int row, int col){
init(show_map, mine_map, row, col);
minea_map(show_map, mine_map, row, col);
int count = 0;
while (1){
printf("请输入扫雷坐标!\n");
scanf("%d %d", &row, &col);
//判断输入坐标的合法性
if (row<0 || row>8 || col<0 || col>8){
continue;
}
//判断是否为地雷
if (mine_map[row][col] == '1'){
system("cls");
printf("踩雷了,真菜!\n");
break;
}
system("cls");
playermove(show_map, mine_map, row, col);
if (show_map[row][col] == '0'){//该处周围无雷时,进一步判断周围空白
number(show_map, mine_map, row, col);
printf("----------------------------\n");
for (row = 0; row < max_row; ++row){
for (col = 0; col < max_col; ++col){
printf("| %c", show_map[row][col]);
}
printf("\n");
printf("----------------------------\n");
}
}
else{
printf("----------------------------\n");
for (row = 0; row < max_row; ++row){
for (col = 0; col < max_col; ++col){
printf("| %c", show_map[row][col]);
}
printf("\n");
printf("----------------------------\n");
}
}
for (row = 0; row < max_row; ++row){
for (col = 0; col < max_col; ++col){
if (show_map[row][col] == '*'){
count++;
}
}
}
if (count == 10){
printf("猜出了所有的雷,perfect!\n");
break;
}
}
}
int main(){
int row = 0;
int col = 0;
char show_map[max_row][max_col];
char mine_map[max_row][max_col];
srand((unsigned int)time(0));
while (1){
int choice = menu();
if (choice == 1){
game(show_map, mine_map, row, col);
break;
}
else if (choice == 0){
break;
}
else
printf("请重新选择!\n");
continue;
}
system("pause");
return 0;
}