class Upwords::Shape

Attributes

positions[R]

Public Class Methods

new(positions = []) click to toggle source
# File lib/upwords/shape.rb, line 9
def initialize(positions = [])
  @positions = Set.new
  positions.reduce(self) {|shape, (row, col)| shape.add(row, col)}
end

Public Instance Methods

<<(row, col)
Alias for: add
add(row, col) click to toggle source
# File lib/upwords/shape.rb, line 83
def add(row, col)
  if row.is_a?(Integer) && col.is_a?(Integer)
    @positions.add [row, col]
    self
  else
    raise ArgumentError, "[#{row}, #{col}] is not a valid position]"
  end
end
Also aliased as: <<
col_range() click to toggle source
# File lib/upwords/shape.rb, line 79
def col_range
  Range.new(*@positions.map {|row, col| col}.minmax)
end
covering_moves?(board) click to toggle source

Check if move shape completely covers any existing word on the board

# File lib/upwords/shape.rb, line 37
def covering_moves?(board)
  (board.word_positions).any? do |word_posns|
    positions >= word_posns
  end
end
empty?() click to toggle source
# File lib/upwords/shape.rb, line 94
def empty?
  @positions.empty?
end
gaps_covered_by?(board) click to toggle source

Check if all empty spaces in the rows and columns spanned by the move shape are covered by a previously-played tile on board For example, if the move shape = [1,1] [1,2] [1,4], then this method returns 'true' if the board has a tile at position [1,3] and 'false' if it does not.

# File lib/upwords/shape.rb, line 46
def gaps_covered_by?(board)
  row_range.all? do |row|
    col_range.all? do |col|
      @positions.include?([row, col]) || board.nonempty_space?(row, col)
    end
  end
end
in_middle_square?(board) click to toggle source

Check if at least one position within the move shape is within the middle 2x2 square on the board This check is only performed at the beginning of the game

# File lib/upwords/shape.rb, line 65
def in_middle_square?(board)
  board.middle_square.any? do |posn|
    @positions.include?(posn)
  end
end
include?(row, col) click to toggle source
# File lib/upwords/shape.rb, line 102
def include? (row, col)
  @positions.include? [row, col]
end
row_range() click to toggle source
# File lib/upwords/shape.rb, line 75
def row_range
  Range.new(*@positions.map {|row, col| row}.minmax)
end
size() click to toggle source
# File lib/upwords/shape.rb, line 98
def size
  @positions.size
end
straight_line?() click to toggle source
# File lib/upwords/shape.rb, line 71
def straight_line?
  row_range.size == 1 || col_range.size == 1
end
touching?(board) click to toggle source

Check if at least one position within the move shape is adjacent to or overlapping any tile on the board

# File lib/upwords/shape.rb, line 55
def touching?(board)
  @positions.any? do |row, col|
    [[0, 0], [1, 0], [-1, 0], [0, 1], [0, -1]].any? do |dr, dc|
      board.nonempty_space?(row + dr, col + dc)
    end
  end
end