Class: Upwords::Move

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

Instance Method Summary collapse

Constructor Details

#initialize(tiles = []) ⇒ Move

Returns a new instance of Move.



4
5
6
7
# File 'lib/upwords/move.rb', line 4

def initialize(tiles = [])
  @shape = Shape.new(tiles.map {|(row, col), letter| [row, col]})
  @move = tiles.to_h
end

Instance Method Details

#[](row, col) ⇒ Object



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

def [](row, col)
  @move[[row, col]]
end

#can_play_letters?(board, raise_exception = false) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/upwords/move.rb', line 27

def can_play_letters?(board, raise_exception = false)
  @move.all? do |(row, col), letter|
    board.can_play_letter?(letter, row, col, raise_exception)
  end
end

#legal?(board, dict, raise_exception = false) ⇒ Boolean

TODO: Add the following legal move checks:

  • Move is not a simple pluralization? (e.g. Cat -> Cats is NOT a legal move)

Returns:

  • (Boolean)


19
20
21
# File 'lib/upwords/move.rb', line 19

def legal?(board, dict, raise_exception = false)
  legal_shape?(board, raise_exception) && legal_words?(board, dict, raise_exception)
end

Returns:

  • (Boolean)


23
24
25
# File 'lib/upwords/move.rb', line 23

def legal_shape?(board, raise_exception = false)
  @shape.legal?(board, raise_exception)
end

TODO: Add the following legal move checks:

  • Move is not a simple pluralization? (e.g. Cat -> Cats is NOT a legal move)

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/upwords/move.rb', line 35

def legal_words?(board, dict, raise_exception = false)

  if can_play_letters?(board, raise_exception)    
    bad_words = self.new_illegal_words(board, dict)
    if bad_words.empty?
      return true
    else
      raise IllegalMove, "#{bad_words.join(', ')} #{bad_words.size==1 ? 'is not a legal word' : 'are not legal words'}!" if raise_exception
    end
  end

  return false
end

#new_illegal_words(board, dict) ⇒ Object



93
94
95
# File 'lib/upwords/move.rb', line 93

def new_illegal_words(board, dict)
  new_words(board).reject {|word| dict.legal_word?(word.to_s)}
end

#new_words(board) ⇒ Object

TODO: handle exceptions when board cannot be updated with new move TODO: move score and new word methods to board class?



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/upwords/move.rb', line 78

def new_words(board)
  # HACK: update board with new move
  words = (board.play_move(self).word_positions).select do |word_posns|
    word_posns.any? {|row, col| position?(row, col)}
    
  end.map do |word_posns|
    Word.new(word_posns, board)
  end

  # HACK: remove move from board
  remove_from(board)

  words
end

#play(board) ⇒ Object



57
58
59
60
61
62
# File 'lib/upwords/move.rb', line 57

def play(board)
  @move.reduce(board) do |b, (posn, letter)| 
    b.play_letter(letter, *posn)
    b
  end
end

#position?(row, col) ⇒ Boolean

Returns:

  • (Boolean)


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

def position?(row, col)
  @move.key?([row, col])
end

#remove_from(board) ⇒ Object

TODO: consider move main subroutine to Shape class?



65
66
67
68
69
70
71
72
73
74
# File 'lib/upwords/move.rb', line 65

def remove_from(board)
  if @move.any? {|(row, col), letter| board.top_letter(row, col) != letter}
    raise IllegalMove, "Move does not exist on board and therefore cannot be removed!"
  else
    (@move.each_key).reduce(board) do |b, posn| 
      b.remove_top_letter(*posn)
      b
    end
  end
end

#score(board, player) ⇒ Object

TODO: remove dict from word class TODO: move score and new word methods to board class?



11
12
13
14
15
# File 'lib/upwords/move.rb', line 11

def score(board, player)
  new_words(board).reduce(player.rack_capacity == @move.size ? 20 : 0) do |total, word|
    total += word.score
  end
end