1 ;;; 5x5.el --- simple little puzzle game
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Dave Pearson <davep@davep.org>
7 ;; Maintainer: Dave Pearson <davep@davep.org>
9 ;; Keywords: games puzzles
11 ;; This file is part of GNU Emacs.
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 3, or (at your option)
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.
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.
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.
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.
38 ;; o Look into tarting up the display with colour. gamegrid.el looks
39 ;; interesting, perhaps that is the way to go?
43 ;; Ralf Fassel <ralf@akutech.de> for his help and introduction to writing an
46 ;; Pascal Q. Porcupine <joshagam@cs.nmsu.edu> for inspiring the animated
59 "5x5 - Silly little puzzle game."
63 (defcustom 5x5-grid-size
5
64 "*Size of the playing area."
68 (defcustom 5x5-x-scale
4
69 "*X scaling factor for drawing the grid."
73 (defcustom 5x5-y-scale
3
74 "*Y scaling factor for drawing the grid."
78 (defcustom 5x5-animate-delay
.01
79 "*Delay in seconds when animating a solution crack."
83 (defcustom 5x5-hassle-me t
84 "*Should 5x5 ask you when you want to do a destructive operation?"
88 (defcustom 5x5-mode-hook nil
89 "*Hook run on starting 5x5."
93 ;; Non-customize variables.
99 "X position of cursor.")
102 "Y position of cursor.")
107 (defvar 5x5-cracking nil
108 "Are we in cracking mode?")
110 (defvar 5x5-buffer-name
"*5x5*"
111 "Name of the 5x5 play buffer.")
113 (defvar 5x5-mode-map nil
114 "Local keymap for the 5x5 game.")
119 (let ((map (make-sparse-keymap)))
120 (suppress-keymap map t
)
121 (define-key map
"?" #'describe-mode
)
122 (define-key map
"\r" #'5x5-flip-current
)
123 (define-key map
" " #'5x5-flip-current
)
124 (define-key map
[up] #'5x5-up)
125 (define-key map [down] #'5x5-down)
126 (define-key map [left] #'5x5-left)
127 (define-key map [tab] #'5x5-right)
128 (define-key map [right] #'5x5-right)
129 (define-key map [(control a)] #'5x5-bol)
130 (define-key map [(control e)] #'5x5-eol)
131 (define-key map [(control p)] #'5x5-up)
132 (define-key map [(control n)] #'5x5-down)
133 (define-key map [(control b)] #'5x5-left)
134 (define-key map [(control f)] #'5x5-right)
135 (define-key map [home] #'5x5-bol)
136 (define-key map [end] #'5x5-eol)
137 (define-key map [prior] #'5x5-first)
138 (define-key map [next] #'5x5-last)
139 (define-key map "r" #'5x5-randomize)
140 (define-key map [(control c) (control r)] #'5x5-crack-randomly)
141 (define-key map [(control c) (control c)] #'5x5-crack-mutating-current)
142 (define-key map [(control c) (control b)] #'5x5-crack-mutating-best)
143 (define-key map [(control c) (control x)] #'5x5-crack-xor-mutate)
144 (define-key map "n" #'5x5-new-game)
145 (define-key map "q" #'5x5-quit-game)
146 (setq 5x5-mode-map map)))
150 (easy-menu-define 5x5-mode-menu 5x5-mode-map "5x5 menu."
152 ["New game" 5x5-new-game t]
153 ["Random game" 5x5-randomize t]
154 ["Quit game" 5x5-quit-game t]
156 ["Crack randomly" 5x5-crack-randomly t]
157 ["Crack mutating current" 5x5-crack-mutating-current t]
158 ["Crack mutating best" 5x5-crack-mutating-best t]
159 ["Crack with xor mutate" 5x5-crack-xor-mutate t]))
161 ;; Gameplay functions.
163 (put '5x5-mode 'mode-class 'special)
166 "A mode for playing `5x5'.
168 The key bindings for 5x5-mode are:
171 (kill-all-local-variables)
172 (use-local-map 5x5-mode-map)
173 (setq major-mode '5x5-mode
175 (run-mode-hooks '5x5-mode-hook)
176 (setq buffer-read-only t
178 (buffer-disable-undo))
181 (defun 5x5 (&optional size)
184 The object of 5x5 is very simple, by moving around the grid and flipping
185 squares you must fill the grid.
187 5x5 keyboard bindings are:
189 Flip \\[5x5-flip-current]
191 Move down \\[5x5-down]
192 Move left \\[5x5-left]
193 Move right \\[5x5-right]
194 Start new game \\[5x5-new-game]
195 New game with random grid \\[5x5-randomize]
196 Random cracker \\[5x5-crack-randomly]
197 Mutate current cracker \\[5x5-crack-mutating-current]
198 Mutate best cracker \\[5x5-crack-mutating-best]
199 Mutate xor cracker \\[5x5-crack-xor-mutate]
200 Quit current game \\[5x5-quit-game]"
203 (setq 5x5-cracking nil)
205 (setq 5x5-grid-size size))
206 (switch-to-buffer 5x5-buffer-name)
207 (if (or (not 5x5-grid) (not (= 5x5-grid-size (length (aref 5x5-grid 0)))))
209 (5x5-draw-grid (list 5x5-grid))
210 (5x5-position-cursor)
213 (defun 5x5-new-game ()
214 "Start a new game of `5x5'."
216 (when (if (interactive-p) (5x5-y-or-n-p "Start a new game? ") t)
217 (setq 5x5-x-pos (/ 5x5-grid-size 2)
218 5x5-y-pos (/ 5x5-grid-size 2)
220 5x5-grid (5x5-make-move (5x5-make-new-grid) 5x5-y-pos 5x5-x-pos))
221 (5x5-draw-grid (list 5x5-grid))
222 (5x5-position-cursor)))
224 (defun 5x5-quit-game ()
225 "Quit the current game of `5x5'."
227 (kill-buffer 5x5-buffer-name))
229 (defun 5x5-make-new-grid ()
230 "Create and return a new `5x5' grid structure."
231 (let ((grid (make-vector 5x5-grid-size nil)))
232 (loop for y from 0 to (1- 5x5-grid-size) do
233 (aset grid y (make-vector 5x5-grid-size nil)))
236 (defun 5x5-cell (grid y x)
237 "Return the value of the cell in GRID at location X,Y."
238 (aref (aref grid y) x))
240 (defun 5x5-set-cell (grid y x value)
241 "Set the value of cell X,Y in GRID to VALUE."
242 (aset (aref grid y) x value))
244 (defun 5x5-flip-cell (grid y x)
245 "Flip the value of cell X,Y in GRID."
246 (5x5-set-cell grid y x (not (5x5-cell grid y x))))
248 (defun 5x5-copy-grid (grid)
249 "Make a new copy of GRID."
250 (let ((copy (5x5-make-new-grid)))
251 (loop for y from 0 to (1- 5x5-grid-size) do
252 (loop for x from 0 to (1- 5x5-grid-size) do
253 (5x5-set-cell copy y x (5x5-cell grid y x))))
256 (defun 5x5-make-move (grid row col)
257 "Make a move on GRID at row ROW and column COL."
258 (5x5-flip-cell grid row col)
260 (5x5-flip-cell grid (1- row) col))
261 (if (< row (- 5x5-grid-size 1))
262 (5x5-flip-cell grid (1+ row) col))
264 (5x5-flip-cell grid row (1- col)))
265 (if (< col (- 5x5-grid-size 1))
266 (5x5-flip-cell grid row (1+ col)))
269 (defun 5x5-row-value (row)
270 "Get the \"on-value\" for grid row ROW."
271 (loop for y from 0 to (1- 5x5-grid-size) sum (if (aref row y) 1 0)))
273 (defun 5x5-grid-value (grid)
274 "Get the \"on-value\" for grid GRID."
275 (loop for y from 0 to (1- 5x5-grid-size) sum (5x5-row-value (aref grid y))))
277 (defun 5x5-draw-grid-end ()
278 "Draw the top/bottom of the grid."
280 (loop for x from 0 to (1- 5x5-grid-size) do
281 (insert "-" (make-string 5x5-x-scale ?-)))
284 (defun 5x5-draw-grid (grids)
285 "Draw the grids GRIDS into the current buffer."
286 (let ((buffer-read-only nil))
288 (loop for grid in grids do (5x5-draw-grid-end))
290 (loop for y from 0 to (1- 5x5-grid-size) do
291 (loop for lines from 0 to (1- 5x5-y-scale) do
292 (loop for grid in grids do
293 (loop for x from 0 to (1- 5x5-grid-size) do
294 (insert (if (zerop x) "| " " ")
295 (make-string 5x5-x-scale
296 (if (5x5-cell grid y x) ?# ?.))))
299 (loop for grid in grids do (5x5-draw-grid-end))
301 (insert (format "On: %d Moves: %d" (5x5-grid-value (car grids)) 5x5-moves))))
303 (defun 5x5-position-cursor ()
304 "Position the cursor on the grid."
305 (goto-line (+ (* 5x5-y-pos 5x5-y-scale) 2))
306 (goto-char (+ (point) (* 5x5-x-pos 5x5-x-scale) (+ 5x5-x-pos 1) 1)))
308 (defun 5x5-made-move ()
309 "Keep track of how many moves have been made."
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))))
321 ;; Cracker functions.
324 (defun 5x5-crack-randomly ()
325 "Attempt to crack 5x5 using random solutions."
327 (5x5-crack #'5x5-make-random-solution))
330 (defun 5x5-crack-mutating-current ()
331 "Attempt to crack 5x5 by mutating the current solution."
333 (5x5-crack #'5x5-make-mutate-current))
336 (defun 5x5-crack-mutating-best ()
337 "Attempt to crack 5x5 by mutating the best solution."
339 (5x5-crack #'5x5-make-mutate-best))
342 (defun 5x5-crack-xor-mutate ()
343 "Attempt to crack 5x5 by xoring the current and best solution.
346 (5x5-crack #'5x5-make-xor-with-mutation))
349 (defun 5x5-crack (breeder)
350 "Attempt to find a solution for 5x5.
352 5x5-crack takes the argument BREEDER which should be a function that takes
353 two parameters, the first will be a grid vector array that is the current
354 solution and the second will be the best solution so far. The function
355 should return a grid vector array that is the new solution."
357 (interactive "aBreeder function: ")
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))
376 (defun 5x5-make-random-solution (&rest ignore)
377 "Make a random solution."
378 (5x5-make-random-grid))
380 (defun 5x5-make-mutate-current (current best)
381 "Mutate the current solution."
382 (5x5-mutate-solution current))
384 (defun 5x5-make-mutate-best (current best)
385 "Mutate the best solution."
386 (5x5-mutate-solution best))
388 (defun 5x5-make-xor-with-mutation (current best)
389 "Xor current and best solution then mutate the result."
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)))
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))))
407 (defun 5x5-play-solution (solution best)
408 "Play a solution on an empty grid. This destroys the current game
409 in progress because it is an animated attempt."
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
416 (if (5x5-cell solution y x)
418 (5x5-draw-grid (list 5x5-grid solution best))
419 (5x5-position-cursor)
420 (sit-for 5x5-animate-delay))))
423 ;; Keyboard response functions.
425 (defun 5x5-flip-current ()
426 "Make a move on the current cursor location."
428 (setq 5x5-grid (5x5-make-move 5x5-grid 5x5-y-pos 5x5-x-pos))
431 (5x5-draw-grid (list 5x5-grid)))
432 (5x5-position-cursor)
433 (when (= (5x5-grid-value 5x5-grid) (* 5x5-grid-size 5x5-grid-size))
435 (message "You win!")))
440 (unless (zerop 5x5-y-pos)
442 (5x5-position-cursor)))
447 (unless (= 5x5-y-pos (1- 5x5-grid-size))
449 (5x5-position-cursor)))
454 (unless (zerop 5x5-x-pos)
456 (5x5-position-cursor)))
461 (unless (= 5x5-x-pos (1- 5x5-grid-size))
463 (5x5-position-cursor)))
466 "Move to beginning of line."
469 (5x5-position-cursor))
472 "Move to end of line."
474 (setq 5x5-x-pos (1- 5x5-grid-size))
475 (5x5-position-cursor))
478 "Move to the first cell."
482 (5x5-position-cursor))
485 "Move to the last cell."
487 (setq 5x5-x-pos (1- 5x5-grid-size)
488 5x5-y-pos (1- 5x5-grid-size))
489 (5x5-position-cursor))
491 (defun 5x5-randomize ()
492 "Randomize the grid."
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)
498 5x5-grid (5x5-make-random-grid))
500 (5x5-draw-grid (list 5x5-grid)))
501 (5x5-position-cursor)))
506 "Boolean exclusive-or of X and Y."
507 (and (or x y) (not (and x y))))
509 (defun 5x5-y-or-n-p (prompt)
510 "5x5 wrapper for `y-or-n-p' which respects the `5x5-hassle-me' setting."
519 ;;; arch-tag: ec4dabd5-572d-41ea-b48c-ec5ce0d68fa9