0
点赞
收藏
分享

微信扫一扫

C语言第三篇:扫雷小游戏

源程序:
一:game.h //存放游戏功能函数的声明

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

#include <stdlib.h>

#include <time.h>


#define ROW 9

#define COL 9

#define ROWS 11

#define COLS 11

#define MINE_COUNT 80


//声明初始化棋盘函数

void initBoard(char ch[ROWS][COLS],char ret);


//声明打印棋盘函数

void displayBoard(char ch[ROWS][COLS]);


//声明布置雷函数

void setMine(char ch[ROWS][COLS]);


//声明排雷函数

void findMine(char mine[ROWS][COLS], char board[ROWS][COLS]);

二:game.c //存放游戏功能函数的实现

#include "game.h"


//初始化棋盘函数的实现

void initBoard(char ch[ROWS][COLS], char ret)

{

int i = 0;

int j = 0;

for (i=0; i < ROWS; i++)  

{

 for( j = 0; j < COLS; j++)

 {

  ch[i][j] = ret;

 }

}

}


//打印棋盘函数的实现

void displayBoard(char ch[ROWS][COLS])

{

//打印列号

for (int i = 0; i <= ROW; i++)

 printf("%d ", i);

printf("\n");

for (int i = 1; i <= ROW; i++)

{

 printf("%d ", i);//打印行号

 //打印棋盘

 for (int j = 1; j <= COL; j++)

 {

  printf("%c ", ch[i][j]);

 }

 printf("\n");

}

}


//布置雷函数的实现

void setMine(char ch[ROWS][COLS])

{

int count = MINE_COUNT; //布置雷的个数

while (count)

{

 //随机生成坐标(i,j)

 int i = rand() % ROW + 1;

 int j = rand() % COL + 1;

 //printf("%d%d", i, j);

 if (ch[i][j] == '0')

 {

  ch[i][j] = '1';

  count--;

 }

}  

}


//排雷函数的实现

void findMine(char mine[ROWS][COLS], char board[ROWS][COLS])

{

int win = 0;

int x, y = 0;

 

while (win<COL*ROW - MINE_COUNT)

{

 //输入坐标

 printf("输入排查坐标;>");

 scanf("%d%d", &x, &y); // 0 <= x,y <= 9;

 if (x >= 1 && x <= ROW && y >= 1 && y <= COL)

 {

  //判断是否为雷

  if (mine[x][y] == '1')

  {   //是雷    --结束

   system("cls");

   printf("嘭!嘭!嘭!排雷失败!\n");

   displayBoard(mine);

   break;

  }

  else//不是雷  --统计周围坐标有几个雷,存到board数组中

  {

   int count = getMineCount(mine, x, y);

   board[x][y] = count + 48;//转换成对应字符型

   displayBoard(board);

   win++;

  }

 }

 else

  printf("坐标不合法,");

}

if (win == COL * ROW - MINE_COUNT)

{

 system("cls");

 printf("恭喜你排雷成功!\n");

 displayBoard(mine);

}

system("pause");

}


//获取坐标周围雷的个数函数的实现

static int getMineCount(char ch[ROWS][COLS], int x, int y)

{

int count = 0;

for (int i = -1; i < 2; i++)

{

 for (int j = -1; j < 2; j++)

 {

  count = count + ch[x + i][y + j];

 }

}

return (count - 48 * 9);//共九个字符型相加,所以减48*9,得到对应整型

}


三:test.c //测试

#include "game.h"



void menu()

{

//屏幕清空

system("cls");

printf("*************************\n");

printf("**** 1:play   0.exit ****\n");

printf("*************************\n");

printf("请选择:>");

}


void game()

{

char mine[ROWS][COLS] = { 0 };  //存放雷的信息

char board[ROWS][COLS] = { 0 };  //存放排查的雷的信息

 

initBoard(mine,'0');//将mine初始化成 '0'

initBoard(board,'*'); //将board初始化成 '*'


//布置雷的位置

setMine(mine);

//打印雷

//displayBoard(mine);

//屏幕清空

system("cls");

printf(" -----开始排雷-----\n");

//打印棋盘

displayBoard(board);


//排雷

findMine(mine, board);


}



int main()

{

int input = 0;

srand((unsigned int)time(NULL));

do

{

 //打印菜单

 menu();

 scanf("%d", &input);

 switch (input)

 {

 case 1:

  game();

  break;

 case 0:

  printf("游戏结束\n");

  break;

 default:

  printf("出错啦!\n");

  break;

 }

 system("pause");

} while (input);

 

return 0;

}

结果:

C语言第三篇:扫雷小游戏_初始化

C语言第三篇:扫雷小游戏_初始化_02

举报

相关推荐

0 条评论