当前位置:网站首页>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
边栏推荐
猜你喜欢
![[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide](/img/36/1c484aec5efbac8ae49851844b7946.png)
[2021 book recommendation] Red Hat Certified Engineer (RHCE) Study Guide

ArcGIS License Server Administrator 无法启动解决方法

给女朋友写个微信双开小工具

MySQL数据库安装与配置详解

机器学习 二:基于鸢尾花(iris)数据集的逻辑回归分类

Itop4412 HDMI display (4.4.4_r1)
![[2021 book recommendation] artistic intelligence for IOT Cookbook](/img/8a/3ff45a911becb895e6dd9e061ac252.png)
[2021 book recommendation] artistic intelligence for IOT Cookbook

【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide

第2章 Pytorch基础1

Project, how to package
随机推荐
Google AdMob advertising learning
DCMTK (dcm4che) works together with dicoogle
Handler进阶之sendMessage原理探索
WebView displays a blank due to a certificate problem
iTOP4412 SurfaceFlinger(4.0.3_r1)
[2021 book recommendation] practical node red programming
Recyclerview batch update view: notifyitemrangeinserted, notifyitemrangeremoved, notifyitemrangechanged
[recommendation of new books in 2021] practical IOT hacking
[2021 book recommendation] effortless app development with Oracle visual builder
MySQL notes 4_ Primary key auto_increment
[2021 book recommendation] red hat rhcsa 8 cert Guide: ex200
Android清除应用缓存
树莓派:双色LED灯实验
winform滚动条美化
【2021年新书推荐】Practical Node-RED Programming
MarkDown基础语法笔记
face_recognition人脸检测
Itop4412 HDMI display (4.0.3_r1)
Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
【2021年新书推荐】Professional Azure SQL Managed Database Administration