forked from Ada-C6/Scrabble
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.rb
More file actions
83 lines (69 loc) · 1.79 KB
/
player.rb
File metadata and controls
83 lines (69 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require_relative 'scoring'
require_relative 'tilebag'
module Scrabble
class Player
attr_reader :name, :plays, :total_score
attr_accessor :players_hand
def initialize(name)
@name = name
@plays = []
@total_score = 0
@players_hand = []
@tile_bag = Scrabble::TileBag.new
end
def word_to_array(word_played)
word_played.upcase.split(//)
end
def letters_in_players_hand?(word_played)
check_hand = @players_hand.clone
word_played_array = word_to_array(word_played)
word_played_array.each do | letter |
if check_hand.include?(letter)
check_hand.slice!(check_hand.index(letter))
else
return false
end
end
word_played_array.each do | letter |
@players_hand.slice!(@players_hand.index(letter))
end
return true
end
def play(word)
@plays << word
# if Returns false if player has already won
word_score = Scrabble::Scoring.score (word)
@total_score += word_score
# Returns the score of the word
if won?
return false
else
return word_score
end
end
def won?
@total_score > 100
end
def highest_scoring_word
Scrabble::Scoring.highest_score_from(@plays)
end
def highest_word_score
highest_word = highest_scoring_word
Scrabble::Scoring.score(highest_word)
end
def tiles
return @players_hand
end
def players_hand_incomplete?
@players_hand.length < 7
end
def draw_tiles
if players_hand_incomplete?
number_of_tiles_to_draw = (7 - @players_hand.length)
new_tiles = @tile_bag.draw_tiles(number_of_tiles_to_draw)
@players_hand += new_tiles
end
return @players_hand
end
end
end