Class: Map

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

Instance Method Summary collapse

Constructor Details

#initialize(w, h) ⇒ Map

Returns a new instance of Map.



2
3
4
5
6
# File 'lib/tetris/map.rb', line 2

def initialize(w, h)
  @w = w
  @h = h
  @tiles = Array.new(h) {Array.new(w, nil)}
end

Instance Method Details

#free?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/tetris/map.rb', line 22

def free?(x, y)
  x >= 0 and x < @w and y >= 0 and y < @h and not @tiles[y][x]
end

#set(x, y, colour) ⇒ Object



8
9
10
# File 'lib/tetris/map.rb', line 8

def set(x, y, colour)
  @tiles[y][x] = colour
end

#squaresObject



12
13
14
15
16
17
18
19
20
# File 'lib/tetris/map.rb', line 12

def squares
  Enumerator.new do |yielder|
    @tiles.each_with_index do |line, y|
      line.each_with_index do |colour, x|
        yielder.yield(x, y, colour) if colour
      end
    end
  end
end

#updateObject



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

def update
  points = 0
  shift_lines! {|line| points += 1 if line.all?}
  points
end