思路
- 设置两个数组
- 初始化两个数组
- 打印数组
- 设置雷
- 排查雷
game.h
#pragma once
#define ROW 9 //定义行为9
#define COL 9 //定义列为9
#define ROWS ROW+2 //排查雷时防止溢出
#define COLS COL+2
#define EASY_COUNT 10 //初步设有10个雷
void InitBoard(char board[ROWS][COLS], int rows,int cols, char set);//初始化
void Display_Board(char board[ROWS][COLS], int row, int col);//打印数组
void Set_mine(char mine[ROWS][COLS], int row, int col);//设置雷
#include<stdlib.h>
#include<time.h>
void Find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//排查雷
#include<stdio.h>
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[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++)
{
board[i][j] = set;
}
}
}
void Display_Board(char board[ROWS][COLS], int row, int col)
{
printf("-------扫雷游戏-------\n");
int i = 0;
for (i = 0; i <= row; i++)
{
printf("%d ",i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
int j = 0;
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-------扫雷游戏-------\n");
}
void Set_mine(char mine[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = 0;
while (count <= EASY_COUNT)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
}
count++;
}
}
int Get_minecount(char mine[ROWS][COLS],int x, int y)
{
return
mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] +
mine[x - 1][y] - 8 * '0';
}
void Find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win<row*col- EASY_COUNT)
{
printf("请输入你找到坐标:>");
scanf("%d%d", &x, &y);
if (x <= row && x >= 1 && y <= col && y >= 1)
{
if (mine[x][y] == '1')
{
printf("********你被炸死了*******\n");
printf("结果如下:>\n");
Display_Board(mine, ROW, COL);
break;
}
else
{
int count=Get_minecount(mine,x, y);
show[x][y] = count + '0';
win++;
Display_Board(show, ROW, COL);
}
}
else
{
printf("输入坐标非法,请重新输入:>\n");
Display_Board(show, ROW, COL);
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你找到了!!!!\n");
}
}
test.c
#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()
{
//创建数组
int mine[ROWS][COLS] = { 0 };
int show[ROWS][COLS] = { 0 };
//初始化
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印数组
Display_Board(show, ROW, COL);
//Display_Board(mine, ROW, COL);
//布置雷
Set_mine(mine, ROW, COL);
//排查雷
Find_mine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
do
{
srand((unsigned int)time(NULL));
menu();
printf("请输入:>");
scanf("%d", &input);
switch (input)
{
case 1:
{
game();
break;
}
case 0:
{
printf("退出游戏");
break;
}
default:
printf("请重新输入:>\n");
}
} while (input);
return 0;
}