扫雷游戏:
1.初始化游戏界面,利用rand()随机布雷。
2.打印游戏界面。
3.扫雷中输入坐标周围雷的个数,界面为99,但要遍历周围的八个坐标,因此设置一个界面为1111,方便之后的计算。
game.h
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define MINE_COUNT 10
void init(char Show_Map[ROWS][COLS], char Mine_Map[ROWS][COLS]);
void Print(char Show_Map[ROWS][COLS]);
void Print(char Mine_Map[ROWS][COLS]);
void Find_Mine(char Mine_Map[ROWS][COLS], char Show_Map[ROWS][COLS],int row, int col);
test.c
#include "game.h"
void menu()
{
printf("**********************\n");
printf("******* 1.PLAY *******\n");
printf("******* 0.EXIT *******\n");
printf("**********************\n");
}
void game()
{
char Mine_Map[ROWS][COLS] = { 0 };
char Show_Map[ROWS][COLS] = { 0 };
init(Show_Map, Mine_Map);//初始化游戏界面,并且布雷
Print(Show_Map);//打印用户游戏界面
//Print(Mine_Map);//打印显示布雷界面
Find_Mine(Mine_Map, Show_Map, ROW, COL);//扫雷
}
void test()
{
int input;
srand((unsigned)time(NULL));
do
{
menu();
printf("请输入您的选择\n");
scanf_s("%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;
}
game.c
#include "game.h"
void init(char Show_Map[ROWS][COLS], char Mine_Map[ROWS][COLS])
{
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
Show_Map[row][col] = '*';
}
}
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
Mine_Map[row][col] = '0';
}
}
int mine_count = MINE_COUNT;
while (mine_count>0)//布雷
{
int row = rand() % ROW+1;
int col = rand() % COL+1;
if (Mine_Map[row][col] == '1')
{
continue;
}
Mine_Map[row][col] = '1';
--mine_count;
}
}
void Print(char SHOW_map[ROWS][COLS])
{
printf(" ");
for (int col = 1; col <= COL; col++)
{
printf("%d ", col);
}
printf("\n ");
for (int col = 0; col <= COL; col++)
{
printf("--");
}
printf("\n");
for (int row = 1; row <= ROW; row++)
{
printf(" %d|", row);
for (int col = 1; col <= COL; col++)
{
printf("%c ", SHOW_map[row][col]);
}
printf("\n");
}
}
int GetMineCount(char Mine_Map[ROWS][COLS], int x, int y)//统计雷的个数。字符1与数字1之间相差一个字符零
{
return Mine_Map[x - 1][y - 1] +
Mine_Map[x - 1][y] +
Mine_Map[x - 1][y + 1] +
Mine_Map[x][y - 1] +
Mine_Map[x][y + 1] +
Mine_Map[x + 1][y - 1] +
Mine_Map[x + 1][y] +
Mine_Map[x + 1][y + 1] - 8 * '0';
}
void Find_Mine(char Mine_Map[ROWS][COLS], char Show_Map[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win<row*col-MINE_COUNT)
{
printf("请输入你想选择的坐标:\n");
scanf_s("%d %d", &x, &y);
if (x >= 1 && x <= row&&y >= 1 && y <= col)
{
if (Mine_Map[x][y] == '1')
{
printf("很遗憾,你被炸死了!\n");
Print(Mine_Map);
break;
}
else
{
int count = GetMineCount(Mine_Map, x, y);
Show_Map[x][y] = count + '0';
//要掀开的区域
Show_Map[x - 1][y - 1] = '0' + GetMineCount(Mine_Map, x - 1, y - 1);
Show_Map[x - 1][y] = '0' + GetMineCount(Mine_Map, x - 1, y);
Show_Map[x - 1][y + 1] = '0' + GetMineCount(Mine_Map, x - 1, y + 1);
Show_Map[x][y - 1] = '0' + GetMineCount(Mine_Map, x, y - 1);
Show_Map[x][y] = '0' + GetMineCount(Mine_Map, x, y);
Show_Map[x][y + 1] = '0' + GetMineCount(Mine_Map, x, y + 1);
Show_Map[x + 1][y - 1] = '0' + GetMineCount(Mine_Map, x + 1, y - 1);
Show_Map[x + 1][y] = '0' + GetMineCount(Mine_Map, x + 1, y);
Show_Map[x + 1][y + 1] = '0' + GetMineCount(Mine_Map, x + 1, y + 1);
Print(Show_Map);
}
}
else
{
printf("输入错误,请重新输入!\n");
}
}
printf("恭喜你排雷成功!\n");
}