当前位置:网站首页>Minesweeping Pro version 2021-08-19
Minesweeping Pro version 2021-08-19
2022-07-26 10:45:00 【ZhuMou】
#1 Code
// The header file game.h
#pragma once
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
struct oneGame {// Data needed for a game ,game() Any subfunction in is modified around it
char* mineField = NULL;// Minefield array , Consider calculating numbers and presenting , And save space ( Prevent memory leaks ), So use char*. To simplify the calculation , So the actual number of rows and columns should be +2.
// Element is '#', It means ray ; Element is ‘0’ or ‘1’, Indicates the number area .‘0’ Description is not expanded ,‘1’ Description has been expanded . Used to reduce the overhead of recursive algorithm .
char* sweptField = NULL;// Minecleared area array , Decide what data type to use according to the subsequent functions .
int row = 0;// Number of minefield rows
int col = 0;// Number of minefield columns
int mines = 0;// The number of Mines needed
int mineNum = 0;// The number of remaining mines
};
int menu(void);// Game start menu , Input mode from the outside , And return it to
void game(int mode);// According to the input game mode (int), Play a game .
/*
game Function has two parts :
1、 According to formal parameters mode, Generate different minefields .generateMineField.
2、 The user sweeps a thunder (x,y External input ), According to different conditions , It produces different results .sweepOnce
Failure : This time, I caught thunder ===》 Print death messages ; Show minefields ; sign out game()
correct : This time, it's not thunder ===》 Print correct information ; Calculate the number of this cell compute; Unfold centered on this case expansion; Returning to the beginning of the loop requires input
success : This time it is correct and has been scanned ===》 Print completion information ; Expand the cleared area ; Quit the game
At least record :
1、 minefields ( answer )( Two dimensional array , Number of Mines , Distribution of thunder , Lei district chief , The minefield is wide )
2、 Swept area ( It can be shown to players )( Swept area )
*/
void generateMineField(oneGame*);// Generate the specified length , Specify width , Specify the number of Mines , Minefields with random distribution of thunder ; Generate minecleared areas and initialize .
/* Implementation : Generate minefields and initialize --》 Generate minecleared areas and initialize .
*/
int sweepOnce(oneGame*);// A minesweeping , Return results .-1 For failure ,1 For success ,0 To be right .
int compute(oneGame*, int, int );
// Calculate the number at the given coordinates ( Without ray ), write in sweptField, Return to it at the same time .
void expansion(oneGame*, int, int);
void display(char*, oneGame*);
// Print 2D array
//1. Transform the design idea into a flow chart
//2. Analyze the functions and data used according to the flow chart ( Just stay at the functional level , Did not consider how to achieve )
//3. Consider the implementation of functions , At the same time, you can also consider how the data used should be stored ( The storage mode depends on the use mode )
//4. Implementation function , You may also need to design sub functions
// Source file game.cpp
#include "game.h"
int menu() {
int mode = 0;
printf("###################################\n");
printf("########### Please choose the difficulty of the game ##########\n");
printf("###########1、 Primary difficulty #############\n");
printf("###########2、 Intermediate difficulty #############\n");
printf("###########3、 Advanced difficulty #############\n");
printf("########### Your choice >: ");
scanf("%d", &mode);
while (getchar() != '\n')
;// Not doing anything , For case input buffer
return mode;
}
void game(int mode) {
int isDead = 0;
oneGame thisGame;
// according to mode Choice mode
if (mode == 1) {// Elementary mode
thisGame.col = thisGame.row = 9 + 2;
thisGame.mines = 2;
thisGame.mineNum = 2;
}
if (mode == 2) {// Intermediate mode
thisGame.col = thisGame.row = 16 + 2;
thisGame.mines = 40;
thisGame.mineNum = 40;
}
if (mode == 3) {
thisGame.col = 30 + 2;
thisGame.row = 16 + 2;
thisGame.mines = 99;
thisGame.mineNum = 99;
}
generateMineField(&thisGame);
display(thisGame.sweptField,&thisGame);
//display(thisGame.mineField,&thisGame);
while (isDead == 0) {
isDead = sweepOnce(&thisGame);
}
free(thisGame.mineField);
free(thisGame.sweptField);
return;
}
void generateMineField(oneGame* thisGame) {
// Initialize minefields and cleared areas
thisGame->mineField = (char*)malloc(thisGame->col*thisGame->row);
thisGame->sweptField = (char*)malloc(thisGame->col*thisGame->row);
for (int i = 0; i < thisGame->col*thisGame->row; ++i) {
*(thisGame->mineField + i) = '0';
*(thisGame->sweptField + i) = '#';
}
for (int i = 0; i < thisGame->row - 2; ++i) {
for (int j = 0; j < thisGame->col - 2; ++j) {
*((thisGame->sweptField + thisGame->col + 1) + j + i * (thisGame->col)) = ' ';
}
}
// Randomly distributed thunder , Distributed in the middle of the minefield , There is no
srand((unsigned long)time(NULL));
for (int i = 0; i < thisGame->mines;) {
int x = rand() % (thisGame->row - 2);
int y = rand() % (thisGame->col - 2);
if (*((thisGame->mineField + thisGame->col + 1) + y + x * (thisGame->col)) == '#') {
;// Don't do anything
}
else {
*((thisGame->mineField + thisGame->col + 1) + y + x * (thisGame->col)) = '#';
++i;
}
}
}
int sweepOnce(oneGame* thisGame) {
// Request input
printf(" Enter coordinates >: ");
int x = 0;
int y = 0;
int choice = 0;
scanf("%d%d%d", &x, &y, &choice);
while (getchar() != '\n')
;// Not doing anything , For case input buffer
// If the error / Failure
if (choice == 0) {
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col)) == '#') {
printf(" Mine clearance failed , Game over !\n");
display(thisGame->mineField, thisGame);
return -1;
}
// If correct
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col)) != '#') {
printf(" This success , Keep playing !\n");
compute(thisGame, x, y);
expansion(thisGame, x, y);
system("cls");
display(thisGame->sweptField, thisGame);
}
}
if (choice == 1) {
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col)) != '#') {
printf(" Mine clearance failed , Game over !\n");
display(thisGame->mineField, thisGame);
return -1;
}
// If correct
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col)) == '#') {
--thisGame->mineNum;
printf(" This success , Keep playing !\n");
*((thisGame->sweptField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col)) = '#';
system("cls");
display(thisGame->sweptField, thisGame);
}
// If it works
if (thisGame->mineNum == 0) {
printf(" Mine clearance succeeded , Game over !\n");
display(thisGame->sweptField, thisGame);
return 1;
}
}
return 0;
}
int compute(oneGame* thisGame, int x, int y) {
int count = 0;
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) + 1) == '#')
++count;
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) - 1) == '#')
++count;
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) + thisGame->col) == '#')
++count;
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) - thisGame->col) == '#')
++count;
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) + thisGame->col + 1) == '#')
++count;
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) - thisGame->col + 1) == '#')
++count;
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) + thisGame->col - 1) == '#')
++count;
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) - thisGame->col - 1) == '#')
++count;
*((thisGame->sweptField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col)) = count + '0';
return count;
}
void expansion(oneGame* thisGame, int x,int y) {
*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col)) = '1';
// Using recursive algorithm leads to stack overflow , So it has been revised repeatedly , Many conditions are added , Minimize space complexity
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) + 1) != '#') {
if(*((thisGame->sweptField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) + 1) != '#'){
if(*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) + 1) != '1')
if(compute(thisGame, x, y + 1) == 0)
expansion(thisGame, x, y + 1);
}
}
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) - 1) != '#') {
if(*((thisGame->sweptField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) - 1) != '#')
if(*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) - 1) != '1')
if(compute(thisGame, x, y - 1) == 0)
expansion(thisGame, x, y - 1);
}
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) + thisGame->col) != '#') {
if(*((thisGame->sweptField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) + thisGame->col) != '#')
if(*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) + thisGame->col) != '1')
if(compute(thisGame, x + 1, y) == 0)
expansion(thisGame, x + 1, y);
}
if (*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) - thisGame->col) != '#') {
if(*((thisGame->sweptField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) - thisGame->col) != '#')
if(*((thisGame->mineField + thisGame->col + 1) + y - 1 + (x - 1) * (thisGame->col) - thisGame->col) != '1')
if(compute(thisGame, x - 1, y) == 0)
expansion(thisGame, x - 1, y);
}
return;
}
void display(char* thisArray,oneGame* thisGame){
for (int i = 0; i < thisGame->col - 1; ++i) {
printf("%2d ", i);
}
printf("\n");
for (int i = 0; i < thisGame->row - 2; ++i) {
printf("%2d ", i + 1);
for (int j = 0; j < thisGame->col - 2; ++j) {
printf("%2c ", *((thisArray + thisGame->col + 1) + j + i * (thisGame->col)));
}
printf("\n");
}
}
// The main function test.cpp
#include "game.h"
int main() {
int choice = 0;
again:
int mode = menu();
game(mode);
printf("######### Do you want to finish it again ?\n");
printf("#########1. Have another play \n");
printf("#########0. Stop playing \n");
printf(" Your choice >: ");
scanf("%d",&choice);
while (getchar() != '\n')
;// Not doing anything , For case input buffer
if (choice == 0)
;// Not doing anything
else
goto again;
return 0;
}
#2 The rules of the game
Follow the prompts to enter the game . After input x y choice.x Represents the abscissa ,y Represents the ordinate ,choice Express choice (1 There is thunder here ,0 It means there is no thunder here ). Fault tolerance mechanism is not done , Input carefully .
边栏推荐
- 2021-08-12函数递归_和鹏哥学习C语言
- [leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits
- 回到顶部的几种方案(js)
- PLC与伺服电机连接
- Error[pe147]: declaration is incompatible with 'error problem
- 控制随机抽中几率 [ C# | Random ]
- 剑指Offer(九):变态跳台阶
- 第6期:大学生应该选择哪种主流编程语言
- 在altium designer中禁用USBJATG
- 344.反转字符串
猜你喜欢
RT thread learning notes (I) -- configure RT thread development environment
2021-08-12函数递归_和鹏哥学习C语言
Codepoint 58880 not found in font, aborting. flutter build apk时报错
2021-08-12 function recursion_ Learn C language with brother Peng
[machine learning notes] [style transfer] deeplearning ai course4 4th week programming(tensorflow2)
Issue 7: how do you choose between curling up and lying flat
Add touch screen driver for stemwin 5.22 on Shenzhou IV development board
RT-Thread 学习笔记(五)---编辑、下载、调试程序
[leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits
The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly
随机推荐
剑指Offer(五十二):正则化表达式
RT-Thread 学习笔记(一)---配置RT-Thread开发环境
Parallelism, concurrency and several directions for high concurrency optimization
Sql Server 数据库之初学体验
C语言鹏哥20210812C语言函数
MySQL速学-2021-09-01
Common classes (understand)
algorithm
Error[pe147]: declaration is incompatible with 'error problem
c结构体中定义的成员指针赋值与结构体指针作为成员函数参数的使用
Koin
[leetcode daily question 2021/4/23]368. Maximum divisible subset
文案秘籍七步曲至----文献团队协作管理
349. 两个数组的交集
SAP ABAP 守护进程的实现方式
创建EOS账户 Action
剑指Offer(二十):包含min函数的栈
1837.K进制表示下的各位数字总和
Add touch screen driver for stemwin 5.22 on Shenzhou IV development board
粽子大战 —— 猜猜谁能赢