text.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
menu()
{
printf("*********************************\n");
printf("************ 1.play *************\n");
printf("************ 0.exit *************\n");
printf("*********************************\n");
}
void game()
{
//11*11布置好雷的信息
char mine[ROWS][COLS] = { 0 };
//排查出雷的信息
char show[ROWS][COLS] = { 0 };
//初始化
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印数组
//PrintBoard(mine, ROW, COL);
PrintBoard(show, ROW, COL);
//布置雷
SetBoard(mine, ROW, COL);
//PrintBoard(mine, ROW, COL);
//找雷
FindBoard(mine,show, ROW, COL);
}
void text()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请输入:\n");
scanf("%d", &input);
switch (input)
{
case 0:
printf("退出\n");
break;
case 1:
game();
break;
default :
printf("输入错误,请重新输入:\n");
}
} while (input);
}
int main()
{
text();
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[ROWS][COLS], int row, int col,char set)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = set;
}
}
}
void PrintBoard(char board[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 ", board[i][j]);
}
printf("\n");
}
}
void SetBoard(char board[ROWS][COLS], int row, int col)
{
int count = Easy_count;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x - 1][y] +
mine[x + 1][y] +
mine[x - 1][y + 1] +
mine[x][y + 1] +
mine[x + 1][y + 1] - 8 * '0';
}
void FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int a = 0;
while (a<row*col- Easy_count)
{
printf("请输入坐标:\n");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("你被炸死\n");
PrintBoard(mine, row, col);
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[x][y] = count+'0';
PrintBoard(show, row, col);
a++;
}
}
else
{
printf("输入不合法,请重新输入:\n");
}
}
if (a == row * col - Easy_count)
{
printf("成功\n");
PrintBoard(mine, row, col);
}
}
game.h
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define Easy_count 10
void InitBoard(char board[ROWS][COLS],int row,int col,char set);
void PrintBoard(char board[ROWS][COLS], int row, int col);
void SetBoard(char board[ROWS][COLS], int row, int col);
void FindBoard(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);