Class: Linotype::Board

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game, args = {}) ⇒ Board

Returns a new instance of Board.



7
8
9
10
# File 'lib/linotype/board.rb', line 7

def initialize(game, args={})
  @game = game
  @tiles = args[:tiles].collect { |row| row.collect { |tile|  Tile.new(self, tile) } }
end

Instance Attribute Details

#gameObject (readonly)

Returns the value of attribute game.



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

def game
  @game
end

#tilesObject

Returns the value of attribute tiles.



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

def tiles
  @tiles
end

Class Method Details

.new_random(game) ⇒ Object



12
13
14
# File 'lib/linotype/board.rb', line 12

def self.new_random(game)
  new(game, new_random_letters)
end

.new_random_letters(rows = 5, columns = 5) ⇒ Object



16
17
18
# File 'lib/linotype/board.rb', line 16

def self.new_random_letters(rows=5, columns=5)
  rows.times.collect { columns.times.collect { ('A'..'Z').to_a[rand(0..25)] } }
end

Instance Method Details

#column(tile) ⇒ Object



36
37
38
# File 'lib/linotype/board.rb', line 36

def column(tile)
  tiles[row(tile)].index(tile)
end

#column_countObject



24
25
26
# File 'lib/linotype/board.rb', line 24

def column_count 
  @column_count ||= tiles.first.count
end

#row(tile) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/linotype/board.rb', line 28

def row(tile)
  row_number = 0
  tiles.each do |row|
    return row_number if row.include?(tile)
    row_number += 1
  end
end

#row_countObject



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

def row_count
  @row_count ||= tiles.count
end