扫雷
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_R 9
#define MAX_C 9
#define MINE_COUNT 10
//void Init(char show_map[MAX_R][MAX_C],char mine_map[MAX_R][MAX_C])
//{
// for (int row = 0; row < MAX_R; row++)
//
// for (int col = 0; col < MAX_C; col++)
// {
// show_map[row][col] = ' * ';
// }
//
// for (int row = 0; row < MAX_R; row++)
// {
// for (int col = 0; col < MAX_C; col++)
// {
// mine_map[row][col] = ' 0 ';
// }
// }
//在mine_map中随机找十个雷
/*int mine_count = MINE_COUNT;
while (mine_count>0)
{
int row = rand() % MAX_R;
int col = rand() % MAX_C;
if (mine_map[row][col] == ' 1 ')
{
continue;
}
mine_map[row][col] = ' 1 ';
--mine_count;
}
}*/
void PrintMap(char map[MAX_R][MAX_C])
{
printf(" |");
for (int col = 0; col < MAX_C; col++){
printf("%d ", col);
}
printf("\n");
for (int col = 0; col < MAX_C+1; col++){
printf("---");
}
printf("\n");
for (int row = 0; row < MAX_R; row++)
{
printf(" %d|", row);
for (int col = 0; col < MAX_R; col++){
printf("%c ", map[row][col]);
}
printf("\n");
}
}
void updateShowMap(int row, int col,
char mine_map[MAX_R][MAX_C],
char show_map[MAX_R][MAX_C])
{
int count = 0;
if (row - 1 >= 0 && col - 1 >= 0
&& mine_map[row - 1][col] == '1')
{
++count;
}
if (row - 1 >= 0
&& mine_map[row - 1][col] == '1')
{
++count;
}
if (row - 1 >= 0 && col + 1 < MAX_C
&& mine_map[row - 1][col + 1] == '1')
{
++count;
}
if (col - 1 >= 0
&& mine_map[row][col - 1] == '1')
{
++count;
}
if (col + 1 < MAX_C
&& mine_map[row][col + 1] == '1')
{
++count;
}
if (row + 1 < MAX_R && col - 1 >= 0
&& mine_map[row + 1][col - 1] == '1')
{
++count;
}
if (row + 1 < MAX_R
&& mine_map[row + 1][col + 1] == '1')
{
++count;
}
if (row+1<MAX_R &&col+1<MAX_C
&& mine_map[row + 1][col + 1] == '1')
{
++count;
}
}
//Game 一局游戏
void Game()
{
char show_map[MAX_R][MAX_C];
char mine_map[MAX_R][MAX_C];
//初始化
// Init(show_map, mine_map);
int blank_count = 0;
while (1)
{
//打印地图
PrintMap(show_map);
//提示输入,并检验
printf("enter the position :(row,col) ");
int row = 0;
int col = 0;
scanf("%d,%d", &row, &col);
if (row < 0 || row >= MAX_R || col < 0 || col >= MAX_R)
{
printf("input errow!!\n");
continue;
}
if (show_map[row][col] != ' * ')
{
printf("repeat input!!\n");
continue;
}
//判定是否踩雷
if (mine_map[row][col] == '1')
{
printf("sorry,you lose!!\n");
return;
}
++blank_count;
//判定是否胜利
if (blank_count ==MAX_R*MAX_C-MINE_COUNT)
{
printf("congratulation you made it!!\n");
return;
}
//告诉玩家当前位置有几个雷
updateShowMap(row,col,mine_map,show_map);
}
}
int Menu()
{
printf("======================\n");
printf(" 1.start game \n");
printf(" 0.game over \n");
printf("======================\n");
printf("enter your choice : ");
int choice = 0;
scanf("%d", &choice);
return choice;
}
int main()
{
srand((unsigned int)time(0));
while (1)
{
int choice = Menu();
if (choice == 1)
{
Game();
}
else if (choice == 0)
{
printf("good bye!\n");
break;
}
else
{
printf("您的输入有误! \n");
}
}
system("pause");
}