Mercurial > emacs
annotate lisp/play/5x5.el @ 97334:07a0633dcf4c
* configure.in (NS_HAVE_INTEGER): Rename to NS_HAVE_NSINTEGER. (C_SWITCH_X_SYSTEM): Drop -MMD -MP under NS_IMPL_GNUstep.* configure: Regenerate.
author | Adrian Robert <Adrian.B.Robert@gmail.com> |
---|---|
date | Wed, 06 Aug 2008 16:19:43 +0000 |
parents | f9d35151b907 |
children | a9dc0e7c3f2b |
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 |
74509 | 3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, |
79716 | 4 ;; 2005, 2006, 2007, 2008 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 | |
62 "*Size of the playing area." | |
63 :type 'integer | |
64 :group '5x5) | |
65 | |
66 (defcustom 5x5-x-scale 4 | |
67 "*X scaling factor for drawing the grid." | |
68 :type 'integer | |
69 :group '5x5) | |
70 | |
71 (defcustom 5x5-y-scale 3 | |
72 "*Y scaling factor for drawing the grid." | |
73 :type 'integer | |
74 :group '5x5) | |
75 | |
76 (defcustom 5x5-animate-delay .01 | |
77 "*Delay in seconds when animating a solution crack." | |
78 :type 'number | |
79 :group '5x5) | |
80 | |
81 (defcustom 5x5-hassle-me t | |
82 "*Should 5x5 ask you when you want to do a destructive operation?" | |
83 :type 'boolean | |
84 :group '5x5) | |
85 | |
86 (defcustom 5x5-mode-hook nil | |
87 "*Hook run on starting 5x5." | |
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 | |
111 (defvar 5x5-mode-map nil | |
112 "Local keymap for the 5x5 game.") | |
113 | |
114 ;; Keymap. | |
115 | |
116 (unless 5x5-mode-map | |
117 (let ((map (make-sparse-keymap))) | |
118 (suppress-keymap map t) | |
119 (define-key map "?" #'describe-mode) | |
120 (define-key map "\r" #'5x5-flip-current) | |
121 (define-key map " " #'5x5-flip-current) | |
122 (define-key map [up] #'5x5-up) | |
123 (define-key map [down] #'5x5-down) | |
124 (define-key map [left] #'5x5-left) | |
125 (define-key map [tab] #'5x5-right) | |
126 (define-key map [right] #'5x5-right) | |
127 (define-key map [(control a)] #'5x5-bol) | |
128 (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
|
129 (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
|
130 (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
|
131 (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
|
132 (define-key map [(control f)] #'5x5-right) |
25143 | 133 (define-key map [home] #'5x5-bol) |
134 (define-key map [end] #'5x5-eol) | |
135 (define-key map [prior] #'5x5-first) | |
136 (define-key map [next] #'5x5-last) | |
137 (define-key map "r" #'5x5-randomize) | |
138 (define-key map [(control c) (control r)] #'5x5-crack-randomly) | |
139 (define-key map [(control c) (control c)] #'5x5-crack-mutating-current) | |
140 (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
|
141 (define-key map [(control c) (control x)] #'5x5-crack-xor-mutate) |
25143 | 142 (define-key map "n" #'5x5-new-game) |
143 (define-key map "q" #'5x5-quit-game) | |
144 (setq 5x5-mode-map map))) | |
145 | |
146 ;; Menu definition. | |
147 | |
148 (easy-menu-define 5x5-mode-menu 5x5-mode-map "5x5 menu." | |
149 '("5x5" | |
150 ["New game" 5x5-new-game t] | |
151 ["Random game" 5x5-randomize t] | |
152 ["Quit game" 5x5-quit-game t] | |
153 "---" | |
154 ["Crack randomly" 5x5-crack-randomly t] | |
155 ["Crack mutating current" 5x5-crack-mutating-current t] | |
156 ["Crack mutating best" 5x5-crack-mutating-best t] | |
157 ["Crack with xor mutate" 5x5-crack-xor-mutate t])) | |
158 | |
159 ;; Gameplay functions. | |
160 | |
161 (put '5x5-mode 'mode-class 'special) | |
162 | |
163 (defun 5x5-mode () | |
75916
1cea4c7de5cf
(5x5-crack-xor-mutate): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75347
diff
changeset
|
164 "A mode for playing `5x5'. |
25143 | 165 |
166 The key bindings for 5x5-mode are: | |
167 | |
168 \\{5x5-mode-map}" | |
169 (kill-all-local-variables) | |
170 (use-local-map 5x5-mode-map) | |
171 (setq major-mode '5x5-mode | |
172 mode-name "5x5") | |
62770 | 173 (run-mode-hooks '5x5-mode-hook) |
25143 | 174 (setq buffer-read-only t |
175 truncate-lines t) | |
62770 | 176 (buffer-disable-undo)) |
25143 | 177 |
178 ;;;###autoload | |
179 (defun 5x5 (&optional size) | |
180 "Play 5x5. | |
181 | |
182 The object of 5x5 is very simple, by moving around the grid and flipping | |
183 squares you must fill the grid. | |
184 | |
185 5x5 keyboard bindings are: | |
186 \\<5x5-mode-map> | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
187 Flip \\[5x5-flip-current] |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
188 Move up \\[5x5-up] |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
189 Move down \\[5x5-down] |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
190 Move left \\[5x5-left] |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
191 Move right \\[5x5-right] |
25143 | 192 Start new game \\[5x5-new-game] |
193 New game with random grid \\[5x5-randomize] | |
194 Random cracker \\[5x5-crack-randomly] | |
195 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
|
196 Mutate best cracker \\[5x5-crack-mutating-best] |
25143 | 197 Mutate xor cracker \\[5x5-crack-xor-mutate] |
198 Quit current game \\[5x5-quit-game]" | |
199 | |
200 (interactive "P") | |
201 (setq 5x5-cracking nil) | |
202 (when size | |
203 (setq 5x5-grid-size size)) | |
204 (switch-to-buffer 5x5-buffer-name) | |
205 (if (or (not 5x5-grid) (not (= 5x5-grid-size (length (aref 5x5-grid 0))))) | |
206 (5x5-new-game)) | |
207 (5x5-draw-grid (list 5x5-grid)) | |
208 (5x5-position-cursor) | |
209 (5x5-mode)) | |
210 | |
211 (defun 5x5-new-game () | |
212 "Start a new game of `5x5'." | |
213 (interactive) | |
214 (when (if (interactive-p) (5x5-y-or-n-p "Start a new game? ") t) | |
215 (setq 5x5-x-pos (/ 5x5-grid-size 2) | |
216 5x5-y-pos (/ 5x5-grid-size 2) | |
217 5x5-moves 0 | |
218 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
|
219 (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
|
220 (5x5-position-cursor))) |
25143 | 221 |
222 (defun 5x5-quit-game () | |
223 "Quit the current game of `5x5'." | |
224 (interactive) | |
225 (kill-buffer 5x5-buffer-name)) | |
226 | |
227 (defun 5x5-make-new-grid () | |
228 "Create and return a new `5x5' grid structure." | |
229 (let ((grid (make-vector 5x5-grid-size nil))) | |
230 (loop for y from 0 to (1- 5x5-grid-size) do | |
231 (aset grid y (make-vector 5x5-grid-size nil))) | |
232 grid)) | |
233 | |
234 (defun 5x5-cell (grid y x) | |
235 "Return the value of the cell in GRID at location X,Y." | |
236 (aref (aref grid y) x)) | |
237 | |
238 (defun 5x5-set-cell (grid y x value) | |
239 "Set the value of cell X,Y in GRID to VALUE." | |
240 (aset (aref grid y) x value)) | |
241 | |
242 (defun 5x5-flip-cell (grid y x) | |
243 "Flip the value of cell X,Y in GRID." | |
244 (5x5-set-cell grid y x (not (5x5-cell grid y x)))) | |
245 | |
246 (defun 5x5-copy-grid (grid) | |
247 "Make a new copy of GRID." | |
248 (let ((copy (5x5-make-new-grid))) | |
249 (loop for y from 0 to (1- 5x5-grid-size) do | |
250 (loop for x from 0 to (1- 5x5-grid-size) do | |
251 (5x5-set-cell copy y x (5x5-cell grid y x)))) | |
252 copy)) | |
253 | |
254 (defun 5x5-make-move (grid row col) | |
255 "Make a move on GRID at row ROW and column COL." | |
256 (5x5-flip-cell grid row col) | |
257 (if (> row 0) | |
258 (5x5-flip-cell grid (1- row) col)) | |
259 (if (< row (- 5x5-grid-size 1)) | |
260 (5x5-flip-cell grid (1+ row) col)) | |
261 (if (> col 0) | |
262 (5x5-flip-cell grid row (1- col))) | |
263 (if (< col (- 5x5-grid-size 1)) | |
264 (5x5-flip-cell grid row (1+ col))) | |
265 grid) | |
266 | |
267 (defun 5x5-row-value (row) | |
268 "Get the \"on-value\" for grid row ROW." | |
269 (loop for y from 0 to (1- 5x5-grid-size) sum (if (aref row y) 1 0))) | |
270 | |
271 (defun 5x5-grid-value (grid) | |
272 "Get the \"on-value\" for grid GRID." | |
273 (loop for y from 0 to (1- 5x5-grid-size) sum (5x5-row-value (aref grid y)))) | |
274 | |
275 (defun 5x5-draw-grid-end () | |
75916
1cea4c7de5cf
(5x5-crack-xor-mutate): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75347
diff
changeset
|
276 "Draw the top/bottom of the grid." |
25143 | 277 (insert "+") |
278 (loop for x from 0 to (1- 5x5-grid-size) do | |
279 (insert "-" (make-string 5x5-x-scale ?-))) | |
280 (insert "-+ ")) | |
281 | |
282 (defun 5x5-draw-grid (grids) | |
283 "Draw the grids GRIDS into the current buffer." | |
284 (let ((buffer-read-only nil)) | |
285 (erase-buffer) | |
286 (loop for grid in grids do (5x5-draw-grid-end)) | |
287 (insert "\n") | |
288 (loop for y from 0 to (1- 5x5-grid-size) do | |
289 (loop for lines from 0 to (1- 5x5-y-scale) do | |
290 (loop for grid in grids do | |
291 (loop for x from 0 to (1- 5x5-grid-size) do | |
292 (insert (if (zerop x) "| " " ") | |
293 (make-string 5x5-x-scale | |
294 (if (5x5-cell grid y x) ?# ?.)))) | |
295 (insert " | ")) | |
296 (insert "\n"))) | |
297 (loop for grid in grids do (5x5-draw-grid-end)) | |
298 (insert "\n") | |
299 (insert (format "On: %d Moves: %d" (5x5-grid-value (car grids)) 5x5-moves)))) | |
300 | |
301 (defun 5x5-position-cursor () | |
302 "Position the cursor on the grid." | |
303 (goto-line (+ (* 5x5-y-pos 5x5-y-scale) 2)) | |
304 (goto-char (+ (point) (* 5x5-x-pos 5x5-x-scale) (+ 5x5-x-pos 1) 1))) | |
305 | |
306 (defun 5x5-made-move () | |
307 "Keep track of how many moves have been made." | |
308 (incf 5x5-moves)) | |
309 | |
310 (defun 5x5-make-random-grid () | |
311 "Make a random grid." | |
312 (let ((grid (5x5-make-new-grid))) | |
313 (loop for y from 0 to (1- 5x5-grid-size) do | |
314 (loop for x from 0 to (1- 5x5-grid-size) do | |
315 (if (zerop (random 2)) | |
316 (5x5-flip-cell grid y x)))) | |
317 grid)) | |
318 | |
319 ;; Cracker functions. | |
320 | |
321 ;;;###autoload | |
322 (defun 5x5-crack-randomly () | |
323 "Attempt to crack 5x5 using random solutions." | |
324 (interactive) | |
325 (5x5-crack #'5x5-make-random-solution)) | |
326 | |
327 ;;;###autoload | |
328 (defun 5x5-crack-mutating-current () | |
329 "Attempt to crack 5x5 by mutating the current solution." | |
330 (interactive) | |
331 (5x5-crack #'5x5-make-mutate-current)) | |
332 | |
333 ;;;###autoload | |
334 (defun 5x5-crack-mutating-best () | |
335 "Attempt to crack 5x5 by mutating the best solution." | |
336 (interactive) | |
337 (5x5-crack #'5x5-make-mutate-best)) | |
338 | |
339 ;;;###autoload | |
340 (defun 5x5-crack-xor-mutate () | |
75916
1cea4c7de5cf
(5x5-crack-xor-mutate): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75347
diff
changeset
|
341 "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
|
342 Mutate the result." |
25143 | 343 (interactive) |
344 (5x5-crack #'5x5-make-xor-with-mutation)) | |
345 | |
346 ;;;###autoload | |
347 (defun 5x5-crack (breeder) | |
348 "Attempt to find a solution for 5x5. | |
349 | |
350 5x5-crack takes the argument BREEDER which should be a function that takes | |
351 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
|
352 solution and the second will be the best solution so far. The function |
25143 | 353 should return a grid vector array that is the new solution." |
354 | |
355 (interactive "aBreeder function: ") | |
356 (5x5) | |
357 (setq 5x5-cracking t) | |
358 (let* ((best-solution (5x5-make-random-grid)) | |
359 (current-solution best-solution) | |
360 (best-result (5x5-make-new-grid)) | |
361 (current-result (5x5-make-new-grid)) | |
362 (target (* 5x5-grid-size 5x5-grid-size))) | |
363 (while (and (< (5x5-grid-value best-result) target) | |
364 (not (input-pending-p))) | |
365 (setq current-result (5x5-play-solution current-solution best-solution)) | |
366 (if (> (5x5-grid-value current-result) (5x5-grid-value best-result)) | |
367 (setq best-solution current-solution | |
368 best-result current-result)) | |
369 (setq current-solution (funcall breeder | |
370 (5x5-copy-grid current-solution) | |
371 (5x5-copy-grid best-solution))))) | |
372 (setq 5x5-cracking nil)) | |
373 | |
374 (defun 5x5-make-random-solution (&rest ignore) | |
375 "Make a random solution." | |
376 (5x5-make-random-grid)) | |
377 | |
378 (defun 5x5-make-mutate-current (current best) | |
379 "Mutate the current solution." | |
380 (5x5-mutate-solution current)) | |
381 | |
382 (defun 5x5-make-mutate-best (current best) | |
383 "Mutate the best solution." | |
384 (5x5-mutate-solution best)) | |
385 | |
386 (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
|
387 "Xor current and best solution then mutate the result." |
25143 | 388 (let ((xored (5x5-make-new-grid))) |
389 (loop for y from 0 to (1- 5x5-grid-size) do | |
390 (loop for x from 0 to (1- 5x5-grid-size) do | |
391 (5x5-set-cell xored y x | |
392 (5x5-xor (5x5-cell current y x) | |
393 (5x5-cell best y x))))) | |
394 (5x5-mutate-solution xored))) | |
395 | |
396 (defun 5x5-mutate-solution (solution) | |
397 "Randomly flip bits in the solution." | |
398 (loop for y from 0 to (1- 5x5-grid-size) do | |
399 (loop for x from 0 to (1- 5x5-grid-size) do | |
400 (if (= (random (/ (* 5x5-grid-size 5x5-grid-size) 2)) | |
401 (/ (/ (* 5x5-grid-size 5x5-grid-size) 2) 2)) | |
402 (5x5-flip-cell solution y x)))) | |
403 solution) | |
404 | |
405 (defun 5x5-play-solution (solution best) | |
75916
1cea4c7de5cf
(5x5-crack-xor-mutate): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75347
diff
changeset
|
406 "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
|
407 in progress because it is an animated attempt." |
25143 | 408 (5x5-new-game) |
33906 | 409 (let ((inhibit-quit t)) |
410 (loop for y from 0 to (1- 5x5-grid-size) do | |
411 (loop for x from 0 to (1- 5x5-grid-size) do | |
412 (setq 5x5-y-pos y | |
413 5x5-x-pos x) | |
414 (if (5x5-cell solution y x) | |
415 (5x5-flip-current)) | |
416 (5x5-draw-grid (list 5x5-grid solution best)) | |
417 (5x5-position-cursor) | |
418 (sit-for 5x5-animate-delay)))) | |
25143 | 419 5x5-grid) |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
420 |
25143 | 421 ;; Keyboard response functions. |
422 | |
423 (defun 5x5-flip-current () | |
424 "Make a move on the current cursor location." | |
425 (interactive) | |
426 (setq 5x5-grid (5x5-make-move 5x5-grid 5x5-y-pos 5x5-x-pos)) | |
427 (5x5-made-move) | |
428 (unless 5x5-cracking | |
429 (5x5-draw-grid (list 5x5-grid))) | |
430 (5x5-position-cursor) | |
431 (when (= (5x5-grid-value 5x5-grid) (* 5x5-grid-size 5x5-grid-size)) | |
432 (beep) | |
433 (message "You win!"))) | |
434 | |
435 (defun 5x5-up () | |
436 "Move up." | |
437 (interactive) | |
438 (unless (zerop 5x5-y-pos) | |
439 (decf 5x5-y-pos) | |
440 (5x5-position-cursor))) | |
441 | |
442 (defun 5x5-down () | |
443 "Move down." | |
444 (interactive) | |
445 (unless (= 5x5-y-pos (1- 5x5-grid-size)) | |
446 (incf 5x5-y-pos) | |
447 (5x5-position-cursor))) | |
448 | |
449 (defun 5x5-left () | |
450 "Move left." | |
451 (interactive) | |
452 (unless (zerop 5x5-x-pos) | |
453 (decf 5x5-x-pos) | |
454 (5x5-position-cursor))) | |
455 | |
456 (defun 5x5-right () | |
457 "Move right." | |
458 (interactive) | |
459 (unless (= 5x5-x-pos (1- 5x5-grid-size)) | |
460 (incf 5x5-x-pos) | |
461 (5x5-position-cursor))) | |
462 | |
463 (defun 5x5-bol () | |
464 "Move to beginning of line." | |
465 (interactive) | |
466 (setq 5x5-x-pos 0) | |
467 (5x5-position-cursor)) | |
468 | |
469 (defun 5x5-eol () | |
470 "Move to end of line." | |
471 (interactive) | |
472 (setq 5x5-x-pos (1- 5x5-grid-size)) | |
473 (5x5-position-cursor)) | |
474 | |
475 (defun 5x5-first () | |
476 "Move to the first cell." | |
477 (interactive) | |
478 (setq 5x5-x-pos 0 | |
479 5x5-y-pos 0) | |
480 (5x5-position-cursor)) | |
481 | |
482 (defun 5x5-last () | |
483 "Move to the last cell." | |
484 (interactive) | |
485 (setq 5x5-x-pos (1- 5x5-grid-size) | |
486 5x5-y-pos (1- 5x5-grid-size)) | |
487 (5x5-position-cursor)) | |
488 | |
489 (defun 5x5-randomize () | |
490 "Randomize the grid." | |
491 (interactive) | |
492 (when (5x5-y-or-n-p "Start a new game with a random grid? ") | |
493 (setq 5x5-x-pos (/ 5x5-grid-size 2) | |
494 5x5-y-pos (/ 5x5-grid-size 2) | |
495 5x5-moves 0 | |
496 5x5-grid (5x5-make-random-grid)) | |
497 (unless 5x5-cracking | |
498 (5x5-draw-grid (list 5x5-grid))) | |
499 (5x5-position-cursor))) | |
500 | |
501 ;; Support functions | |
502 | |
503 (defun 5x5-xor (x y) | |
504 "Boolean exclusive-or of X and Y." | |
505 (and (or x y) (not (and x y)))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
41950
diff
changeset
|
506 |
25143 | 507 (defun 5x5-y-or-n-p (prompt) |
75916
1cea4c7de5cf
(5x5-crack-xor-mutate): Doc fix.
Juanma Barranquero <lekktu@gmail.com>
parents:
75347
diff
changeset
|
508 "5x5 wrapper for `y-or-n-p' which respects the `5x5-hassle-me' setting." |
25143 | 509 (if 5x5-hassle-me |
510 (y-or-n-p prompt) | |
511 t)) | |
512 | |
76953 | 513 (random t) |
514 | |
25143 | 515 (provide '5x5) |
516 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
517 ;; arch-tag: ec4dabd5-572d-41ea-b48c-ec5ce0d68fa9 |
25143 | 518 ;;; 5x5.el ends here |