综合上节我们对于扫雷代码的分析与代码的尝试
我们就写出了下面的扫雷代码
game.h
#define _CRT_SECURE_NO_WARNINGS
#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_court 10 //雷的个数
//初始化棋盘
void initboard1(char mine[ROWS][COLS], int rows, int cols, int set);
void initboard2(char show[ROWS][COLS], int rows, int cols, int set);
//布置雷
void set(char mine[ROW][COL],int row,int col);
//棋盘打印
void displayboard(char arr[ROWS][COLS],int row,int col);
//排查雷
void findmine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col);
game.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include"game.h"
//棋盘初始化
void initboard1(char mine[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++) {
int j = 0;
for (j = 0; j < cols; j++) {
mine[i][j] = set;
}
}
}
void initboard2(char show[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++) {
int j = 0;
for (j = 0; j < cols; j++) {
show[i][j] = set;
}
}
}
//布置雷
void set(char mine[ROW][COL], int row, int col)
{
int court = easy_court;
while (court) {
//产生1~9的数字
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (mine[x][y] == '0') {
mine[x][y] = '1';
court--;
}
}
}
//打印棋盘
void displayboard(char arr[ROWS][COLS], int row, int col)
{
printf("--------扫雷游戏--------\n");
int i = 0;
for (i = 0; i < row+1; i++) {
printf("%d ", i);
}
printf("\n");
for (i = 1; i < col+1; i++) {
printf("%d ", i);
int j = 0;
for (j = 1; j < col + 1; j++) {
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
int get_mine_court(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][y+1] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0');
}
//排查雷
void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
int a=0;
int b=0;
int win = 0;
while (win < row * col - easy_court)
{
printf("请输入你要排查的坐标:>");
scanf("%d%d", &a, &b);
system("cls");
if (a >= 1 && a <= row && b >= 1 && b <= col)
{
if (mine[a][b] == '1')
{
printf("很遗憾,你被炸死了\n");
displayboard(mine, ROW, COL);
break;
}
else
{
int court_ = get_mine_court(mine, ROW, COL);
show[a][b] = court_+'0';
displayboard(show, ROW, COL);
win++;
}
}
else {
printf("输入错误,请重新输入\n");
}
if (win == row * col - easy_court) {
printf("恭喜你,排雷成功\n");
displayboard(mine, ROW, COL);
}
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include"game.h"
void menu()
{
printf("***********************\n");
printf("********1.play*********\n");
printf("********0.exit*********\n");
printf("***********************\n");
printf("请选择:>");
}
void play()
{
//扫雷
char mine[ROWS][COLS];//数组初始化全部为‘0’;
char show[ROWS][COLS];//数组初始化全部为‘*’
//初始化棋盘
initboard1(mine,ROWS,COLS,'0');
initboard2(show,ROWS,COLS,'*');
//布置雷
set(mine,ROW,COL);
//打印棋盘
//displayboard(mine,ROW,COL);
displayboard(show,ROW,COL);
//排查雷
findmine(mine,show, ROW, COL);
}
void game()
{
int input;
srand((unsigned int)time(NULL));
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
printf("扫雷\n");
play();
break;
case 0:
printf("游戏结束,退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
}
int main()
{
game();
return 0;
}