class Upwords::Word

Attributes

length[R]
score[R]

Public Class Methods

calc_score(board, posns) click to toggle source

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

# 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) click to toggle source
# File lib/upwords/word.rb, line 46
def self.make_string(board, posns)
  posns.map{|row, col| board.top_letter(row, col)}.join
end
new(posns, board) click to toggle source
# 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

Public Instance Methods

to_s() click to toggle source
# File lib/upwords/word.rb, line 13
def to_s
  @text.to_s
end