-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.lisp
249 lines (219 loc) · 7.35 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
;;;; game.lisp
(in-package #:game)
(defun make-counter ()
(let ((count 0))
(lambda ()
(setq count (+ 1 count)))))
(defun board-print-data (board)
(minesweeper:board-collect board (lambda (b coord)
(cons coord (minesweeper:board-tile-at b coord)))))
(defun game-bombs-locations (board)
(flatten (minesweeper:board-collect board
(lambda (b coord)
(let ((tile (minesweeper:board-tile-at b coord)))
(if (eq 't (tile-bomb tile))
(cons coord tile)))))))
(defun game-print-debug (game)
(let ((string ""))
(dolist (row-data (board-print-data (game-board game)))
(setf string (format t "~C ~C" #\linefeed #\linefeed ))
(dolist (column row-data)
(setf string (format t "|~A ~A,~A ~A ~A" (determine-char column) (minesweeper:xy-x (car column)) (minesweeper:xy-y (car column)) (minesweeper:tile-bomb-count (cdr column)) #\linefeed ))
))
(format t "~s ~C" string #\linefeed)))
(defun determine-char (data)
(let ((tile (cdr data)))
(cond
((and (eq 't (minesweeper:tile-open tile))
(eq 't (minesweeper:tile-bomb tile))) "!")
((eq 't (minesweeper:tile-open tile))
(if (= 0 (minesweeper:tile-bomb-count tile))
"."
(write-to-string (minesweeper:tile-bomb-count tile))))
(t "X"))))
(defun game-print (game)
(let ((string ""))
(dolist (row-data (board-print-data (game-board game)))
(setf string (format t "~C ~C" #\linefeed #\linefeed ))
(dolist (column row-data)
(setf string (format t "|~2,'0d: ~A" (minesweeper:tile-id (cdr column)) (determine-char column) #\linefeed ))
))
(format t "~a ~C" string #\linefeed)))
;; Game
(defstruct game board win turn-counter)
(defun collect-closed-tiles (game)
(flatten
(board-collect (game-board game)
(lambda (board coord)
(if (not (eq 't (minesweeper:tile-open (minesweeper:board-tile-at board coord))))
(minesweeper:board-tile-at board coord))))))
(defun only-mines-left? (game)
(every
(lambda (tile)
(eq (minesweeper:tile-bomb tile) 't))
(collect-closed-tiles game)))
(defun game-finished-condition-p (game)
(cond
((eq 'f (game-win game)) 't)
((only-mines-left? game)
(progn
(setf (game-win game) 't)
't))
(t 'f)))
(defvar *game*)
(defun determine-widget (x)
(cond
((and (eq 't (minesweeper:tile-open x))
(eq 't (minesweeper:tile-bomb x)))
(make-instance 'gtk-button
:label
"BOMB!"))
((and (eq 't (minesweeper:tile-open x)) (= 0 (minesweeper:tile-bomb-count x)))
(make-instance 'gtk-button
:label
"-"))
((and (eq 't (minesweeper:tile-open x)))
(make-instance 'gtk-button
:label
(write-to-string (minesweeper:tile-bomb-count x))))
(t
(make-instance 'gtk-button
:label
""))))
(defun gui-table (game window)
(let ((table (make-instance 'gtk-table
:n-rows 8
:n-columns 8
:row-spacing 0
:column-spacing 0
:homogeneous nil)))
(dotimes (i 8)
(dotimes (j 8)
(let ((b (determine-widget (board-tile-at
(game-board game)
(minesweeper:make-xy :x i :y j) ))))
(g-signal-connect b "clicked"
((lambda (i j)
(lambda (button)
(declare (ignore button))
(evolve game window table
(minesweeper:make-xy :x i :y j)))) i j))
(gtk-table-attach table
b
i (+ i 1) j (+ j 1)))))
table))
(defun gui-replace-table (game window table)
(gtk-container-remove window table)
(let ((table (gui-table game window)))
(gtk-container-add window table)
(gtk-widget-show-all window)))
(defun gui-win (game window table)
(gtk-container-remove window table)
(let ((table (gui-table game window))
(vbox1 (make-instance 'gtk-box
:orientation :vertical
:homogeneous nil
:spacing 6))
(label (make-instance 'gtk-label :label "YOU WIN")))
(gtk-box-pack-start vbox1 label)
(gtk-box-pack-start vbox1 table)
(gtk-container-add window vbox1)
(gtk-widget-show-all window)))
(defun gui-lose (game window table)
(gtk-container-remove window table)
(let ((table (gui-table game window))
(vbox1 (make-instance 'gtk-box
:orientation :vertical
:homogeneous nil
:spacing 6))
(label (make-instance 'gtk-label :label "YOU LOSE")))
(gtk-box-pack-start vbox1 label)
(gtk-box-pack-start vbox1 table)
(gtk-container-add window vbox1)
(gtk-widget-show-all window)))
(defun evolve (game window table coord)
(let ((tile (game-tile-open2! game coord)))
(check-and-set-win-flag game tile )
(cond ((eq 'f (game-win game))
(progn
(gui-lose game window table)))
((eq 't (game-win game))
(gui-win game window table))
(t (gui-replace-table game window table)))))
(defun play-gui ()
(let ((game (make-game :turn-counter (make-counter)
:board (minesweeper:board-init 8))))
(setf *game* game)
(within-main-loop
(let* ((window (make-instance 'gtk-window
:type :toplevel
:title "Minesweeper"
:border-width 12
:default-width 300))
(table (gui-table game window))
(quit (make-instance 'gtk-button
:label "Quit")))
(g-signal-connect window "destroy"
(lambda (widget)
(declare (ignore widget))
(leave-gtk-main)))
(g-signal-connect quit "clicked"
(lambda (widget)
(declare (ignore widget))
(gtk-widget-destroy window)))
(gtk-table-attach table quit 0 2 1 2)
(gtk-container-add window table)
(gtk-widget-show-all window)))))
(defun play-game ()
(let ((game (make-game :board (minesweeper:board-init 8))))
(setf *game* game)
(loop while (not (eq 't (game-finished-condition-p game))) do
(game-print game)
(setq id (minesweeper:prompt-read "Enter #: "))
(game-tile-open! game
(minesweeper:tile-id-to-coord
(game-board game) id)))
(game-print game)
(cond ((eq 'f (game-win game))
(print "You Lose"))
((eq 't (game-win game))
(print "You win")))))
(defun check-and-set-win-flag (game tile)
(cond
((eq 't (minesweeper:tile-bomb tile))
(setf (game-win game) 'f))
(t (if (only-mines-left? game)
(setf (game-win game) 't)))))
(defun game-tile-open2! (game coord)
(let* ((board (game-board game))
(tile (minesweeper:board-tile-at board coord)))
(print (format t "Opening tile: ~A" coord))
(setf (minesweeper:tile-open tile) 't)
(if (and
(not (eq 't (minesweeper:tile-bomb tile)))
(= 0 (minesweeper:tile-bomb-count tile)))
(dolist (n (minesweeper:board-tile-neighbors board coord))
(if (not (eq 't (minesweeper:tile-open (minesweeper:board-tile-at board n))))
(game-tile-open2! game n))))
tile))
(defun game-tile-open! (game coord)
(let* ((board (game-board game))
(tile (minesweeper:board-tile-at board coord)))
(cond
((eq 't (minesweeper:tile-bomb tile))
(progn
(setf (minesweeper:tile-open tile) 't)
(setf (game-win game) 'f)))
(t
(progn
(print (format t "Opening tile: ~A" coord))
(setf (minesweeper:tile-open tile) 't)
(if (only-mines-left? game)
(setf (game-win game) 't))
(if (and 't
(= 0 (minesweeper:tile-bomb-count tile)))
(dolist (n (minesweeper:board-tile-neighbors board coord))
(if (and
(not (eq 't (minesweeper:tile-bomb tile)))
(not (eq 't (minesweeper:tile-open (minesweeper:board-tile-at board n)))))
(game-tile-open! game n)))))))))