Skip to content

Instantly share code, notes, and snippets.

@pzula
Created November 25, 2013 01:27
Show Gist options
  • Save pzula/7634871 to your computer and use it in GitHub Desktop.
Save pzula/7634871 to your computer and use it in GitHub Desktop.
Scrabble players
require './lib/scrabble'
class Player
attr_reader :name, :letters
def initialize(name)
@name = name
@store = []
@letters = []
end
def plays(word)
if can_play?(word)
word_score = Scrabble.score(word)
@store << word_score
word.chars.each do |char|
index = @letters.index(char)
@letters.delete_at(index) if index
end
return word_score
else
raise "#{@name} cannot play #{word}, has letters #{@letters}"
end
end
def can_play?(word)
word.chars.all? do |char|
@letters.include?(char)
end
end
def score
@store.reduce(:+)
end
def leading?(other_player)
self.score > other_player.score
end
def add_letter(letter)
@letters << letter
end
def add_letters(letters)
@letters << letters
@letters = @letters.flatten
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment