C语言扫雷函数成绩,C语言实现扫雷

实现用C语言编写一个扫雷游戏

大家想必都玩过扫雷,无论那个版本都有难度供已选择,下面来分享一个自己用C语言编写的扫雷游戏吧!

编写语言:C语言

编写软件:visual studio 2017

1.首先是将游戏的测试模块写好,要有提示玩家进入的菜单函数以及选择函数等各种需要编写的函数想出一个整体框架来

//测试模块test。c

#include "game.h"

void Menu()

{

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

printf("************************1.PLAY 0.EXIT*************************\n");

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

}

int Game()

{

int x = 0;

int y = 0;

int i = 0;

int j = 0;

int ret = 0;

int num = count;

char real[ROWS][COLS] = {‘0‘};

char show[ROWS][COLS] = {‘*‘};

srand((unsigned)time(NULL));

Init(real,ROWS, COLS, ‘0‘);

Init(show, ROWS, COLS, ‘*‘);

SetMine(real, ROWS, COLS, count);

Display(show, ROW, COL);

printf("Pelase Input x and y :\n");

scanf_s("%d%d", &x, &y);

if (real[x][y] == ‘1‘)//保证第一次不踩到雷

{

do

{

SetMine(real, ROWS, COLS, count);

} while (real[x][y] == ‘1‘);

}

show[x][y] = SearchMind(real, &x, &y, ROWS, COLS) + ‘0‘;

for (i = 0; i

{

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

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

{

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

}

printf("\n");

}

printf("\n");

Display(show, ROW, COL);

while ((ROW*COL) > (ROW*COL - num))

{

printf("Pelase Input x and y :\n");

scanf_s("%d%d", &x, &y);

ret = SearchMind(real, &x, &y, ROWS, COLS) + 1;

if (ret!=0)//不是踩到雷的情况进入

{

show[x][y] = SearchMind(real, &x, &y, ROWS, COLS) + ‘0‘;//转化成字符型

Display(show, ROW, COL);

}

if (ret == 0)//踩到雷的情况进入

{

return 0;

}

num--;

}

return 1;

}

int main()

{

Menu();

while (1)

{

int m = 0;

scanf_s("%d", &m);

switch (m)

{

case 1:

{

if (Game())

{

printf("you win\n");

}

else

{

printf("you failed\n");

Menu();

}

break;

}

case 2:

return 0;

break;

default:

printf("请重新输入\n");

break;

}

}

}

测试函数中的头文件 #include "game.h",而不是#include

接下来是头文件game.h

#ifndef __GAME_H__

#define __GAME_H__

#include

#include

#include

#include

#define ROWS 11

#define COLS 11

#define ROW 9

#define COL 9

#define count 10

void Init(char arr[ROWS][COLS],int rows, int cols, const char ch);

void SetMine(char str[ROWS][COLS], int rows, int cols, int cot);

void Display(char str[ROWS][COLS], int rows, int cols);

int SearchMind(char str[ROWS][COLS], int* x, int* y, int rows, int cols);

#endif

接下来是最重要的部分也是该游戏的核心部分游戏函数文件game.c

#include "game.h"

void Init(char arr[ROWS][COLS], int rows, int cols, const char ch)

{

int i = 0;

int j = 0;

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

{

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

{

arr[i][j] = ch;

}

}

}

//随机埋雷 SetMine()

void SetMine(char str[ROWS][COLS], int rows, int cols, int cot)

{

int x = 0;

int y = 0;

while (cot > 0)

{

x = rand() % 9 + 1;

y = rand() % 9 + 1;

if (str[x][y]==‘0‘)

{

str[x][y] = ‘1‘;

cot--;

}

}

}

//显示棋盘 Display()

void Display(char str[ROWS][COLS], int rows, int cols)

{

int i = 0;

int j = 0;

for (i = -1; i < rows; i++)

{

printf("%3d", i+1);

}

printf("\n");

for (i = 1; i <=rows; i++)

{

printf("%3d", i);

for (j = 1; j <= cols; j++)

{

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

}

printf("\n");

}

}

//输入坐标,判断周围的雷数,并回馈数字,为0时递归SearchMine()

int SearchMind(char str[ROWS][COLS], int* x, int* y, int rows,int cols)

{

int number = 0;

//判断是否踩到雷

if (str[*x][*y] == ‘1‘)

{

return -1;

}

number = str[*x - 1][*y - 1] + str[*x - 1][*y] + str[*x - 1][*y + 1]

+ str[*x][*y - 1] + str[*x][*y + 1]

+ str[*x + 1][*y - 1] + str[*x + 1][*y] + str[*x + 1][*y + 1] - 8 * ‘0‘;

//if (number == 0)

//{

// //改变x,y的值实现爆炸效果

// number = SearchMind(str, &x, &y, rows, cols);

//}//递归

return number;

}

//判断输赢 Distinguish()

原文:https://www.cnblogs.com/liuzhengkai/p/9096968.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值