Created
November 25, 2013 01:27
-
-
Save pzula/7634871 to your computer and use it in GitHub Desktop.
Scrabble players
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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