当前位置:网站首页>Minesweeper game - C language
Minesweeper game - C language
2022-07-31 04:29:00 【Luo hand bugs @】
目录
一、游戏规则:
二、运行逻辑:
三、基本步骤:
2、Design two arrays to store information:
6、Demining and judging whether the demining has been completed:
四、总代码:
一、游戏规则:
Minesweeper game is to find all non-mine squares according to the numbers that appear on the clicked squares,同时避免踩雷,Step on a thunder and fail.
二、运行逻辑:
1、Create and print game menus.
2、创建两个棋盘数组,One is a checkerboard array where mines are placed,One is a checkerboard array to check mine.
3、初始化两个棋盘,Initialize all the chessboards on which the mines are arranged' 0 ' ,Initialize all checkerboards to ' * ' .
4、打印棋盘.
5、布置雷,The number of mines can be set by macro definition in the header file.
6、排查雷,Check in the array of laying mines,If it was mine, the print was killed,并退出游戏.如果不是雷,则统计雷的个数,是0then expand blank,不是0Then pass the number of mines to the array that checks mines.
7、判断输赢,If the total number of spaces is the product of rows and columns minus the number of Brays,则表示排雷成功.
三、基本步骤:
1、显示菜单界面:
void menu()
{
printf("************ 1. play **********\n");
printf("************ 0. exit **********\n");
}2、Design two arrays to store information:
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };3、初始化棋盘:
void init_board(char board[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}4、布置雷:
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--;
}
}
}
5、打印棋盘:
void display_board(char board[ROWS][COLS], int row, int col)
{
//列号
for (int j = 0; j <= col; j++)
{
printf("%d ", j);
}
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");
}
}6、Demining and judging whether the demining has been completed:
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 - EASY_COUNT)
{
printf("请输入要排查雷的坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//Coordinates are checked
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
display_board(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
display_board(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查过了\n");
}
}
else
{
printf("该坐标已经被排查过了\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
display_board(mine, ROW, COL);
}
}
四、总代码:
Implemented using multiple files,一个头文件game.h,两个源文件game.c和test.c.
1、game.h:
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 30
//初始化棋盘
void init_board(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void display_board(char board[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);2、game.c:
#include"game.h"
//初始化棋盘
void init_board(char board[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
//打印棋盘
void display_board(char board[ROWS][COLS], int row, int col)
{
//列号
for (int j = 0; j <= col; j++)
{
printf("%d ", j);
}
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");
}
}
//布置雷
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 < row * col - EASY_COUNT)
{
printf("请输入要排查雷的坐标:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
//Coordinates are checked
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
display_board(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
display_board(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查过了\n");
}
}
else
{
printf("该坐标已经被排查过了\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
display_board(mine, ROW, COL);
}
}
3、test.c:
#include"game.h"
void menu()
{
printf("************ 1. play **********\n");
printf("************ 0. exit **********\n");
}
void game()
{
//设计2An array to store information
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化棋盘
//mine初始化为全‘0’
//show初始化为全‘*’
init_board(mine, ROWS, COLS, '0');
init_board(show, ROWS, COLS, '*');
//布置雷
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("请选择:\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
边栏推荐
- type_traits元编程库学习
- MySQL修改root账号密码
- "A daily practice, happy water problem" 1331. Array serial number conversion
- qlib架构
- open failed: EACCES (Permission denied)
- $attrs/$listeners
- 开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
- MySQL数据库增删改查(基础操作命令详解)
- HCIP Day 10_BGP Route Summary Experiment
- (8) Math class, Arrays class, System class, Biglnteger and BigDecimal classes, date class
猜你喜欢

Win10 CUDA CUDNN installation configuration (torch paddlepaddle)

prompt.ml/15中<svg>标签使用解释

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

exsl文件预览,word文件预览网页方法

数字经济时代的开源数据库创新 | 2022开放原子全球开源峰会数据库分论坛圆满召开

两个地址池r2负责管地址池r1负责管dhcp中继

Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined

【C语言进阶】文件操作(一)

Notes on the establishment of the company's official website (6): The public security record of the domain name is carried out and the record number is displayed at the bottom of the web page

XSS靶场(三)prompt to win
随机推荐
beforeDestroy与destroyed的使用
Port inspection steps - 7680 port analysis - Dosvc service
Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us
exsl文件预览,word文件预览网页方法
No qualifying bean of type question
关于出现大量close_wait状态的理解
Why don't you programmers make a living off your own projects?And have to work for someone else?
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
MySQL database must add, delete, search and modify operations (CRUD)
产学研用 共建开源人才生态 | 2022开放原子全球开源峰会教育分论坛圆满召开
MySQL数据库必会的增删查改操作(CRUD)
open failed: EACCES (Permission denied)
Understanding of the presence of a large number of close_wait states
Safety 20220722
el-image tag doesn't work after binding click event
[Paper reading] Mastering the game of Go with deep neural networks and tree search
【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
Recursive implementation of the Tower of Hanoi problem
Gaussian distribution and its maximum likelihood estimation