Mercurial > emacs
annotate lisp/play/5x5.el @ 110934:a36fccf2a365
lisp/net/telnet.el (telnet-mode-map): Fix previous change.
author | Juanma Barranquero <lekktu@gmail.com> |
---|---|
date | Tue, 12 Oct 2010 03:48:58 +0200 |
parents | cc035ccb9275 |
children | 417b1e4d63cd |
rev | line source |
---|---|
38425
c6e12c6b1498
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34006
diff
changeset
|
1 ;;; 5x5.el --- simple little puzzle game |
25143 | 2 |
104848
1382a0cd8022
Remove leading * from defcustom and defface docs.
Glenn Morris <rgm@gnu.org>
parents:
104390
diff
changeset
|
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, |
106815 | 4 ;; 2008, 2009, 2010 Free Software Foundation, Inc. |
25143 | 5 |
33906 | 6 ;; Author: Dave Pearson <davep@davep.org> |
7 ;; Maintainer: Dave Pearson <davep@davep.org> | |
25143 | 8 ;; Created: 1998-10-03 |
9 ;; Keywords: games puzzles | |
10 | |
11 ;; This file is part of GNU Emacs. | |
12 | |
94675
949bd6ad1ba4
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
13 ;; GNU Emacs is free software: you can redistribute it and/or modify |
25143 | 14 ;; it under the terms of the GNU General Public License as published by |
94675
949bd6ad1ba4
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
15 ;; the Free Software Foundation, either version 3 of the License, or |
949bd6ad1ba4
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
16 ;; (at your option) any later version. |
25143 | 17 |
18 ;; GNU Emacs is distributed in the hope that it will be useful, | |
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 ;; GNU General Public License for more details. | |
22 | |
23 ;; You should have received a copy of the GNU General Public License | |
94675
949bd6ad1ba4
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
25143 | 25 |
26 ;;; Commentary: | |
27 | |
28 ;; The aim of 5x5 is to fill in all the squares. If you need any more of an | |
29 ;; explanation you probably shouldn't play the game. | |
30 | |
31 ;;; TODO: | |
32 | |
33 ;; o The code for updating the grid needs to be re-done. At the moment it | |
34 ;; simply re-draws the grid every time a move is made. | |
35 ;; | |
96363
f9d35151b907
American English spelling fix.
Glenn Morris <rgm@gnu.org>
parents:
94675
diff
changeset
|
36 ;; o Look into tarting up the display with color. gamegrid.el looks |
25143 | 37 ;; interesting, perhaps that is the way to go? |
38 | |
39 ;;; Thanks: | |
40 | |
41 ;; Ralf Fassel <ralf@akutech.de> for his help and introduction to writing an | |
42 ;; emacs mode. | |
43 ;; | |
44 ;; Pascal Q. Porcupine <joshagam@cs.nmsu.edu> for inspiring the animated | |
45 ;; solver. | |
46 | |
38425
c6e12c6b1498
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34006
diff
changeset
|
47 ;;; Code: |
c6e12c6b1498
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
34006
diff
changeset
|
48 |
25143 | 49 ;; Things we need. |
50 | |
51 (eval-when-compile | |
52 (require 'cl)) | |
53 | |
54 ;; Customize options. | |
55 | |
56 (defgroup 5x5 nil | |
57 "5x5 - Silly little puzzle game." | |
58 :group 'games | |
59 :prefix "5x5-") | |
60 | |
61 (defcustom 5x5-grid-size 5 | |
104848
1382a0cd8022
Remove leading * from defcustom and defface docs.
Glenn Morris <rgm@gnu.org>
parents:
104390
diff
changeset
|
62 "Size of the playing area." |
25143 | 63 :type 'integer |
64 :group '5x5) | |
65 | |
66 (defcustom 5x5-x-scale 4 | |
104848
1382a0cd8022
Remove leading * from defcustom and defface docs.
Glenn Morris <rgm@gnu.org>
parents:
104390
diff
changeset
|
67 "X scaling factor for drawing the grid." |
25143 | 68 :type 'integer |
69 :group '5x5) | |
70 | |
71 (defcustom 5x5-y-scale 3 | |
104848
1382a0cd8022
Remove leading * from defcustom and defface docs.
Glenn Morris <rgm@gnu.org>
parents:
104390
diff
changeset
|
72 "Y scaling factor for drawing the grid." |
25143 | 73 :type 'integer |
74 :group '5x5) | |
75 | |
76 (defcustom 5x5-animate-delay .01 | |
104848
1382a0cd8022
Remove leading * from defcustom and defface docs.
Glenn Morris <rgm@gnu.org>
parents:
104390
diff
changeset
|
77 "Delay in seconds when animating a solution crack." |
25143 | 78 :type 'number |
79 :group '5x5) | |
80 | |
81 (defcustom 5x5-hassle-me t | |
104848
1382a0cd8022
Remove leading * from defcustom and defface docs.
Glenn Morris <rgm@gnu.org>
parents:
104390
diff
changeset
|
82 "Should 5x5 ask you when you want to do a destructive operation?" |
25143 | 83 :type 'boolean |
84 :group '5x5) | |
85 | |
86 (defcustom 5x5-mode-hook nil | |
104848
1382a0cd8022
Remove leading * from defcustom and defface docs.
Glenn Morris <rgm@gnu.org>
parents:
104390
diff
changeset
|
87 "Hook run on starting 5x5." |
25143 | 88 :type 'hook |
89 :group '5x5) | |
90 | |
91 ;; Non-customize variables. | |
92 | |
93 (defvar 5x5-grid nil | |
94 "5x5 grid contents.") | |
95 | |
96 (defvar 5x5-x-pos 2 | |
97 "X position of cursor.") | |
98 | |
99 (defvar 5x5-y-pos 2 | |
100 "Y position of cursor.") | |
101 | |
102 (defvar 5x5-moves 0 | |
103 "Moves made.") | |
104 | |
105 (defvar 5x5-cracking nil | |
106 "Are we in cracking mode?") | |
107 | |
108 (defvar 5x5-buffer-name "*5x5*" | |
109 "Name of the 5x5 play buffer.") | |
110 | |
110909
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
111 (defvar 5x5-mode-map |
25143 | 112 (let ((map (make-sparse-keymap))) |
113 (suppress-keymap map t) | |
114 (define-key map "?" #'describe-mode) | |
115 (define-key map "\r" #'5x5-flip-current) | |
116 (define-key map " " #'5x5-flip-current) | |
117 (define-key map [up] #'5x5-up) | |
118 (define-key map [down] #'5x5-down) | |
119 (define-key map [left] #'5x5-left) | |
120 (define-key map [tab] #'5x5-right) | |
121 (define-key map [right] #'5x5-right) | |
122 (define-key map [(control a)] #'5x5-bol) | |
123 (define-key map [(control e)] #'5x5-eol) | |
25164
8e7561d048f5
(5x5-mode-map): Bind C-p, C-n, C-b, C-f.
Karl Heuer <kwzh@gnu.org>
parents:
25160
diff
changeset
|
124 (define-key map [(control p)] #'5x5-up) |
8e7561d048f5
(5x5-mode-map): Bind C-p, C-n, C-b, C-f.
Karl Heuer <kwzh@gnu.org>
parents:
25160
diff
changeset
|
125 (define-key map [(control n)] #'5x5-down) |
8e7561d048f5
(5x5-mode-map): Bind C-p, C-n, C-b, C-f.
Karl Heuer <kwzh@gnu.org>
parents:
25160
diff
changeset
|
126 (define-key map [(control b)] #'5x5-left) |
8e7561d048f5
(5x5-mode-map): Bind C-p, C-n, C-b, C-f.
Karl Heuer <kwzh@gnu.org>
parents:
25160
diff
changeset
|
127 (define-key map [(control f)] #'5x5-right) |
25143 | 128 (define-key map [home] #'5x5-bol) |
129 (define-key map [end] #'5x5-eol) | |
130 (define-key map [prior] #'5x5-first) | |
131 (define-key map [next] #'5x5-last) | |
132 (define-key map "r" #'5x5-randomize) | |
133 (define-key map [(control c) (control r)] #'5x5-crack-randomly) | |
134 (define-key map [(control c) (control c)] #'5x5-crack-mutating-current) | |
135 (define-key map [(control c) (control b)] #'5x5-crack-mutating-best) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
136 (define-key map [(control c) (control x)] #'5x5-crack-xor-mutate) |
25143 | 137 (define-key map "n" #'5x5-new-game) |
138 (define-key map "q" #'5x5-quit-game) | |
110909
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
139 map) |
cc035ccb9275
Declare and define in one step various mode maps.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
106815
diff
changeset
|
140 "Local keymap for the 5x5 game.") |
25143 | 141 |
142 ;; Menu definition. | |
143 | |
144 (easy-menu-define 5x5-mode-menu 5x5-mode-map "5x5 menu." | |
145 '("5x5" | |
146 ["New game" 5x5-new-game t] | |
147 ["Random game" 5x5-randomize t] | |
148 ["Quit game" 5x5-quit-game t] | |
149 "---" | |
150 ["Crack randomly" 5x5-crack-randomly t] | |
151 ["Crack mutating current" 5x5-crack-mutating-current t] | |
152 ["Crack mutating best" 5x5-crack-mutating-best t] | |
153 ["Crack with xor mutate" 5x5-crack-xor-mutate t])) | |
154 | |
155 ;; Gameplay functions. | |
156 | |
157 (put '5x5-mode 'mode-class 'special) | |
158 | |
159 (defun 5x5-mode () | |
75916
1cea4c7de5cf
(5x5-crack-xor-mutate): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75347
diff
changeset
|
160 "A mode for playing `5x5'. |
25143 | 161 |
162 The key bindings for 5x5-mode are: | |
163 | |
164 \\{5x5-mode-map}" | |
165 (kill-all-local-variables) | |
166 (use-local-map 5x5-mode-map) | |
167 (setq major-mode '5x5-mode | |
168 mode-name "5x5") | |
62770 | 169 (run-mode-hooks '5x5-mode-hook) |
25143 | 170 (setq buffer-read-only t |
171 truncate-lines t) | |
62770 | 172 (buffer-disable-undo)) |
25143 | 173 |
174 ;;;###autoload | |
175 (defun 5x5 (&optional size) | |
176 "Play 5x5. | |
177 | |
178 The object of 5x5 is very simple, by moving around the grid and flipping | |
179 squares you must fill the grid. | |
180 | |
181 5x5 keyboard bindings are: | |
182 \\<5x5-mode-map> | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
183 Flip \\[5x5-flip-current] |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
184 Move up \\[5x5-up] |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
185 Move down \\[5x5-down] |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
186 Move left \\[5x5-left] |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
187 Move right \\[5x5-right] |
25143 | 188 Start new game \\[5x5-new-game] |
189 New game with random grid \\[5x5-randomize] | |
190 Random cracker \\[5x5-crack-randomly] | |
191 Mutate current cracker \\[5x5-crack-mutating-current] | |
25164
8e7561d048f5
(5x5-mode-map): Bind C-p, C-n, C-b, C-f.
Karl Heuer <kwzh@gnu.org>
parents:
25160
diff
changeset
|
192 Mutate best cracker \\[5x5-crack-mutating-best] |
25143 | 193 Mutate xor cracker \\[5x5-crack-xor-mutate] |
194 Quit current game \\[5x5-quit-game]" | |
195 | |
196 (interactive "P") | |
197 (setq 5x5-cracking nil) | |
198 (when size | |
199 (setq 5x5-grid-size size)) | |
200 (switch-to-buffer 5x5-buffer-name) | |
201 (if (or (not 5x5-grid) (not (= 5x5-grid-size (length (aref 5x5-grid 0))))) | |
202 (5x5-new-game)) | |
203 (5x5-draw-grid (list 5x5-grid)) | |
204 (5x5-position-cursor) | |
205 (5x5-mode)) | |
206 | |
207 (defun 5x5-new-game () | |
208 "Start a new game of `5x5'." | |
209 (interactive) | |
105372
bd2966850aac
Use `called-interactively-p' instead of `interactive-p'.
Juanma Barranquero <lekktu@gmail.com>
parents:
104848
diff
changeset
|
210 (when (if (called-interactively-p 'interactive) |
bd2966850aac
Use `called-interactively-p' instead of `interactive-p'.
Juanma Barranquero <lekktu@gmail.com>
parents:
104848
diff
changeset
|
211 (5x5-y-or-n-p "Start a new game? ") t) |
25143 | 212 (setq 5x5-x-pos (/ 5x5-grid-size 2) |
213 5x5-y-pos (/ 5x5-grid-size 2) | |
214 5x5-moves 0 | |
215 5x5-grid (5x5-make-move (5x5-make-new-grid) 5x5-y-pos 5x5-x-pos)) | |
57826
26b411f0feaf
(5x5-new-game): Set up the buffer even if not interactive.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
216 (5x5-draw-grid (list 5x5-grid)) |
26b411f0feaf
(5x5-new-game): Set up the buffer even if not interactive.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
217 (5x5-position-cursor))) |
25143 | 218 |
219 (defun 5x5-quit-game () | |
220 "Quit the current game of `5x5'." | |
221 (interactive) | |
222 (kill-buffer 5x5-buffer-name)) | |
223 | |
224 (defun 5x5-make-new-grid () | |
225 "Create and return a new `5x5' grid structure." | |
226 (let ((grid (make-vector 5x5-grid-size nil))) | |
227 (loop for y from 0 to (1- 5x5-grid-size) do | |
228 (aset grid y (make-vector 5x5-grid-size nil))) | |
229 grid)) | |
230 | |
231 (defun 5x5-cell (grid y x) | |
232 "Return the value of the cell in GRID at location X,Y." | |
233 (aref (aref grid y) x)) | |
234 | |
235 (defun 5x5-set-cell (grid y x value) | |
236 "Set the value of cell X,Y in GRID to VALUE." | |
237 (aset (aref grid y) x value)) | |
238 | |
239 (defun 5x5-flip-cell (grid y x) | |
240 "Flip the value of cell X,Y in GRID." | |
241 (5x5-set-cell grid y x (not (5x5-cell grid y x)))) | |
242 | |
243 (defun 5x5-copy-grid (grid) | |
244 "Make a new copy of GRID." | |
245 (let ((copy (5x5-make-new-grid))) | |
246 (loop for y from 0 to (1- 5x5-grid-size) do | |
247 (loop for x from 0 to (1- 5x5-grid-size) do | |
248 (5x5-set-cell copy y x (5x5-cell grid y x)))) | |
249 copy)) | |
250 | |
251 (defun 5x5-make-move (grid row col) | |
252 "Make a move on GRID at row ROW and column COL." | |
253 (5x5-flip-cell grid row col) | |
254 (if (> row 0) | |
255 (5x5-flip-cell grid (1- row) col)) | |
256 (if (< row (- 5x5-grid-size 1)) | |
257 (5x5-flip-cell grid (1+ row) col)) | |
258 (if (> col 0) | |
259 (5x5-flip-cell grid row (1- col))) | |
260 (if (< col (- 5x5-grid-size 1)) | |
261 (5x5-flip-cell grid row (1+ col))) | |
262 grid) | |
263 | |
264 (defun 5x5-row-value (row) | |
265 "Get the \"on-value\" for grid row ROW." | |
266 (loop for y from 0 to (1- 5x5-grid-size) sum (if (aref row y) 1 0))) | |
267 | |
268 (defun 5x5-grid-value (grid) | |
269 "Get the \"on-value\" for grid GRID." | |
270 (loop for y from 0 to (1- 5x5-grid-size) sum (5x5-row-value (aref grid y)))) | |
271 | |
272 (defun 5x5-draw-grid-end () | |
75916
1cea4c7de5cf
(5x5-crack-xor-mutate): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75347
diff
changeset
|
273 "Draw the top/bottom of the grid." |
25143 | 274 (insert "+") |
275 (loop for x from 0 to (1- 5x5-grid-size) do | |
276 (insert "-" (make-string 5x5-x-scale ?-))) | |
277 (insert "-+ ")) | |
278 | |
279 (defun 5x5-draw-grid (grids) | |
280 "Draw the grids GRIDS into the current buffer." | |
281 (let ((buffer-read-only nil)) | |
282 (erase-buffer) | |
283 (loop for grid in grids do (5x5-draw-grid-end)) | |
284 (insert "\n") | |
285 (loop for y from 0 to (1- 5x5-grid-size) do | |
286 (loop for lines from 0 to (1- 5x5-y-scale) do | |
287 (loop for grid in grids do | |
288 (loop for x from 0 to (1- 5x5-grid-size) do | |
289 (insert (if (zerop x) "| " " ") | |
290 (make-string 5x5-x-scale | |
291 (if (5x5-cell grid y x) ?# ?.)))) | |
292 (insert " | ")) | |
293 (insert "\n"))) | |
294 (loop for grid in grids do (5x5-draw-grid-end)) | |
295 (insert "\n") | |
296 (insert (format "On: %d Moves: %d" (5x5-grid-value (car grids)) 5x5-moves)))) | |
297 | |
298 (defun 5x5-position-cursor () | |
299 "Position the cursor on the grid." | |
104390
c6ae8d43800c
Use forward-line, not goto-line.
Glenn Morris <rgm@gnu.org>
parents:
100908
diff
changeset
|
300 (goto-char (point-min)) |
c6ae8d43800c
Use forward-line, not goto-line.
Glenn Morris <rgm@gnu.org>
parents:
100908
diff
changeset
|
301 (forward-line (1+ (* 5x5-y-pos 5x5-y-scale))) |
25143 | 302 (goto-char (+ (point) (* 5x5-x-pos 5x5-x-scale) (+ 5x5-x-pos 1) 1))) |
303 | |
304 (defun 5x5-made-move () | |
305 "Keep track of how many moves have been made." | |
306 (incf 5x5-moves)) | |
307 | |
308 (defun 5x5-make-random-grid () | |
309 "Make a random grid." | |
310 (let ((grid (5x5-make-new-grid))) | |
311 (loop for y from 0 to (1- 5x5-grid-size) do | |
312 (loop for x from 0 to (1- 5x5-grid-size) do | |
313 (if (zerop (random 2)) | |
314 (5x5-flip-cell grid y x)))) | |
315 grid)) | |
316 | |
317 ;; Cracker functions. | |
318 | |
319 ;;;###autoload | |
320 (defun 5x5-crack-randomly () | |
321 "Attempt to crack 5x5 using random solutions." | |
322 (interactive) | |
323 (5x5-crack #'5x5-make-random-solution)) | |
324 | |
325 ;;;###autoload | |
326 (defun 5x5-crack-mutating-current () | |
327 "Attempt to crack 5x5 by mutating the current solution." | |
328 (interactive) | |
329 (5x5-crack #'5x5-make-mutate-current)) | |
330 | |
331 ;;;###autoload | |
332 (defun 5x5-crack-mutating-best () | |
333 "Attempt to crack 5x5 by mutating the best solution." | |
334 (interactive) | |
335 (5x5-crack #'5x5-make-mutate-best)) | |
336 | |
337 ;;;###autoload | |
338 (defun 5x5-crack-xor-mutate () | |
75916
1cea4c7de5cf
(5x5-crack-xor-mutate): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75347
diff
changeset
|
339 "Attempt to crack 5x5 by xoring the current and best solution. |
41950
ac3936e792ac
(5x5-crack-xor-mutate): Doc fix.
Pavel Janík <Pavel@Janik.cz>
parents:
38425
diff
changeset
|
340 Mutate the result." |
25143 | 341 (interactive) |
342 (5x5-crack #'5x5-make-xor-with-mutation)) | |
343 | |
344 ;;;###autoload | |
345 (defun 5x5-crack (breeder) | |
346 "Attempt to find a solution for 5x5. | |
347 | |
348 5x5-crack takes the argument BREEDER which should be a function that takes | |
349 two parameters, the first will be a grid vector array that is the current | |
75916
1cea4c7de5cf
(5x5-crack-xor-mutate): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75347
diff
changeset
|
350 solution and the second will be the best solution so far. The function |
25143 | 351 should return a grid vector array that is the new solution." |
352 | |
353 (interactive "aBreeder function: ") | |
354 (5x5) | |
355 (setq 5x5-cracking t) | |
356 (let* ((best-solution (5x5-make-random-grid)) | |
357 (current-solution best-solution) | |
358 (best-result (5x5-make-new-grid)) | |
359 (current-result (5x5-make-new-grid)) | |
360 (target (* 5x5-grid-size 5x5-grid-size))) | |
361 (while (and (< (5x5-grid-value best-result) target) | |
362 (not (input-pending-p))) | |
363 (setq current-result (5x5-play-solution current-solution best-solution)) | |
364 (if (> (5x5-grid-value current-result) (5x5-grid-value best-result)) | |
365 (setq best-solution current-solution | |
366 best-result current-result)) | |
367 (setq current-solution (funcall breeder | |
368 (5x5-copy-grid current-solution) | |
369 (5x5-copy-grid best-solution))))) | |
370 (setq 5x5-cracking nil)) | |
371 | |
372 (defun 5x5-make-random-solution (&rest ignore) | |
373 "Make a random solution." | |
374 (5x5-make-random-grid)) | |
375 | |
376 (defun 5x5-make-mutate-current (current best) | |
377 "Mutate the current solution." | |
378 (5x5-mutate-solution current)) | |
379 | |
380 (defun 5x5-make-mutate-best (current best) | |
381 "Mutate the best solution." | |
382 (5x5-mutate-solution best)) | |
383 | |
384 (defun 5x5-make-xor-with-mutation (current best) | |
75916
1cea4c7de5cf
(5x5-crack-xor-mutate): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75347
diff
changeset
|
385 "Xor current and best solution then mutate the result." |
25143 | 386 (let ((xored (5x5-make-new-grid))) |
387 (loop for y from 0 to (1- 5x5-grid-size) do | |
388 (loop for x from 0 to (1- 5x5-grid-size) do | |
389 (5x5-set-cell xored y x | |
390 (5x5-xor (5x5-cell current y x) | |
391 (5x5-cell best y x))))) | |
392 (5x5-mutate-solution xored))) | |
393 | |
394 (defun 5x5-mutate-solution (solution) | |
395 "Randomly flip bits in the solution." | |
396 (loop for y from 0 to (1- 5x5-grid-size) do | |
397 (loop for x from 0 to (1- 5x5-grid-size) do | |
398 (if (= (random (/ (* 5x5-grid-size 5x5-grid-size) 2)) | |
399 (/ (/ (* 5x5-grid-size 5x5-grid-size) 2) 2)) | |
400 (5x5-flip-cell solution y x)))) | |
401 solution) | |
402 | |
403 (defun 5x5-play-solution (solution best) | |
75916
1cea4c7de5cf
(5x5-crack-xor-mutate): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75347
diff
changeset
|
404 "Play a solution on an empty grid. This destroys the current game |
1cea4c7de5cf
(5x5-crack-xor-mutate): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75347
diff
changeset
|
405 in progress because it is an animated attempt." |
25143 | 406 (5x5-new-game) |
33906 | 407 (let ((inhibit-quit t)) |
408 (loop for y from 0 to (1- 5x5-grid-size) do | |
409 (loop for x from 0 to (1- 5x5-grid-size) do | |
410 (setq 5x5-y-pos y | |
411 5x5-x-pos x) | |
412 (if (5x5-cell solution y x) | |
413 (5x5-flip-current)) | |
414 (5x5-draw-grid (list 5x5-grid solution best)) | |
415 (5x5-position-cursor) | |
416 (sit-for 5x5-animate-delay)))) | |
25143 | 417 5x5-grid) |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
418 |
25143 | 419 ;; Keyboard response functions. |
420 | |
421 (defun 5x5-flip-current () | |
422 "Make a move on the current cursor location." | |
423 (interactive) | |
424 (setq 5x5-grid (5x5-make-move 5x5-grid 5x5-y-pos 5x5-x-pos)) | |
425 (5x5-made-move) | |
426 (unless 5x5-cracking | |
427 (5x5-draw-grid (list 5x5-grid))) | |
428 (5x5-position-cursor) | |
429 (when (= (5x5-grid-value 5x5-grid) (* 5x5-grid-size 5x5-grid-size)) | |
430 (beep) | |
431 (message "You win!"))) | |
432 | |
433 (defun 5x5-up () | |
434 "Move up." | |
435 (interactive) | |
436 (unless (zerop 5x5-y-pos) | |
437 (decf 5x5-y-pos) | |
438 (5x5-position-cursor))) | |
439 | |
440 (defun 5x5-down () | |
441 "Move down." | |
442 (interactive) | |
443 (unless (= 5x5-y-pos (1- 5x5-grid-size)) | |
444 (incf 5x5-y-pos) | |
445 (5x5-position-cursor))) | |
446 | |
447 (defun 5x5-left () | |
448 "Move left." | |
449 (interactive) | |
450 (unless (zerop 5x5-x-pos) | |
451 (decf 5x5-x-pos) | |
452 (5x5-position-cursor))) | |
453 | |
454 (defun 5x5-right () | |
455 "Move right." | |
456 (interactive) | |
457 (unless (= 5x5-x-pos (1- 5x5-grid-size)) | |
458 (incf 5x5-x-pos) | |
459 (5x5-position-cursor))) | |
460 | |
461 (defun 5x5-bol () | |
462 "Move to beginning of line." | |
463 (interactive) | |
464 (setq 5x5-x-pos 0) | |
465 (5x5-position-cursor)) | |
466 | |
467 (defun 5x5-eol () | |
468 "Move to end of line." | |
469 (interactive) | |
470 (setq 5x5-x-pos (1- 5x5-grid-size)) | |
471 (5x5-position-cursor)) | |
472 | |
473 (defun 5x5-first () | |
474 "Move to the first cell." | |
475 (interactive) | |
476 (setq 5x5-x-pos 0 | |
477 5x5-y-pos 0) | |
478 (5x5-position-cursor)) | |
479 | |
480 (defun 5x5-last () | |
481 "Move to the last cell." | |
482 (interactive) | |
483 (setq 5x5-x-pos (1- 5x5-grid-size) | |
484 5x5-y-pos (1- 5x5-grid-size)) | |
485 (5x5-position-cursor)) | |
486 | |
487 (defun 5x5-randomize () | |
488 "Randomize the grid." | |
489 (interactive) | |
490 (when (5x5-y-or-n-p "Start a new game with a random grid? ") | |
491 (setq 5x5-x-pos (/ 5x5-grid-size 2) | |
492 5x5-y-pos (/ 5x5-grid-size 2) | |
493 5x5-moves 0 | |
494 5x5-grid (5x5-make-random-grid)) | |
495 (unless 5x5-cracking | |
496 (5x5-draw-grid (list 5x5-grid))) | |
497 (5x5-position-cursor))) | |
498 | |
499 ;; Support functions | |
500 | |
501 (defun 5x5-xor (x y) | |
502 "Boolean exclusive-or of X and Y." | |
503 (and (or x y) (not (and x y)))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
504 |
25143 | 505 (defun 5x5-y-or-n-p (prompt) |
75916
1cea4c7de5cf
(5x5-crack-xor-mutate): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75347
diff
changeset
|
506 "5x5 wrapper for `y-or-n-p' which respects the `5x5-hassle-me' setting." |
25143 | 507 (if 5x5-hassle-me |
508 (y-or-n-p prompt) | |
509 t)) | |
510 | |
76953 | 511 (random t) |
512 | |
25143 | 513 (provide '5x5) |
514 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
515 ;; arch-tag: ec4dabd5-572d-41ea-b48c-ec5ce0d68fa9 |
25143 | 516 ;;; 5x5.el ends here |