-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdice-game-demo.scm
More file actions
35 lines (29 loc) · 1.08 KB
/
dice-game-demo.scm
File metadata and controls
35 lines (29 loc) · 1.08 KB
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
(define (dice-throw)
(+ 1 (modulo (random-next) 6)))
(define player-states #((list #f) (list #f)))
(define (player-dummy2 sum-my sum-his acc cur st)
(display "his: ")
(display cur)
(newline)
(zero? acc))
(define (player-manual sum-my sum-his acc cur st)
(for-each display (list "my=" sum-my ", his=" sum-his ", acc=" acc ", cur=" cur))
(newline)
(display "throw again? (#t or #f): ")
(read))
(define (rep-throw pl my his st)
(let loop ((acc 0))
(let* ((nxt (dice-throw)) (tot (+ acc nxt)) (ur (pl my his acc nxt st)))
(if (or (= nxt 1) (not ur))
(if (= nxt 1) 0 tot)
(loop tot)))))
(define (game player1 player2)
(let loop ((p1 player1) (p2 player2) (s1 0) (s2 0) (side 0))
(let* ((ur (rep-throw p1 s1 s2 (vector-ref player-states side)))
(s1a (+ s1 ur)))
(if (>= s1a 100)
(begin (display "Winner is #")
(display side)
(newline))
(loop p2 p1 s2 s1a (- 1 side))))))
(game player-dummy2 player-manual)