一、test.c (测试游戏)
void menu()
{
printf("***********************\n");
printf("******* 1.play ********\n");
printf("******* 0.eixt ********\n");
printf("***********************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
InitBoard(mine,ROWS,COLS,'0');
InitBoard(show,ROWS,COLS,'*');
DisPlayBoard(show, ROW, COL);
SetBoard(mine, ROW, COL);
FindBoard(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("eixt\n");
break;
default:
printf("Re enter\n");
}
} while (input);
return 0;
}
二、game.h(游戏函数的声明)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define COUNT 7
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret);
void DisPlayBoard(char board[ROWS][COLS], int row, int col);
void SetBoard(char board[ROWS][COLS], int row, int col);
void FindBoard(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row, int col);
三、game.c(游戏函数的实现)
1.棋盘的初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = ret;
}
}
}
2.棋盘的打印
void DisPlayBoard(char board[ROWS][COLS], int row, int col)
{
printf("****** 扫雷 *******\n");
for (int i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);
for (int j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
3.布置雷
void SetBoard(char board[ROWS][COLS], int row, int col)
{
int count = COUNT;
while(count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (x > 0 && x <= row && y > 0 && y < col)
{
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
}
4.排查雷
void FindBoard(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while(1)
{
printf("请输入要排查的坐标\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine_board[x][y] == '1')
{
printf("很遗憾你被炸死了\n");
DisPlayBoard(mine_board, ROW, COL);
break;
}
else
{
Expand(mine_board, show_board, x, y);
if (Judge(show_board, ROW, COL) == 0)
{
printf("恭喜你排雷成功\n");
DisPlayBoard(show_board, ROW, COL);
break;
}
DisPlayBoard(show_board, ROW, COL);
}
}
else
{
printf("输入的坐标超出棋盘限制,请重新输入\n");
}
}
}
5.判断是否排雷成功
int Judge(char show_board[ROWS][COLS], int row, int col)
{
int count = 0;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if (show_board[i][j] == '*')
{
count++;
}
}
}
if (count == COUNT)
{
return 0;
}
else
{
return 1;
}
}
6.爆炸展开显示周围雷的数量
int GetCount(char board[ROWS][COLS], int x, int y)
{
return board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1]
+ board[x][y - 1] + board[x][y + 1]
+ board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1] - 8 * '0';
}
void Expand(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int x, int y)
{
if(x >= 1 && x <= ROW && y >= 1 && y <= COL)
{
int count = GetCount(mine_board, x, y);
if (count != 0)
{
show_board[x][y] = count + '0';
}
else
{
show_board[x][y] = ' ';
int i = 0;
for (i = x - 1; i <= x + 1; i++)
{
int j = 0;
for (j = y - 1; j <= y + 1; j++)
{
if (show_board[i][j] == '*')
{
Expand(mine_board, show_board, i, j);
}
}
}
}
}
}
四、完整代码
void menu()
{
printf("***********************\n");
printf("******* 1.play ********\n");
printf("******* 0.eixt ********\n");
printf("***********************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
InitBoard(mine,ROWS,COLS,'0');
InitBoard(show,ROWS,COLS,'*');
DisPlayBoard(show, ROW, COL);
SetBoard(mine, ROW, COL);
FindBoard(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("eixt\n");
break;
default:
printf("Re enter\n");
}
} while (input);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define COUNT 7
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret);
void DisPlayBoard(char board[ROWS][COLS], int row, int col);
void SetBoard(char board[ROWS][COLS], int row, int col);
void FindBoard(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row, int col);
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = ret;
}
}
}
void DisPlayBoard(char board[ROWS][COLS], int row, int col)
{
printf("****** 扫雷 *******\n");
for (int i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);
for (int j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetBoard(char board[ROWS][COLS], int row, int col)
{
int count = COUNT;
while(count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (x > 0 && x <= row && y > 0 && y < col)
{
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
}
int GetCount(char board[ROWS][COLS], int x, int y)
{
return board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1]
+ board[x][y - 1] + board[x][y + 1]
+ board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1] - 8 * '0';
}
void Expand(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int x, int y)
{
if(x >= 1 && x <= ROW && y >= 1 && y <= COL)
{
int count = GetCount(mine_board, x, y);
if (count != 0)
{
show_board[x][y] = count + '0';
}
else
{
show_board[x][y] = ' ';
int i = 0;
for (i = x - 1; i <= x + 1; i++)
{
int j = 0;
for (j = y - 1; j <= y + 1; j++)
{
if (show_board[i][j] == '*')
{
Expand(mine_board, show_board, i, j);
}
}
}
}
}
}
int Judge(char show_board[ROWS][COLS], int row, int col)
{
int count = 0;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if (show_board[i][j] == '*')
{
count++;
}
}
}
if (count == COUNT)
{
return 0;
}
else
{
return 1;
}
}
void FindBoard(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while(1)
{
printf("请输入要排查的坐标\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine_board[x][y] == '1')
{
printf("很遗憾你被炸死了\n");
DisPlayBoard(mine_board, ROW, COL);
break;
}
else
{
Expand(mine_board, show_board, x, y);
if (Judge(show_board, ROW, COL) == 0)
{
printf("恭喜你排雷成功\n");
DisPlayBoard(show_board, ROW, COL);
break;
}
DisPlayBoard(show_board, ROW, COL);
}
}
else
{
printf("输入的坐标超出棋盘限制,请重新输入\n");
}
}
}