所属专栏:C语言
博主首页:初阳785
代码托管:chuyang785
感谢大家的支持,您的点赞和关注是对我最大的支持!!!
博主也会更加的努力,创作出更优质的博文!!
关注我,关注我,关注我,重要的事情说三遍!!!!!!!!
void menu()
{
printf("***********************\n");
printf("******** 1.play *******\n");
printf("******** 0.exit *******\n");
printf("***********************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏\n\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
printf("选择错误,请重新选择:>");
default:
break;
}
} while (input);
return 0;
}
这个可以说是一个模板了,我们之前写的猜数字游戏和三子棋游戏都是用到了这个模板的,现在我们的扫了游戏也用到了,可以说是固定搭配了,小伙伴们可以慢慢品味。
再看我我们的引用的头文件:
#pragma once
#include
#include
#include
//设置棋盘规格
#define ROW 9
#define COL 9
//扩大的棋盘
#define ROWS ROW+2
#define COLS COL+2
//雷的个数
#define MIN_NUM 10
//存放雷的棋盘
Initboard(min, ROWS, COLS,'0');
//展示的棋盘
Initboard(shar, ROWS, COLS,'*');
这里我们用字符’*'来当作我我们遮挡雷的格子,而我们布置了的棋盘用字符’0’初始化。
void Initboard(char board[ROWS][COLS], int rows, int cols, char ret)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = ret;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("--------扫雷游戏-------\n");
int i = 0;
for (i = 0; i <= row; i++)
{
printf("%d ", i);
if (i == 0)
printf("|");
}
printf("\n");
for (i = 0; i <= row; i++)
{
printf("--");
if (i == 0)
printf("|");
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%-2d|", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetMin(char min[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = MIN_NUM;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
//放置生成的随机数越界了。
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (min[x][y] == '0')
{
min[x][y] = '1';
count--;
}
}
}
//这个是不用打印出来的,这里打印出来是观察一下我们布置雷有没有成功。
//DisplayBoard(min, ROW, COL);
}
void PlayGame(char min[ROWS][COLS], char shar[ROWS][COLS], int row, int col)
{
int win = 0;
while (win:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (min[x][y] == '1')
{
printf("抱歉,你被炸死了\n");
//被炸死了,打印除布置雷的情况
DisplayBoard(min, ROW, COL);
break;
}
if (shar[x][y] != '*')
{
printf("输入坐标重已显示,请重新输入\n");
break;
}
//x,y坐标不是雷
spand(min, shar,x, y,ROW,COL, &win);
DisplayBoard(shar, ROW, COL);
}
else
{
printf("输入坐标错误,请重新输入\n");
}
if (win == ROW * COL - MIN_NUM)
printf("恭喜你,成功过关\n");
}
}
void spand(char min[ROWS][COLS],char shar[ROWS][COLS], int x, int y,int row,int col, int* count)
{
//防止越界
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
int ret = TheMieCount(min, x, y);
if (ret == 0)
{
shar[x][y] = ' ';
(*count)++;
int i = 0;
for (i = x - 1; i <= x + 1; i++)
{
int j = 0;
for (j = y - 1; j <= y + 1; j++)
{
if (shar[i][j] == '*')//避免已经递归过的再次递归,以放置死递归
{
spand(min, shar, i, j, ROW, COL,count);
}
}
}
}
else
{
shar[x][y] = ret + '0';
(*count)++;
}
}
}
#pragma once
#include
#include
#include
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define MIN_NUM 10
//初始化棋盘
void Initboard(char board[ROWS][COLS], int rows, int cols, char ret);
//展示棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//布置雷
void SetMin(char min[ROWS][COLS],int row,int col);
//开始扫雷
void PlayGame(char min[ROWS][COLS],char shar[ROWS][COLS],int rows,int cols);
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void Initboard(char board[ROWS][COLS], int rows, int cols, char ret)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = ret;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("--------扫雷游戏-------\n");
int i = 0;
for (i = 0; i <= row; i++)
{
printf("%d ", i);
if (i == 0)
printf("|");
}
printf("\n");
for (i = 0; i <= row; i++)
{
printf("--");
if (i == 0)
printf("|");
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%-2d|", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetMin(char min[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = MIN_NUM;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (min[x][y] == '0')
{
min[x][y] = '1';
count--;
}
}
}
//DisplayBoard(min, ROW, COL);
}
int TheMieCount(char min[ROWS][COLS], int x, int y)
{
return (min[x - 1][y] + min[x - 1][y - 1] + min[x][y - 1] +
min[x - 1][y + 1] + min[x + 1][y] + min[x + 1][y + 1] +
min[x][y + 1]+min[x+1][y-1] - 8 * '0');
}
//实现扩展
void spand(char min[ROWS][COLS],char shar[ROWS][COLS], int x, int y,int row,int col, int* count)
{
//防止越界
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
int ret = TheMieCount(min, x, y);
if (ret == 0)
{
shar[x][y] = ' ';
(*count)++;
int i = 0;
for (i = x - 1; i <= x + 1; i++)
{
int j = 0;
for (j = y - 1; j <= y + 1; j++)
{
if (shar[i][j] == '*')
{
spand(min, shar, i, j, ROW, COL,count);
}
}
}
}
else
{
shar[x][y] = ret + '0';
(*count)++;
}
}
}
void PlayGame(char min[ROWS][COLS], char shar[ROWS][COLS], int row, int col)
{
int win = 0;
while (win<ROW*COL-MIN_NUM)
{
int x = 0;
int y = 0;
printf("请输入坐标,中间用空格隔开>:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (min[x][y] == '1')
{
printf("抱歉,你被炸死了\n");
DisplayBoard(min, ROW, COL);
break;
}
if (shar[x][y] != '*')
{
printf("输入坐标重已显示,请重新输入\n");
break;
}
//x,y坐标不是雷
spand(min, shar,x, y,ROW,COL, &win);
DisplayBoard(shar, ROW, COL);
}
else
{
printf("输入坐标错误,请重新输入\n");
}
if (win == ROW * COL - MIN_NUM)
printf("恭喜你,成功过关\n");
}
}
#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 min[ROWS][COLS];
char shar[ROWS][COLS];
//存放雷的棋盘
Initboard(min, ROWS, COLS,'0');
//展示的棋盘
Initboard(shar, ROWS, COLS,'*');
//打印棋盘
//DisplayBoard(min, ROW, COL);
DisplayBoard(shar, ROW, COL);
//布置雷
SetMin(min, ROW, COL);
//开始玩,输入坐标
//计算雷的个数
PlayGame(min, shar, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏\n\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
printf("选择错误,请重新选择:>");
default:
break;
}
} while (input);
return 0;
}