]> code.delx.au - gnu-emacs/blob - lisp/play/solitaire.el
Move keymap initialization into declaration.
[gnu-emacs] / lisp / play / solitaire.el
1 ;;; solitaire.el --- game of solitaire in Emacs Lisp
2
3 ;; Copyright (C) 1994, 2001-2011 Free Software Foundation, Inc.
4
5 ;; Author: Jan Schormann <Jan.Schormann@rechen-gilde.de>
6 ;; Created: Fri afternoon, Jun 3, 1994
7 ;; Keywords: games
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This mode is for playing a well-known game of solitaire
27 ;; in which you jump pegs across other pegs.
28
29 ;; The game itself is somehow self-explanatory. Read the help text to
30 ;; solitaire, and try it.
31
32 ;;; Code:
33
34 (defgroup solitaire nil
35 "Game of Solitaire."
36 :prefix "solitaire-"
37 :group 'games)
38
39 (defcustom solitaire-mode-hook nil
40 "Hook to run upon entry to Solitaire."
41 :type 'hook
42 :group 'solitaire)
43
44 (defvar solitaire-mode-map
45 (let ((map (make-sparse-keymap)))
46 (set-keymap-parent map special-mode-map)
47
48 (define-key map "\C-f" 'solitaire-right)
49 (define-key map "\C-b" 'solitaire-left)
50 (define-key map "\C-p" 'solitaire-up)
51 (define-key map "\C-n" 'solitaire-down)
52 (define-key map "\r" 'solitaire-move)
53 (define-key map [remap undo] 'solitaire-undo)
54 (define-key map " " 'solitaire-do-check)
55
56 (define-key map [right] 'solitaire-right)
57 (define-key map [left] 'solitaire-left)
58 (define-key map [up] 'solitaire-up)
59 (define-key map [down] 'solitaire-down)
60
61 (define-key map [S-right] 'solitaire-move-right)
62 (define-key map [S-left] 'solitaire-move-left)
63 (define-key map [S-up] 'solitaire-move-up)
64 (define-key map [S-down] 'solitaire-move-down)
65
66 (define-key map [kp-6] 'solitaire-right)
67 (define-key map [kp-4] 'solitaire-left)
68 (define-key map [kp-8] 'solitaire-up)
69 (define-key map [kp-2] 'solitaire-down)
70 (define-key map [kp-5] 'solitaire-center-point)
71
72 (define-key map [S-kp-6] 'solitaire-move-right)
73 (define-key map [S-kp-4] 'solitaire-move-left)
74 (define-key map [S-kp-8] 'solitaire-move-up)
75 (define-key map [S-kp-2] 'solitaire-move-down)
76
77 (define-key map [kp-enter] 'solitaire-move)
78 (define-key map [kp-0] 'solitaire-undo)
79
80 ;; spoil it with s ;)
81 (define-key map [?s] 'solitaire-solve)
82
83 ;; (define-key map [kp-0] 'solitaire-hint) - Not yet provided ;)
84 map)
85 "Keymap for playing Solitaire.")
86
87 ;; Solitaire mode is suitable only for specially formatted data.
88 (put 'solitaire-mode 'mode-class 'special)
89
90 (define-derived-mode solitaire-mode special-mode "Solitaire"
91 "Major mode for playing Solitaire.
92 To learn how to play Solitaire, see the documentation for function
93 `solitaire'.
94 \\<solitaire-mode-map>
95 The usual mnemonic keys move the cursor around the board; in addition,
96 \\[solitaire-move] is a prefix character for actually moving a stone on the board."
97 (setq truncate-lines t)
98 (setq show-trailing-whitespace nil))
99
100 (defvar solitaire-stones 0
101 "Counter for the stones that are still there.")
102
103 (defvar solitaire-center nil
104 "Center of the board.")
105
106 (defvar solitaire-start nil
107 "Upper left corner of the board.")
108
109 (defvar solitaire-start-x nil)
110 (defvar solitaire-start-y nil)
111
112 (defvar solitaire-end nil
113 "Lower right corner of the board.")
114
115 (defvar solitaire-end-x nil)
116 (defvar solitaire-end-y nil)
117
118 (defcustom solitaire-auto-eval t
119 "Non-nil means check for possible moves after each major change.
120 This takes a while, so switch this on if you like to be informed when
121 the game is over, or off, if you are working on a slow machine."
122 :type 'boolean
123 :group 'solitaire)
124
125 (defconst solitaire-valid-directions
126 '(solitaire-left solitaire-right solitaire-up solitaire-down))
127
128 ;;;###autoload
129 (defun solitaire (arg)
130 "Play Solitaire.
131
132 To play Solitaire, type \\[solitaire].
133 \\<solitaire-mode-map>
134 Move around the board using the cursor keys.
135 Move stones using \\[solitaire-move] followed by a direction key.
136 Undo moves using \\[solitaire-undo].
137 Check for possible moves using \\[solitaire-do-check].
138 \(The variable `solitaire-auto-eval' controls whether to automatically
139 check after each move or undo.)
140
141 What is Solitaire?
142
143 I don't know who invented this game, but it seems to be rather old and
144 its origin seems to be northern Africa. Here's how to play:
145 Initially, the board will look similar to this:
146
147 Le Solitaire
148 ============
149
150 o o o
151
152 o o o
153
154 o o o o o o o
155
156 o o o . o o o
157
158 o o o o o o o
159
160 o o o
161
162 o o o
163
164 Let's call the o's stones and the .'s holes. One stone fits into one
165 hole. As you can see, all holes but one are occupied by stones. The
166 aim of the game is to get rid of all but one stone, leaving that last
167 one in the middle of the board if you're cool.
168
169 A stone can be moved if there is another stone next to it, and a hole
170 after that one. Thus there must be three fields in a row, either
171 horizontally or vertically, up, down, left or right, which look like
172 this: o o .
173
174 Then the first stone is moved to the hole, jumping over the second,
175 which therefore is taken away. The above thus `evaluates' to: . . o
176
177 That's all. Here's the board after two moves:
178
179 o o o
180
181 . o o
182
183 o o . o o o o
184
185 o . o o o o o
186
187 o o o o o o o
188
189 o o o
190
191 o o o
192
193 Pick your favourite shortcuts:
194
195 \\{solitaire-mode-map}"
196
197 (interactive "P")
198 (switch-to-buffer "*Solitaire*")
199 (setq buffer-read-only t)
200 (setq solitaire-stones 32)
201 (solitaire-insert-board)
202 (solitaire-build-modeline)
203 (goto-char (point-max))
204 (setq solitaire-center (search-backward "."))
205 (setq buffer-undo-list (list (point)))
206 (solitaire-mode))
207
208 (defun solitaire-build-modeline ()
209 (setq mode-line-format
210 (list "" "---" 'mode-line-buffer-identification
211 (if (< 1 solitaire-stones)
212 (format "--> There are %d stones left <--" solitaire-stones)
213 "------")
214 'global-mode-string " %[(" 'mode-name 'minor-mode-alist "%n"
215 ")%]-%-"))
216 (force-mode-line-update))
217
218 (defun solitaire-insert-board ()
219 (let* ((buffer-read-only nil)
220 (w (window-width))
221 (h (window-height))
222 (hsep (cond ((> w 26) " ")
223 ((> w 20) " ")
224 (t "")))
225 (vsep (cond ((> h 17) "\n\n")
226 (t "\n")))
227 (indent (make-string (/ (- w 7 (* 6 (length hsep))) 2) ?\s)))
228 (erase-buffer)
229 (insert (make-string (/ (- h 7 (if (> h 12) 3 0)
230 (* 6 (1- (length vsep)))) 2) ?\n))
231 (when (or (string= vsep "\n\n") (> h 12))
232 (insert (format "%sLe Solitaire\n" indent))
233 (insert (format "%s============\n\n" indent)))
234 (insert indent)
235 (setq solitaire-start (point))
236 (setq solitaire-start-x (current-column))
237 (setq solitaire-start-y (solitaire-current-line))
238 (insert (format " %s %so%so%so%s" hsep hsep hsep hsep vsep))
239 (insert (format "%s %s %so%so%so%s" indent hsep hsep hsep hsep vsep))
240 (insert (format "%so%so%so%so%so%so%so%s" indent hsep hsep hsep hsep hsep hsep vsep))
241 (insert (format "%so%so%so%s" indent hsep hsep hsep))
242 (setq solitaire-center (point))
243 (insert (format ".%so%so%so%s" hsep hsep hsep vsep))
244 (insert (format "%so%so%so%so%so%so%so%s" indent hsep hsep hsep hsep hsep hsep vsep))
245 (insert (format "%s %s %so%so%so%s" indent hsep hsep hsep hsep vsep))
246 (insert (format "%s %s %so%so%so%s %s " indent hsep hsep hsep hsep hsep hsep))
247 (setq solitaire-end (point))
248 (setq solitaire-end-x (current-column))
249 (setq solitaire-end-y (solitaire-current-line))))
250
251 (defun solitaire-right ()
252 (interactive)
253 (let ((start (point)))
254 (forward-char)
255 (while (= ?\s (following-char))
256 (forward-char))
257 (when (or (= 0 (following-char))
258 (= ?\s (following-char))
259 (= ?\n (following-char)))
260 (goto-char start))))
261
262 (defun solitaire-left ()
263 (interactive)
264 (let ((start (point)))
265 (backward-char)
266 (while (= ?\s (following-char))
267 (backward-char))
268 (when (or (= 0 (preceding-char))
269 (= ?\s (following-char))
270 (= ?\n (following-char)))
271 (goto-char start))))
272
273 (defun solitaire-up ()
274 (interactive)
275 (let ((start (point))
276 (c (current-column)))
277 (forward-line -1)
278 (move-to-column c)
279 (while (and (= ?\n (following-char))
280 (forward-line -1)
281 (move-to-column c)
282 (not (bolp))))
283 (when (or (= 0 (preceding-char))
284 (= ?\s (following-char))
285 (= ?\= (following-char))
286 (= ?\n (following-char)))
287 (goto-char start))))
288
289 (defun solitaire-down ()
290 (interactive)
291 (let ((start (point))
292 (c (current-column)))
293 (forward-line 1)
294 (move-to-column c)
295 (while (and (= ?\n (following-char))
296 (forward-line 1)
297 (move-to-column c)
298 (not (eolp))))
299 (when (or (= 0 (following-char))
300 (= ?\s (following-char))
301 (= ?\n (following-char)))
302 (goto-char start))))
303
304 (defun solitaire-center-point ()
305 (interactive)
306 (goto-char solitaire-center))
307
308 (defun solitaire-move-right () (interactive) (solitaire-move '[right]))
309 (defun solitaire-move-left () (interactive) (solitaire-move '[left]))
310 (defun solitaire-move-up () (interactive) (solitaire-move '[up]))
311 (defun solitaire-move-down () (interactive) (solitaire-move '[down]))
312
313 (defun solitaire-possible-move (movesymbol)
314 "Check if a move is possible from current point in the specified direction.
315 MOVESYMBOL specifies the direction.
316 Returns either a string, indicating cause of contraindication, or a
317 list containing three numbers: starting field, skipped field (from
318 which a stone will be taken away) and target."
319
320 (save-excursion
321 (if (memq movesymbol solitaire-valid-directions)
322 (let ((start (point))
323 (skip (progn (funcall movesymbol) (point)))
324 (target (progn (funcall movesymbol) (point))))
325 (if (= skip target)
326 "Off Board!"
327 (if (or (/= ?o (char-after start))
328 (/= ?o (char-after skip))
329 (/= ?. (char-after target)))
330 "Wrong move!"
331 (list start skip target))))
332 "Not a valid direction")))
333
334 (defun solitaire-move (dir)
335 "Pseudo-prefix command to move a stone in Solitaire."
336 (interactive "kMove where? ")
337 (let* ((class (solitaire-possible-move (lookup-key solitaire-mode-map dir)))
338 (buffer-read-only nil))
339 (if (stringp class)
340 (error class)
341 (let ((start (car class))
342 (skip (car (cdr class)))
343 (target (car (cdr (cdr class)))))
344 (goto-char start)
345 (delete-char 1)
346 (insert ?.)
347 (goto-char skip)
348 (delete-char 1)
349 (insert ?.)
350 (goto-char target)
351 (delete-char 1)
352 (insert ?o)
353 (goto-char target)
354 (setq solitaire-stones (1- solitaire-stones))
355 (solitaire-build-modeline)
356 (if solitaire-auto-eval (solitaire-do-check))))))
357
358 (defun solitaire-undo (arg)
359 "Undo a move in Solitaire."
360 (interactive "P")
361 (let ((buffer-read-only nil))
362 (undo arg))
363 (save-excursion
364 (setq solitaire-stones
365 (let ((count 0))
366 (goto-char solitaire-end)
367 (while (search-backward "o" solitaire-start 'done)
368 (and (>= (current-column) solitaire-start-x)
369 (<= (current-column) solitaire-end-x)
370 (>= (solitaire-current-line) solitaire-start-y)
371 (<= (solitaire-current-line) solitaire-end-y)
372 (setq count (1+ count))))
373 count)))
374 (solitaire-build-modeline)
375 (when solitaire-auto-eval (solitaire-do-check)))
376
377 (defun solitaire-check ()
378 (save-excursion
379 (if (= 1 solitaire-stones)
380 0
381 (goto-char solitaire-end)
382 (let ((count 0))
383 (while (search-backward "o" solitaire-start 'done)
384 (and (>= (current-column) solitaire-start-x)
385 (<= (current-column) solitaire-end-x)
386 (>= (solitaire-current-line) solitaire-start-y)
387 (<= (solitaire-current-line) solitaire-end-y)
388 (mapc
389 (lambda (movesymbol)
390 (when (listp (solitaire-possible-move movesymbol))
391 (setq count (1+ count))))
392 solitaire-valid-directions)))
393 count))))
394
395 (defun solitaire-do-check (&optional arg)
396 "Check for any possible moves in Solitaire."
397 (interactive "P")
398 (let ((moves (solitaire-check)))
399 (cond ((= 1 solitaire-stones)
400 (message "Yeah! You made it! Only the King is left!"))
401 ((zerop moves)
402 (message "Sorry, no more possible moves."))
403 ((= 1 moves)
404 (message "There is one possible move."))
405 (t (message "There are %d possible moves." moves)))))
406
407 (defun solitaire-current-line ()
408 "Return the vertical position of point.
409 Seen in info on text lines."
410 (+ (count-lines (point-min) (point))
411 (if (= (current-column) 0) 1 0)
412 -1))
413
414 ;; And here's the spoiler:)
415 (defun solitaire-solve ()
416 "Spoil Solitaire by solving the game for you - nearly ...
417 ... stops with five stones left ;)"
418 (interactive)
419 (when (< solitaire-stones 32)
420 (error "Cannot solve game in progress"))
421 (let ((allmoves [up up S-down up left left S-right up up left S-down
422 up up right right S-left down down down S-up up
423 S-down down down down S-up left left down
424 S-right left left up up S-down right right right
425 S-left left S-right right right right S-left
426 right down down S-up down down left left S-right
427 up up up S-down down S-up up up up S-down up
428 right right S-left down right right down S-up
429 left left left S-right right S-left down down
430 left S-right S-up S-left S-left S-down S-right
431 up S-right left left])
432 ;; down down S-up left S-right
433 ;; right S-left
434 (solitaire-auto-eval nil))
435 (solitaire-center-point)
436 (mapc (lambda (op)
437 (when (memq op '(S-left S-right S-up S-down))
438 (sit-for 0.2))
439 (execute-kbd-macro (vector op))
440 (when (memq op '(S-left S-right S-up S-down))
441 (sit-for 0.4)))
442 allmoves))
443 (solitaire-do-check))
444
445 (provide 'solitaire)
446
447 ;;; solitaire.el ends here