game.h
#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY 10
void init_board(char arr[ROWS][COLS], int row, int col, char set);
void display_board(char arr[ROWS][COLS], int row, int col);
void set_mine(char arr[ROWS][COLS], int row, int col);
void clear_mine(char arr[ROWS][COLS], char arr2[ROWS][COLS], int row, int col);
void extend_board(char arr[ROWS][COLS], char arr2[ROWS][COLS], int x, int y);
#endif//__GAME_H__
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init_board(char arr[ROWS][COLS], int row, int col,char set)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
arr[i][j] = set;
}
}
}
void display_board(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
printf(" ");
for (i = 1; i < 10; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i < row - 1; i++)
{
int j = 0;
printf("%d ", i);
for (j = 1; j < col - 1; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
void set_mine(char arr[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int i = 0;
int tmp = EASY;
while (tmp)
{
x = (rand() % 9) + 1;
y = (rand() % 9) + 1;
if (arr[x][y] != '1')
{
arr[x][y] = '1';
tmp--;
}
}
}
void clear_mine(char arr[ROWS][COLS], char arr2[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int x1 = 0;
int y1 = 0;
char sum = 0;
int count = (row - 2)*(col - 2) - EASY;//不是雷的个数
int count2 = 0;//记录第几次输入坐标
while (count)
{
printf("请输入坐标:");
scanf("%d%d", &x, &y);
count2++;
while (count2 == 1)
{
while (arr[x][y] == '1')
{
arr[x][y] = '0';
while (1)
{
x1 = (rand() % 9) + 1;
y1 = (rand() % 9) + 1;
if (arr[x1][y1] != '1')
{
arr[x1][y1] = '1';
break;
}
}
}
//display_board(arr, ROWS, COLS);
break;
}
if (arr[x][y] == '1')
{
printf("很遗憾,你被炸死了!\n");
break;
}
else
{
//统计旁边有几个雷
sum = ((arr[x - 1][y] + arr[x - 1][y - 1] + arr[x][y - 1] + \
+ arr[x + 1][y - 1] + arr[x + 1][y] + arr[x + 1][y + 1] + \
+ arr[x][y + 1] + arr[x - 1][y + 1]) - 8 * '0')+'0';
arr2[x][y] = sum;
//display_board(arr2, ROWS, COLS);
count--;
extend_board(arr,arr2, x, y);//展开
}
}
if (count == 0)
printf("恭喜你,排雷成功!");
}
void extend_board(char arr[ROWS][COLS], char arr2[ROWS][COLS], int x, int y)
{
int sum = 0;
sum = (arr[x - 1][y] + arr[x - 1][y - 1] + arr[x][y - 1] + \
+ arr[x + 1][y - 1] + arr[x + 1][y] + arr[x + 1][y + 1] + \
+ arr[x][y + 1] + arr[x - 1][y + 1]) - 8 * '0';
if (sum == 0)
{
if (arr2[x - 1][y] == '*')
arr2[x - 1][y] = ' ';
if (arr2[x - 1][y - 1] == '*')
arr2[x - 1][y - 1] = ' ';
if (arr2[x][y - 1] == '*')
arr2[x][y - 1] = ' ';
if (arr2[x + 1][y - 1] == '*')
arr2[x + 1][y - 1] = ' ';
if (arr2[x + 1][y] == '*')
arr2[x + 1][y] = ' ';
if (arr2[x + 1][y + 1] == '*')
arr2[x + 1][y + 1] = ' ';
if (arr2[x][y + 1] == '*')
arr2[x][y + 1] = ' ';
if (arr2[x - 1][y + 1] == '*')
arr2[x - 1][y + 1] = ' ';
}
display_board(arr2, ROWS, COLS);
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
init_board(mine, ROWS, COLS,'0');
init_board(show, ROWS, COLS, '*');
//display_board(mine, ROWS, COLS);
//display_board(show, ROWS, COLS);
srand((unsigned int)time(NULL));
set_mine(mine, ROWS, COLS);
display_board(mine, ROWS, COLS);
clear_mine(mine, show, ROWS, COLS);
}
void menu()
{
printf("************************\n");
printf("***1.play 0.exit***\n");
printf("************************\n");
}
int main()
{
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 0:
printf("退出游戏\n");
break;
case 1:
//printf("开始游戏\n");
game();
break;
default:
break;
}
} while (input);
system("pause");
return 0;
}