C语言代码实现扫雷游戏

来自:互联网
时间:2020-05-26
阅读:

本文实例为大家分享了C语言实现扫雷游戏的具体代码,供大家参考,具体内容如下

该游戏我们也是利用数组完成,设置俩个数组一个mine数组,一个show数组。

mine数组存放雷,show数组显示棋盘并且显示该位置是否有雷或者显示该位置周围有几个雷。

数组大小有讲究,我们宏定义变量 ROW COL 为9 定义ROWS COLS为11,我们显示的是9X9的棋盘,也是将雷设置在9X9的位置内,但是我们设置数组是设置11X11,因为这样方便我们遍历9X9棋盘四边位置上某位置四周雷的数目,不然的话会发生越界错误。

对于雷的符号,我们设置空位置为 0,有雷的位置为1,这样易于我们统计某一位置周围有多少雷的数目。

重点在于扫雷函数,玩家输入x y位置,我们判断该位置是否有雷,否的话判断该位置周围有多少雷并在存于show数组display给玩家。并且我们输入一个位置当该位置不是雷的时候,计数器count++;若果该计数器count==col*row-EAXY_COUNT;我们就判断玩家赢。

#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 ChushiBoard(char board[ROWS][COLS],int rows,int cols,char set);
void Dayinboard(char board[ROWS][COLS], int row, int col);
void BuzhiBoard(char mine[ROWS][COLS], int row, int col);
void CaoleiBoard(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);

void menu()
{
 printf("*****************************************n");
 printf("*****************************************n");
 printf("**** 输入1开始游戏 输入0退出游戏 *****n");
 printf("*****************************************n");
 printf("*****************************************nnn");
}

void game()
{
 char mine[ROWS][COLS];//存放雷
 char show[ROWS][COLS];//显示排查出来的雷
 //初始化
 ChushiBoard(mine, ROWS, COLS,'0');//'0'
 ChushiBoard(show, ROWS, COLS,'*');//'*'
 //布置雷
 BuzhiBoard(mine,ROW,COL);
 //Dayinboard(mine, ROW, COL);
 //打印棋盘
 Dayinboard(show, ROW, COL);
 //扫雷
 CaoleiBoard(mine,show,ROW,COL);
}



void test()
{
 int input = 0;
 do
 {
 menu();
 printf("请输入:>");
 scanf("%d", &input);
 switch (input)
 {
 case 1:
 game();
 break;
 case 0:
 printf("退出游戏nn");
 break;
 default:
 printf("输入错误,重新输入nn");
 break;
 }
 } while (input);
}


int main()
{
 test();
 system("pause");
 return 0;
}


void ChushiBoard(char board[ROWS][COLS], int rows, int cols,char set)
{
 int i = 0;
 int j = 0;
 for (i = 0; i < rows; i++)
 {
 for (j = 0; j < cols; j++)
 {
 board[i][j] = set;
 }
 }
}

void Dayinboard(char board[ROWS][COLS], int row, int col)
{
 int i = 0;
 int j = 0;
 printf("------------------------------n");
 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");
 }
 printf("------------------------------n");
}


void BuzhiBoard(char mine[ROWS][COLS], int row, int col)
{
 int count = EASY_COUNT;
 while (count)
 {
 int x = rand()%row+1;
 int y = rand()%col+1;
 if (mine[x][y] == '0')
 {
 mine[x][y] = '1';
 count--;
 }
 }
}

static int GetMineCount(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';
}



void CaoleiBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
 int x = 0;
 int y = 0;
 int win = 0;
 while (win<row*col-EASY_COUNT)
 {
 printf("请输入要排查的坐标(格式:X空格X回车):>");
 scanf("%d%d", &x, &y);
 if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
 {
 if (mine[x][y] == '1')
 {
 printf(" 很遗憾,你被炸死了!!!n");
 Dayinboard(mine, row, col);
 break;
 }
 else
 {
 int count = GetMineCount(mine,x,y);
 show[x][y] = count+'0';
 Dayinboard(show, row, col);
 win++;
 }

 }
 else
 {
 printf("输入的坐标非法n");
 }
 }
 if (win == row*col - EASY_COUNT)
 {
 printf(" 恭喜你,你排雷成功了!!!n");
 }
}

运行结果:

C语言代码实现扫雷游戏

C语言代码实现扫雷游戏

C语言代码实现扫雷游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

返回顶部
顶部