-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdice-of-doom-v2.lisp
174 lines (157 loc) · 6.71 KB
/
dice-of-doom-v2.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
(load "dice-of-doom-v1.lisp")
(load "lazy.lisp")
(defparameter *board-length* 5)
(defparameter *board-hex-count* (* *board-length* *board-length*))
(defparameter *ai-level* 4)
(defun add-passing-move (board player spare-dice first-move moves)
(if first-move
moves
(lazy-cons (list nil
(game-tree (add-new-dice board player
(1- spare-dice))
(mod (1+ player) *number-of-players*)
0
t))
moves)))
(defun attacking-moves (board current-player spare-dice)
(labels ((player-at (position)
(car (aref board position)))
(dice (position)
(cadr (aref board position))))
(lazy-mapcan
(lambda (source)
(if (eq (player-at source) current-player)
(lazy-mapcan
(lambda (destination)
(if (and (not (eq (player-at destination)
current-player))
(> (dice source) (dice destination)))
(make-lazy
(list (list (list source destination)
(game-tree (board-attack board
current-player
source
destination
(dice source))
current-player
(+ spare-dice (dice destination))
nil))))
(lazy-nil)))
(make-lazy (neighbors source)))
(lazy-nil)))
(make-lazy (loop for n below *board-hex-count*
collect n)))))
(defun handle-human (tree)
(fresh-line)
(princ "Choose your move: ")
(let ((moves (caddr tree)))
(labels ((print-moves (moves n)
(unless (lazy-null moves)
(let* ((move (lazy-car moves))
(action (car move)))
(fresh-line)
(format t "~a. " n)
(if action
(format t "~a -> ~a" (car action) (cadr action))
(princ "end turn")))
(print-moves (lazy-cdr moves) (1+ n)))))
(print-moves moves 1))
(fresh-line)
(cadr (lazy-nth (1- (read)) moves))))
(defun play-versus-human (tree)
(print-info tree)
(if (not (lazy-null (caddr tree)))
(play-versus-human (handle-human tree))
(announce-winner (cadr tree))))
(defun limit-tree-depth (tree depth)
(list (car tree)
(cadr tree)
(if (zerop depth)
(lazy-nil)
(lazy-mapcar (lambda (move)
(list (car move)
(limit-tree-depth (cadr move) (1- depth))))
(caddr tree)))))
(defun handle-computer (tree)
(let ((ratings (get-ratings (limit-tree-depth tree *ai-level*)
(car tree))))
(cadr (lazy-nth (position (apply #'max ratings) ratings)
(caddr tree)))))
(defun play-versus-computer (tree)
(print-info tree)
(cond ((lazy-null (caddr tree)) (announce-winner (cadr tree)))
((zerop (car tree)) (play-versus-computer (handle-human tree)))
(t (play-versus-computer (handle-computer tree)))))
(defun score-board (board player)
(loop for hex across board
for position from 0
sum (if (eq (car hex) player)
(if (threatened position board)
1
2)
-1)))
(defun threatened (position board)
(let* ((hex (aref board position))
(player (car hex))
(dice (cadr hex)))
(loop for neighbor in (neighbors position)
do (let* ((neighboring-hex (aref board neighbor))
(neighboring-player (car neighboring-hex))
(neighboring-dice (cadr neighboring-hex)))
(when (and (not (eq player neighboring-player))
(> neighboring-dice dice))
(return t))))))
(defun get-ratings (tree player)
(take-all (lazy-mapcar (lambda (move)
(rate-position (cadr move) player))
(caddr tree))))
(defun rate-position (tree player)
(let ((moves (caddr tree)))
(if (not (lazy-null moves))
(apply (if (eq (car tree) player)
#'max
#'min)
(get-ratings tree player))
(score-board (cadr tree) player))))
(defun a-b-get-ratings-max (tree player upper-limit lower-limit)
(labels ((f (moves lower-limit)
(unless (lazy-null moves)
(let ((x (a-b-rate-position (cadr (lazy-car moves))
player
upper-limit
lower-limit)))
(if (>= x upper-limit)
(list x)
(cons x (f (lazy-cdr moves) (max x lower-limit))))))))
(f (caddr tree) lower-limit)))
(defun a-b-get-ratings-min (tree player upper-limit lower-limit)
(labels ((f (moves upper-limit)
(unless (lazy-null moves)
(let ((x (a-b-rate-position (cadr (lazy-car moves))
player
upper-limit
lower-limit)))
(if (<= x lower-limit)
(list x)
(cons x (f (lazy-cdr moves) (min x upper-limit))))))))
(f (caddr tree) upper-limit)))
(defun a-b-rate-position (tree player upper-limit lower-limit)
(let ((moves (caddr tree)))
(if (not (lazy-null moves))
(if (eq (car tree) player)
(apply #'max (a-b-get-ratings-max tree
player
upper-limit
lower-limit))
(apply #'min (a-b-get-ratings-min tree
player
upper-limit
lower-limit)))
(score-board (cadr tree) player))))
(defun handle-computer (tree)
(let ((ratings (a-b-get-ratings-max (limit-tree-depth tree *ai-level*)
(car tree)
most-positive-fixnum
most-negative-fixnum)))
(cadr (lazy-nth (position (apply #'max ratings) ratings) (caddr tree)))))
;; (play-versus-computer (game-tree (generate-board) 0 0 t))