当前位置:网站首页>c语言小游戏扫雷
c语言小游戏扫雷
2022-07-29 23:36:00 【某某小卢】
c语言小游戏扫雷
头文件
#pragma once
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define easy_count 10
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void start__board(char board[ROWS][COLS], int rows, int cols, char rew);
void start__qipan(char board[ROWS][COLS], int row, int col);
void start__mine(char mine[ROWS][COLS], int row, int col);
void find__mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
实现个个小功能的源文件
#include"game.h"
void start__board(char board[ROWS][COLS], int rows, int cols, char rew)
{
int i = 0;
int j = 0;
for (int i = 0; i <= rows; i++)
{
for (int j = 0; j <= cols; j++)
{
board[i][j] = rew;
}
}
}
void start__qipan(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (int i = 0; i <= row; i++)
{
printf("%d ", i);
}
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");
}
printf("\n");
}
void start__mine(char mine[ROWS][COLS], int row, int col)
{
int count = 10;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
int count__mine(char mine[ROWS][COLS], int x, int y)
{
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');
}
void find__mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int sum = 0;
int x, y;
int win = 0;
while (win < row * col - easy_count)
{
printf("请输入要排雷的坐标:->");
scanf_s("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
start__qipan(mine, ROW, COL);
break;
}
else
{
int sum = count__mine(mine, x, y);
show[x][y] = sum + '0';
start__qipan(show, ROW, COL);
win++;
}
}
else
{
printf("改坐标已经被排查过了");
}
}
else
{
printf("坐标非法,请重新输入");
}
if (win = row * col - easy_count)
{
printf("恭喜你排雷成功\n");
start__qipan(mine, ROW, COL);
}
}
}
主函数文件
#include"game.h"
void menu()
{
printf("***************************\n");
printf("********* 1.play ********\n");
printf("********* 0.exit ********\n");
printf("***************************\n");
}
void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
start__board(mine,ROWS,COLS,'0');
start__board(show, ROW, COL, '*');
//start__qipan(mine, ROW, COL);
//start__qipan(show, ROW, COL);
start__mine(mine, ROW, COL);
start__qipan(show, ROW, COL);
find__mine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int m;
do
{
menu();
printf("请选择:->\n");
scanf_s("%d", &m);
switch (m)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择:\n");
break;
}
} while (m);
return 0;
}
边栏推荐
- 暴力递归到动态规划 03 (背包问题)
- [leetcode] 82. Delete duplicate elements in sorted linked list II (medium)
- High Numbers|Calculation of Triple Integral 3|Uncle High Numbers|Handwritten Notes
- 容器化数据库必经之道
- 仿牛客论坛项目部署总结
- Foxmail是什么邮箱?
- Allure环境部署与生成+Allure内容增强
- DFS对树的遍历及一些优化
- SQL Server、MySQL主从搭建,EF Core读写分离代码实现
- esp12f + tft 显示图片问题
猜你喜欢
随机推荐
DNA修饰碳纳米管|DNA修饰单层二硫化钼|DNA修饰二硫化钨(注意事项)
go语言(函数、闭包、defer、panic/recover,递归,结构体,json序列化与反序列化)
High Numbers|Calculation of Triple Integral 3|Uncle High Numbers|Handwritten Notes
rk-boot框架实战(1)
JetsonNano learning (5) JetsonNano installs PyTorch and Torchvision
全国双非院校考研信息汇总整理 Part.8
新标杆!美创科技助力广西桂林某三甲医院实现勒索病毒主动防御
Prometheus 的功能特性
MySQL六脉神剑,SQL通关大总结
【2023校招刷题】常见面试问题总结(七、常见总线协议篇)(随后续面试不断更新....)
[2023 School Recruitment Questions] Summary of knowledge points and hand-tear code in the written test and interview
esp12f + tft display picture problem
Elephant Swap:借助ePLATO提供加密市场的套利空间
MySQL事务(transaction) (有这篇就足够了..)
管理区解耦架构见过吗?能帮客户解决大难题的
Single chip ds1302 clock program (51 single chip liquid crystal display program)
【openlayers】地图【一】
Windows 安装 MySQL 5.7详细步骤
在树莓派上安装 PyCharm
C陷阱与缺陷 第4章 链接 4.3 命名冲突与static修饰符