examples/pending/netris/src/grid.rb
# Look for complete rows, and remove them
Look for complete rows, and remove them
Look for complete rows and remove them
def check_row_removal
    to_remove = []
    @field.each_with_index do |row, idx|
      next if row == @field[-1]
      good = true
      row.each do |col|
        good = false if col.nil?
      end
      to_remove << idx if good
    end
    to_remove.each do |row|
      @field[row].each_index do |col|
        @field[row][col].remove if @field[row][col] != 1
        @field[row][col] = nil
      end
    end
    to_remove.each do |row|
      (1..row).to_a.reverse.each do |r|
        @field[r].length.times do |i|
          if @field[r-1][i].is_a?(Actor)
            @field[r][i] = @field[r-1][i]
            @field[r][i].y += BLOCK_SIZE
            @field[r-1][i] = nil
            @field[r][0] = 1
            @field[r][-1] = 1
          end
        end
      end
    end
    update_game_data(to_remove.length) if to_remove.length > 0
  end
def check_row_removal to_remove = [ ] @field . each_with_index do | row , idx | next if row == @field [ - 1 ] good = true row . each do | col | good = false if col . nil? end to_remove << idx if good end to_remove . each do | row | @field [ row ] . each_index do | col | @field [ row ] [ col ] . remove if @field [ row ] [ col ] != 1 @field [ row ] [ col ] = nil end end to_remove . each do | row | ( 1 .. row ) . to_a . reverse . each do | r | @field [ r ] . length . times do | i | if @field [ r - 1 ] [ i ] . is_a? ( Actor ) @field [ r ] [ i ] = @field [ r - 1 ] [ i ] @field [ r ] [ i ] . y += BLOCK_SIZE @field [ r - 1 ] [ i ] = nil @field [ r ] [ 0 ] = 1 @field [ r ] [ - 1 ] = 1 end end end end update_game_data ( to_remove . length ) if to_remove . length > 0 end
Look for complete rows, and remove them
Look for complete rows and remove them
# First, we find all rows that need removing # Ignore last row # Then we out the rows to remove # And finally move rows above nulled rows down to # collapse the field