yahtzee 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/yahtzee.rb +405 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 10f492f5c86781d0d7542d35cdad4406f601e5e6
|
4
|
+
data.tar.gz: 7e3fcb65fb907d5840ec412e94b434f9a381ebfa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d62578caf18b66adbd3da8d58f5e13c9ff92c818fdc9bdc700078517b069cd6b745ae3641a8eb129282bee5c1424356811d6e077bd47d863e1dc1e2239b97f8b
|
7
|
+
data.tar.gz: 0d9a6dd0a474a3060a6cadafdd63578f36836dab76d8d49bbd4e4c627aca9f2d95b02d2ddb955a186ae435b9a4e89e0490a75f0a40026d90bb5cc48a0922554e
|
data/lib/yahtzee.rb
ADDED
@@ -0,0 +1,405 @@
|
|
1
|
+
#require 'grape'
|
2
|
+
|
3
|
+
module Yahtzee
|
4
|
+
# class API < Grape::API
|
5
|
+
# version 'v1.1', using: :header, vendor: :yahtzee
|
6
|
+
# end
|
7
|
+
|
8
|
+
class Controller
|
9
|
+
def initialize
|
10
|
+
@message = ""
|
11
|
+
|
12
|
+
@sc = Scorecard.new
|
13
|
+
|
14
|
+
@turn = Turn.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def roll
|
18
|
+
@turn = Turn.new
|
19
|
+
end
|
20
|
+
|
21
|
+
def reroll a
|
22
|
+
@turn.reroll a
|
23
|
+
end
|
24
|
+
|
25
|
+
def score a
|
26
|
+
case @sc.score a, @turn.get_dice, @turn.joker?
|
27
|
+
when -1
|
28
|
+
return -1
|
29
|
+
when 0
|
30
|
+
@turn.set_joker true
|
31
|
+
return 0
|
32
|
+
else
|
33
|
+
next_turn
|
34
|
+
return 1
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def next_turn
|
39
|
+
@turn = Turn.new
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_dice
|
43
|
+
return @turn.get_dice
|
44
|
+
end
|
45
|
+
|
46
|
+
def set_dice a
|
47
|
+
@turn.set_dice a
|
48
|
+
end
|
49
|
+
|
50
|
+
def get_data
|
51
|
+
return @sc.get_cats
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_potential
|
55
|
+
return sc.calculate_dice @turn.get_dice
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Turn
|
60
|
+
def initialize
|
61
|
+
@dice = roll_dice 5
|
62
|
+
|
63
|
+
@reroll_count = 0
|
64
|
+
@reroll_max = 2
|
65
|
+
|
66
|
+
@joker = false
|
67
|
+
@done = false
|
68
|
+
end
|
69
|
+
|
70
|
+
def roll_dice num_dice
|
71
|
+
a = []
|
72
|
+
|
73
|
+
num_dice.times do
|
74
|
+
a.push Random.rand(6) + 1
|
75
|
+
end
|
76
|
+
|
77
|
+
return a
|
78
|
+
end
|
79
|
+
|
80
|
+
def can_reroll?
|
81
|
+
return true if @reroll_count < @reroll_max
|
82
|
+
end
|
83
|
+
|
84
|
+
def set_preserve pr
|
85
|
+
@preserve = pr
|
86
|
+
end
|
87
|
+
|
88
|
+
def set_dice a
|
89
|
+
@dice = a
|
90
|
+
end
|
91
|
+
|
92
|
+
def get_preserve
|
93
|
+
return @preserve
|
94
|
+
end
|
95
|
+
|
96
|
+
def get_dice
|
97
|
+
return @dice
|
98
|
+
end
|
99
|
+
|
100
|
+
def get_reroll_count
|
101
|
+
return @reroll_count
|
102
|
+
end
|
103
|
+
|
104
|
+
def joker?
|
105
|
+
return @joker
|
106
|
+
end
|
107
|
+
|
108
|
+
def set_joker value
|
109
|
+
@joker = value
|
110
|
+
end
|
111
|
+
|
112
|
+
def done?
|
113
|
+
return @done
|
114
|
+
end
|
115
|
+
|
116
|
+
def finish
|
117
|
+
@done = true
|
118
|
+
end
|
119
|
+
|
120
|
+
def reroll preserve # [1,1,1,1,1] preserves all
|
121
|
+
if @reroll_count < @reroll_max
|
122
|
+
a = roll_dice 5
|
123
|
+
5.times do |i|
|
124
|
+
if preserve[i] == 0
|
125
|
+
@dice[i] = a[i]
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
@reroll_count += 1
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
class Scorecard
|
134
|
+
@@bonus_value = 35
|
135
|
+
@@full_house_value = 25
|
136
|
+
@@small_straight_value = 30
|
137
|
+
@@large_straight_value = 40
|
138
|
+
@@yahtzee_value = 50
|
139
|
+
@@yahtzee_bonus_value = 100
|
140
|
+
|
141
|
+
def initialize
|
142
|
+
@upper_section = [ :aces, :twos, :threes, :fours, :fives, :sixes ]
|
143
|
+
@lower_section = [ :three_of_a_kind, :four_of_a_kind, :full_house, :small_straight,
|
144
|
+
:large_straight, :yahtzee, :chance, :yahtzee_bonus ]
|
145
|
+
|
146
|
+
@categories = {
|
147
|
+
:aces => -1,
|
148
|
+
:twos => -1,
|
149
|
+
:threes => -1,
|
150
|
+
:fours => -1,
|
151
|
+
:fives => -1,
|
152
|
+
:sixes => -1,
|
153
|
+
:upper_section_bonus => 0,
|
154
|
+
|
155
|
+
:three_of_a_kind => -1,
|
156
|
+
:four_of_a_kind => -1,
|
157
|
+
:full_house => -1,
|
158
|
+
:small_straight => -1,
|
159
|
+
:large_straight => -1,
|
160
|
+
:yahtzee => -1,
|
161
|
+
:chance => -1,
|
162
|
+
:yahtzee_bonus => 0,
|
163
|
+
|
164
|
+
:upper_section_subtotal => 0,
|
165
|
+
:upper_section_total => 0,
|
166
|
+
:lower_section_total => 0,
|
167
|
+
|
168
|
+
:grand_total => 0
|
169
|
+
}
|
170
|
+
end
|
171
|
+
|
172
|
+
def calculate_dice dice, joker
|
173
|
+
cats = {
|
174
|
+
:aces => 0,
|
175
|
+
:twos => 0,
|
176
|
+
:threes => 0,
|
177
|
+
:fours => 0,
|
178
|
+
:fives => 0,
|
179
|
+
:sixes => 0,
|
180
|
+
:three_of_a_kind => 0,
|
181
|
+
:four_of_a_kind => 0,
|
182
|
+
:full_house => 0,
|
183
|
+
:small_straight => 0,
|
184
|
+
:large_straight => 0,
|
185
|
+
:yahtzee => 0,
|
186
|
+
:chance => 0
|
187
|
+
}
|
188
|
+
|
189
|
+
# upper section
|
190
|
+
dice.each do |i|
|
191
|
+
case i
|
192
|
+
when 1
|
193
|
+
cats[:aces] += 1
|
194
|
+
when 2
|
195
|
+
cats[:twos] += 2
|
196
|
+
when 3
|
197
|
+
cats[:threes] += 3
|
198
|
+
when 4
|
199
|
+
cats[:fours] += 4
|
200
|
+
when 5
|
201
|
+
cats[:fives] += 5
|
202
|
+
when 6
|
203
|
+
cats[:sixes] += 6
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
# lower section
|
208
|
+
freq = [0,0,0,0,0,0]
|
209
|
+
sum = 0
|
210
|
+
dice.size.times do |i|
|
211
|
+
sum += dice[i]
|
212
|
+
freq[dice[i]-1] += 1
|
213
|
+
end
|
214
|
+
sfreq = freq.sort.reverse
|
215
|
+
max = sfreq[0] # number of most frequent dice
|
216
|
+
smax = sfreq[1] # number of second most frequent
|
217
|
+
|
218
|
+
small = false
|
219
|
+
large = false
|
220
|
+
ssum = 0
|
221
|
+
sdice = dice.sort.uniq
|
222
|
+
sdice.size.times do |i|
|
223
|
+
ssum += sdice[i]
|
224
|
+
end
|
225
|
+
large = true if sdice.length >= 5 and (ssum == 15 or ssum == 20)
|
226
|
+
small = true if sdice.length >= 4 and (ssum == 10 or ssum == 14 or ssum == 18) or large
|
227
|
+
|
228
|
+
|
229
|
+
cats[:three_of_a_kind] = sum if (max >= 3 || joker)
|
230
|
+
cats[:four_of_a_kind] = sum if (max >= 4 || joker)
|
231
|
+
cats[:full_house] = @@full_house_value if ((max == 3 and smax == 2) || joker)
|
232
|
+
cats[:small_straight] = @@small_straight_value if (small || joker)
|
233
|
+
cats[:large_straight] = @@large_straight_value if (large || joker)
|
234
|
+
cats[:yahtzee] = @@yahtzee_value if (max >= 5 || joker)
|
235
|
+
cats[:chance] = sum
|
236
|
+
|
237
|
+
return cats
|
238
|
+
end
|
239
|
+
|
240
|
+
def score cat, dice, joker
|
241
|
+
pot = calculate_dice dice, joker
|
242
|
+
|
243
|
+
case cat
|
244
|
+
when (:aces or :twos or :threes or :fours or :fives or :sixes)
|
245
|
+
if @categories[cat] == -1
|
246
|
+
@categories[cat] = pot[cat]
|
247
|
+
calculate_totals
|
248
|
+
return 1
|
249
|
+
end
|
250
|
+
when :yahtzee
|
251
|
+
return -1 if joker
|
252
|
+
case @categories[:yahtzee]
|
253
|
+
when -1
|
254
|
+
@categories[:yahtzee] = pot[:yahtzee]
|
255
|
+
calculate_totals
|
256
|
+
return 1
|
257
|
+
when @@yahtzee_value
|
258
|
+
@categories[:yahtzee_bonus] += @@yahtzee_bonus_value
|
259
|
+
end
|
260
|
+
which = @upper_section[dice[0]-1]
|
261
|
+
if @categories[which] == -1
|
262
|
+
return score which, dice, joker
|
263
|
+
end
|
264
|
+
return 0
|
265
|
+
else
|
266
|
+
if @categories[cat] == -1
|
267
|
+
@categories[cat] = pot[cat]
|
268
|
+
calculate_totals
|
269
|
+
return 1
|
270
|
+
end
|
271
|
+
end
|
272
|
+
return -1
|
273
|
+
end
|
274
|
+
|
275
|
+
def add_section sect
|
276
|
+
sum = 0
|
277
|
+
|
278
|
+
@categories.each do |key, value|
|
279
|
+
if (sect.include? key) && value > -1
|
280
|
+
sum += value
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
return sum
|
285
|
+
end
|
286
|
+
|
287
|
+
def calculate_totals
|
288
|
+
@categories[:upper_section_subtotal] = add_section @upper_section
|
289
|
+
|
290
|
+
@categories[:upper_section_total] = @categories[:upper_section_subtotal]
|
291
|
+
@categories[:upper_section_bonus] = @@bonus_value if @categories[:upper_section_subtotal] >= 63
|
292
|
+
@categories[:upper_section_total] += @categories[:upper_section_bonus]
|
293
|
+
|
294
|
+
@categories[:lower_section_total] = add_section @lower_section
|
295
|
+
|
296
|
+
@categories[:grand_total] = @categories[:upper_section_total] + @categories[:lower_section_total]
|
297
|
+
end
|
298
|
+
|
299
|
+
def get_cats
|
300
|
+
return @categories
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
304
|
+
# CLI interface
|
305
|
+
|
306
|
+
class Game
|
307
|
+
def format(score)
|
308
|
+
return " " if score == -1
|
309
|
+
return "%2d" %[score]
|
310
|
+
end
|
311
|
+
|
312
|
+
def print(score, dice, m1, m2, m3)
|
313
|
+
puts " ______________________________________ "
|
314
|
+
printf "|a Aces [%s] |g 3 of a kind [%s] | Dice:\n", format(score[:aces]), format(score[:three_of_a_kind])
|
315
|
+
printf "|b Twos [%s] |h 4 of a kind [%s] | a %d\n", format(score[:twos]), format(score[:four_of_a_kind]), dice[0]
|
316
|
+
printf "|c Threes [%s] |i Full house [%s] | b %d\n", format(score[:threes]), format(score[:full_house]), dice[1]
|
317
|
+
printf "|d Fours [%s] |j Small straight [%s] | c %d\n", format(score[:fours]), format(score[:small_straight]), dice[2]
|
318
|
+
printf "|e Fives [%s] |k Large straight [%s] | d %d\n", format(score[:fives]), format(score[:large_straight]), dice[3]
|
319
|
+
printf "|f Sixes [%s] |l Yahtzee [%s] | e %d\n", format(score[:sixes]), format(score[:yahtzee]), dice[4]
|
320
|
+
printf "| | Yahtzee bonus %3d |\n", score[:yahtzee_bonus]
|
321
|
+
printf "|Subtotal: %2d |m Chance [%s] | %s\n", score[:upper_section_subtotal], format(score[:chance]), m1
|
322
|
+
printf "|Bonus: %2d | | %s\n", score[:upper_section_bonus], m2
|
323
|
+
printf "|Total: %3d |Total: %3d | %s\n", score[:upper_section_total], score[:lower_section_total], m3
|
324
|
+
printf "|______ Grand Total:%4d ______________| > ", score[:grand_total]
|
325
|
+
input = STDIN.gets.chomp
|
326
|
+
return input
|
327
|
+
end
|
328
|
+
|
329
|
+
|
330
|
+
def determine_preserve(s)
|
331
|
+
ret = [1,1,1,1,1]
|
332
|
+
s.split("").each do |c|
|
333
|
+
case c
|
334
|
+
when 'a'
|
335
|
+
ret[0] = 0
|
336
|
+
when 'b'
|
337
|
+
ret[1] = 0
|
338
|
+
when 'c'
|
339
|
+
ret[2] = 0
|
340
|
+
when 'd'
|
341
|
+
ret[3] = 0
|
342
|
+
when 'e'
|
343
|
+
ret[4] = 0
|
344
|
+
end
|
345
|
+
end
|
346
|
+
return ret
|
347
|
+
end
|
348
|
+
|
349
|
+
def parse c
|
350
|
+
case c
|
351
|
+
when 'a'
|
352
|
+
cat = :aces
|
353
|
+
when 'b'
|
354
|
+
cat = :twos
|
355
|
+
when 'c'
|
356
|
+
cat = :threes
|
357
|
+
when 'd'
|
358
|
+
cat = :fours
|
359
|
+
when 'e'
|
360
|
+
cat = :fives
|
361
|
+
when 'f'
|
362
|
+
cat = :sixes
|
363
|
+
when 'g'
|
364
|
+
cat = :three_of_a_kind
|
365
|
+
when 'h'
|
366
|
+
cat = :four_of_a_kind
|
367
|
+
when 'i'
|
368
|
+
cat = :full_house
|
369
|
+
when 'j'
|
370
|
+
cat = :small_straight
|
371
|
+
when 'k'
|
372
|
+
cat = :large_straight
|
373
|
+
when 'l'
|
374
|
+
cat = :yahtzee
|
375
|
+
when 'm'
|
376
|
+
cat = :chance
|
377
|
+
else
|
378
|
+
return false
|
379
|
+
end
|
380
|
+
return cat
|
381
|
+
end
|
382
|
+
|
383
|
+
def start
|
384
|
+
game = Yahtzee::Controller.new
|
385
|
+
|
386
|
+
13.times do |i|
|
387
|
+
print game.get_data, [0,0,0,0,0], "", "", "Press enter to roll"
|
388
|
+
game.roll
|
389
|
+
selection = print game.get_data, game.get_dice, "", "Select which dice you would like to roll again.", "Type the letters of the dice followed by enter: "
|
390
|
+
game.reroll determine_preserve(selection)
|
391
|
+
selection = print game.get_data, game.get_dice, "", "Select which dice you would like to roll again.", "Type the letters of the dice followed by enter: "
|
392
|
+
game.reroll determine_preserve(selection)
|
393
|
+
|
394
|
+
m1 = ""
|
395
|
+
success = false
|
396
|
+
while !success
|
397
|
+
selection = print game.get_data, game.get_dice, m1, "Select the category for this roll. Type the", "letter of the category followed by enter: "
|
398
|
+
m1 = "Selection invalid"
|
399
|
+
success = game.score(parse(selection.split("")[0]))
|
400
|
+
end
|
401
|
+
end
|
402
|
+
print game.get_data, [0,0,0,0,0], "", "Game complete", ""
|
403
|
+
end
|
404
|
+
end
|
405
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yahtzee
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Christopher Hinstorff
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-09 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A ruby implementation of the game Yahtzee
|
14
|
+
email: christopherhinstorff@me.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/yahtzee.rb
|
20
|
+
homepage: http://chinstorff.com
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.0.7
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Yahtzee
|
44
|
+
test_files: []
|