首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扫雷船,我怎么知道有多少0是在彼此旁边?

扫雷船,我怎么知道有多少0是在彼此旁边?
EN

Stack Overflow用户
提问于 2016-12-09 06:27:16
回答 1查看 322关注 0票数 2

我想要做的是能够“翻转”每0是在彼此旁边,就像一般的扫雷游戏。

代码语言:javascript
复制
#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,它恰好是一个0,我想要做的是,不仅能够将这个特定的坐标从X转换成一个空白,还可以把它旁边的所有其他0,就像普通的Minesweeper一样,还有我在西班牙语中使用的变量名,所以让我也键入一个翻译。

  • 布卡米娜-扫雷艇
  • 平板板
  • lqeuv - wtus (用户所看到的)
  • 小型地雷(总地雷)
  • cm (地雷计数器)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-09 08:07:52

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

另外,我强烈建议使用“X”和“”,而不是88和9。把瓷砖放在结构中,可以告诉你里面是什么,显示什么,如果它被挑选出来,以及它有多少附近的地雷将是真正有用的,但这不是问题的重点。

下面是我做的一个修改版本:

代码语言:javascript
复制
#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函数放到一个类中,这样我就可以轻松地访问这两个数组。此外,我已经使两个数组的大小相同,并包括范围警卫。他们的大小不同,这使它真的不必要地难以使用。

如果你不想使用类,你可以像你的buscamina是一样让洪水填充非会员函数,并添加2个参数,传递给lqeuv和tablero的引用。

所以,解决你的问题是在洪水填埋场的功能。您只需调用x和y播放器输入。首先,它会进行边界检查,还会检查瓷砖是否已经被摘走。如果是的话,它就会回来。然后,就像在您的版本中一样,它检查瓷砖是否为0。如果是的话,它会将lqeuv设置为“”,并为四周的所有4个瓷砖调用自己。这使得所有与0连接的瓷砖都会被设置为‘’。然后,如果瓷砖有相邻的地雷(因此tablero != 0),则将lqeuv设置为所述数字。

输入0 0的结果如下所示:

代码语言:javascript
复制
    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 

如果你有任何其他问题,请随便问。另外,如果你想知道如何更好地组织你的计划,我会很高兴的帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41054336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档