Class: Board
- Inherits:
-
Object
- Object
- Board
- 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
-
#matrix ⇒ Object
readonly
Returns the value of attribute matrix.
Instance Method Summary collapse
-
#absorb(piece) ⇒ Object
Lock a piece into the matrix.
-
#initialize ⇒ Board
constructor
A new instance of Board.
-
#simplify ⇒ Object
Clear completed lines, return [score, count].
-
#valid?(point) ⇒ Boolean
Check if a point is valid (in bounds and empty).
Constructor Details
Instance Attribute Details
#matrix ⇒ Object (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 |
#simplify ⇒ Object
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)
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 |