Class: Linotype::Tile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_byObject

Returns the value of attribute covered_by.



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

def covered_by
  @covered_by
end

#letterObject (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_tilesObject



62
63
64
# File 'lib/linotype/tile.rb', line 62

def adjacent_tiles
  @adjacent_tiles ||= calculate_adjacent_tiles
end

#boardObject



22
23
24
# File 'lib/linotype/tile.rb', line 22

def board
  @board
end

#calculate_adjacent_tilesObject



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

#columnObject



42
43
44
# File 'lib/linotype/tile.rb', line 42

def column
  @column ||= @board.column(self)
end

#corner?Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

#gameObject



30
31
32
# File 'lib/linotype/tile.rb', line 30

def game
  @board.game
end

#next?(coordinate_type) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


54
55
56
# File 'lib/linotype/tile.rb', line 54

def previous?(coordinate_type)
  send(coordinate_type) > 0
end

#random_letterObject



34
35
36
# File 'lib/linotype/tile.rb', line 34

def random_letter
  ('A'..'Z').to_a[rand(0..25)]
end

#rowObject



38
39
40
# File 'lib/linotype/tile.rb', line 38

def row
  @row ||= @board.row(self)
end

#to_aObject



26
27
28
# File 'lib/linotype/tile.rb', line 26

def to_a
  [row, column]
end

#to_hashObject



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