Class: Board

Inherits:
Object
  • Object
show all
Defined in:
lib/board.rb

Constant Summary collapse

SCORE =
{
  Action.Single => 100,
  Action.Double => 300,
  Action.Triple => 500,
  Action.Bettys => 800,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBoard

Returns a new instance of Board.



13
14
15
16
# File 'lib/board.rb', line 13

def initialize
  # Each row must be a separate array instance
  @matrix = Array.new(40) { Array.new(10) }
end

Instance Attribute Details

#matrixObject (readonly)

Returns the value of attribute matrix.



4
5
6
# File 'lib/board.rb', line 4

def matrix
  @matrix
end

Instance Method Details

#absorb(piece) ⇒ Object

Lock a piece into the matrix



26
27
28
29
30
# File 'lib/board.rb', line 26

def absorb(piece)
  piece.niños.each do |niño|
    @matrix[niño.y][niño.x] = COLOR_PAIR[piece.shape]
  end
end

#simplifyObject

Clear completed lines, return [score, count]



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/board.rb', line 33

def simplify
  cleared = []

  # Check visible rows (below skyline)
  ((SKYLINE_INDEX + 1)...40).each do |y|
    cleared << y if @matrix[y].all?
  end

  # Remove cleared rows and add empty rows at top
  cleared.each do |y|
    @matrix.delete_at(y)
    @matrix.unshift(Array.new(10))

    # Adjust remaining indices since we modified the array
    cleared.map! { |i| i >= y ? i : i }
  end

  [SCORE[cleared.size] || 0, cleared.size]
end

#valid?(point) ⇒ Boolean

Check if a point is valid (in bounds and empty)

Returns:

  • (Boolean)


19
20
21
22
23
# File 'lib/board.rb', line 19

def valid?(point)
  point.x.between?(0, 9) &&
    point.y.between?(0, 39) &&
    @matrix[point.y][point.x].nil?
end