当前位置:网站首页>扫雷游戏(c语言写)
扫雷游戏(c语言写)
2022-07-31 03:46:00 【不能在白了呀】
扫雷游戏分为三个板块分别是:
* test.c (主函数)
* game.c(游戏部分代码实现)
* game.h(头文件,宏定义,函数的声明)
test.c
#include"game.h"
void menu()
{
//打印扫雷游戏目录
printf("########################################\n");
printf("######### 1.play #########\n");
printf("######### 0.exit #########\n");
printf("########################################\n");
}
void game()
{
//设计2个数组存放信息
char mine[ROWS][COLS] = {
0 };
char show[ROWS][COLS] = {
0 };
//初始化棋盘
init_board(mine, ROWS, COLS, '0');
init_board(show, ROWS, COLS, '*');
//打印棋盘
//display_board(mine, ROW, COL);
//display_board(show, ROW, COL);
//存放地雷
set_mine(mine, ROW, COL);
display_board(show, ROW, COL);//打印棋盘
//排雷
find_mine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择>:");
scanf_s("%d", &input);
if (input == 1)
{
printf("开始游戏\n");
game();
break;
}
else if (input == 0)
{
printf("游戏结束");
break;
}
else
printf("输入错误,请从新输入\n");
} while (input);
}
##################################################################
game.c
#include"game.h"
//初始化两个数组
void init_board(char board[ROWS][COLS], int rows, int cols,char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
//打印数组
void display_board(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col)
{
//布置10个雷
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
//数在你所输入的坐标一圈有几个雷
int get_mine_count(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 + 1][y] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] - 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 <(col * row) - EASY_COUNT)
{
printf("请输入坐标》:");
scanf_s("%d %d",&x,&y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '0')
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
display_board(show, ROW, COL);
win++;
}
else
{
printf("爆炸了\n");
display_board(mine, ROW, COL);
break;
}
}
else
{
printf("该坐标已被占用\n");
}
}
else
{
printf("输入坐标有误,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
display_board(mine, ROW, COL);
}
}
#####################################################################
game.h
#include<stdio.h>
#pragma once
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10//雷的个数
extern void init_board(char mine[ROWS][COLS], int rows, int cols,char set);
extern void display_board(char board[ROWS][COLS], int row, int col);
extern void set_mine(char mine[ROWS][COLS], int row, int col);
extern void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
###############################################################
游戏演示

############################################
############################################
############################################
############################################
############################################
############################################
############################################
最后:文章有什么不对的地方或者有什么更好的写法欢迎大家在评论区指出来。
边栏推荐
- BP神经网络
- No qualifying bean of type question
- Ambiguous method call.both
- ClickHouse:设置远程连接
- Several common errors when using MP
- (四)递归、可变参数、访问修饰符、理解 main 方法、代码块
- Point Cloud DBSCAN Clustering (MATLAB, not built-in function)
- 浅识Flutter 基本组件之CheckboxListTile组件
- How to develop a high-quality test case?
- $parent/$children 与 ref
猜你喜欢

Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea

Key Technologies of Interface Testing

(八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类

【Exception】The field file exceeds its maximum permitted size of 1048576 bytes.
![[Compilation principle] Design principle and implementation of recursive descent parsing](/img/51/cd054a8246dc108520d6ff9ea26c60.png)
[Compilation principle] Design principle and implementation of recursive descent parsing

What is a system?

Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)

BP神经网络

A brief introduction to the showDatePicker method of the basic components of Flutter
![[Dynamic programming] Maximum sum of consecutive subarrays](/img/3d/10731cc64d1c69d2beb3666ae0f064.png)
[Dynamic programming] Maximum sum of consecutive subarrays
随机推荐
A brief introduction to the CheckboxListTile component of the basic components of Flutter
「 每日一练,快乐水题 」1331. 数组序号转换
Difference between unallocated blocks and unused blocks in database files
Postgresql 15 source code analysis (5) - pg_control
安全20220715
$parent/$children and ref
ClickHouse:设置远程连接
【论文阅读】Mastering the game of Go with deep neural networks and tree search
LeetCode每日一练 —— OR36 链表的回文结构
Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
Atomic operation CAS
Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea
What is a system?
安全20220709
Good place to download jar packages
Redis counts new and retained users
数据库实现分布式锁
LocalDate addition and subtraction operations and comparison size
【AUTOSAR-RTE】-4-Port and Interface and Data Type
Select the smoke test case, and make the first pass for the product package entering QA