-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loader
619 lines (524 loc) · 20 KB
/
Loader
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
;; =================================
;; CMPU-101, Prof. Hunsberger
;; Yuan Yuan, Model (Yahtzee)
;; =================================
;; Create data structure yahtzee
(define-struct yahtzee
(state fill? dice scores num-rolls-left num-turns-left))
;; =================================
;; CMPU-101, Prof. Hunsberger
;; Yuan Yuan, Controller (Yahtzee)
;; =================================
(define tester
(lambda (datum)
(printf "~A ===> " datum)
(eval datum)))
;; ROLL-DICE
;; -----------------------------------------------------------------
;; INPUTS: None.
;; OUTPUT: A random toss of a 6-sided die.
(define roll-dice
(lambda ()
(+ 1 (random 6))))
;; COMPUTE-HISTOGRAM
;; ---------------------------------------------------------------
;; INPUT: VECK-O-DICE, a vector of dice values (each 1 thru 6)
;; OUTPUT: A vector of length 7, where the ith slot contains
;; the number of occurrences of i in VECK-O-DICE.
;; (The 0th slot of the output vector is ignored.)
(define compute-histogram-helper
(lambda (veck-o-dice histy)
(dotimes (i (vector-length veck-o-dice))
(let ((index (vector-ref veck-o-dice i)))
(vector-set! histy index (+ 1 (vector-ref histy index)))))
histy))
(define compute-histogram
(lambda (veck-o-dice)
(compute-histogram-helper veck-o-dice (make-vector 7))))
;; HAS-THREE-OF-A-KIND?
;; ------------------------------------------------------------------------
;; INPUT: DICE, a vector of length 5 (each element should be one of
;; {1,2,3,4,5,6}
;; OUTPUT: #t, if any three dices have the same number
;; #f, otherwise
(define has-three-of-a-kind?
(lambda (dice)
(let* ((states (compute-histogram dice))
(answer (make-vector 1)))
(vector-set! answer 0 #f)
(dotimes (i (vector-length states))
(if (<= 3 (vector-ref states i))
(if (equal? #f (vector-ref answer 0))
(vector-set! answer 0 #t))))
(vector-ref answer 0))))
;; HAS-FOUR-OF-A-KIND?
;; --------------------------------------------------------------------------
;; INPUT: VECKY, a vector of length 5 (each element should be one of
;; {1,2,3,4,5,6}
;; OUTPUT: #t, if any four dices have the same number
;; #f, otherwise
(define has-four-of-a-kind?
(lambda (dice)
(let* ((states (compute-histogram dice))
(answer (make-vector 1)))
(vector-set! answer 0 #f)
(dotimes (i (vector-length states))
(if (<= 4 (vector-ref states i))
(if (equal? #f (vector-ref answer 0))
(vector-set! answer 0 #t))))
(vector-ref answer 0))))
;; HAS-YAHTZEE?
;; --------------------------------------------------------------------------
;; INPUT: VECKY, a vector of length 5 (each element should be one of
;; {1,2,3,4,5,6}
;; OUTPUT: #t, if all five dices have the same number
;; #f, otherwise
(define has-yahtzee?
(lambda (dice)
(let* ((states (compute-histogram dice))
(answer (make-vector 1)))
(vector-set! answer 0 #f)
(dotimes (i (vector-length states))
(if (<= 5 (vector-ref states i))
(if (equal? #f (vector-ref answer 0))
(vector-set! answer 0 #t))))
(vector-ref answer 0))))
;; HAS-FULL-HOUSE?
;; --------------------------------------------------------------------------
;; INPUT: VECKY, a vector of length 5 (each element should be one of
;; {1,2,3,4,5,6}
;; OUTPUT: #t for 2 of one kind and 3 of another, or 5 of a kind
;; #f, otherwise
(define has-full-house?
(lambda (vecky)
(let* ((states (compute-histogram vecky))
(indy1 (vector-ref states 1))
(indy2 (vector-ref states 2))
(indy3 (vector-ref states 3))
(indy4 (vector-ref states 4))
(indy5 (vector-ref states 5))
(indy6 (vector-ref states 6)))
(if (or (= 3 indy1) (= 3 indy2) (= 3 indy3) (= 3 indy4) (= 3 indy5) (= 3 indy6))
(if (or (= 2 indy1) (= 2 indy2) (= 2 indy3) (= 2 indy4) (= 2 indy5) (= 2 indy6))
#t
#f)
(if (has-yahtzee? vecky)
#t
#f)))))
;; HAS-SMALL-STRAIGHT?
;; --------------------------------------------------------------------------
;; INPUT: VECKY, a vector of length 5 (each element should be one of
;; {1,2,3,4,5,6}
;; OUTPUT: #t if dice contain 1,2,3,4 in any order;
;; or 2,3,4,5 in any order;
;; or 3,4,5,6 in any order
;; #f, otherwise
(define has-small-straight?
(lambda (vecky)
(let* ((states (compute-histogram vecky))
(indy1 (vector-ref states 1))
(indy2 (vector-ref states 2))
(indy3 (vector-ref states 3))
(indy4 (vector-ref states 4))
(indy5 (vector-ref states 5))
(indy6 (vector-ref states 6)))
(cond
((and (>= indy1 1) (>= indy2 1) (>= indy3 1) (>= indy4 1))
#t)
((and (>= indy2 1) (>= indy3 1) (>= indy4 1) (>= indy5 1))
#t)
((and (>= indy3 1) (>= indy4 1) (>= indy5 1) (>= indy6 1))
#t)
(else
#f)))))
;; HAS-LARGE-STRAIGHT?
;; --------------------------------------------------------------------------
;; INPUT: VECKY, a vector of length 5 (each element should be one of
;; {1,2,3,4,5,6}
;; OUTPUT: #t if dice contain 1,2,3,4,5 in any order;
;; or 2,3,4,5,6 in any order
;; #f, otherwise
(define has-large-straight?
(lambda (vecky)
(let* ((states (compute-histogram vecky))
(indy1 (vector-ref states 1))
(indy2 (vector-ref states 2))
(indy3 (vector-ref states 3))
(indy4 (vector-ref states 4))
(indy5 (vector-ref states 5))
(indy6 (vector-ref states 6)))
(cond
((and (>= indy1 1) (>= indy2 1) (>= indy3 1) (>= indy4 1) (>= indy5 1))
#t)
((and (>= indy2 1) (>= indy3 1) (>= indy4 1) (>= indy5 1) (>= indy6 1))
#t)
(else
#f)))))
;; SUM-DICE
;; ---------------------------
;; INPUT: DICE, a vector of length 5, where each element
;; is a number in {1,2,3,4,5,6}
;; OUTPUT: The sum of the dice
(define sum-dice
(lambda (dice)
(let* ((states (compute-histogram dice))
(indy1 (vector-ref states 1))
(indy2 (vector-ref states 2))
(indy3 (vector-ref states 3))
(indy4 (vector-ref states 4))
(indy5 (vector-ref states 5))
(indy6 (vector-ref states 6)))
(+ (* indy1 1) (* indy2 2) (* indy3 3) (* indy4 4) (* indy5 5) (* indy6 6)))))
;; compute-score
;; -----------------------------
;; INPUTS: DICE, a vector of length 5, where each element
;; is a number in {1,2,3,4,5,6}
;; CATEGORY, a number specifying a scoring category
;; as follows:
;; 1 -- 6, the top six scoring categories
;; 7, full house
;; 8, 3 of a kind
;; 9, 4 of a kind
;; 10, small straight
;; 11, large straight
;; 12, yahtzee
;; 13, chance
;; OUTPUT: The numerical score that the given vector of DICE
;; should receive if the player wants to score them
;; in the specified CATEGORY.
;; SIDE EFFECT: Slot of yahtzee-fill? with an index as CATEGORY
;; will change from #f to #t
;; Note: For some of the categories (e.g., full house, 3 of a kind, 4
;; of a kind, small straight, large straight, yahtzee), if the
;; DICE do not satisfy appropriate conditions, then the computed
;; score should be 0.
(define compute-score
(lambda (dice category)
(let* ((states (compute-histogram dice))
(indy1 (vector-ref states 1))
(indy2 (vector-ref states 2))
(indy3 (vector-ref states 3))
(indy4 (vector-ref states 4))
(indy5 (vector-ref states 5))
(indy6 (vector-ref states 6)))
(cond
((= category 1)
(* indy1 1))
((= category 2)
(* indy2 2))
((= category 3)
(* indy3 3))
((= category 4)
(* indy4 4))
((= category 5)
(* indy5 5))
((= category 6)
(* indy6 6))
((= category 7)
(if (has-full-house? dice)
25
0))
((= category 8)
(if (has-three-of-a-kind? dice)
(sum-dice dice)
0))
((= category 9)
(if (has-four-of-a-kind? dice)
(sum-dice dice)
0))
((= category 10)
(if (has-small-straight? dice)
30
0))
((= category 11)
(if (has-large-straight? dice)
40
0))
((= category 12)
(if (has-yahtzee? dice)
50
0))
((= category 13)
(sum-dice dice))))))
;; roll-some!
;; -----------------------------------------------------------------
;; INPUTS: YAHTZ, a yahtzee data structure
;; ROLLER, a vector of length 5, where each element
;; is either 1 or 0.
;; OUTPUT: Don't care
;; SIDE EFFECT: (Can only run the Program when NUM-ROLLS-LEFT and
;; NUM-TURNS-LEFT do not equal 0.) Looks at each element of ROLLER.
;; If the Ith element of ROLLER is a 1, then it destructively modifies
;; the Ith slot of the dice vector in the yahtzee data structure,
;; replacing it with a random toss of a 6-sided die; if the
;; Ith element of ROLLER is a 0, then it does not change the
;; Ith slot of the dice vector in the yahtzee data structure.
(define roll-some!
(lambda (yahtz roller)
(let ((num-rolls-left (yahtzee-num-rolls-left yahtz))
(num-turns-left (yahtzee-num-turns-left yahtz)))
(cond
;;No rolls left. Find current score
((<= num-rolls-left 0)
(vector-ref (yahtzee-state yahtz) 2))
;;No turns left. Find total score
((<= num-turns-left 0)
(vector-ref (yahtzee-state yahtz) 3))
(else
(dotimes (i 5)
(if (= (vector-ref roller i) 1)
(vector-set! (yahtzee-dice yahtz) i (roll-dice))))
(printf "Your Current Dice: ~A" (yahtzee-dice yahtz))
(set-yahtzee-num-rolls-left! yahtz (- (yahtzee-num-rolls-left yahtz) 1)))))))
;; roll-all!
;; -----------------------------------------------
;; INPUT: YAHTZ, a yahtzee data structure
;; OUTPUT: Don't care
;; SIDE EFFECT: If NUM-ROLLS-LEFT and NUM-TURNS-LEFT
;; are larger than 0, destructive modifies the vector of dice
;; in the yahtzee data structure, replacing each element
;; with a random toss of a six-sided die. Decrease the
;; NUM-ROLLS-LEFT by 1. Print out the
;; Otherwise, do not run the function and tell
;; players what they should do next.
(define roll-all!
(lambda (yahtz)
(let ((num-rolls-left (yahtzee-num-rolls-left yahtz))
(num-turns-left (yahtzee-num-turns-left yahtz)))
(cond
;;No rolls left. Find current score
((<= num-rolls-left 0)
(vector-ref (yahtzee-state yahtz) 2))
;;No turns left. Find total score
((<= num-turns-left 0)
(vector-ref (yahtzee-state yahtz) 3))
(else
(dotimes (i 5)
(vector-set! (yahtzee-dice yahtz) i (roll-dice)))
(printf "Your Current Dice: ~A" (yahtzee-dice yahtz))
(set-yahtzee-num-rolls-left! yahtz (- (yahtzee-num-rolls-left yahtz) 1)))))))
;; score!
;; -------------------------------
;; INPUTS: YAHTZ, a yahtzee data structure
;; CATEGORY, a number from 1 to 13, inclusive, that
;; specifies a category for the compute-score function
;; SIDE EFFECT: The scores field of the yahtzee data structure
;; contains a scoresheet (however you implement it). If the
;; slot in that scoresheet specified by CATEGORY is currently
;; unfilled, then this function destructively modifies the scoresheet
;; by inserting the appropriate score (as computed by compute-score)
;; into the specified slot; otherwise, it makes no change to the
;; scoresheet.
;; OUTPUT: If a score was entered, the output should be the value
;; of that score; otherwise, the output should be #f.
(define score!
(lambda (yahtz category)
(let ((fill? (vector-ref (yahtzee-fill? yahtz) category))
(curr-score (compute-score (yahtzee-dice yahtz) category))
(curr-turn (yahtzee-num-turns-left yahtz)))
(cond
;;#f if the CATEGORY of the scoresheet is filled
(fill?
#f)
;;Current Score if the CATEGORY of the scoresheet is not filled
(else
(vector-set! (yahtzee-scores yahtz) category curr-score)
(set-yahtzee-num-turns-left! yahtz (- curr-turn 1))
(set-yahtzee-num-rolls-left! yahtz 3)
(vector-set! (yahtzee-fill? yahtz) category #t)
curr-score)))))
;; new-game -- in your "controller" file
;;--------------------------------------------------
;; INPUTS: None
;; OUTPUT: A yahtzee data structure whose contents are
;; suitably initialized for the start of a new
;; game of yahtzee.
(define new-game
(lambda ()
(let ((vecky (make-vector 16))
(vecky2 (make-vector 4)))
(dotimes (i 16)
(vector-set! vecky i #f))
(vector-set! vecky2 1 "You can roll the dice now")
(vector-set! vecky2 2 "You have no rolls left. Save the score")
(vector-set! vecky2 3 "You have no turns left. Find your total score now")
(make-yahtzee
vecky2
vecky
(make-vector 5)
(make-vector 16)
3
13))))
;; =================================
;; CMPU-101, Prof. Hunsberger
;; Yuan Yuan, View (Yahtzee)
;; =================================
;; curr-score-in-cat -- in your "view" file
;;------------------------------------------------------
;; INPUTS: YAHTZ, a yahtzee data structure
;; CATEGORY, a number from 1 to 13, inclusive, as described
;; for the compute-score function
;; OUTPUT: If the scoresheet in the yahtzee data structure contains
;; a numerical score in the specified CATEGORY, then the
;; output should be that number; otherwise, it should be #f.
;; NOTE: You should only use 0 to represent that a player has
;; entered a score of 0 in a given slot; if the slot is
;; currently empty, use something such as #f or the symbol _.
(define curr-score-in-cat
(lambda (yahtz category)
(let ((fill (vector-ref (yahtz-fill? yahtz) category))
(num (vector-ref (yahtz-scores yahtz) category)))
(if (equal? fill #f)
num
#f))))
;; print-scoresheet -- in your "view" file
;; -------------------------------------------------------
;; INPUTS: YAHTZ, a yahtzee data structure
;; OUTPUT: Don't care
;; SIDE EFFECT: Prints out the contents of the scores
;; in the yahtzee data structure, showing the name of
;; each category and the corresponding score.
;;
;; For example, it might look something like this:
;;
;; CAT SCORE
;; 1 __
;; 2 6
;; 3 9
;; 4 16
;; 5 __
;; 6 __
;;
;; FullH 25
;; 3ofKind 21
;; 4ofKind __
;; SmStr 30
;; LgStr 0
;; Yahtzee __
;; Chance __
;; Bonus __
;; Total 107
;; Don't worry if your columns don't line up perfectly.
(define print-scoresheet
(lambda (yahtz)
(let* ((num1 (vector-ref (yahtzee-scores yahtz) 1))
(num2 (vector-ref (yahtzee-scores yahtz) 2))
(num3 (vector-ref (yahtzee-scores yahtz) 3))
(num4 (vector-ref (yahtzee-scores yahtz) 4))
(num5 (vector-ref (yahtzee-scores yahtz) 5))
(num6 (vector-ref (yahtzee-scores yahtz) 6))
(num7 (vector-ref (yahtzee-scores yahtz) 7))
(num8 (vector-ref (yahtzee-scores yahtz) 8))
(num9 (vector-ref (yahtzee-scores yahtz) 9))
(num10 (vector-ref (yahtzee-scores yahtz) 10))
(num11 (vector-ref (yahtzee-scores yahtz) 11))
(num12 (vector-ref (yahtzee-scores yahtz) 12))
(num13 (vector-ref (yahtzee-scores yahtz) 13))
(num14 (vector-ref (yahtzee-scores yahtz) 14))
(num15 (vector-ref (yahtzee-scores yahtz) 15))
(sum (make-vector 1)))
(printf
"CAT SCORE~%")
(if (vector-ref (yahtzee-fill? yahtz) 1)
(printf
"1 ~A~%" num1)
(printf
"1 _~%"))
(if (vector-ref (yahtzee-fill? yahtz) 2)
(printf
"2 ~A~%" num2)
(printf
"2 _~%"))
(if (vector-ref (yahtzee-fill? yahtz) 3)
(printf
"3 ~A~%" num3)
(printf
"3 _~%"))
(if (vector-ref (yahtzee-fill? yahtz) 4)
(printf
"4 ~A~%" num4)
(printf
"4 _~%"))
(if (vector-ref (yahtzee-fill? yahtz) 5)
(printf
"5 ~A~%" num5)
(printf
"5 _~%"))
(if (vector-ref (yahtzee-fill? yahtz) 6)
(printf
"6 ~A~%" num6)
(printf
"6 _~%"))
(if (vector-ref (yahtzee-fill? yahtz) 7)
(printf
"FullH ~A~%" num7)
(printf
"FullH _~%"))
(if (vector-ref (yahtzee-fill? yahtz) 8)
(printf
"3ofKind ~A~%" num8)
(printf
"3ofKind _~%"))
(if (vector-ref (yahtzee-fill? yahtz) 9)
(printf
"4ofKind ~A~%" num9)
(printf
"4ofKind _~%"))
(if (vector-ref (yahtzee-fill? yahtz) 10)
(printf
"SmStr ~A~%" num10)
(printf
"SmStr _~%"))
(if (vector-ref (yahtzee-fill? yahtz) 11)
(printf
"LgStr ~A~%" num11)
(printf
"LgStr _~%"))
(if (vector-ref (yahtzee-fill? yahtz) 12)
(printf
"Yahtzee ~A~%" num12)
(printf
"Yahtzee _~%"))
(if (vector-ref (yahtzee-fill? yahtz) 13)
(printf
"Chance ~A~%" num13)
(printf
"Chance _~%"))
(if (and (vector-ref (yahtzee-fill? yahtz) 1)
(vector-ref (yahtzee-fill? yahtz) 2)
(vector-ref (yahtzee-fill? yahtz) 3)
(vector-ref (yahtzee-fill? yahtz) 4)
(vector-ref (yahtzee-fill? yahtz) 5)
(vector-ref (yahtzee-fill? yahtz) 6))
(if (>= (+ num1 num2 num3 num4 num5 num6) 63)
(vector-set! (yahtzee-score yahtz) 14 35)))
(if (vector-ref (yahtzee-fill? yahtz) 14)
(printf
"Bonus ~A~%" num14)
(printf
"Bonus _~%"))
(dotimes (i 15)
(let ((num (vector-ref sum 0)))
(vector-set! sum 0 (+ num (vector-ref (yahtzee-scores yahtz) i)))))
(printf "Total ~A" (vector-ref sum 0)))))
;; FINAL-SCORE
;; ------------------------------------------------
;; INPUT: YAHTZ, a yahtzee data structure
;; OUTPUT: A number that shows the total score of
;; the current game
(define final-score
(lambda (yahtz)
(let ((sum (make-vector 1)))
(dotimes (i 15)
(let ((num (vector-ref sum 0)))
(vector-set! sum 0 (+ num (vector-ref (yahtzee-scores yahtz) i)))))
(vector-ref sum 0))))
(define y (new-game)) ;; create a new game
(roll-all! y) ;; roll *all* of the dice
(roll-some! y #(1 1 0 0 0)) ;; roll some of the dice (two times)
(roll-some! y #(0 1 0 0 0))
(score! y 3) ;; score the dice in some category
(roll-all! y) ;; roll *all* of the dice
(roll-some! y #(1 1 0 0 0)) ;; roll some of the dice (two times)
(roll-some! y #(0 1 0 0 0))
(score! y 9) ;; score the dice in some category