Sha256: 2436de453cf4f99cd5b34a22672b091ea49b4f160e185a6bfaf463be6db3df66
Contents?: true
Size: 1.5 KB
Versions: 1
Compression:
Stored size: 1.5 KB
Contents
module Linotype class Tile attr_accessor :covered_by attr_reader :letter def initialize(board, letter=random_letter) @board = board @letter = letter end def to_hash { letter: @letter, row: row, column: column, covered_by: (game.player_number(covered_by) if covered_by), defended: defended? } end def to_a [row, column] end def game @board.game end def random_letter ('A'..'Z').to_a[rand(0..25)] end def row @row ||= @board.row(self) end def column @column ||= @board.column(self) end def previous?(coordinate_type) send(coordinate_type) > 0 end def next?(coordinate_type) send(coordinate_type) < @board.send("#{coordinate_type}_count") - 1 end def adjacent_tiles @adjacent_tiles ||= calculate_adjacent_tiles end def defended? adjacent_tiles.select { |tile| tile.covered_by == covered_by && covered_by }.count == adjacent_tiles.count end 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 end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
linotype-0.0.3 | lib/linotype/tile.rb |