-
Notifications
You must be signed in to change notification settings - Fork 0
/
yahtzee.rb
228 lines (199 loc) · 8.52 KB
/
yahtzee.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
=begin
Yahtzee Calculator
Program by Dylan McClain, June 2018
Current version September 2018
Please see ReadMe for an explanation of the calculator and
notes on future revisions.
Happy Rolling!
=end
require_relative 'yahtzee-calculations'
puts %q{Welcome to Yahtzee Calculator
Enter your roll by just listing the numbers on the dice.
Type 'exit' to exit.
Happy rolling!}
class Game
attr_accessor :dice, :roll_count, :multiples
def initialize(dice, roll_count, multiples)
@dice = dice
@roll_count = roll_count
@multiples = multiples
end
def start_game
puts "\n~~~NEW TURN~~~"
puts "What did you roll?"
get_dice
puts "How many rolls do you have left?"
get_roll
organize_dice
parse_dice_and_check
end
def get_dice
input = gets.chomp
if input == 'exit'
puts 'bye!'
exit
else
unless input.length == 5 && input.to_i.digits.all? {|x| x.between?(1,6)}
puts "Hmmm...that can't be right"
puts "What did you really roll?"
get_dice
end
end
sort_dice(input)
end
def sort_dice(input)
@dice = input.to_i.digits.sort
end
def get_roll
input = gets.chomp
if input == 'exit'
puts 'bye!'
exit
else
@roll_count = input.to_i
unless @roll_count.between?(1,2)
puts "Well, that's not right.\nIt's either 1 or 2. Which is it?"
get_roll
end
end
end
# Method to create two arrays, one for the numbers the user rolled
# and one for the amount of times user rolled each number
# then map these arrays into the hash @multiples.
def organize_dice
unique_numbers = @dice.uniq.each
occurrences = []
@dice.uniq.each { |n| occurrences << @dice.count(n)}
@multiples = unique_numbers.zip(occurrences).to_h
end
# Method to parse the hash @multiples and then call the methods that check for
# all the possible scenarios.
def parse_dice_and_check
singletons = @multiples.map { |k,v| v==1 ? k : nil }.compact
pairs = @multiples.map { |k,v| v==2 ? k : nil }.compact
triples = @multiples.map { |k,v| v==3 ? k : nil }.compact
quadruples = @multiples.map { |k,v| v==4 ? k : nil }.compact
yahtzees = @multiples.map { |k,v| v ==5 ? k : nil }.compact
check_full_house(singletons, pairs, triples)
check_yahtzee(singletons, pairs, triples, quadruples, yahtzees)
check_straight
end
# Method to calculate probability of full house
# SCENARIOS ARE: three of a kind or two pair
def check_full_house(singletons, pairs, triples)
if triples.any? && pairs.empty?
chance = (((1.0/6.0) + (5.0/36.0)*(@roll_count-1))*100).truncate(2)
puts "If you save your #{triples[0]}s along with your #{singletons.join(' or your ')}, "\
"your chance of rolling a full house is #{chance}%"
elsif pairs.length == 2
chance = (((1.0/3.0) + (10.0/36.0)*(@roll_count-1))*100).truncate(2)
puts "If you save your two pairs your chance of rolling a full house is #{chance}%"
elsif pairs.any? && triples.any?
puts "You already have a full house!"
end
end
# Method to calculate probability of yahtzee
def check_yahtzee(singletons, pairs, triples, quadruples, yahtzees)
if pairs.any? && !triples.any?
pairs.each { |pair| puts "You have a pair of " + pair.to_s + "s" }
puts "Your chance of rolling three of a kind is #{pairs_check(@roll_count)[0]}%"
puts "Your chance of rolling four of a kind is #{pairs_check(@roll_count)[1]}%"
puts "Your chance of rolling YAHTZEE is #{pairs_check(@roll_count)[2]}%"
end
if triples.any?
puts "You already have three of a kind!"
puts "Your chance of rolling four of a kind with #{triples[0]}s is #{triples_check(@roll_count)[0]}% "\
"and your chance of rolling YAHTZEE is #{triples_check(@roll_count)[1]}%"
end
if quadruples.any?
puts "You already have four of a kind!"
chance = (((1.0/6.0) + (5.0/36.0)*(@roll_count-1))*100).truncate(2)
puts "Your chance of rolling a YAHTZEE of #{quadruples[0]}s is #{chance}%"
end
if yahtzees.any?
sleep(1)
puts "wait"
sleep(1)
puts "wait a sec"
sleep(1)
puts "OMG YOU HAVE YAHTZEE!\nDID YOU NOT NOTICE?!\nSTOP ROLLING AND COLLECT UR POINTS!"
sleep(2)
end
end
# Method to identify straight draws and call appropriate methods
# from Yahtzee_Probability_Calculations.rb
def check_straight
if five_in_a_row?
puts "You already have a large straight!"
elsif four_in_a_row?
puts "You already have a small straight!"
if (@multiples.keys & [1,6]).any?
puts "If you save (#{@multiples.keys.join(',')}) "\
"your chance of rolling a large straight is #{straight_E(@roll_count)}%"
else
puts "If you save (#{@multiples.keys.join(',')}) "\
"your chance of rolling a large straight is #{straight_F(@roll_count)}%"
end
elsif three_in_a_row?
if four_keepers_one_gap?
puts "If you keep (#{@multiples.keys[0..3].join(',')}) "\
"your chance of rolling a large straight is #{straight_E(@roll_count)}%"
elsif (@multiples.keys & [1,6]).any?
if @multiples.keys.include?(3)
print "If you keep (1,2,3) "
else
print "If you keep (4,5,6) "
end
puts "your chance of rolling a small straight is #{straight_A(@roll_count)[0]}% "\
"and your chance of rolling a large straight is #{straight_A(@roll_count)[1]}%"
else
puts "If you keep (#{@multiples.keys.join(',')}) "\
"your chance of rolling a small straight is #{straight_D(@roll_count)[0]}% "\
"and your chance of rolling a large straight is #{straight_D(@roll_count)[1]}%"
end
elsif four_keepers_one_gap?
puts "If you keep (#{@multiples.keys[0..3].join(',')}) "\
"your chance of rolling a large straight is #{straight_E(@roll_count)}%"
elsif three_keepers_one_gap?
if (@multiples.keys & [1,6]).any?
if @multiples.keys.include?(5)
print "If you keep (3,5,6) "
else
print "If you keep (#{@multiples.keys[0..2].join(',')}) "
end
puts "Your chance of rolling a small straight is #{straight_A(@roll_count)[0]}% "\
"and your chance of rolling a large straight is #{straight_A(@roll_count)[1]}%"
else
puts "If you keep (#{@multiples.keys.join(',')}) "\
"your chance of rolling a small straight is #{straight_B(@roll_count)[0]}% "\
"and your chance of rolling a large straight is #{straight_B(@roll_count)[1]}%"
end
elsif three_keepers_two_gaps?
puts "If you keep (#{@multiples.keys[0..2].join(',')}) "\
"your chance of rolling a large straight is #{straight_C(@roll_count)}%"
end
end
# Methods called by check_straight method
def three_in_a_row?
@multiples.keys.each_cons(3).any? { |a| a[2] - a[0] == 2 }
end
def four_in_a_row?
@multiples.keys.each_cons(4).any? { |a| a[3] - a[0] == 3 }
end
def five_in_a_row?
@multiples.keys.each_cons(5).any? { |a| a[4] - a[0] == 4 }
end
def three_keepers_one_gap?
@multiples.keys.each_cons(3).any? { |a| a[2] - a[0] == 3 }
end
def three_keepers_two_gaps?
@multiples.keys.each_cons(3).any? { |a| a[2] - a[0] == 4 }
end
def four_keepers_one_gap?
@multiples.keys.each_cons(4).any? { |a| a[3] - a[0] == 4 }
end
end
new_game = Game.new([], 0, {})
loop do
new_game.start_game
end