过一遍 扫雷游戏 ,不是很熟
在头文件写的
#include <stdio.h>
#define ROW 9//行
#define COL 9//列
#define ROWS ROW+2
#define COLS COL+2
#define EARY_COUNT 10
#include <stdlib.h>
#include <time.h>
void Initborad(char borad[ROWS][COLS], int rows, int cols,char set );
//mine和show数组区分开在传一个参数set
void DisplayBorad( char borad [ROWS][COLS],int row ,int col);
void SetMine( char mine [ROWS][COLS], int row, int col);
void FIndMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
函数的调用
#define _CRT_SECURE_NO_WARNINGS
#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 };//存放排查雷的信息
//初始化棋盘
Initborad(mine, ROWS, COLS,'0');//给出想要的参数
Initborad(show, ROWS, COLS,'*');
//打印棋盘
DisplayBorad(show, ROW, COL);
//DisplayBorad(mine, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
DisplayBorad(mine, ROW, COL);
//排查雷
FIndMine(mine, show,ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
函数的实现
#define _CRT_SECURE_NO_WARNINGS
#include "game .h"
int GetMine(char mine[ROWS][COLS],int x,int y)
{
int i = 0;
int count = 0;
for (i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
count += (mine[x + i][y + j] - '0');
}
}
return count;
}
void Initborad(char borad[ROWS][COLS], int rows, int cols,char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
borad[i][j] = set;//传参数'*'和'0'
}
}
}
void DisplayBorad(char borad[ROWS][COLS], int row, int col)
{
printf("-----扫雷游戏-----\n");
for (int i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
int i = 0;
for (i = 1; i <= row; i++)
{
printf("%d ", i);//打印行号
for (int j = 1; j <= col; j++)
{
printf("%c ", borad[i][j]);//打印出'*'和'0'
}
printf("\n");
}
printf("-----扫雷游戏-----\n");
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EARY_COUNT;//设置10个雷
int x = 0;//产生1~9的数字
int y = 0;
while (count)
{
x = rand()% row +1; //因为这里产生的数字是0~8,加上1就是1~9了
y = rand() % col + 1;
if (mine[x][y] != '1')
{
mine[x][y] = '1';//布置雷
count--;
}
}
}
void FIndMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win = 0;
int x = 0;
int y = 0;
while (win < row * col- EARY_COUNT)
{
printf("请输入要排查的坐标:");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayBorad(mine, ROW, COL);
break;
}
else
{
int count = GetMine(mine, x, y);//得到坐标位置
show[x][y] = count + '0';
DisplayBorad(show, ROW, COL);
win++;
}
}
else
{
printf("输入的坐标有误,请重新输入\n");
}
}
if (win == col * row - EARY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBorad(mine, ROW, COL);
}
}
原文地址:https://blog.csdn.net/2402_82715436/article/details/142304017
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!