当前位置:网站首页>C语言 扫雷
C语言 扫雷
2022-08-08 03:28:00 【TheshyO_o】
目录
头文件
#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 Lei 15//雷的数量
#define zongzuobiao ROW*COL
char ch;
//初始化棋盘
void chushihua(char mine[ROWS][COLS], int rows, int cols,char a);
//打印棋盘
void printboard(char mine[ROWS][COLS], int row, int col);
//布置雷
void setlei(char mine[ROWS][COLS], int row, int col);
//游戏
void saolei(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//各函数分工明确,哪步不明白直接找函数
源文件1
#include "game2.h"
void chushihua(char arr[ROWS][COLS], int rows, int cols,char a)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
{
arr[i][j] = a;
}
}
void printboard(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= row; i++)
printf("%d ", i);
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= row; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
void setlei(char mine[ROWS][COLS], int row, int col)
{
int count = Lei;//雷的数量
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
//if(count==0)
//mine[x][y]=tishi()+'0'
int tishi(char mine[ROWS][COLS],int x,int y)
{
int count = 0;
for (int z = x-1; z <= x + 1; z++)
for (int f = y - 1; f <= y + 1; f++)
{
if (mine[z][f] == '1')
count++;
}
return count;
}
void biaoji(char show[ROWS][COLS], int row, int col)
{
int x, y;
while (1)
{
printf("\n请输入一个坐标->");
while ((ch = getchar()) != '\n');//
scanf("%d %d", &x, &y);
if (x > 0 && y > 0 && x <= row && y <= col)
{
show[x][y] = 'X';
break;
}
else
printf("坐标不合法,请重新输入");
}
}
void saolei(char mine[ROWS][COLS],char show[ROWS][COLS],int row, int col)
{
int x,y,z;
int a = 0;
//int* pa = &a;
while (a < zongzuobiao - Lei)
{
printf("1扫雷,2标记\n");
while ((ch = getchar()) != '\n');
scanf("%d", &z);
if (z == 1)
{
printf("\n请输入一个坐标->");
while ((ch = getchar()) != '\n');
scanf("%d %d", &x, &y);
if (x > 0 && y > 0 && x <= row && y <= col)
{
if (mine[x][y] == '1')
{
printf("您被炸死了\n");
printboard(mine, row, col);
break;
}
else
{
//zhankai(mine,show,row,col,x,y,pa);//
system("cls");
int count = tishi(mine, x, y);
show[x][y] = count + '0';
//printboard(mine, ROW, COL);
printboard(show, row, col);
a++;
}
}
else
printf("坐标不合法,请重新输入\n");
}
else if (z == 2)
{
biaoji(show,row,col);
system("cls");
printboard(show, row, col);
}
else
printf("输入错误,请重新输入\n");
}
if (a == zongzuobiao - Lei)
printf("恭喜您排雷成功!!!\n");
}
源文件2
#include "game2.h"
判断函数
//int main()
//{
// int x, y;
// char map[][3] = { 0 };
// int count = 0;
// for(int z=x-1;z<=x+1;z++)
// for (int f = y - 1; f <= y + 1; f++)
// {
// if (map[z][f] == '1')
// count++;
// }
// return count;
//}
void menu()
{
printf("**************************\n");
printf("******1. 开始游戏******\n");
printf("******2. 退出游戏******\n");
printf("**************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化数组
chushihua(mine, ROWS, COLS,'0');
chushihua(show, ROWS, COLS,'*');
//打印棋盘
//printboard(mine, ROW, COL);
printboard(show, ROW, COL);
//布置雷
setlei(mine,ROW,COL);
//printboard(mine, ROW, COL);
//扫雷
saolei(mine,show,ROW,COL);
}
int main()
{
int input;
srand((unsigned int)time(NULL));
do
{
menu();
printf("是否开始游戏,请输入1/0\n");
scanf("%d", &input);
switch (input)
{
case 1:
system("cls");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择\n");
}
} while (input);
return 0;
}
边栏推荐
- 第4周 一步步搭建多层神经网络以及应用(1 & 2)
- An egg - Nodemailer - qq email verification code development configuration
- 新零售项目及离线数仓核心面试,,220807,,
- The impact of rays on performance in three.js
- 【实战讲解】数据血缘落地实施
- 剑指Offer 18.删除链表的节点
- Hangzhou Electric Multi-School 6 1009. Map
- 妙才周刊
- Several daily LeetCode exercises
- 模拟登录——添加cookies,使用postmanget请求网页数据
猜你喜欢

机器学习笔记 - 基于CNN+OpenCV的图像着色

KDD'22 | CausalMTA: Unbiased Advertising Multi-Touch Attribution Technology Based on Causal Inference

Where to open a futures account with low fees and high returns

find prime problem

PC博物馆(番外01)-城会玩,初中生开发实体尺规大航海游戏

hreg说明备忘

如何验证期货公司开户的正规性

程序中的负数存储及类型转换

Nanny level tutorial!Golang microservices simple architecture in practice

CGAN theory explanation and code implementation
随机推荐
C#《原CSharp》第四回 人常见岁月更替 却难知人文相继
Several daily LeetCode exercises
SQL 2016 如何跟踪是哪个存储或语句导致记录表中记录被修改了
NVIDIA - DeepStream configuration file parsing
Simulate login - add cookies, use postmanget to request web page data
包 package
Redis-事物
新零售项目及离线数仓核心面试,,220807,,
egg-validate-custom validation method error language (error Chinese prompt)
依赖导致原则改善代码
SQL提问!求帮助,急!
11 High Availability Tips You Can't Miss
Solve the problem of word flashback when Endnote inserts references
响应式pbootcms模板健身器械类网站
流程控制语句顺序分支循环结构
典型的数据仓库建模方法论
力扣第 305 场周赛
hreg说明备忘
模拟登录——添加cookies,使用postmanget请求网页数据
The difference between orElse and orElseGet in Optional