当前位置:网站首页>C language games - minesweeping
C language games - minesweeping
2022-07-06 20:32:00 【farewell12345】
The game passes 3 Source file implementation
1. The header file game.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS ROW + 2
#define COLS COL + 2
// The number of rows on the chessboard
#define ROW 9
// The number of rows on the chessboard
#define COL 9
// Number of Mines arranged
#define EASY_COUNT 10
// Initialize array
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
// Print array
void DisplayBoard(char board[ROWS][COLS], int row, int col);
// Arrange thunder
void SetMine(char board[ROWS][COLS], int row, int col);
// Check the thunder
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
2. The game logic test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void game()
{
// Create two two-dimensional arrays , A record of the location of the mine , A message used to display thunder
// Record the location of the mine
char mine[ROWS][COLS] = { 0 };
// Show Ray's information
char show[ROWS][COLS] = { 0 };
// Initialize array
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
// Print array
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
// Arrange thunder
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);
// Check the thunder
FindMine(mine, show, ROW, COL);
}
void menu()
{
printf("*************************\n");
printf("******** 1. Mine clearance *******\n");
printf("******** 0. sign out *******\n");
printf("*************************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf(" Please enter :");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" sign out \n");
break;
default:
printf(" Input error , Please re-enter \n");
break;
}
} while (input);
return 0;
}
3. The subject of the game game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
// Initialize array
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;
}
}
}
// Print array
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
// Print the number of checkerboard columns
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
// Print checkerboard lines
printf("%d ", i);
for (j = 1; j <= col; j++)
{
// Print chessboard information
printf("%c ", board[i][j]);
}
printf("\n");
}
}
// Arrange thunder
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while(count > 0)
{
int i = rand() % row + 1;
int j = rand() % col + 1;
if (board[i][j] == '0')
{
board[i][j] = '1';
count--;
}
}
}
// Calculate the information of surrounding mines
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
//1+'0'='1' 2+'0'='2' …………
return mine[x - 1][y - 1] +
mine[x - 1][y] +
mine[x - 1][y + 1] +
mine[x][y - 1] +
mine[x][y + 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] - 8 * '0';
}
// Expand ray
void ExpandMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
// Recursive constraints
if (x == 0 || y == 0 || x == ROWS - 1 || y == COLS - 1)
{
return;
}
// Judge whether the coordinates have been calculated
if (show[x][y] != '*')
{
return;
}
int count = get_mine_count(mine, x, y);
if (count != 0)
{
show[x][y] = count + '0';
}
else
{
show[x][y] = ' ';
ExpandMine(mine, show, x - 1, y - 1);
ExpandMine(mine, show, x - 1, y);
ExpandMine(mine, show, x - 1, y + 1);
ExpandMine(mine, show, x, y - 1);
ExpandMine(mine, show, x, y + 1);
ExpandMine(mine, show, x + 1, y - 1);
ExpandMine(mine, show, x + 1, y);
ExpandMine(mine, show, x + 1, y + 1);
}
}
// Check the thunder
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
//win Used to judge whether all mines have been discharged
int win = row * col - EASY_COUNT;
do
{
printf(" Please enter the coordinates :");
scanf("%d %d", &x, &y);
// Judge the legitimacy of the input coordinates
if (x > 0 && x <= row && y > 0 && y <= col)
{
if (mine[x][y] == '1')
{
printf(" The game failed \n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
// Calculate the information of surrounding mines
ExpandMine(mine, show, x, y);
DisplayBoard(show, ROW, COL);
//win--;
}
}
else
{
printf(" Illegal input coordinates , Please re-enter \n");
}
} while (win);
if (win == 0)
{
printf(" congratulations , Mine clearance is successful \n");
}
}
边栏推荐
- Force deduction brush question - 98 Validate binary search tree
- BUUCTF---Reverse---easyre
- Implementation of packaging video into MP4 format and storing it in TF Card
- 永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
- Learn to punch in Web
- Rhcsa Road
- 5. 无线体内纳米网:十大“可行吗?”问题
- I've seen many tutorials, but I still can't write a program well. How can I break it?
- 知识图谱之实体对齐二
- Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
猜你喜欢
Build your own application based on Google's open source tensorflow object detection API video object recognition system (IV)
What programming do children learn?
[DIY]自己设计微软MakeCode街机,官方开源软硬件
"Penalty kick" games
Value of APS application in food industry
2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
02 基础入门-数据包拓展
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
Rhcsa Road
随机推荐
【Yann LeCun点赞B站UP主使用Minecraft制作的红石神经网络】
Comment faire une radio personnalisée
Zoom with unity mouse wheel: zoom the camera closer or farther
2022 portal crane driver registration examination and portal crane driver examination materials
Introduction of Xia Zhigang
Node.js: express + MySQL实现注册登录,身份认证
Core principles of video games
Tencent architects first, 2022 Android interview written examination summary
Tencent Android development interview, basic knowledge of Android Development
Summary of different configurations of PHP Xdebug 3 and xdebug2
New generation garbage collector ZGC
[weekly pit] information encryption + [answer] positive integer factorization prime factor
2022 construction electrician (special type of construction work) free test questions and construction electrician (special type of construction work) certificate examination
Review questions of anatomy and physiology · VIII blood system
JMeter server resource indicator monitoring (CPU, memory, etc.)
持续测试(CT)实战经验分享
【每周一坑】正整数分解质因数 +【解答】计算100以内质数之和
Error analysis ~csdn rebound shell error
(work record) March 11, 2020 to March 15, 2021
Tencent cloud database public cloud market ranks top 2!