【问题标题】:Detect consecutive objects with a common attribute in 2-D array检测二维数组中具有共同属性的连续对象
【发布时间】:2020-06-07 09:16:45
【问题描述】:

我有一个二维宝石数组,所有的宝石都被赋予了随机颜色。现在我想检测一行或一列中三个或更多连续的宝石是否具有相同的颜色。如果是这样,我想对这些宝石采取一些行动。

Gem gems[10][10];

for(int i=0;i<10;++i){
    for(int j=0;j<10;++j){
        gems[i][j].setColor(GetRandInRange(0,6));   
    }
}
bool detectMatch(){
  for(int i=0;i<10;++i){
    for(int j=0;j<10;++j){
        // Code to detect three or more consecutive gems with same color
        // Give some special color to the matched gems
    }
}

这是我尝试的方法,但它不起作用

  bool GameBoard::findMatch(){
    for(int i=0;i<10;++i){
        int count=0;
        for(int j=0;j<10;++j){
            if(j!=0){  
            if(gems[i][j].getColor()==gems[i][j-1].getColor()){ //Color same with previous one
                int a=i, b=j;
                while(gems[a][b].getColor()==gems[i][j].getColor() && (a<10 && b<10)){ // Check till the color does not match
                    count++;
                    a++;
                    b++;
                }
                if(count>=3){  //If 3 or more have matched
                    for(int a=i, b=j, c=0;c<count;++c){
                      gems[a][b].setColor(0);       
                    }
                    return true;
                }
            }
        }
      }
    }
    return false;
}

如果您可以帮助我处理此代码

【问题讨论】:

  • 有几个问题不太清楚:数组限制为10乘10?或者可以更大?如果你连续发现三个连续的宝石颜色相同,并且如果你找到的宝石的上排或下排有宝石,你是否也需要改变这些宝石的颜色?
  • @TigerYu 我希望它改变检测到的第一个匹配的颜色,因为它在主程序的 do while 循环中被调用,直到它返回 false。
  • 并且数组大小限制为 10 x 10

标签: c++ arrays loops


【解决方案1】:

这就是我的做法。您需要进行两次扫描。首先,您需要扫描一个方向的运行,然后是另一个方向。这比尝试循环一次要简单得多。

我首先检查水平运行,一旦找到比 2 长的运行就退出。垂直的也一样。您的函数具有 bool 签名,因此我假设您将使用另一个函数来确定运行的位置 - 您可以轻松地从 findMatch 方法返回一个保存位置、方向和长度的结构。

"use strict";
window.addEventListener('load', onLoaded, false);

var gems;

function onLoaded(evt) {
  // create and initialize the array
  gems = new Array();
  for (var row = 0; row < 10; row++) {
    let curRow = new Array();
    for (var col = 0; col < 10; col++) {
      let curCell = new Gem();
      curCell.setColor(GetRandInRange(0, 6));
      curRow.push(curCell);
    }
    gems.push(curRow);
  }

  // display it for the user
  displayGems();

  // check if there's 3 or more in a vertical or horizontal line.
  console.log(hasMatch());
}

class Gem {
  setColor(colIndex) {
    this.color = colIndex;
  }
  getColor() {
    return this.color;
  }
};

function displayGems() {
  var str = '';
  for (var row = 0; row < 10; row++) {
    if (row != 0)
      str += "\n";

    var dat = gems[row];

    dat.forEach((gem, idx) => {
      if (idx != 0)
        str += ', ';
      str += gem.getColor();
    });
  }
  console.log(str);
}

function GetRandInRange(lower, upper) {
  return ((Math.random() * (upper - lower)) + lower + 0.5) >> 0;
}

function hasMatch() {
  let matchFound = 0;

  // scan #1 - horizontally on each row
  for (var row = 0; row < 10; row++) {
    let last = undefined;
    let runLength = 0;
    for (var col = 0; col < 10; col++) {
      let cur = gems[row][col].getColor();
      if (cur == last) {
        runLength++
        if (runLength > 2) {
          console.log(`horiz - ${row+1}`);
          return true;
        }
      } else {
        runLength = 0;
        last = cur;
      }
    }
  }

  for (var col = 0; col < 10; col++) {
    let last = undefined;
    let runLength = 1;
    for (var row = 0; row < 10; row++) {
      let cur = gems[row][col].getColor();
      if (cur == last) {
        runLength++;
        if (runLength > 2) {
          console.log(`vert - ${col+1}`);
          return true;
        }
      } else {
        runLength = 1;
        last = cur;
      }
    }
  }
  return false;
}

【讨论】:

    猜你喜欢
    • 2014-06-08
    • 1970-01-01
    • 2021-12-05
    • 2023-03-26
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多