javascript - 将 ruby​​ 方法移植到 javascript

标签 javascript ruby arrays

我正在尝试将一些 ruby​​ 代码移植到 javascript,但在某一特定行上遇到了困难

Ruby 代码如下: 它会删除俄罗斯方 block 游戏中的所有完整行:

  # removes all filled rows and replaces them with empty ones, dropping all rows
  # above them down each time a row is removed and increasing the score.  
  def remove_filled
    (2..(@grid.size-1)).each{|num| row = @grid.slice(num);
      # see if this row is full (has no nil)
      if @grid[num].all?
        # remove from canvas blocks in full row
        (0..(num_columns-1)).each{|index|
          @grid[num][index].remove;
          @grid[num][index] = nil
        }
        # move down all rows above and move their blocks on the canvas
        ((@grid.size - num + 1)..(@grid.size)).each{|num2|
          @grid[@grid.size - num2].each{|rect| rect && rect.move(0, block_size)};

          # I can't port this line
          @grid[@grid.size-num2+1] = Array.new(@grid[@grid.size - num2])
        }
        # insert new blank row at top
        @grid[0] = Array.new(num_columns);
        # adjust score for full flow
        @score += 10;
      end
    }
    self
  end

其中@grid是一个二维数组,初始化如下:

@grid = Array.new(num_rows) {Array.new(num_columns)}

我到目前为止所做的javascript如下

我在评论中指出了我无法解决的问题

removeFilled() {
            for (var i = 2; i < this.grid.length; i++) {
                var row = this.grid.slice(i);

                var allFull = true;

                for (var g = 0; g < this.grid[i].length; g++ ) {
                    if (this.grid[i][g] == undefined) {
                        allFull = false;
                        break;
                    }
                }

                if (allFull) {
                    for (var j = 0; j < this.numColumns; j++) {
                        this.grid[i][j].remove();
                        this.grid[i][j] = undefined;
                    }

                    for (var k = this.grid.length - i + 1; k <= this.grid.length; k++) {

                        var rects = this.grid[this.grid.length - k];

                        for(var l = 0; l < rects.length; l++) {
                            var rect  = rects[l];
                            if (rect) {
                                rect.move(0, this.blockSize);
                            }
                        }

                        // ***this is the line I can't port
                        this.grid[this.grid.length - k + 1] = new Array(this.grid[this.grid.length - k]);
                    }

                    this.grid[0] =  new Array(this.numColumns);
                    this.score += 10;
                }
            }
        }

有什么想法如何移植有问题的线路吗?

最佳答案

如果我理解正确的话,您希望将数组放在给定位置并将其向前复制一个位置。

你可以这样做:

  this.grid[this.grid.length - k + 1] =  this.grid[this.grid.length - k].slice(0);

关于javascript - 将 ruby​​ 方法移植到 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17478536/

相关文章:

javascript - 使用 javascript 和 getElementsByClassName 检索显示属性

ruby - 从代码中的 require 行中查找 gem

php - 将php循环数据添加到js数组

javascript - 请帮我解决有关日期和功能的问题

javascript - 将输入限制为数字和 。在输入字段

javascript - 单击表格时未选择正确的表格单元格

ruby-on-rails - Rails 得到两个日期之间的差异

ruby-on-rails - Rails 与非 Rails 数据库设计

java - 给我一个数组越界错误,我不知道如何修复它

javascript - 如何在 JavaScript 中合并和分布第一个数组到第二个数组?