当前位置:网站首页>Mining game (C language)
Mining game (C language)
2022-08-02 06:44:00 【Fruit Chenchen】
文章目录
Game preface preparation
1.Initialize the game layout for Minesweeper using a 2D array,Changed by changing the row and column of the array,用ROWto specify the number of rows in the array,COLto specify the number of columns in the array,Minesweeper requires two two-dimensional arrays,An array is used to store mine information,An array is used to store display interface information,Used for fear of border crossingROW+2和COL+2array to avoid out of bounds;
2.Displays the minesweeper game interface using a 2D array,In addition to displaying array information,It is also necessary to display the coordinates in the first row and first column so that the player can determine the coordinates of the mine clearance;
3.设置雷区,A fixed number of mines are randomly placed according to the difficulty of the game,使用#defineIt is defined to change the value at any time to achieve a different number of mines;
4.排雷,Whenever the coordinates selected by the player are not mine,Then calculate the number of mines in a circle of eight grids around this coordinate.
5.判断输赢,If the player steps on a thunder,The game ends immediately,If the number of unlined areas is equal to the number of mines and the player is not killed,则玩家赢.
一、建立游戏菜单
void menu()
{
printf("********************************\n");
printf("********* 扫雷游戏 ********\n");
printf("********************************\n");
printf("********* 1.play ********\n");
printf("********* 0.exit ********\n");
printf("********************************\n");
printf("********************************\n");
}
nt main()
{
int input = 0;
do
{
menu();
printf("请输入你的选择\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
二、建立棋盘
void init_broad(char broad[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++)
{
broad[i][j] = set;
}
}
}
三、初始化棋盘
void display_broad(char broad[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 ", broad[i][j]);
}
printf("\n");
}
}
四、布置雷
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = DEFAULT_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
五、Check for mines and determine whether to step on mines
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 < row * col - DEFAULT_COUNT)
{
printf("请输入要排查雷的坐标:>\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("你被炸死了\n");
display_broad(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, row, col);
show[x][y] = count + '0';
display_broad(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - DEFAULT_COUNT)
{
printf("恭喜你,排雷成功\n");
display_broad(mine, ROW, COL);
}
}
六、源码
1.test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("********************************\n");
printf("********* 扫雷游戏 ********\n");
printf("********************************\n");
printf("********* 1.play ********\n");
printf("********* 0.exit ********\n");
printf("********************************\n");
printf("********************************\n");
}
void game()
{
char mine[ROWS][COLS] = {
0 };//Design arrays to store information
char show[ROWS][COLS] = {
0 };
//初始化棋盘
init_broad(mine, ROWS, COLS, '0');
init_broad(show, ROWS, COLS, '*');
//打印棋盘
display_broad(show, ROW, COL);
//布置雷
set_mine(mine, ROW, COL);
//排雷
find_mine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请输入你的选择\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
2.game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init_broad(char broad[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++)
{
broad[i][j] = set;
}
}
}
void display_broad(char broad[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 ", broad[i][j]);
}
printf("\n");
}
}
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = DEFAULT_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 < row * col - DEFAULT_COUNT)
{
printf("请输入要排查雷的坐标:>\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("你被炸死了\n");
display_broad(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, row, col);
show[x][y] = count + '0';
display_broad(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - DEFAULT_COUNT)
{
printf("恭喜你,排雷成功\n");
display_broad(mine, ROW, COL);
}
}
3.game.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define DEFAULT_COUNT 10
//初始化棋盘
void init_broad(char broad[ROWS][COLS],int rows,int cols,char set);
//打印棋盘
void display_broad(char broad[ROWS][COLS], int row, int col);
//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col);
//排雷
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
边栏推荐
- BGP+MPLS综合实验
- 上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展
- How to perform concurrent calculation (stability test and stress test)?
- leetcode括号匹配问题——32.最长有效括号
- Difference and analysis of CPU usage and load
- npm 和 yarn的区别
- Different ways of shell scripting
- Analysis of the source code of the JS UI framework of Hongmeng system
- Linux CentOS8安装Redis6
- Important concepts of target detection - IOU, receptive field, hole convolution, mAP
猜你喜欢

51 microcontroller peripherals article: dot-matrix LCD

Important concepts of target detection - IOU, receptive field, hole convolution, mAP

View source and switch mirrors in two ways: npm and nrm

字节面试题:如何保证缓存和数据库的一致性

selenium + robotframework的运行原理

HCIP BGP综合实验 建立对等体、路由反射器、联邦、路由宣告及聚合

Block elements, inline elements (
随机推荐
npm、nrm两种方式查看源和切换镜像
HCIP BGP Comprehensive Experiment Establishing peers, route reflectors, federation, route announcement and aggregation
Different ways of shell scripting
Double for loop case (use js jiujiu printing multiplication table)
无代码生产新模式探索
The virtual reality real estate display system foresees the future decoration effect in advance
双重for循环案例(用js打印九九乘法表)
What is the most important ability of a programmer?
selenium + robotframework的运行原理
保证家里和企业中的WIFI安全-附AC与AP组网实验
国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
The installation of NPM, CNPM
Smart people's game improvement: Chapter 3, Lesson 2: "Number of Tongtong" (number)
Tips for programmers to write PPT
pytorch常用函数
Integrate ssm (1)
股价屡创新低 地产SaaS巨头陷入困境 明源云该如何转型自救?
制作web3d动态产品展示的优点
zabbix自动发现和自动注册
Contents of encoding-indexes.js file printed with Bluetooth:


