-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.lisp
232 lines (194 loc) · 7.28 KB
/
game.lisp
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
;; FILE: game.lisp
;; AUTHORS: Daniel Melody & Aidan Lavis
;; ==========================================
;; functions relating to the actual play of Cribbage
;; PLAY-ROUND
;; ------------------------------------------
;; INPUTS: C, a Cribbage game
;; P1-CRIB-FN, a fxn for deciding cards given to CRIB for player 1
;; P1-PILE-FN, a fxn for deciding cards given to PILE for player 1
;; P2-CRIB-FN, a fxn for deciding cards given to CRIB for player 2
;; P2-PILE-FN, a fxn for deciding cards given to PILE for player 2
;; OUTPUTS: none
;; SIDE EFFECTS: plays a round of Cribbage and updates Cribbage struct
(defun play-round (c p1-crib-fn p1-pile-fn p2-crib-fn p2-pile-fn)
(cond
((not (game-over? c))
(let ((dlr (cribbage-whose-dealer? c))
(card nil)
(last-card nil))
;; set turn to DLR
(setf (cribbage-whose-turn? c) dlr)
;; DEAL the cards
(format t "DEAL~%")
(deal c)
;; give cards to CRIB
(cond
;; DLR = Player One
((equal dlr *player-one*)
(format t "DLR to crib.~%")
(funcall p1-crib-fn c)
(format t "NON-DLR to crib.~%")
(funcall p2-crib-fn c)
;; set first CARD selection to P2-PILE-FN
(setf card (funcall p2-pile-fn c)))
((equal dlr *player-two*)
(format t "DLR to crib.~%")
(funcall p2-crib-fn c)
(format t "NON-DLR to crib.~%")
(funcall p1-crib-fn c)
;; set first CARD selection to P1-PILE-FN
(setf card (funcall p1-pile-fn c))))
;; the PLAY, call hand-to-pile! as many times as necessary/possible
(loop while (or card last-card) do
(setf last-card card)
(cond
;; call P1-PILE-FN
((= (cribbage-whose-turn? c) *player-one*)
(setf card (funcall p1-pile-fn c)))
;; call P2-PILE-FN
((= (cribbage-whose-turn? c) *player-two*)
(setf card (funcall p2-pile-fn c)))
;; default, assign CARD to NIL to ward off infinite loop
(t
(setf card nil))))
;; call SHOW on Cribbage game
(when (not (game-over? c))
(show c))))
(t
(format t "Unable to play a round.~%"))))
;; PLAY-GAME
;; ------------------------------------------
;; INPUTS: P1-CRIB-FN, a function governing hand-to-crib! choices for player 1
;; P1-PILE-FN, a function governing hand-to-pile! choices for player 1
;; P2-CRIB-FN, a function governing hand-to-crib! choices for player 2
;; P2-PILE-FN, a function governing hand-to-pile! choices for player 2
;; OUTPUTS: the winning player
(defun play-game (p1-crib-fn p1-pile-fn p2-crib-fn p2-pile-fn)
(let ((c (make-cribbage)))
;; PLAY-ROUND while NOT GAME-OVER?
(loop while (not (game-over? c)) do
(play-round c p1-crib-fn p1-pile-fn p2-crib-fn p2-pile-fn))
;; return who won this game
(who-won? c)))
;; PLAY-ONLY-GAME
(defun play-only-game (p1-crib-fn p1-pile-fn p2-crib-fn p2-pile-fn)
(let ((c (make-cribbage)))
;; PLAY-ROUND while NOT GAME-OVER?
(loop while (not (game-over? c)) do
(play-only-round c p1-crib-fn p1-pile-fn p2-crib-fn p2-pile-fn))
;; return who won this game
(who-won? c)))
;; PLAY-ONLY-ROUND
(defun play-only-round (c p1-crib-fn p1-pile-fn p2-crib-fn p2-pile-fn)
(cond
((not (game-over? c))
(let ((dlr (cribbage-whose-dealer? c))
(card nil)
(last-card nil))
;; set turn to DLR
(setf (cribbage-whose-turn? c) dlr)
;; DEAL the cards
(format t "DEAL~%")
(deal c)
;; give cards to CRIB
(cond
;; DLR = Player One
((equal dlr *player-one*)
(format t "DLR to crib.~%")
(funcall p1-crib-fn c)
(format t "NON-DLR to crib.~%")
(funcall p2-crib-fn c)
;; set first CARD selection to P2-PILE-FN
(setf card (funcall p2-pile-fn c)))
((equal dlr *player-two*)
(format t "DLR to crib.~%")
(funcall p2-crib-fn c)
(format t "NON-DLR to crib.~%")
(funcall p1-crib-fn c)
;; set first CARD selection to P1-PILE-FN
(setf card (funcall p1-pile-fn c))))
;; the PLAY, call hand-to-pile! as many times as necessary/possible
(loop while (or card last-card) do
(setf last-card card)
(cond
;; call P1-PILE-FN
((= (cribbage-whose-turn? c) *player-one*)
(setf card (funcall p1-pile-fn c)))
;; call P2-PILE-FN
((= (cribbage-whose-turn? c) *player-two*)
(setf card (funcall p2-pile-fn c)))
;; default, assign CARD to NIL to ward off infinite loop
(t
(setf card nil))))
))
(t
(format t "Unable to play a round.~%"))))
;; RANDOM-ROUND
;; ------------------------------------------
;; INPUTS: C, a Cribbage game
;; OUTPUTS: none
;; SIDE EFFECTS: plays a round of Cribbage with all random functions
(defun random-round (c)
(play-round c #'random-to-crib! #'random-to-pile!
#'random-to-crib! #'random-to-pile!))
;; RANDOM-DO-PILE
;; ------------------------------------------
;; INPUTS: C, a Cribbage game (after hands have gone to Crib!)
;; OUTPUTS: none
(defun random-do-pile (c)
;; check for legal CRIB
(when (not (= (length (cribbage-crib c)) 4))
(return-from random-do-pile 'error))
;; Put cards in the pile until you can't anymore.
(let* ((card (random-to-pile! c))
(last-card card))
(loop while (or card last-card) do
(setf last-card card)
(setf card (random-to-pile! c)))))
;; RANDOM-GAME
;; ------------------------------------------
;; INPUTS: none
;; OUTPUTS: the winning player
(defun random-game ()
(play-game #'random-to-crib! #'random-to-pile!
#'random-to-crib! #'random-to-pile!))
;; PI-MCTS-TO-PILE!
(defun pi-mcts-to-pile! (c)
(hand-to-pile! c nil (uct-search c 50 2) (cribbage-whose-turn? c)))
;; PR-ROUND
(defun pr-round (c)
(play-round c #'random-to-crib! #'pi-mcts-to-pile!
#'random-to-crib! #'random-to-pile!))
;; PR-ONLY-ROUND
(defun pr-only-round (c)
(play-only-round c #'random-to-crib! #'pi-mcts-to-pile!
#'random-to-crib! #'random-to-pile!))
;; PI-VS-RANDOM
(defun pi-vs-random ()
(play-game #'random-to-crib! #'pi-mcts-to-pile!
#'random-to-crib! #'random-to-pile!))
;; AVG-GAME-WINS
(defun avg-game-wins (times)
(let ((r (avg-random-wins times))
(p1 (avg-mcts-p1-wins times))
(p2 (avg-mcts-p2-wins times)))
(format t "random: ~A~%p1: ~A~%p2: ~A~%" r p1 p2)))
(defun avg-random-wins (times)
(let ((x 0))
(dotimes (i times)
(incf x (play-only-game #'random-to-crib! #'random-to-pile!
#'random-to-crib! #'random-to-pile!)))
(- x times)))
(defun avg-mcts-p1-wins (times)
(let ((x 0))
(dotimes (i times)
(incf x (play-only-game #'random-to-crib! #'pi-mcts-to-pile!
#'random-to-crib! #'random-to-pile!)))
(- times (- x times))))
(defun avg-mcts-p2-wins (times)
(let ((x 0))
(dotimes (i times)
(incf x (play-only-game #'random-to-crib! #'random-to-pile!
#'random-to-crib! #'pi-mcts-to-pile!)))
(- x times)))