gpt4 book ai didi

c++ - 扫雷,我怎么知道有多少个0挨着?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:11:18 27 4
gpt4 key购买 nike

我想做的是能够“转动”每个相邻的 0,就像普通的扫雷游戏一样。

#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <iostream>

int buscamina();
using namespace std;

int main() {
buscamina();
return 0;
}


int buscamina(){
srand(time(NULL));

int size=12, minastot=10;
int tablero[size][size];
char lqeuv[size-2][size-2];
int x, y, cm=0;

for(int i=0; i<size-2; i++)
for(int j=0; j<size-2; j++)
lqeuv[i][j]=88;

for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
tablero[i][j]=0;

for(int i=0; i<minastot; i++){
int a=0, b=0;

a=rand()%(size-2);
b=rand()%(size-2);
++a; ++b;

if(tablero[a][b]==9)
minastot++;

tablero[a][b]=9;
}

for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
if(tablero[i][j]==9)
for(int a=i-1; a<i+2; a++)
for(int b=j-1; b<j+2; b++)
if(tablero[a][b]!=9)
tablero[a][b]++;

for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
if(tablero[i][j]==9)
++cm;

do{
cout << endl;
cout << setw(5);
for(int i=0; i<size-2; i++)
cout << i << " ";
cout << endl << endl;
for(int i=0; i<size-2; i++){
cout << i << setw(4);
for(int j=0; j<size-2; j++)
cout << lqeuv[i][j] << " ";
cout << endl;
}

do {
cout << "Coordenadas: ";
} while(scanf("%d %d", &x, &y)!=2);

if(tablero[x+1][y+1]==0)
lqeuv[x][y]=32;
else
lqeuv[x][y]=(tablero[x+1][y+1]+48);

}while (tablero[x+1][y+1]!=9);

for(int i=0; i<size; i++){
for(int j=0; j<size; j++)
cout << tablero[i][j] << " ";
cout << endl;
}
return 0;
}

假设用户输入坐标 0 2,它恰好是零,我想要做的是不仅能够将该特定坐标从 X 更改为空白,而且还能够将所有其他坐标更改为空白旁边是 0,就像常规扫雷一样,我使用的变量名称也是西类牙语,所以让我也输入翻译。

  • buscamina - 扫雷
  • table 板
  • lqeuv - wtus(用户看到的)
  • minastot - totmines(矿山总数)
  • cm - mc(地雷计数器)

最佳答案

为此,您可以使用洪水填充算法。从玩家选择的位置开始它,然后洪水填充它周围所有也有零的瓷砖。

此外,我强烈建议使用“X”和“”,而不是 88 和 9。将图 block 放入结构中,可以告诉您里面有什么、显示的是什么、是否被采摘以及有多少相邻地雷它真的很有用,但这不是这个问题的重点。

所以这是我制作的修改版本:

#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <iostream>

using namespace std;

template <size_t size>
class Buscamina {
public:
Buscamina() : minastot(3) {}

void run() {
srand(time(NULL));
int x, y, cm=0;

// Fill draw with X
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
lqeuv[i][j]= 'X';

// Fill mines with empty
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
tablero[i][j]=0;

// Generate mines
for(int i=0; i<minastot; i++){
int a=0, b=0;

a=rand()%(size-2);
b=rand()%(size-2);
++a; ++b;

if(tablero[a][b]==9)
minastot++;

tablero[a][b]=9;
}

// Set count of surrounding mines
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
if(tablero[i][j]==9)
for(int a=i-1; a<=i+1; a++)
for(int b=j-1; b<=j+1; b++)
if(tablero[a][b]!=9)
tablero[a][b]++;

// Set total mines
for(int i=0; i<size; i++)
for(int j=0; j<size; j++)
if(tablero[i][j]==9)
++cm;

// Main loop
do{
// Print table
cout << endl;
cout << setw(5);
for(int i=0; i<size; i++)
cout << i << " ";
cout << endl << endl;
for(int i=0; i<size; i++){
cout << i << setw(4);
for(int j=0; j<size; j++)
cout << lqeuv[i][j] << " ";
cout << endl;
}

// Get input
do {
cout << "Coordenadas: ";
} while(scanf("%d %d", &x, &y)!=2);

// Pick a mine
floodfill(x, y);

}while (tablero[x][y]!=9);

for(int i=0; i<size; i++){
for(int j=0; j<size; j++)
cout << tablero[i][j] << " ";
cout << endl;
}
}

void floodfill(int x, int y) {
if (x < 0 || y < 0 || x >= size || y >= size || lqeuv[x][y] != 'X')
return;
if (tablero[x][y] == 0) {
lqeuv[x][y] = ' ';
floodfill(x, y - 1);
floodfill(x - 1, y);
floodfill(x + 1, y);
floodfill(x, y + 1);
} else {
lqeuv[x][y]=(tablero[x][y]+48);
}

}

int minastot;
int tablero[size][size];
char lqeuv[size][size];
};

int main() {
Buscamina<10> game;
game.run();
return 0;
}

我已将整个 buscamina 函数放入一个类中,这样我就可以轻松访问这 2 个数组。此外,我还使两个阵列大小相同,并包括范围保护装置。它们的大小不同,使用起来真的很困难。

如果你不想使用类,你总是可以像你的 buscamina 一样创建 floodfill 非成员函数,并添加 2 个参数,传递对 lqeuv 和 tablero 的引用。

因此,您的问题的解决方案是使用填充功能。您只需在输入的 x 和 y 玩家上调用它。首先,它会进行边界检查,并检查该图 block 是否尚未被选中。如果是,它只是返回。然后就像在您的版本中一样,它检查图 block 是否为 0。如果是,它将 lqeuv 设置为“”并为所有 4 个周围的图 block 调用自身。这使得所有具有 0 的连接图 block 都将设置为“”。然后如果瓷砖有一个相邻的地雷(因此 tablero != 0)它设置 lqeuv 为所述数字。
输入 0 0 的结果如下所示:

    0 1 2 3 4 5 6 7 8 9 

0
1 1 1 2 1 1
2 1 X X X 1
3 1 X X X 1
4 1 X 1
5 1 X 1
6 1 1 1
7
8
9

如果您有任何其他问题,请随时提出。此外,如果您需要一些关于如何更好地组织您的计划的提示,我将很乐意提供帮助。

关于c++ - 扫雷,我怎么知道有多少个0挨着?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41054336/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com