class Upwords::MoveManager

Public Class Methods

new(board, dictionary) click to toggle source
# File lib/upwords/move_manager.rb, line 4
def initialize(board, dictionary) 
  @board = board
  @dict = dictionary
  @pending_move = []
  @move_history = [] # TODO: Add filled board spaces as first move if board is not empty
end

Public Instance Methods

add(player, letter, row, col) click to toggle source

Player-Board Interaction Methods


# File lib/upwords/move_manager.rb, line 15
def add(player, letter, row, col)
  if self.include?(row, col) 
    raise IllegalMove, "You can't stack on a space more than once in a single turn!"
  elsif
    @pending_move << player.play_letter(@board, letter, row, col)
  end
end
include?(row, col) click to toggle source
# File lib/upwords/move_manager.rb, line 23
def include?(row, col)
  @pending_move.map {|m| m[0]}.include?([row, col])
end
pending_score(player) click to toggle source
# File lib/upwords/move_manager.rb, line 61
def pending_score(player)
  prev_board = Board.build(@move_history, @board.size, @board.max_height)
  Move.new(@pending_move).score(prev_board, player)
end
pending_words() click to toggle source

TODO: cache previous board in local variable…

# File lib/upwords/move_manager.rb, line 56
def pending_words
  prev_board = Board.build(@move_history, @board.size, @board.max_height)
  Move.new(@pending_move).new_words(prev_board)
end
submit(player) click to toggle source
# File lib/upwords/move_manager.rb, line 41
def submit(player)
  if @pending_move.empty?
    raise IllegalMove, "You haven't played any letters!"
  elsif legal?
    player.score += pending_score(player)
    @move_history << Move.new(@pending_move)
    @pending_move.clear
  end
end
undo_all(player) click to toggle source
# File lib/upwords/move_manager.rb, line 35
def undo_all(player)
  until @pending_move.empty? do
    undo_last(player)
  end
end
undo_last(player) click to toggle source
# File lib/upwords/move_manager.rb, line 27
def undo_last(player)
  if @pending_move.empty?
    raise IllegalMove, "No moves to undo!"
  else
    player.take_from(@board, *@pending_move.pop[0]) # TODO: make Tile class
  end
end