实现功能要求:
1>第一次下子,不炸死。
2>坐标周围没雷,可以实现展开
游戏介绍:
可能有些人没有玩过扫雷,在这里先简单介绍一下扫雷游戏,系统会设置雷的个数并且随机放置,由玩家扫雷,没扫一次会显示周围雷的个数,一旦触碰到雷便会炸死,即游戏结束。
设计思路:
首先我们需要用二维数组来存放空位以及雷,即我们布置雷的过程;
其次我们还需要一个二维数组存放周围雷的个数,即玩家版的扫雷;
具体板块:
game.c
#1、初始化棋盘
void InitBoard(char board[ROWS][COLS], int row, int col, char ch)
{
memset(board, ch, row*col*sizeof(board[0][0]));
}
2、设置雷的位置
void SetMine(char board[ROWS][COLS], int row, int col)
{
srand((unsigned int)time(NULL));
int count = COUNT;
while (count)
{ //生成随机坐标
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';//放置雷
count--;
}
}
}
3、打印棋盘
void PrintBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf(" ");
for (i = 1; i <= row; 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");
}
}
4、计算雷的个数
int Count_Mine(char mine[ROWS][COLS], int x, int y)
{
return (mine[x - 1][y] +
mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1]) - 8 * '0';//注意最后要转化成字符形式
}
5、第一次不炸死
void FirstFind(char mine[ROWS][COLS], int x, int y)
{
int a = 0;
int b = 0;
if (mine[x][y] == '1')
{
mine[x][y] = '0';//如果第一次踩到雷,调整此雷的位置
while (1)//重新安置此雷
{
a = rand() % 9 + 1;
b = rand() % 9 + 1;
if (mine[a][b] != '1')
{
mine[a][b] = '0';
break;
}
}
}
}
5、展开无雷区域
void clearing(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
if (0 == Add(mine, x, y))
{
show[x][y] = ' ';
if ((x - 1)>0 && (y - 1)>0 && show[x - 1][y - 1] == '*')
clearing(mine, show, x - 1, y - 1);
if ((y - 1)>0 && show[x][y - 1] == '*')
clearing(mine, show, x - 1, y - 1);
if ((x + 1) <= ROW && (y - 1)>0 && show[x + 1][y - 1] == '*')
clearing(mine, show, x + 1, y - 1);
if ((x + 1) <= ROW&&show[x + 1][y] == '*')
clearing(mine, show, x + 1, y);
if ((x + 1) <= ROW && (y + 1) <= COL&&show[x + 1][y + 1] == '*')
clearing(mine, show, x + 1, y + 1);
if ((y + 1) <= COL&&show[x][y + 1] == '*')
clearing(mine, show, x, y + 1);
if ((x - 1)>0 && (y + 1) <= COL&&show[x - 1][y + 1] == '*')
clearing(mine, show, x - 1, y + 1);
if ((x - 1)>0 && show[x - 1][y] == '*')
clearing(mine, show, x - 1, y);
}
else
show[x][y] = Add(mine, x, y) + '0';
}
6、扫雷
int Sweep(char mine[ROWS][COLS], char show[ROWS][COLS])
{
int count = 0;
int x = 0;
int y = 0;
while (count != ((ROWS - 2)*(COLS - 2) - 9))
{
printf("请输入坐标:\n");
scanf("%d%d", &x, &y);
if (mine[x][y] == '1')
{
printf("你踩到雷了!\n");
return 0;
}
else
{
int ret = Add(mine, x, y);
show[x][y] = ret + '0';
clearing(mine, show, x, y);
PrintShow(show, ROWS, COLS);
count++;
}
}
printf("恭喜你赢了!\n");
PrintShow(show, ROWS, COLS);
return 0;
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("*****************************************\n");
printf("************* 1. play *****************\n");
printf("************* 0. exit *****************\n");
printf("*****************************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };//布置雷的数组
char show[ROWS][COLS] = { 0 };//排除雷的数组
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
SetMine(mine, ROW, COL);//布置雷
PrintBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
void test()
{
int num = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &num);
switch (num)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
}while (num);
}
int main()
{
char arr[ROWS][COLS] = { 0 };
test();
return 0;
}
game.h
#ifndef __GAME_H__
#define __GAME_H__
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define EASY_COUNT 80
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void SetMine(char mine[ROWS][COLS], int row, int col);
void PrintShow(char mine[ROWS][COLS], int row, int col);
void FirstFind(char mine[ROWS][COLS], int x, int y);
int Add(char mine[ROWS][COLS], int x, int y);
int Sweep(char mine[ROWS][COLS], char show[ROWS][COLS]);
#endif //__GAME_H__
演示