game.h
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
void Initboard(char board[ROWS][COLS],int rows, int cols,int set);
void Displayboard(char board[ROWS][COLS], int row, int col);
void Setmine(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
test.c
#include"game.h"
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化
Initboard(mine, ROWS, COLS,'0');
Initboard(show, ROWS, COLS,'*');
//打印
//Displayboard(mine, ROW, COL);
//Displayboard(show, ROW, COL);
//放置地雷
Setmine(mine, ROW, COL);
//Displayboard(mine, ROW, COL);
Displayboard(show, ROW, COL);
//寻找地雷
FindMine(mine, show, ROW, COL);
}
void manu()
{
printf("*************************************\n");
printf("*********** 1 . play ************\n");
printf("*********** 2 . exit ************\n");
printf("*************************************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
manu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("开始游戏\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误");
}
} while (input);
}
game.c
#include"game.h"
void Initboard(char board[ROWS][COLS], int rows, int cols, int set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void Displayboard(char board[ROWS][COLS], int row, int col)
{
printf("---------扫雷游戏---------\n");
for (int i = 0; i <= 9; i++)
{
printf("%d ", i);
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);
for (int j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("---------扫雷游戏---------\n");
}
void Setmine(char board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = EASY_COUNT;
while(count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
int Number(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1]+
mine[x][y - 1] + mine[x][y + 1]+
mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1]-8*'0';
}
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-EASY_COUNT)
{
printf("请输入你要排查的坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf("你的坐标已经排查过了,请重新输入");
}
else
{
if (mine[x][y] == '1')
{
printf("你被炸死了\n");
Displayboard(mine, ROW, COL);
break;
}
else
{
win++;
int count = Number(mine, x, y);
show[x][y] = count + '0';
Displayboard(show, ROW, COL);
}
}
}
else
{
printf("输入的为非法坐标,请重新输入");
}
}
if (win == col * row - EASY_COUNT)
{
printf("雷已经被全部排完,获得胜利\n");
Displayboard(mine, ROW, COL);
}
}
扫雷小游戏
最新推荐文章于 2025-07-26 21:13:40 发布