当前位置:网站首页>Minesweeping games
Minesweeping games
2022-04-23 07:19:00 【Amyniez】
Complete the minesweeping game , Need to create a new project , And create three sub files :mine_sweeper.c、game.c、game.h
1.game.h: Game statement encapsulation code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 15
#define COL 15
#define ROWS ROW + 2
#define COLS COL + 2
#define COUNT 20
// Statement
void InitBoard(char board[ROWS][COLS], int rows, int cols, char 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);
int get_mine_number(char mine[ROWS][COLS], int x, int y);
2.mine_sweeper.c: Game function framework code
#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()
{
// Information about stored mines
// First , Set up Ray's information
char mine[ROWS][COLS] = {
0 };
// The second step , Check out the information of mine
char show[ROWS][COLS] = {
0 };
// initialization
InitBoard(mine, ROWS, COLS, '0');// Store non mines as 0, Table running in the background , Don't print it out
InitBoard(show, ROWS, COLS, '*');// Store non mines as *, The table the user sees
// Print forms
//DisplayBoard(mine, ROW, COL);// Just print 9*9 Just use the form
DisplayBoard(show, ROW, COL);
// Arrange thunder
SetMine(mine, ROW, COL);
// Start minesweeping
FindMine(mine, show, ROW, COL);
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input)
{
case 0:
printf(" Game over , Quit the game !\n");
break;
case 1:
game();
break;
default:
printf(" Wrong choice , Please reselect !\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
3.game.c: Specific function implementation code
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(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 DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
// Print column number ( from 0 Start , To 9 end , altogether 10 Number , The upper left corner of 0)
for (i = 0; i <= col; i++)
{
printf("%2d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
// Print line number ( from 1 Start , To 9 end , altogether 9 Number )
printf("%2d ", i);
for (j = 1; j <= col; j++)
{
printf(" %c ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = COUNT;// Set the number of mines to 10
while (count)
{
int x = rand() % row + 1;// Randomly generated mine coordinates
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;// When the number of thunder is 0 when , Out of the loop , Stop laying thunder
}
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (1)
{
printf(" Please enter the coordinates you want to :>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row&&y >= 1 && y <= col)
{
// The coordinates entered by the player are legal
//1. Step on thunder
if (mine[x][y] == '1')
{
printf(" unfortunately , You stepped on thunder !\n");
DisplayBoard(mine, row, col);
break;
}
else// No stepping on thunder
{
// Calculation (x,y) There are several mines around the coordinates
int count = get_mine_number(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf(" The coordinates entered are illegal , Please re-enter !\n");
}
}
if (win == row*col - COUNT)
{
printf(" congratulations , Mine clearance is successful !!!\n");
DisplayBoard(mine, row, col);
}
}
int get_mine_number(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';
}
版权声明
本文为[Amyniez]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230610096466.html
边栏推荐
猜你喜欢
[recommendation for new books in 2021] professional azure SQL managed database administration
Itop4412 LCD backlight drive (PWM)
1.1 PyTorch和神经网络
winform滚动条美化
[2021 book recommendation] effortless app development with Oracle visual builder
[2021 book recommendation] practical node red programming
取消远程依赖,用本地依赖
GEE配置本地开发环境
机器学习笔记 一:学习思路
C# EF mysql更新datetime字段报错Modifying a column with the ‘Identity‘ pattern is not supported
随机推荐
Markdown basic grammar notes
三种实现ImageView以自身中心为原点旋转的方法
Thanos.sh灭霸脚本,轻松随机删除系统一半的文件
cmder中文乱码问题
[recommendation for new books in 2021] professional azure SQL managed database administration
Itop4412 kernel restarts repeatedly
WebView displays a blank due to a certificate problem
第3章 Pytorch神经网络工具箱
adb shell常用模拟按键keycode
微信小程序 使用wxml2canvas插件生成图片部分问题记录
Recyclerview 批量更新View:notifyItemRangeInserted、notifyItemRangeRemoved、notifyItemRangeChanged
【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide
MySQL notes 1_ database
5种方法获取Torch网络模型参数量计算量等信息
MySQL笔记5_操作数据
HandlerThread原理和实际应用
Project, how to package
[dynamic programming] Yang Hui triangle
图像分类白盒对抗攻击技术总结
PaddleOCR 图片文字提取