扫雷小游戏
作者:互联网
此代码在课堂老师所讲述的基础之上添加了搜寻周围是否有雷的递归算法。
#include"function.h"
int main()
{
int loop = 1;
while (loop) {
int choose = Menu();
switch (choose) {
case 1:Game();
break;
case 2:printf("欢迎下次再玩,再见。\n");
loop = 0;
break;
case 3:printf("你要个锤子的说明,自己看!!!\n");
default:printf("请输入您的选项(1或者2):\n");
break;
}
}
return 0;
}
#include"function.h"
void Game()
{
char data[ROW][COL] = { 0 };
int count_ = (ROW - 2) * (COL - 2) - NUM;
int count = 0;
int x = 0, y = 0;
memset(data, ' ', ROW * COL);
Mine(data, NUM);//埋一定数量的雷
while (1) {
system("cls");
Print(data);
printf("已找出%d个安全区,", count);
printf("剩余%d个安全区待搜寻\n", (COL - 2) * (ROW - 2) - count - NUM);
//PrintOver(data);
count=0;
printf("请输入你要清理的坐标:");
scanf("%d %d", &x, &y);
if (!(x >= 1 && x <= (COL - 2) && y >= 1 && y <= (ROW - 2))) {
continue;
}
if (data[y][x]=='*') {
printf("你输了\n");
PrintOver(data);
break;
}
Find(data, y, x);
for (int i = 1; i < COL - 1; i++) {
for (int j = 1; j < ROW; j++) {
if (data[i][j] == '#' ||
(data[i][j]>'0'&&data[i][j]<'9')) {
count++;
}
}
}
if (count == count_) {
printf("恭喜您通关了!\n");
break;
}
}
}
void Mine(char data[ROW][COL], int num)
{
srand(time(NULL));
while (num) {
int x = rand() % (ROW - 2) + 1;
int y = rand() % (COL - 2) + 1;
if (data[y][x] != '*') {
data[y][x] = '*';
//printf("(%d,%d),", x, y);
num--;
}
}
}
void Print(char data[ROW][COL])
{
printf("\n");
printf(" 0 ");
for (int i = 1; i <= ROW - 2; i++) {
printf("%2d ", i);
}
printf(" X ");
for (int i = 1; i < ROW - 1; i++) {
printf("\n%2d ", i);
for (int j = 1; j < COL - 1; j++) {
if (data[i][j] != '*') {
printf(" %c ", data[i][j]);
}
else {
printf(" ");
}
}
printf("%2d ", i);
}
printf("\n Y ");
for (int i = 1; i <= ROW - 2; i++) {
printf("%2d ", i);
}
printf("\n");
}
int Menu()
{
static int a = 0;
system("cls");
printf("\n");
printf("====================================\n");
printf("=== 欢迎进入扫雷游戏! ===\n");
printf("=== 1、开始游戏 ===\n");
printf("=== 2、下次再玩 ===\n");
printf("=== 3、游戏说明 ===\n");
printf("=== 请输入您的选项 ... ===\n");
printf("====================================\n");
int choose = 0;
scanf("%d", &choose);
if (choose > 0 && choose < 4) {
return choose;
}
return Menu();
}
int Find(char data[ROW][COL], int x, int y)
{
char num = '0';
if (data[x - 1][y - 1] == '*') {
num++;
}
if (data[x - 1][y] == '*') {
num++;
}
if (data[x - 1][y + 1] == '*') {
num++;
}
if (data[x][y + 1] == '*') {
num++;
}
if (data[x + 1][y + 1] == '*') {
num++;
}
if (data[x + 1][y] == '*') {
num++;
}
if (data[x + 1][y - 1] == '*') {
num++;
}
if (data[x][y - 1] == '*') {
num++;
}
if (num == '0') {
data[x][y] = '#';
if (data[x-1][y-1]!='#'&&
(data[x - 1][y - 1] != '*')&&
(x-1>= 1 && x-1 <= (COL - 2) &&
y-1 >= 1 && y-1 <= (ROW - 2))) {
Find(data, x - 1, y - 1);
}
if ((data[x - 1][y] != '#')&&
(data[x - 1][y] != '*')&&
(x-1 >= 1 && x-1 <= (COL - 2) &&
y >= 1 && y <= (ROW - 2))) {
Find(data, x - 1, y);
}
if ((data[x - 1][y + 1] != '#')&&
(data[x - 1][y + 1] != '*')&&
(x-1 >= 1 && x-1 <= (COL - 2) &&
y+1 >= 1 && y+1 <= (ROW - 2))){
Find(data, x - 1, y + 1);
}
if ((data[x][y + 1] != '#')&&
(data[x][y + 1] != '*') &&
(x >= 1 && x <= (COL - 2) &&
y+1 >= 1 && y+1 <= (ROW - 2))) {
Find(data, x, y + 1);
}
if ((data[x + 1][y + 1] != '#') &&
(data[x+1][y + 1] != '*') &&
(x+1 >= 1 && x+1 <= (COL - 2) &&
y + 1 >= 1 && y + 1 <= (ROW - 2))) {
Find(data, x + 1, y + 1);
}
if ((data[x + 1][y] != '#') &&
(data[x+1][y] != '*') &&
(x+1 >= 1 && x+1 <= (COL - 2) &&
y >= 1 && y <= (ROW - 2))) {
Find(data, x + 1, y);
}
if ((data[x + 1][y - 1] != '#') &&
(data[x+1][y - 1] != '*') &&
(x+1 >= 1 && x+1 <= (COL - 2) &&
y - 1 >= 1 && y - 1 <= (ROW - 2))) {
Find(data, x + 1, y - 1);
}
if ((data[x][y - 1] != '#') &&
(data[x][y - 1] != '*') &&
(x >= 1 && x <= (COL - 2) &&
y - 1 >= 1 && y - 1 <= (ROW - 2))) {
Find(data, x, y - 1);
}
return;
}
else {
data[x][y] = num;
//count++;
}
}
void PrintOver(char data[ROW][COL])
{
printf("\n");
printf(" 0 ");
for (int i = 1; i <= ROW - 2; i++) {
printf("%2d ", i);
}
printf(" X ");
for (int i = 1; i < ROW - 1; i++) {
printf("\n%2d ", i);
for (int j = 1; j < COL - 1; j++) {
if (data[i][j] == '*') {
printf(" * ", data[i][j]);
}
else {
printf("%2c ", data[i][j]);
}
}
printf("%2d ", i);
}
printf("\n Y ");
for (int i = 1; i <= ROW - 2; i++) {
printf("%2d ", i);
}
printf("\n");
}
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<Windows.h>
#include<WinBase.h>
#define ROW 16
#define COL 16
#define NUM 30
void Mine(char data[ROW][COL], int num);
void Print(char data[ROW][COL]);
int Menu();
int Find(char data[ROW][COL], int x, int y);
void PrintOver(char data[ROW][COL]);
void Game();
标签:int,num,扫雷,&&,小游戏,data,COL,ROW 来源: https://blog.csdn.net/weixin_50394543/article/details/122269292