当前位置:网站首页>Trial writing C language minesweeper
Trial writing C language minesweeper
2022-08-05 12:36:00 【kongqizyd146】
练习.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "lei.h"
void menu()
{
printf("*************************************\n");
printf("********* 1. play *********\n");
printf("********* 0. exit *********\n");
printf("*************************************\n");
}
void game()
{
char lei[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
init_board(lei, ROWS, COLS, '0');
init_board(show, ROWS, COLS, '*');
display_board(show, ROW, COL);
set_lei(lei, ROW, COL);
find_lei(lei, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择: >");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误, 请重新选择\n");
}
} while (input);
return 0;
}
lei.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "lei.h"
void init_board(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 display_board(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void set_lei(char lei[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (lei[x][y] == '0')
{
lei[x][y] = '1';
count--;
}
}
}
int get_lei_count(char lei[ROWS][COLS], int x, int y)
{
return (lei[x - 1][y] +
lei[x - 1][y - 1] +
lei[x][y - 1] +
lei[x + 1][y - 1] +
lei[x + 1][y] +
lei[x + 1][y + 1] +
lei[x][y + 1] +
lei[x - 1][y + 1] - 8 * '0');
}
void find_lei(char lei[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)
{
if (show[x][y] == 'x')
{
if (lei[x][y] == '1')
{
printf("has been bombed\n");
display_board(lei, ROW, COL);
return 0;
}
else
{
int count = get_lei_count(lei, 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(lei, ROW, COL);
}
}
lei.h
#pragma once
#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 10
//初始化数组
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_lei(char lei[ROWS][COLS], int row, int col);
//排查雷
void find_lei(char lei[ROWS][COLS], char show[ROWS][COLS], int row, int col);
结束.
边栏推荐
- The fourth SQL general command (DML)
- STM32H743IIT6 study notes 03 - using third-party components FreeRTOS
- 江南春:2022年下半年逆势翻盘的22条法则
- 素士科技IPO被终止:年营收18.7亿 顺为小米景林昆仲是股东
- wallys/QCA9882/2×2 MIMO 802.11ac Mini PCIe Wi-Fi Module,
- Dialogue with Zhuang Biaowei: The first lesson of open source
- 尚硅谷-JVM-性能监控与调优篇(P302~P381)
- 一:OpenCV图片读取与写入文件帮助文档
- RK3588+FPGA高速图像处理通信处理机解决方案
- 我和 TiDB 的故事 | 学tidb半年,社区治好了我的精神内耗
猜你喜欢
尚硅谷-JUC篇
华朗复读衔接营励志开营!全名师阵容护航 解读高考成功秘钥
C进阶-动态内存管理
微信开发者工具更换默认用户存储目录方法,将C盘数据User Data迁移到D盘
【Untitled】
CC2530实现按键中断
The fourth SQL general command (DML)
RT-Thread记录(二、RT-Thread内核启动流程 — 启动文件和源码分析)
hello world, hello Keke people
Kuaike Electronics is listed on Shenzhen Stock Exchange: market value of 8.2 billion, annual revenue of 700 million and fundraising of 560 million
随机推荐
Translation of "Grandmaster level in StarCraft II using multi-agent reinforcement learning"
Sentinel introduction and use
RK3588+FPGA high-speed image processing communication processor solution
华朗复读衔接营励志开营!全名师阵容护航 解读高考成功秘钥
2022.08.01_Daily Question
Methods in Reflect
[Share] Code programming habits: In the case of a multi-parameter method, the method needs to be sorted out - that is, the method with the most parameters is the main implementation
The memory problem is difficult to locate, that's because you don't use ASAN
stm32项目之SPI通信2.4G模块(NF2401L)
One: OpenCV image reading and writing file help documentation
hello world, hello Keke people
2022.08.04_Daily Question
STM32H743IIT6 study notes 03 - using third-party components FreeRTOS
Matplotlib 使用指南
AVL树大总结
一次多表关联顺序的慢查询——TiDB 关联特性
RT-Thread记录(二、RT-Thread内核启动流程 — 启动文件和源码分析)
Query optimization (TTFB is too long) left join index does not take effect
LeetCode 623 在二叉树中增加一行[BFS DFS] HERODING的LeetCode之路
OGC WebGIS 常用服务标准(WMS/WMTS/TMS/WFS)速查