❤️ :热爱编程学习,期待一起交流!
:博主水平有限,如有发现错误,求告知,多谢!
menu()//菜单的实现
{
printf("******* 1.ply *****\n");
printf("******* 0.exit *****\n");
}
test()
{
srand((unsigned int)time(NULL));//设置随机数的生成器
int n = 0;
do
{
menu();//调用menu函数
scanf("%d", &n);
switch (n)
{
case 1:
game();//调用game函数,游戏的大致思路都在这里面。
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("请重新选择\n");
break;
}
} while (n);
}
int main()
{
test();
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define EASY_COUNT 10//我们布置10个雷
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char mine[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
game()
{
char mine[ROWS][COLS] = {
0 };
char show[ROWS][COLS] = {
0 };
InitBoard(mine, ROWS, COLS, '0');//在game()函数里需要穿两次实参,第一次传mine数组
InitBoard(show, ROWS, COLS, '@');//第二次传show数组,但在game.c里定义的时候只需要一次。一次就可以完成两次函数调用的实现。
SetMine(mine, ROW, COL);//设置雷的时候只需要在9*9的雷区布置雷就好。所以传参传的是ROW,COL。但需要注意,接收的时候需要用11*11接收。这是因为防止在扫雷(FindMine)的时候数组越界。
DisplayBoard(show, ROW, COL);//给玩家看只需要展出9*9就行,所以传参还是传的ROW和COL,但在game.c中接收的时候需要用11*11的接收。
FindMine(mine, show, ROW, COL);//需要程序员根据第一个雷区的信息,来判断周围八个格子有几颗雷,然后呈现到第二个雷区上给玩家看。所以需要把两个雷区mine和show两个实参都传到game.c中。
}
char mine[ROWS][COLS] = {
0 };
char show[ROWS][COLS] = {
0 };
void InitBoard(char mine[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
mine[i][j] = set;
}
}
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;//这里是设置雷的个数。
while (count)
{
int x = rand() % row + 1;//这里实参传的row为9,一个数对9求余得到的0~8的数字,再加1就是1~9。
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
void DisplayBoard(char mine[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", mine[i][j]);
}
printf("\n");
}
}
根据ASCII码值表,字符2减去字符0就是数字2。eg:‘2’ - ‘0’ = 2
void SpreadMine(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
int count = get_mine_count(mine, x, y);
if (0 == count)
{
show[x][y] = ' ';
if (show[x][y + 1] == '@')
SpreadMine(mine, show, x, y+1);
if (show[x][y-1] == '@')
SpreadMine(mine, show, x, y-1);
if (show[x+1][y] == '@')
SpreadMine(mine, show, x+1, y);
if (show[x+1][y-1] == '@')
SpreadMine(mine, show, x+1, y-1);
if (show[x+1][y+1] == '@')
SpreadMine(mine, show, x+1, y+1);
if (show[x-1][y] == '@')
SpreadMine(mine, show, x-1, y);
if (show[x-1][y+1] == '@')
SpreadMine(mine, show, x-1, y+1);
if (show[x-1][y-1] == '@')
SpreadMine(mine, show, x-1, y-1);
}
else
{
show[x][y] = count + '0';
}
}
static int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
//加static的原因让其无法在另外一个源文件中访问这个自定义函数
return mine[x-1][y]+ mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] - 8 * '0';//计算周围多少个字符1,然后减去八个‘0’,就是得到的数字了。
}
int is_win(char show[ROWS][COLS], int row, int col)
{
int c = 0;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if(show[i][j]=='@')
c++;
}
}
return c;
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入要排查的坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遗憾你被炸死了\n");
DisplayBoard(mine, row, col);
}
else
{
SpreadMine(mine, show, x, y);
DisplayBoard(show, ROW, COL);
if (is_win(show, ROW, COL) == EASY_COUNT)//判断是否赢了。
{
printf("恭喜你赢了");
DisplayBoard(show, ROW, COL);
}
}
}
else
{
printf("输入坐标非法,无法排雷,请重新输入\n");
}
}
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define EASY_COUNT 10
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char mine[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
#include "game2.h"
menu()
{
printf("*******1.ply *****\n");
printf("*******0.exit *****\n");
}
game()
{
char mine[ROWS][COLS] = {
0 };
char show[ROWS][COLS] = {
0 };
InitBoard(mine, ROWS, COLS, '0');//在game()函数里需要穿两次实参,第一次传mine数组
InitBoard(show, ROWS, COLS, '@');//第二次传show数组,但在game.c里定义的时候只需要一次。一次就可以完成两次函数调用的实现。
SetMine(mine, ROW, COL);//设置雷的时候只需要在9*9的雷区布置雷就好。所以传参传的是ROW,COL。但需要注意,接收的时候需要用11*11接收。这是因为防止在扫雷(FindMine)的时候数组越界。
DisplayBoard(show, ROW, COL);//给玩家看只需要展出9*9就行,所以传参还是传的ROW,和COL,但在game.c中接收的时候需要用11*11的接收。
FindMine(mine, show, ROW, COL);//需要程序员根据第一个雷区的信息,来判断周围八个格子有几颗雷,然后呈现到第二个雷区上给玩家看。所以需要把两个雷区mine和show两个实参都传到game.c中。
}
test()
{
srand((unsigned int)time(NULL));
int n = 0;
do
{
menu();
scanf("%d", &n);
switch (n)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("请重新选择\n");
break;
}
} while (n);
}
int main()
{
test();
return 0;
}
#include "game2.h"
void InitBoard(char mine[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
mine[i][j] = set;
}
}
}
void DisplayBoard(char mine[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", mine[i][j]);
}
printf("\n");
}
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
void SpreadMine(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
int count = get_mine_count(mine, x, y);
if (0 == count)
{
show[x][y] = ' ';
if (show[x][y + 1] == '@')
SpreadMine(mine, show, x, y+1);
if (show[x][y-1] == '@')
SpreadMine(mine, show, x, y-1);
if (show[x+1][y] == '@')
SpreadMine(mine, show, x+1, y);
if (show[x+1][y-1] == '@')
SpreadMine(mine, show, x+1, y-1);
if (show[x+1][y+1] == '@')
SpreadMine(mine, show, x+1, y+1);
if (show[x-1][y] == '@')
SpreadMine(mine, show, x-1, y);
if (show[x-1][y+1] == '@')
SpreadMine(mine, show, x-1, y+1);
if (show[x-1][y-1] == '@')
SpreadMine(mine, show, x-1, y-1);
}
else
{
show[x][y] = count + '0';
}
}
static int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
return mine[x-1][y]+ mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] - 8 * '0';
}
int is_win(char show[ROWS][COLS], int row, int col)
{
int c = 0;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if(show[i][j]=='@')
c++;
}
}
return c;
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入要排查的坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("遗憾了你被炸死了\n");
DisplayBoard(mine, row, col);
break;
}
else
{
SpreadMine(mine, show, x, y);
DisplayBoard(show, ROW, COL);
if (is_win(show, ROW, COL) == EASY_COUNT)
{
printf("恭喜你赢了");
DisplayBoard(show, ROW, COL);
}
}
}
else
{
printf("输入坐标非法,无法排雷,请重新输入\n");
}
}
}