-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrabble.rb
70 lines (60 loc) · 1.89 KB
/
scrabble.rb
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
require 'pry'
class Scrabble
attr_accessor :letter, :double, :triple
def initialize(word)
@word = word
end
def self.score(word) # you can invert the keys and value to make a find easier.
letter_scores = {
1 => ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'],
2 => ['D', 'G'],
3 => ['B', 'C', 'M', 'P'],
4 => ['F', 'H', 'V', 'W', 'Y'],
5 => ['K'],
8 => ['J', 'X'],
10 => ['Q', 'Z']
}
broken_word = word.upcase.split ""
total_score = 0
broken_word.each do |letter_in_word|
letter_value = letter_scores.select {|scores, letter| letter.include? letter_in_word }.keys.join.to_i
total_score += letter_value
end
puts "The score for #{ broken_word.join } is #{total_score}!"
end
end
Scrabble.score("cabbage")
# class Scrabble
# attr_accessor :letter
# def initialize(word)
# @word = word
# end
# def self.score(word)
# letter_scores = {
# 1 => ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'],
# 2 => ['D', 'G'],
# 3 => ['B', 'C', 'M', 'P'],
# 4 => ['F', 'H', 'V', 'W', 'Y'],
# 5 => ['K'],
# 8 => ['J', 'X'],
# 10 => ['Q', 'Z']
# }
# broken_word = word.upcase.split ""
# binding.pry
# if broken_word.first.to_i.is_a? Integer && broken_word.last.to_i.is_a? Integer
# word_multiplier = broken_word.first
# broken_word.pop
# broken_word.shift
# elsif broken_word.include? Integer
# letter_multiplier = broken_word.index Integer
# end
# total_score = 0
# broken_word.each do |letter_in_word|
# letter_value = letter_scores.select {|letter_scores, letter| letter.include? letter_in_word }.keys.join.to_i
# total_score += letter_value
# end
# total score * word_muliplier if word_multiplier != nil
# puts "The score for #{ broken_word.join } is #{total_score}!"
# end
# end
# Scrabble.score("3cabbage3")