-
Notifications
You must be signed in to change notification settings - Fork 0
/
about_extra_credit.rb
106 lines (101 loc) · 1.94 KB
/
about_extra_credit.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
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
# EXTRA CREDIT:
#
# Create a program that will play the Greed Game.
# Rules for the game are in GREED_RULES.TXT.
#
# You already have a DiceSet class and score function you can use.
# Write a player class and a Game class to complete the project. This
# is a free form assignment, so approach it however you desire.
# Create Player class and Game class + Refactor code
class DiceSet
def roll(num)
@arr = Array.new
num.times do
@arr << rand(1..6)
end
end
def values
@arr
end
end
dice = DiceSet.new
dice.roll(5)
$arr = dice.values
$points = 0
def get_arr()
acc = Array.new
$arr.each do |item|
if item == 1 or 5
acc << item
elsif $arr.count(item) > 2
acc << item
end
end
return acc #factored array
end
def add_s(num)
$points += num
return $points
end
def calc(val)
if val == 2
add_s(200)
elsif val == 3
add_s(300)
elsif val == 4
add_s(400)
elsif val == 6
add_s(600)
end
end
arx = get_arr
while !arx.empty? do
arx.each do |x|
if x == 1
value = arx.count(x)
arx.delete(x)
if value == 1
add_s(100)
elsif value > 1
if value == 2
add_s(200)
elsif value == 3
add_s(1000)
elsif value == 4
add_s(1100)
elsif value == 5
add_s(1200)
end
end
elsif x == 5
value = arx.count(x)
arx.delete(x)
if value == 1
add_s(50)
elsif value > 1
if value == 2
add_s(100)
elsif value == 3
add_s(500)
elsif value == 4
add_s(550)
elsif value == 5
add_s(600)
end
end
elsif x != 1 and 5
c = arx.count(x)
if c > 2
calc(x)
end
arx.delete(x)
end
end
end
if arx.empty?
p "empty - success!"
else
p "not_empty - failed!"
end
puts "Dice roll: " + $arr.to_s
puts "Score: " + $points.to_s