扫雷游戏
game.h
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#define DIF_ROW 21
#define DIF_COL 21
#define DIF_COUNT 40
#define DIF_ROWS DIF_ROW+2
#define DIF_COLS DIF_COL+2
#define EASY_COUNT 10
//初始化棋盘
extern void InitBoard(char board[DIF_ROWS][DIF_COLS], int rows, int cols, char ch);
//打印棋盘
extern void DisPlayBoard(char board[DIF_ROWS][DIF_COLS], int row, int col);
//存放雷
extern void SetMine(char mine[DIF_ROWS][DIF_COLS], int row, int col, int cnt);
//排查雷
extern void FindMine(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int row, int col, int cnt, int visted[DIF_ROWS][DIF_COLS]);
extern int MineCount(char mine[DIF_ROWS][DIF_COLS], int x, int y);
extern void Expend(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int x, int y, int visted[DIF_ROWS][DIF_COLS]);
game.c
#include "game.h"
void InitBoard(char board[DIF_ROWS][DIF_COLS], int rows, int cols, char ch)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = ch;
}
}
}
void DisPlayBoard(char board[DIF_ROWS][DIF_COLS], int row, int col)
{
int i = 0;
printf(" ");
for (i = 1; i <= row; i++)
{
printf("%2d ", i);
}
printf("\n");
printf(" |");
for (i = 1; i <= row; i++)
{
printf("---");
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%2d|", i);
int j = 0;
for (j = 1; j <= col; j++)
{
printf(" %c ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char mine[DIF_ROWS][DIF_COLS], int row, int col, int cnt)
{
while (cnt > 0)
{
int x = 0;
int y = 0;
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
cnt--;
}
}
}
int MineCount(char mine[DIF_ROWS][DIF_COLS], int x, int y)
{
int cnt = mine[x - 1][y - 1] +
mine[x - 1][y] +
mine[x - 1][y + 1] +
mine[x][y - 1] +
mine[x][y + 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] - 8 * '0';
return cnt;
}
void Expend(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int x, int y, int visted[DIF_ROWS][DIF_COLS])
{
//visted[x][y] == 1;
int vist[DIF_ROWS][DIF_COLS] = { 0 };
int cnt = MineCount(mine, x, y);
if (cnt > 0)
show[x][y] = cnt + '0';
else
{
show[x][y] = ' ';
if (visted[x - 1][y - 1] == 0)
{
vist[x - 1][y - 1] = 1;
visted[x - 1][y - 1] = 1;
}
if (visted[x - 1][y] == 0)
{
vist[x - 1][y] = 1;
visted[x - 1][y] = 1;
}
if (visted[x - 1][y + 1] == 0)
{
vist[x - 1][y + 1] = 1;
visted[x - 1][y + 1] = 1;
}
if (visted[x][y - 1] == 0)
{
vist[x][y - 1] = 1;
visted[x][y - 1] = 1;
}
if (visted[x][y + 1] == 0)
{
vist[x][y + 1] = 1;
visted[x][y + 1] = 1;
}
if (visted[x + 1][y - 1] == 0)
{
vist[x + 1][y - 1] = 1;
visted[x + 1][y - 1] = 1;
}
if (visted[x + 1][y] == 0)
{
vist[x + 1][y] = 1;
visted[x + 1][y] = 1;
}
if (visted[x + 1][y + 1] == 0)
{
vist[x + 1][y + 1] = 1;
visted[x + 1][y + 1] = 1;
}
if (vist[x - 1][y - 1] == 1)
{
Expend(mine, show, x - 1, y - 1, visted);
}
if (vist[x - 1][y] == 1)
{
Expend(mine, show, x - 1, y, visted);
}
if (vist[x - 1][y + 1] == 1)
{
Expend(mine, show, x - 1, y + 1, visted);
}
if (vist[x][y - 1] == 1)
{
Expend(mine, show, x, y - 1, visted);
}
if (vist[x][y + 1] == 1)
{
Expend(mine, show, x, y + 1, visted);
}
if (vist[x + 1][y - 1] == 1)
{
Expend(mine, show, x + 1, y - 1, visted);
}
if (vist[x + 1][y] == 1)
{
Expend(mine, show, x + 1, y, visted);
}
if (vist[x + 1][y + 1] == 1)
{
Expend(mine, show, x + 1, y + 1, visted);
}
}
}
void FindMine(char mine[DIF_ROWS][DIF_COLS], char show[DIF_ROWS][DIF_COLS], int row, int col, int cnt, int visted[DIF_ROWS][DIF_COLS])
{
while (1)
{
int x = 0;
int y = 0;
printf("请输入:>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (1 == visted[x][y])
{
printf("该位子已被占用\n");
}
else if (mine[x][y] == '1')
{
show[x][y] = '$';
DisPlayBoard(show, row, col);
printf("你踩到雷了\n");
break;
}
else
{
visted[x][y] == 1;
Expend(mine, show, x, y, visted);
DisPlayBoard(show, row, col);
}
}
else
{
printf("超出范围\n");
}
int count = 0;
int i = 0;
for (i = 1; i <= row; i++)
{
int j = 0;
for (j = 1; j <= col; j++)
{
if (show[i][j] == '*')
{
count++;
}
}
}
if (count == cnt)
{
printf("恭喜你,你赢了\n");
break;
}
}
}
text.c
#include "game.h"
int ROW = 0;
int COL = 0;
int ROWS = 0;
int COLS = 0;
int count = 0;
void menu()
{
printf("********************************\n");
printf("******** 1. play *********\n");
printf("******** 0. exit *********\n");
printf("********************************\n");
}
void game()
{
char mine[DIF_ROWS][DIF_COLS];
char show[DIF_ROWS][DIF_COLS];
int visted[DIF_ROWS][DIF_COLS];
int i = 0;
for (i = 0; i < ROWS; i++)
{
int j = 0;
for (j = 0; j < COLS; j++)
{
if (i == 0)
visted[i][j] = 1;
else if (j == 0)
visted[i][j] = 1;
else if (i == ROWS - 1)
visted[i][j] = 1;
else if (j == COLS - 1)
visted[i][j] = 1;
else
visted[i][j] = 0;
}
}
//初始化棋盘
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印棋盘
DisPlayBoard(show, ROW, COL);
//存放雷
SetMine(mine, ROW, COL, count);
//排查雷
FindMine(mine, show, ROW, COL, count, visted);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
ROW = DIF_ROW;
COL = DIF_COL;
ROWS = DIF_ROWS;
COLS = DIF_COLS;
count = DIF_COUNT;
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
扫雷游戏的实现: