Class: Linotype::Tile
- Inherits:
-
Object
- Object
- Linotype::Tile
- Defined in:
- lib/linotype/tile.rb
Instance Attribute Summary collapse
-
#covered_by ⇒ Object
Returns the value of attribute covered_by.
-
#letter ⇒ Object
readonly
Returns the value of attribute letter.
Instance Method Summary collapse
- #adjacent_tiles ⇒ Object
- #board ⇒ Object
- #calculate_adjacent_tiles ⇒ Object
- #column ⇒ Object
- #corner? ⇒ Boolean
- #defended? ⇒ Boolean
- #edge? ⇒ Boolean
- #game ⇒ Object
-
#initialize(board, letter = random_letter) ⇒ Tile
constructor
A new instance of Tile.
- #next?(coordinate_type) ⇒ Boolean
- #previous?(coordinate_type) ⇒ Boolean
- #random_letter ⇒ Object
- #row ⇒ Object
- #to_a ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(board, letter = random_letter) ⇒ Tile
Returns a new instance of Tile.
7 8 9 10 |
# File 'lib/linotype/tile.rb', line 7 def initialize(board, letter=random_letter) @board = board @letter = letter end |
Instance Attribute Details
#covered_by ⇒ Object
Returns the value of attribute covered_by.
4 5 6 |
# File 'lib/linotype/tile.rb', line 4 def covered_by @covered_by end |
#letter ⇒ Object (readonly)
Returns the value of attribute letter.
5 6 7 |
# File 'lib/linotype/tile.rb', line 5 def letter @letter end |
Instance Method Details
#adjacent_tiles ⇒ Object
62 63 64 |
# File 'lib/linotype/tile.rb', line 62 def adjacent_tiles @adjacent_tiles ||= calculate_adjacent_tiles end |
#board ⇒ Object
22 23 24 |
# File 'lib/linotype/tile.rb', line 22 def board @board end |
#calculate_adjacent_tiles ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/linotype/tile.rb', line 70 def calculate_adjacent_tiles @adjacent_tiles = [] @adjacent_tiles << @board.tiles[row - 1][column] if previous?(:row) @adjacent_tiles << @board.tiles[row + 1][column] if next?(:row) @adjacent_tiles << @board.tiles[row][column - 1] if previous?(:column) @adjacent_tiles << @board.tiles[row][column + 1] if next?(:column) @adjacent_tiles end |
#column ⇒ Object
42 43 44 |
# File 'lib/linotype/tile.rb', line 42 def column @column ||= @board.column(self) end |
#corner? ⇒ Boolean
50 51 52 |
# File 'lib/linotype/tile.rb', line 50 def corner? (row == 0 || row == board.row_count - 1) && (column == 0 || column == board.column_count - 1) end |
#defended? ⇒ Boolean
66 67 68 |
# File 'lib/linotype/tile.rb', line 66 def defended? adjacent_tiles.select { |tile| tile.covered_by == covered_by && covered_by }.count == adjacent_tiles.count end |
#edge? ⇒ Boolean
46 47 48 |
# File 'lib/linotype/tile.rb', line 46 def edge? (row == 0 || row == board.row_count - 1) || (column == 0 || column == board.column_count - 1) end |
#game ⇒ Object
30 31 32 |
# File 'lib/linotype/tile.rb', line 30 def game @board.game end |
#next?(coordinate_type) ⇒ Boolean
58 59 60 |
# File 'lib/linotype/tile.rb', line 58 def next?(coordinate_type) send(coordinate_type) < @board.send("#{coordinate_type}_count") - 1 end |
#previous?(coordinate_type) ⇒ Boolean
54 55 56 |
# File 'lib/linotype/tile.rb', line 54 def previous?(coordinate_type) send(coordinate_type) > 0 end |
#random_letter ⇒ Object
34 35 36 |
# File 'lib/linotype/tile.rb', line 34 def random_letter ('A'..'Z').to_a[rand(0..25)] end |
#row ⇒ Object
38 39 40 |
# File 'lib/linotype/tile.rb', line 38 def row @row ||= @board.row(self) end |
#to_a ⇒ Object
26 27 28 |
# File 'lib/linotype/tile.rb', line 26 def to_a [row, column] end |
#to_hash ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/linotype/tile.rb', line 12 def to_hash { letter: @letter, row: row, column: column, covered_by: (game.player_number(covered_by) if covered_by), defended: defended? } end |