Class: Upwords::Word
- Inherits:
-
Object
- Object
- Upwords::Word
- Defined in:
- lib/upwords/word.rb
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#score ⇒ Object
readonly
Returns the value of attribute score.
Class Method Summary collapse
-
.calc_score(board, posns) ⇒ Object
Calculate the score of word on board NOTE: this method assumes that the word has already been played on the board.
- .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.
5 6 7 8 9 10 11 |
# File 'lib/upwords/word.rb', line 5 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)
Returns the value of attribute length.
3 4 5 |
# File 'lib/upwords/word.rb', line 3 def length @length end |
#score ⇒ Object (readonly)
Returns the value of attribute score.
3 4 5 |
# File 'lib/upwords/word.rb', line 3 def score @score end |
Class Method Details
.calc_score(board, posns) ⇒ Object
Calculate the score of word on board NOTE: this method assumes that the word has already been played on the board
A word’s score is calculated as follows:
-
Sum tile heights of all positions in word
-
Multiple score by 2 if all positions in word are only 1 tile high
-
Add two points for all ‘Qu’ tiles in word, if all positions in word are only 1 tile high (somewhat strange rule but whatever)
NOTE: players get a 20 point bonus for using all of their tiles in a move -> this logic is in a separate class
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/upwords/word.rb', line 29 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
46 47 48 |
# File 'lib/upwords/word.rb', line 46 def self.make_string(board, posns) posns.map{|row, col| board.top_letter(row, col)}.join end |
Instance Method Details
#legal?(dict) ⇒ Boolean
17 18 19 |
# File 'lib/upwords/word.rb', line 17 def legal?(dict) dict.legal_word?(self.to_s) end |
#to_s ⇒ Object
13 14 15 |
# File 'lib/upwords/word.rb', line 13 def to_s @text.to_s end |