Add 2010 to copyright years.
[bpt/emacs.git] / lisp / play / 5x5.el
CommitLineData
6e44da43 1;;; 5x5.el --- simple little puzzle game
116f0564 2
67d110f1 3;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
114f9c96 4;; 2008, 2009, 2010 Free Software Foundation, Inc.
116f0564 5
26a8d08d
GM
6;; Author: Dave Pearson <davep@davep.org>
7;; Maintainer: Dave Pearson <davep@davep.org>
116f0564 8;; Created: 1998-10-03
116f0564
KH
9;; Keywords: games puzzles
10
11;; This file is part of GNU Emacs.
12
b1fc2b50 13;; GNU Emacs is free software: you can redistribute it and/or modify
116f0564 14;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
116f0564
KH
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
b1fc2b50 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
116f0564
KH
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;;
6772c8e1 36;; o Look into tarting up the display with color. gamegrid.el looks
116f0564
KH
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
6e44da43
PJ
47;;; Code:
48
116f0564
KH
49;; Things we need.
50
51(eval-when-compile
52 (require 'cl))
53
116f0564
KH
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
67d110f1 62 "Size of the playing area."
116f0564
KH
63 :type 'integer
64 :group '5x5)
65
66(defcustom 5x5-x-scale 4
67d110f1 67 "X scaling factor for drawing the grid."
116f0564
KH
68 :type 'integer
69 :group '5x5)
70
71(defcustom 5x5-y-scale 3
67d110f1 72 "Y scaling factor for drawing the grid."
116f0564
KH
73 :type 'integer
74 :group '5x5)
75
76(defcustom 5x5-animate-delay .01
67d110f1 77 "Delay in seconds when animating a solution crack."
116f0564
KH
78 :type 'number
79 :group '5x5)
80
81(defcustom 5x5-hassle-me t
67d110f1 82 "Should 5x5 ask you when you want to do a destructive operation?"
116f0564
KH
83 :type 'boolean
84 :group '5x5)
85
86(defcustom 5x5-mode-hook nil
67d110f1 87 "Hook run on starting 5x5."
116f0564
KH
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)
08f87e3c
KH
129 (define-key map [(control p)] #'5x5-up)
130 (define-key map [(control n)] #'5x5-down)
131 (define-key map [(control b)] #'5x5-left)
132 (define-key map [(control f)] #'5x5-right)
116f0564
KH
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)
a1506d29 141 (define-key map [(control c) (control x)] #'5x5-crack-xor-mutate)
116f0564
KH
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 ()
849aa553 164 "A mode for playing `5x5'.
116f0564
KH
165
166The 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")
c83c9654 173 (run-mode-hooks '5x5-mode-hook)
116f0564
KH
174 (setq buffer-read-only t
175 truncate-lines t)
c83c9654 176 (buffer-disable-undo))
116f0564
KH
177
178;;;###autoload
179(defun 5x5 (&optional size)
180 "Play 5x5.
181
182The object of 5x5 is very simple, by moving around the grid and flipping
183squares you must fill the grid.
184
1855x5 keyboard bindings are:
186\\<5x5-mode-map>
a1506d29
JB
187Flip \\[5x5-flip-current]
188Move up \\[5x5-up]
189Move down \\[5x5-down]
190Move left \\[5x5-left]
191Move right \\[5x5-right]
116f0564
KH
192Start new game \\[5x5-new-game]
193New game with random grid \\[5x5-randomize]
194Random cracker \\[5x5-crack-randomly]
195Mutate current cracker \\[5x5-crack-mutating-current]
08f87e3c 196Mutate best cracker \\[5x5-crack-mutating-best]
116f0564
KH
197Mutate xor cracker \\[5x5-crack-xor-mutate]
198Quit 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)
32226619
JB
214 (when (if (called-interactively-p 'interactive)
215 (5x5-y-or-n-p "Start a new game? ") t)
116f0564
KH
216 (setq 5x5-x-pos (/ 5x5-grid-size 2)
217 5x5-y-pos (/ 5x5-grid-size 2)
218 5x5-moves 0
219 5x5-grid (5x5-make-move (5x5-make-new-grid) 5x5-y-pos 5x5-x-pos))
ca5f43fa
RS
220 (5x5-draw-grid (list 5x5-grid))
221 (5x5-position-cursor)))
116f0564
KH
222
223(defun 5x5-quit-game ()
224 "Quit the current game of `5x5'."
225 (interactive)
226 (kill-buffer 5x5-buffer-name))
227
228(defun 5x5-make-new-grid ()
229 "Create and return a new `5x5' grid structure."
230 (let ((grid (make-vector 5x5-grid-size nil)))
231 (loop for y from 0 to (1- 5x5-grid-size) do
232 (aset grid y (make-vector 5x5-grid-size nil)))
233 grid))
234
235(defun 5x5-cell (grid y x)
236 "Return the value of the cell in GRID at location X,Y."
237 (aref (aref grid y) x))
238
239(defun 5x5-set-cell (grid y x value)
240 "Set the value of cell X,Y in GRID to VALUE."
241 (aset (aref grid y) x value))
242
243(defun 5x5-flip-cell (grid y x)
244 "Flip the value of cell X,Y in GRID."
245 (5x5-set-cell grid y x (not (5x5-cell grid y x))))
246
247(defun 5x5-copy-grid (grid)
248 "Make a new copy of GRID."
249 (let ((copy (5x5-make-new-grid)))
250 (loop for y from 0 to (1- 5x5-grid-size) do
251 (loop for x from 0 to (1- 5x5-grid-size) do
252 (5x5-set-cell copy y x (5x5-cell grid y x))))
253 copy))
254
255(defun 5x5-make-move (grid row col)
256 "Make a move on GRID at row ROW and column COL."
257 (5x5-flip-cell grid row col)
258 (if (> row 0)
259 (5x5-flip-cell grid (1- row) col))
260 (if (< row (- 5x5-grid-size 1))
261 (5x5-flip-cell grid (1+ row) col))
262 (if (> col 0)
263 (5x5-flip-cell grid row (1- col)))
264 (if (< col (- 5x5-grid-size 1))
265 (5x5-flip-cell grid row (1+ col)))
266 grid)
267
268(defun 5x5-row-value (row)
269 "Get the \"on-value\" for grid row ROW."
270 (loop for y from 0 to (1- 5x5-grid-size) sum (if (aref row y) 1 0)))
271
272(defun 5x5-grid-value (grid)
273 "Get the \"on-value\" for grid GRID."
274 (loop for y from 0 to (1- 5x5-grid-size) sum (5x5-row-value (aref grid y))))
275
276(defun 5x5-draw-grid-end ()
849aa553 277 "Draw the top/bottom of the grid."
116f0564
KH
278 (insert "+")
279 (loop for x from 0 to (1- 5x5-grid-size) do
280 (insert "-" (make-string 5x5-x-scale ?-)))
281 (insert "-+ "))
282
283(defun 5x5-draw-grid (grids)
284 "Draw the grids GRIDS into the current buffer."
285 (let ((buffer-read-only nil))
286 (erase-buffer)
287 (loop for grid in grids do (5x5-draw-grid-end))
288 (insert "\n")
289 (loop for y from 0 to (1- 5x5-grid-size) do
290 (loop for lines from 0 to (1- 5x5-y-scale) do
291 (loop for grid in grids do
292 (loop for x from 0 to (1- 5x5-grid-size) do
293 (insert (if (zerop x) "| " " ")
294 (make-string 5x5-x-scale
295 (if (5x5-cell grid y x) ?# ?.))))
296 (insert " | "))
297 (insert "\n")))
298 (loop for grid in grids do (5x5-draw-grid-end))
299 (insert "\n")
300 (insert (format "On: %d Moves: %d" (5x5-grid-value (car grids)) 5x5-moves))))
301
302(defun 5x5-position-cursor ()
303 "Position the cursor on the grid."
9b4c5ecd
GM
304 (goto-char (point-min))
305 (forward-line (1+ (* 5x5-y-pos 5x5-y-scale)))
116f0564
KH
306 (goto-char (+ (point) (* 5x5-x-pos 5x5-x-scale) (+ 5x5-x-pos 1) 1)))
307
308(defun 5x5-made-move ()
309 "Keep track of how many moves have been made."
310 (incf 5x5-moves))
311
312(defun 5x5-make-random-grid ()
313 "Make a random grid."
314 (let ((grid (5x5-make-new-grid)))
315 (loop for y from 0 to (1- 5x5-grid-size) do
316 (loop for x from 0 to (1- 5x5-grid-size) do
317 (if (zerop (random 2))
318 (5x5-flip-cell grid y x))))
319 grid))
320
321;; Cracker functions.
322
323;;;###autoload
324(defun 5x5-crack-randomly ()
325 "Attempt to crack 5x5 using random solutions."
326 (interactive)
327 (5x5-crack #'5x5-make-random-solution))
328
329;;;###autoload
330(defun 5x5-crack-mutating-current ()
331 "Attempt to crack 5x5 by mutating the current solution."
332 (interactive)
333 (5x5-crack #'5x5-make-mutate-current))
334
335;;;###autoload
336(defun 5x5-crack-mutating-best ()
337 "Attempt to crack 5x5 by mutating the best solution."
338 (interactive)
339 (5x5-crack #'5x5-make-mutate-best))
340
341;;;###autoload
342(defun 5x5-crack-xor-mutate ()
849aa553 343 "Attempt to crack 5x5 by xoring the current and best solution.
36912c16 344Mutate the result."
116f0564
KH
345 (interactive)
346 (5x5-crack #'5x5-make-xor-with-mutation))
347
348;;;###autoload
349(defun 5x5-crack (breeder)
350 "Attempt to find a solution for 5x5.
351
3525x5-crack takes the argument BREEDER which should be a function that takes
353two parameters, the first will be a grid vector array that is the current
849aa553 354solution and the second will be the best solution so far. The function
116f0564
KH
355should return a grid vector array that is the new solution."
356
357 (interactive "aBreeder function: ")
358 (5x5)
359 (setq 5x5-cracking t)
360 (let* ((best-solution (5x5-make-random-grid))
361 (current-solution best-solution)
362 (best-result (5x5-make-new-grid))
363 (current-result (5x5-make-new-grid))
364 (target (* 5x5-grid-size 5x5-grid-size)))
365 (while (and (< (5x5-grid-value best-result) target)
366 (not (input-pending-p)))
367 (setq current-result (5x5-play-solution current-solution best-solution))
368 (if (> (5x5-grid-value current-result) (5x5-grid-value best-result))
369 (setq best-solution current-solution
370 best-result current-result))
371 (setq current-solution (funcall breeder
372 (5x5-copy-grid current-solution)
373 (5x5-copy-grid best-solution)))))
374 (setq 5x5-cracking nil))
375
376(defun 5x5-make-random-solution (&rest ignore)
377 "Make a random solution."
378 (5x5-make-random-grid))
379
380(defun 5x5-make-mutate-current (current best)
381 "Mutate the current solution."
382 (5x5-mutate-solution current))
383
384(defun 5x5-make-mutate-best (current best)
385 "Mutate the best solution."
386 (5x5-mutate-solution best))
387
388(defun 5x5-make-xor-with-mutation (current best)
849aa553 389 "Xor current and best solution then mutate the result."
116f0564
KH
390 (let ((xored (5x5-make-new-grid)))
391 (loop for y from 0 to (1- 5x5-grid-size) do
392 (loop for x from 0 to (1- 5x5-grid-size) do
393 (5x5-set-cell xored y x
394 (5x5-xor (5x5-cell current y x)
395 (5x5-cell best y x)))))
396 (5x5-mutate-solution xored)))
397
398(defun 5x5-mutate-solution (solution)
399 "Randomly flip bits in the solution."
400 (loop for y from 0 to (1- 5x5-grid-size) do
401 (loop for x from 0 to (1- 5x5-grid-size) do
402 (if (= (random (/ (* 5x5-grid-size 5x5-grid-size) 2))
403 (/ (/ (* 5x5-grid-size 5x5-grid-size) 2) 2))
404 (5x5-flip-cell solution y x))))
405 solution)
406
407(defun 5x5-play-solution (solution best)
849aa553
JB
408 "Play a solution on an empty grid. This destroys the current game
409in progress because it is an animated attempt."
116f0564 410 (5x5-new-game)
26a8d08d
GM
411 (let ((inhibit-quit t))
412 (loop for y from 0 to (1- 5x5-grid-size) do
413 (loop for x from 0 to (1- 5x5-grid-size) do
414 (setq 5x5-y-pos y
415 5x5-x-pos x)
416 (if (5x5-cell solution y x)
417 (5x5-flip-current))
418 (5x5-draw-grid (list 5x5-grid solution best))
419 (5x5-position-cursor)
420 (sit-for 5x5-animate-delay))))
116f0564 421 5x5-grid)
a1506d29 422
116f0564
KH
423;; Keyboard response functions.
424
425(defun 5x5-flip-current ()
426 "Make a move on the current cursor location."
427 (interactive)
428 (setq 5x5-grid (5x5-make-move 5x5-grid 5x5-y-pos 5x5-x-pos))
429 (5x5-made-move)
430 (unless 5x5-cracking
431 (5x5-draw-grid (list 5x5-grid)))
432 (5x5-position-cursor)
433 (when (= (5x5-grid-value 5x5-grid) (* 5x5-grid-size 5x5-grid-size))
434 (beep)
435 (message "You win!")))
436
437(defun 5x5-up ()
438 "Move up."
439 (interactive)
440 (unless (zerop 5x5-y-pos)
441 (decf 5x5-y-pos)
442 (5x5-position-cursor)))
443
444(defun 5x5-down ()
445 "Move down."
446 (interactive)
447 (unless (= 5x5-y-pos (1- 5x5-grid-size))
448 (incf 5x5-y-pos)
449 (5x5-position-cursor)))
450
451(defun 5x5-left ()
452 "Move left."
453 (interactive)
454 (unless (zerop 5x5-x-pos)
455 (decf 5x5-x-pos)
456 (5x5-position-cursor)))
457
458(defun 5x5-right ()
459 "Move right."
460 (interactive)
461 (unless (= 5x5-x-pos (1- 5x5-grid-size))
462 (incf 5x5-x-pos)
463 (5x5-position-cursor)))
464
465(defun 5x5-bol ()
466 "Move to beginning of line."
467 (interactive)
468 (setq 5x5-x-pos 0)
469 (5x5-position-cursor))
470
471(defun 5x5-eol ()
472 "Move to end of line."
473 (interactive)
474 (setq 5x5-x-pos (1- 5x5-grid-size))
475 (5x5-position-cursor))
476
477(defun 5x5-first ()
478 "Move to the first cell."
479 (interactive)
480 (setq 5x5-x-pos 0
481 5x5-y-pos 0)
482 (5x5-position-cursor))
483
484(defun 5x5-last ()
485 "Move to the last cell."
486 (interactive)
487 (setq 5x5-x-pos (1- 5x5-grid-size)
488 5x5-y-pos (1- 5x5-grid-size))
489 (5x5-position-cursor))
490
491(defun 5x5-randomize ()
492 "Randomize the grid."
493 (interactive)
494 (when (5x5-y-or-n-p "Start a new game with a random grid? ")
495 (setq 5x5-x-pos (/ 5x5-grid-size 2)
496 5x5-y-pos (/ 5x5-grid-size 2)
497 5x5-moves 0
498 5x5-grid (5x5-make-random-grid))
499 (unless 5x5-cracking
500 (5x5-draw-grid (list 5x5-grid)))
501 (5x5-position-cursor)))
502
503;; Support functions
504
505(defun 5x5-xor (x y)
506 "Boolean exclusive-or of X and Y."
507 (and (or x y) (not (and x y))))
a1506d29 508
116f0564 509(defun 5x5-y-or-n-p (prompt)
849aa553 510 "5x5 wrapper for `y-or-n-p' which respects the `5x5-hassle-me' setting."
116f0564
KH
511 (if 5x5-hassle-me
512 (y-or-n-p prompt)
513 t))
514
d2fe6685
GM
515(random t)
516
116f0564
KH
517(provide '5x5)
518
cbee283d 519;; arch-tag: ec4dabd5-572d-41ea-b48c-ec5ce0d68fa9
116f0564 520;;; 5x5.el ends here