Class: Upwords::MoveManager
- Inherits:
-
Object
- Object
- Upwords::MoveManager
- Defined in:
- lib/upwords/move_manager.rb
Instance Method Summary collapse
-
#add(player, letter, row, col) ⇒ Object
——————————– Player-Board Interaction Methods ——————————–.
- #include?(row, col) ⇒ Boolean
-
#initialize(board, dictionary) ⇒ MoveManager
constructor
A new instance of MoveManager.
- #legal? ⇒ Boolean
- #pending_score(player) ⇒ Object
-
#pending_words ⇒ Object
TODO: cache previous board in local variable…
- #submit(player) ⇒ Object
- #undo_all(player) ⇒ Object
- #undo_last(player) ⇒ Object
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
15 16 17 18 19 20 21 |
# 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) ⇒ Boolean
23 24 25 |
# File 'lib/upwords/move_manager.rb', line 23 def include?(row, col) @pending_move.map {|m| m[0]}.include?([row, col]) end |
#legal? ⇒ Boolean
66 67 68 69 |
# File 'lib/upwords/move_manager.rb', line 66 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
61 62 63 64 |
# 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 ⇒ Object
TODO: cache previous board in local variable…
56 57 58 59 |
# 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) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# 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) ⇒ Object
35 36 37 38 39 |
# 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) ⇒ Object
27 28 29 30 31 32 33 |
# 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 |