1 ;;; 5x5.el --- simple little puzzle game
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009 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 of the License, or
16 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
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.
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.
36 ;; o Look into tarting up the display with color. gamegrid.el looks
37 ;; interesting, perhaps that is the way to go?
41 ;; Ralf Fassel <ralf@akutech.de> for his help and introduction to writing an
44 ;; Pascal Q. Porcupine <joshagam@cs.nmsu.edu> for inspiring the animated
57 "5x5 - Silly little puzzle game."
61 (defcustom 5x5-grid-size
5
62 "Size of the playing area."
66 (defcustom 5x5-x-scale
4
67 "X scaling factor for drawing the grid."
71 (defcustom 5x5-y-scale
3
72 "Y scaling factor for drawing the grid."
76 (defcustom 5x5-animate-delay
.01
77 "Delay in seconds when animating a solution crack."
81 (defcustom 5x5-hassle-me t
82 "Should 5x5 ask you when you want to do a destructive operation?"
86 (defcustom 5x5-mode-hook nil
87 "Hook run on starting 5x5."
91 ;; Non-customize variables.
97 "X position of cursor.")
100 "Y position of cursor.")
105 (defvar 5x5-cracking nil
106 "Are we in cracking mode?")
108 (defvar 5x5-buffer-name
"*5x5*"
109 "Name of the 5x5 play buffer.")
111 (defvar 5x5-mode-map nil
112 "Local keymap for the 5x5 game.")
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)
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)
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)
141 (define-key map [(control c) (control x)] #'5x5-crack-xor-mutate)
142 (define-key map "n" #'5x5-new-game)
143 (define-key map "q" #'5x5-quit-game)
144 (setq 5x5-mode-map map)))
148 (easy-menu-define 5x5-mode-menu 5x5-mode-map "5x5 menu."
150 ["New game" 5x5-new-game t]
151 ["Random game" 5x5-randomize t]
152 ["Quit game" 5x5-quit-game t]
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]))
159 ;; Gameplay functions.
161 (put '5x5-mode 'mode-class 'special)
164 "A mode for playing `5x5'.
166 The key bindings for 5x5-mode are:
169 (kill-all-local-variables)
170 (use-local-map 5x5-mode-map)
171 (setq major-mode '5x5-mode
173 (run-mode-hooks '5x5-mode-hook)
174 (setq buffer-read-only t
176 (buffer-disable-undo))
179 (defun 5x5 (&optional size)
182 The object of 5x5 is very simple, by moving around the grid and flipping
183 squares you must fill the grid.
185 5x5 keyboard bindings are:
187 Flip \\[5x5-flip-current]
189 Move down \\[5x5-down]
190 Move left \\[5x5-left]
191 Move right \\[5x5-right]
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]
196 Mutate best cracker \\[5x5-crack-mutating-best]
197 Mutate xor cracker \\[5x5-crack-xor-mutate]
198 Quit current game \\[5x5-quit-game]"
201 (setq 5x5-cracking nil)
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)))))
207 (5x5-draw-grid (list 5x5-grid))
208 (5x5-position-cursor)
211 (defun 5x5-new-game ()
212 "Start a new game of `5x5'."
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)
218 5x5-grid (5x5-make-move (5x5-make-new-grid) 5x5-y-pos 5x5-x-pos))
219 (5x5-draw-grid (list 5x5-grid))
220 (5x5-position-cursor)))
222 (defun 5x5-quit-game ()
223 "Quit the current game of `5x5'."
225 (kill-buffer 5x5-buffer-name))
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)))
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))
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))
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))))
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))))
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)
258 (5x5-flip-cell grid (1- row) col))
259 (if (< row (- 5x5-grid-size 1))
260 (5x5-flip-cell grid (1+ row) col))
262 (5x5-flip-cell grid row (1- col)))
263 (if (< col (- 5x5-grid-size 1))
264 (5x5-flip-cell grid row (1+ col)))
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)))
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))))
275 (defun 5x5-draw-grid-end ()
276 "Draw the top/bottom of the grid."
278 (loop for x from 0 to (1- 5x5-grid-size) do
279 (insert "-" (make-string 5x5-x-scale ?-)))
282 (defun 5x5-draw-grid (grids)
283 "Draw the grids GRIDS into the current buffer."
284 (let ((buffer-read-only nil))
286 (loop for grid in grids do (5x5-draw-grid-end))
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) ?# ?.))))
297 (loop for grid in grids do (5x5-draw-grid-end))
299 (insert (format "On: %d Moves: %d" (5x5-grid-value (car grids)) 5x5-moves))))
301 (defun 5x5-position-cursor ()
302 "Position the cursor on the grid."
303 (goto-char (point-min))
304 (forward-line (1+ (* 5x5-y-pos 5x5-y-scale)))
305 (goto-char (+ (point) (* 5x5-x-pos 5x5-x-scale) (+ 5x5-x-pos 1) 1)))
307 (defun 5x5-made-move ()
308 "Keep track of how many moves have been made."
311 (defun 5x5-make-random-grid ()
312 "Make a random grid."
313 (let ((grid (5x5-make-new-grid)))
314 (loop for y from 0 to (1- 5x5-grid-size) do
315 (loop for x from 0 to (1- 5x5-grid-size) do
316 (if (zerop (random 2))
317 (5x5-flip-cell grid y x))))
320 ;; Cracker functions.
323 (defun 5x5-crack-randomly ()
324 "Attempt to crack 5x5 using random solutions."
326 (5x5-crack #'5x5-make-random-solution))
329 (defun 5x5-crack-mutating-current ()
330 "Attempt to crack 5x5 by mutating the current solution."
332 (5x5-crack #'5x5-make-mutate-current))
335 (defun 5x5-crack-mutating-best ()
336 "Attempt to crack 5x5 by mutating the best solution."
338 (5x5-crack #'5x5-make-mutate-best))
341 (defun 5x5-crack-xor-mutate ()
342 "Attempt to crack 5x5 by xoring the current and best solution.
345 (5x5-crack #'5x5-make-xor-with-mutation))
348 (defun 5x5-crack (breeder)
349 "Attempt to find a solution for 5x5.
351 5x5-crack takes the argument BREEDER which should be a function that takes
352 two parameters, the first will be a grid vector array that is the current
353 solution and the second will be the best solution so far. The function
354 should return a grid vector array that is the new solution."
356 (interactive "aBreeder function: ")
358 (setq 5x5-cracking t)
359 (let* ((best-solution (5x5-make-random-grid))
360 (current-solution best-solution)
361 (best-result (5x5-make-new-grid))
362 (current-result (5x5-make-new-grid))
363 (target (* 5x5-grid-size 5x5-grid-size)))
364 (while (and (< (5x5-grid-value best-result) target)
365 (not (input-pending-p)))
366 (setq current-result (5x5-play-solution current-solution best-solution))
367 (if (> (5x5-grid-value current-result) (5x5-grid-value best-result))
368 (setq best-solution current-solution
369 best-result current-result))
370 (setq current-solution (funcall breeder
371 (5x5-copy-grid current-solution)
372 (5x5-copy-grid best-solution)))))
373 (setq 5x5-cracking nil))
375 (defun 5x5-make-random-solution (&rest ignore)
376 "Make a random solution."
377 (5x5-make-random-grid))
379 (defun 5x5-make-mutate-current (current best)
380 "Mutate the current solution."
381 (5x5-mutate-solution current))
383 (defun 5x5-make-mutate-best (current best)
384 "Mutate the best solution."
385 (5x5-mutate-solution best))
387 (defun 5x5-make-xor-with-mutation (current best)
388 "Xor current and best solution then mutate the result."
389 (let ((xored (5x5-make-new-grid)))
390 (loop for y from 0 to (1- 5x5-grid-size) do
391 (loop for x from 0 to (1- 5x5-grid-size) do
392 (5x5-set-cell xored y x
393 (5x5-xor (5x5-cell current y x)
394 (5x5-cell best y x)))))
395 (5x5-mutate-solution xored)))
397 (defun 5x5-mutate-solution (solution)
398 "Randomly flip bits in the solution."
399 (loop for y from 0 to (1- 5x5-grid-size) do
400 (loop for x from 0 to (1- 5x5-grid-size) do
401 (if (= (random (/ (* 5x5-grid-size 5x5-grid-size) 2))
402 (/ (/ (* 5x5-grid-size 5x5-grid-size) 2) 2))
403 (5x5-flip-cell solution y x))))
406 (defun 5x5-play-solution (solution best)
407 "Play a solution on an empty grid. This destroys the current game
408 in progress because it is an animated attempt."
410 (let ((inhibit-quit t))
411 (loop for y from 0 to (1- 5x5-grid-size) do
412 (loop for x from 0 to (1- 5x5-grid-size) do
415 (if (5x5-cell solution y x)
417 (5x5-draw-grid (list 5x5-grid solution best))
418 (5x5-position-cursor)
419 (sit-for 5x5-animate-delay))))
422 ;; Keyboard response functions.
424 (defun 5x5-flip-current ()
425 "Make a move on the current cursor location."
427 (setq 5x5-grid (5x5-make-move 5x5-grid 5x5-y-pos 5x5-x-pos))
430 (5x5-draw-grid (list 5x5-grid)))
431 (5x5-position-cursor)
432 (when (= (5x5-grid-value 5x5-grid) (* 5x5-grid-size 5x5-grid-size))
434 (message "You win!")))
439 (unless (zerop 5x5-y-pos)
441 (5x5-position-cursor)))
446 (unless (= 5x5-y-pos (1- 5x5-grid-size))
448 (5x5-position-cursor)))
453 (unless (zerop 5x5-x-pos)
455 (5x5-position-cursor)))
460 (unless (= 5x5-x-pos (1- 5x5-grid-size))
462 (5x5-position-cursor)))
465 "Move to beginning of line."
468 (5x5-position-cursor))
471 "Move to end of line."
473 (setq 5x5-x-pos (1- 5x5-grid-size))
474 (5x5-position-cursor))
477 "Move to the first cell."
481 (5x5-position-cursor))
484 "Move to the last cell."
486 (setq 5x5-x-pos (1- 5x5-grid-size)
487 5x5-y-pos (1- 5x5-grid-size))
488 (5x5-position-cursor))
490 (defun 5x5-randomize ()
491 "Randomize the grid."
493 (when (5x5-y-or-n-p "Start a new game with a random grid? ")
494 (setq 5x5-x-pos (/ 5x5-grid-size 2)
495 5x5-y-pos (/ 5x5-grid-size 2)
497 5x5-grid (5x5-make-random-grid))
499 (5x5-draw-grid (list 5x5-grid)))
500 (5x5-position-cursor)))
505 "Boolean exclusive-or of X and Y."
506 (and (or x y) (not (and x y))))
508 (defun 5x5-y-or-n-p (prompt)
509 "5x5 wrapper for `y-or-n-p' which respects the `5x5-hassle-me' setting."
518 ;; arch-tag: ec4dabd5-572d-41ea-b48c-ec5ce0d68fa9