game.c
#define _CRT_SECURE_NO_WARNINGS 1#include"game.h"//初始化void init_board(char mine[ROWS][COLS], int rows, int cols, charset)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
mine[i][j] = set;
}
}
}
//打印棋盘void print_board(char mine[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", mine[i][j]);
}
printf("\n");
}
printf("\n");
}
//置雷void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
//移雷void move_mine(char mine[ROWS][COLS], int row, int col)
{
while (1)
{
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
break;
}
}
}
// 统计周围雷的个数int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
int num = 0;
num = (mine[x - 1][y - 1] - '0') + (mine[x - 1][y] - '0')
+ (mine[x - 1][y + 1] - '0') + (mine[x][y + 1] - '0')
+ (mine[x + 1][y + 1] - '0') + (mine[x + 1][y] - '0')
+ (mine[x + 1][y - 1] - '0') + (mine[x][y - 1] - '0');
return num;
}
//展开void reveal(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int flag = get_mine_count(mine, x, y);
if (flag == 0)
{
show[x][y] = '0';
if (x - 1 > 0 && y - 1 > 0 && show[x - 1][y - 1] == '*')
reveal(mine, show, x - 1, y - 1, ROWS, COLS);
if (x - 1 > 0 && y > 0 && show[x - 1][y] == '*')
reveal(mine, show, x - 1, y, ROWS, COLS);
if (x - 1 > 0 && y + 1 <= COL && show[x - 1][y + 1] == '*')
reveal(mine, show, x - 1, y + 1, ROWS, COLS);
if (x > 0 && y - 1 > 0 && show[x][y - 1] == '*')
reveal(mine, show, x, y - 1, ROWS, COLS);
if (x > 0 && y + 1 <= COL && show[x][y + 1] == '*')
reveal(mine, show, x, y + 1, ROWS, COLS);
if (x + 1 <= ROW && y - 1 > 0 && show[x + 1][y - 1] == '*')
reveal(mine, show, x + 1, y - 1, ROWS, COLS);
if (x + 1 <= ROW && y > 0 && show[x + 1][y] == '*')
reveal(mine, show, x + 1, y, ROWS, COLS);
if (x + 1 > 0 && y + 1 > 0 && show[x + 1][y + 1] == '*')
reveal(mine, show, x + 1, y + 1, ROWS, COLS);
}
else
show[x][y] = get_mine_count(mine, x, y) + '0';
}
//排雷void clearn_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int num)
{
int x = 0;
int y = 0;
int win = 0;
int count = 0;
while (win < COL*ROW-EASY_COUNT)
{
printf("请输入坐标:>");
scanf("%d,%d", &x,&y);
count++;
if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
{
if (count == 1 && mine[x][y] == '1')
{
move_mine(mine, row, col);//移雷
print_board(mine, ROW, COL);
}
if (mine[x][y] == '1' && count != 1)
{
printf("很遗憾,被炸死\n");
print_board(mine, ROW, COL);
break;
}
else
{
show[x][y] = get_mine_count(mine, x, y) + '0';
reveal(mine, show, x, y);
print_board(show, row, col);
win++;
}
}
else
{
printf("坐标有误, 请重新输入\n");
}
}
if (win == ROW * COL - EASY_COUNT)
{
printf("排雷成功\n");
print_board(mine, row, col);
}
}
game.h
#define _CRT_SECURE_NO_WARNINGS 1#ifndef __GAME_H__#define __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 10void init_board(char mine[ROWS][COLS], int rows, int cols, charset);
void print_board(char mine[ROWS][COLS], int row, int col);
void set_mine(char mine[ROWS][COLS], int row, int col);
void clearn_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int get_mine_count(char mine[ROWS][COLS], int x, int y);
void move_mine(char mine[ROWS][COLS], int row, int col);
void reveal(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
#endif//__GAME_H__