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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 | # author: Alexander Corey
# file: scrabble.rb
# date: 2019-10-22
# brief: This program calculates a Scrabble word score.
$flag = true
puts "---------------------------\n SCRABBLE SCORE CALCULATOR\n---------------------------"
while $flag
puts "\nPlease enter a word: "
userWord = gets.chomp
$validInput = true
$doubleScore = false
$tripleScore = false
$lastAddedPoint = 0
if userWord.empty?
$validInput = false
$wordScore = 0
$flag = false
puts "\nGoodbye!"
end
def calculateScore(userWord)
i = 0
stringLength = userWord.length
indx = 0
letter = ''
$wordScore = 0
while i < stringLength
letter = userWord[indx,1]
case letter
when "a", "A", "e", "E", "i", "I", "l", "L", "n", "N", "o", "O", "r", "R", "s", "S", "t", "T", "u", "U"
$wordScore += 1
$lastAddedPoint = 1
when "b", "B", "d", "D", "g", "G"
$wordScore += 2
$lastAddedPoint = 2
when "c", "C", "m", "M", "p", "P"
$wordScore += 3
$lastAddedPoint = 3
when "f", "F", "h", "H", "v", "V", "w", "W", "y", "Y"
$wordScore += 4
$lastAddedPoint = 4
when "k", "K"
$wordScore += 5
$lastAddedPoint = 5
when "j", "J", "x", "X"
$wordScore += 8
$lastAddedPoint = 8
when "z", "Z", "q", "Q"
$wordScore += 10
$lastAddedPoint = 10
when "2"
if (indx == 0)
elsif
$wordScore += $lastAddedPoint
else
$validInput = false
$wordScore = 0
puts "Error. Invalid Character."
end
when "3"
if (indx == 0)
elsif
$wordScore += (2 * $lastAddedPoint)
else
$validInput = false
$wordScore = 0
puts "Error. Invalid Character."
end
else
$validInput = false
$wordScore = 0
puts "Error. Please enter one VALID word at a time."
end
i+= 1
indx+= 1
end
end
calculateScore(userWord)
if $validInput
if (userWord.start_with?("2"))
$wordScore *= 2
end
if (userWord.start_with?("3"))
$wordScore *= 3
end
puts "last add: #{ $lastAddedPoint}"
puts "Score for \"#{userWord}\" is: #{ $wordScore}"
end
end
|