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