Class: Upwords::MoveManager

Inherits:
Object
  • Object
show all
Defined in:
lib/upwords/move_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(board, dictionary) ⇒ MoveManager

Returns a new instance of MoveManager.



4
5
6
7
8
9
# 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

Instance Method Details

#add(player, letter, row, col) ⇒ Object


Player-Board Interaction Methods




14
15
16
17
18
19
20
21
# File 'lib/upwords/move_manager.rb', line 14

def add(player, letter, row, col)
  # TODO: remove the need for @pending_move.map
  if (@pending_move.map {|m| m[0]}).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

#legal?Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/upwords/move_manager.rb', line 58

def legal?
  prev_board = Board.build(@move_history, @board.size, @board.max_height)
  Move.new(@pending_move).legal?(prev_board, @dict, raise_exception = true)
end

#pending_score(player) ⇒ Object



53
54
55
56
# File 'lib/upwords/move_manager.rb', line 53

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_wordsObject

TODO: cache prev board in local variable…



48
49
50
51
# File 'lib/upwords/move_manager.rb', line 48

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) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/upwords/move_manager.rb', line 37

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) ⇒ Object



31
32
33
34
35
# File 'lib/upwords/move_manager.rb', line 31

def undo_all(player)
  until @pending_move.empty? do
    undo_last(player)
  end
end

#undo_last(player) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/upwords/move_manager.rb', line 23

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