Class: Upwords::Word
- Inherits:
-
Object
- Object
- Upwords::Word
- Defined in:
- lib/upwords/word.rb
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
MIN_WORD_LENGTH = 2.
-
#score ⇒ Object
readonly
MIN_WORD_LENGTH = 2.
Class Method Summary collapse
-
.calc_score(board, posns) ⇒ Object
A word’s score is the sum of the tile heights of its letters However, if all of a word’s tile heights are exactly 1, then the score is double the word’s length also add two points for each ‘Qu’ if all words are 1 tile high Add 20 points if a player uses all their letters to form a word (this logic is in the MoveManager class, because it requires the player as an input.
- .make_string(board, posns) ⇒ Object
Instance Method Summary collapse
-
#initialize(posns, board) ⇒ Word
constructor
A new instance of Word.
- #legal?(dict) ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(posns, board) ⇒ Word
Returns a new instance of Word.
8 9 10 11 12 13 |
# File 'lib/upwords/word.rb', line 8 def initialize(posns, board) posns = posns.uniq if posns.is_a?(Array) @text = Word.make_string(board, posns) @score = Word.calc_score(board, posns) @length = @text.length end |
Instance Attribute Details
#length ⇒ Object (readonly)
MIN_WORD_LENGTH = 2
6 7 8 |
# File 'lib/upwords/word.rb', line 6 def length @length end |
#score ⇒ Object (readonly)
MIN_WORD_LENGTH = 2
6 7 8 |
# File 'lib/upwords/word.rb', line 6 def score @score end |
Class Method Details
.calc_score(board, posns) ⇒ Object
A word’s score is the sum of the tile heights of its letters However, if all of a word’s tile heights are exactly 1, then the score is double the word’s length
also add two points for each 'Qu' if all words are 1 tile high
Add 20 points if a player uses all their letters to form a word (this logic is in the MoveManager class,
because it requires the player as an input
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/upwords/word.rb', line 28 def self.calc_score(board, posns) stack_heights = posns.map{|row, col| board.stack_height(row, col)} score = stack_heights.inject(0) {|sum, h| sum + h} # Double score if all letters are only 1 tile high if stack_heights.all? {|h| h == 1} score *= 2 # Add two points for each Qu (only all letters 1 tile high) score += (2 * posns.count {|posn| board.top_letter(*posn) == 'Qu'}) end # TODO: Add 20 points if a player uses all of their entire rack in one turn. 7 is the maximum rack capacity score end |
.make_string(board, posns) ⇒ Object
45 46 47 |
# File 'lib/upwords/word.rb', line 45 def self.make_string(board, posns) posns.map{|row, col| board.top_letter(row, col)}.join end |
Instance Method Details
#legal?(dict) ⇒ Boolean
19 20 21 |
# File 'lib/upwords/word.rb', line 19 def legal?(dict) dict.legal_word?(self.to_s) end |
#to_s ⇒ Object
15 16 17 |
# File 'lib/upwords/word.rb', line 15 def to_s @text.to_s end |