1 ;;; solitaire.el --- game of solitaire in Emacs Lisp
3 ;; Copyright (C) 1994, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Jan Schormann <Jan.Schormann@rechen-gilde.de>
6 ;; Created: Fri afternoon, Jun 3, 1994
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; This mode is for playing a well-known game of solitaire
27 ;; in which you jump pegs across other pegs.
29 ;; The game itself is somehow self-explanatory. Read the help text to
30 ;; solitaire, and try it.
34 (defgroup solitaire nil
39 (defcustom solitaire-mode-hook nil
40 "Hook to run upon entry to Solitaire."
44 (defvar solitaire-mode-map
45 (let ((map (make-sparse-keymap)))
46 (suppress-keymap map t
)
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 (define-key map
"q" 'quit-window
)
57 (define-key map
[right] 'solitaire-right)
58 (define-key map [left] 'solitaire-left)
59 (define-key map [up] 'solitaire-up)
60 (define-key map [down] 'solitaire-down)
62 (define-key map [S-right] 'solitaire-move-right)
63 (define-key map [S-left] 'solitaire-move-left)
64 (define-key map [S-up] 'solitaire-move-up)
65 (define-key map [S-down] 'solitaire-move-down)
67 (define-key map [kp-6] 'solitaire-right)
68 (define-key map [kp-4] 'solitaire-left)
69 (define-key map [kp-8] 'solitaire-up)
70 (define-key map [kp-2] 'solitaire-down)
71 (define-key map [kp-5] 'solitaire-center-point)
73 (define-key map [S-kp-6] 'solitaire-move-right)
74 (define-key map [S-kp-4] 'solitaire-move-left)
75 (define-key map [S-kp-8] 'solitaire-move-up)
76 (define-key map [S-kp-2] 'solitaire-move-down)
78 (define-key map [kp-enter] 'solitaire-move)
79 (define-key map [kp-0] 'solitaire-undo)
82 (define-key map [?s] 'solitaire-solve)
84 ;; (define-key map [kp-0] 'solitaire-hint) - Not yet provided ;)
86 "Keymap for playing Solitaire.")
88 ;; Solitaire mode is suitable only for specially formatted data.
89 (put 'solitaire-mode 'mode-class 'special)
91 (define-derived-mode solitaire-mode nil "Solitaire"
92 "Major mode for playing Solitaire.
93 To learn how to play Solitaire, see the documentation for function
95 \\<solitaire-mode-map>
96 The usual mnemonic keys move the cursor around the board; in addition,
97 \\[solitaire-move] is a prefix character for actually moving a stone on the board."
98 (setq truncate-lines t)
99 (setq show-trailing-whitespace nil))
101 (defvar solitaire-stones 0
102 "Counter for the stones that are still there.")
104 (defvar solitaire-center nil
105 "Center of the board.")
107 (defvar solitaire-start nil
108 "Upper left corner of the board.")
110 (defvar solitaire-start-x nil)
111 (defvar solitaire-start-y nil)
113 (defvar solitaire-end nil
114 "Lower right corner of the board.")
116 (defvar solitaire-end-x nil)
117 (defvar solitaire-end-y nil)
119 (defcustom solitaire-auto-eval t
120 "Non-nil means check for possible moves after each major change.
121 This takes a while, so switch this on if you like to be informed when
122 the game is over, or off, if you are working on a slow machine."
126 (defconst solitaire-valid-directions
127 '(solitaire-left solitaire-right solitaire-up solitaire-down))
130 (defun solitaire (arg)
133 To play Solitaire, type \\[solitaire].
134 \\<solitaire-mode-map>
135 Move around the board using the cursor keys.
136 Move stones using \\[solitaire-move] followed by a direction key.
137 Undo moves using \\[solitaire-undo].
138 Check for possible moves using \\[solitaire-do-check].
139 \(The variable `solitaire-auto-eval' controls whether to automatically
140 check after each move or undo.)
144 I don't know who invented this game, but it seems to be rather old and
145 its origin seems to be northern Africa. Here's how to play:
146 Initially, the board will look similar to this:
165 Let's call the o's stones and the .'s holes. One stone fits into one
166 hole. As you can see, all holes but one are occupied by stones. The
167 aim of the game is to get rid of all but one stone, leaving that last
168 one in the middle of the board if you're cool.
170 A stone can be moved if there is another stone next to it, and a hole
171 after that one. Thus there must be three fields in a row, either
172 horizontally or vertically, up, down, left or right, which look like
175 Then the first stone is moved to the hole, jumping over the second,
176 which therefore is taken away. The above thus `evaluates' to: . . o
178 That's all. Here's the board after two moves:
194 Pick your favourite shortcuts:
196 \\{solitaire-mode-map}"
199 (switch-to-buffer "*Solitaire*")
201 (setq buffer-read-only t)
202 (setq solitaire-stones 32)
203 (solitaire-insert-board)
204 (solitaire-build-modeline)
205 (goto-char (point-max))
206 (setq solitaire-center (search-backward "."))
207 (setq buffer-undo-list (list (point)))
208 (set-buffer-modified-p nil))
210 (defun solitaire-build-modeline ()
211 (setq mode-line-format
212 (list "" "---" 'mode-line-buffer-identification
213 (if (< 1 solitaire-stones)
214 (format "--> There are %d stones left <--" solitaire-stones)
216 'global-mode-string " %[(" 'mode-name 'minor-mode-alist "%n"
218 (force-mode-line-update))
220 (defun solitaire-insert-board ()
221 (let* ((buffer-read-only nil)
224 (hsep (cond ((> w 26) " ")
227 (vsep (cond ((> h 17) "\n\n")
229 (indent (make-string (/ (- w 7 (* 6 (length hsep))) 2) ?\s)))
231 (insert (make-string (/ (- h 7 (if (> h 12) 3 0)
232 (* 6 (1- (length vsep)))) 2) ?\n))
233 (when (or (string= vsep "\n\n") (> h 12))
234 (insert (format "%sLe Solitaire\n" indent))
235 (insert (format "%s============\n\n" indent)))
237 (setq solitaire-start (point))
238 (setq solitaire-start-x (current-column))
239 (setq solitaire-start-y (solitaire-current-line))
240 (insert (format " %s %so%so%so%s" hsep hsep hsep hsep vsep))
241 (insert (format "%s %s %so%so%so%s" indent hsep hsep hsep hsep vsep))
242 (insert (format "%so%so%so%so%so%so%so%s" indent hsep hsep hsep hsep hsep hsep vsep))
243 (insert (format "%so%so%so%s" indent hsep hsep hsep))
244 (setq solitaire-center (point))
245 (insert (format ".%so%so%so%s" hsep hsep hsep vsep))
246 (insert (format "%so%so%so%so%so%so%so%s" indent hsep hsep hsep hsep hsep hsep vsep))
247 (insert (format "%s %s %so%so%so%s" indent hsep hsep hsep hsep vsep))
248 (insert (format "%s %s %so%so%so%s %s " indent hsep hsep hsep hsep hsep hsep))
249 (setq solitaire-end (point))
250 (setq solitaire-end-x (current-column))
251 (setq solitaire-end-y (solitaire-current-line))))
253 (defun solitaire-right ()
255 (let ((start (point)))
257 (while (= ?\s (following-char))
259 (when (or (= 0 (following-char))
260 (= ?\s (following-char))
261 (= ?\n (following-char)))
264 (defun solitaire-left ()
266 (let ((start (point)))
268 (while (= ?\s (following-char))
270 (when (or (= 0 (preceding-char))
271 (= ?\s (following-char))
272 (= ?\n (following-char)))
275 (defun solitaire-up ()
277 (let ((start (point))
278 (c (current-column)))
281 (while (and (= ?\n (following-char))
285 (when (or (= 0 (preceding-char))
286 (= ?\s (following-char))
287 (= ?\= (following-char))
288 (= ?\n (following-char)))
291 (defun solitaire-down ()
293 (let ((start (point))
294 (c (current-column)))
297 (while (and (= ?\n (following-char))
301 (when (or (= 0 (following-char))
302 (= ?\s (following-char))
303 (= ?\n (following-char)))
306 (defun solitaire-center-point ()
308 (goto-char solitaire-center))
310 (defun solitaire-move-right () (interactive) (solitaire-move '[right]))
311 (defun solitaire-move-left () (interactive) (solitaire-move '[left]))
312 (defun solitaire-move-up () (interactive) (solitaire-move '[up]))
313 (defun solitaire-move-down () (interactive) (solitaire-move '[down]))
315 (defun solitaire-possible-move (movesymbol)
316 "Check if a move is possible from current point in the specified direction.
317 MOVESYMBOL specifies the direction.
318 Returns either a string, indicating cause of contraindication, or a
319 list containing three numbers: starting field, skipped field (from
320 which a stone will be taken away) and target."
323 (if (memq movesymbol solitaire-valid-directions)
324 (let ((start (point))
325 (skip (progn (funcall movesymbol) (point)))
326 (target (progn (funcall movesymbol) (point))))
329 (if (or (/= ?o (char-after start))
330 (/= ?o (char-after skip))
331 (/= ?. (char-after target)))
333 (list start skip target))))
334 "Not a valid direction")))
336 (defun solitaire-move (dir)
337 "Pseudo-prefix command to move a stone in Solitaire."
338 (interactive "kMove where? ")
339 (let* ((class (solitaire-possible-move (lookup-key solitaire-mode-map dir)))
340 (buffer-read-only nil))
343 (let ((start (car class))
344 (skip (car (cdr class)))
345 (target (car (cdr (cdr class)))))
356 (setq solitaire-stones (1- solitaire-stones))
357 (solitaire-build-modeline)
358 (if solitaire-auto-eval (solitaire-do-check))))))
360 (defun solitaire-undo (arg)
361 "Undo a move in Solitaire."
363 (let ((buffer-read-only nil))
366 (setq solitaire-stones
368 (goto-char solitaire-end)
369 (while (search-backward "o" solitaire-start 'done)
370 (and (>= (current-column) solitaire-start-x)
371 (<= (current-column) solitaire-end-x)
372 (>= (solitaire-current-line) solitaire-start-y)
373 (<= (solitaire-current-line) solitaire-end-y)
374 (setq count (1+ count))))
376 (solitaire-build-modeline)
377 (when solitaire-auto-eval (solitaire-do-check)))
379 (defun solitaire-check ()
381 (if (= 1 solitaire-stones)
383 (goto-char solitaire-end)
385 (while (search-backward "o" solitaire-start 'done)
386 (and (>= (current-column) solitaire-start-x)
387 (<= (current-column) solitaire-end-x)
388 (>= (solitaire-current-line) solitaire-start-y)
389 (<= (solitaire-current-line) solitaire-end-y)
392 (when (listp (solitaire-possible-move movesymbol))
393 (setq count (1+ count))))
394 solitaire-valid-directions)))
397 (defun solitaire-do-check (&optional arg)
398 "Check for any possible moves in Solitaire."
400 (let ((moves (solitaire-check)))
401 (cond ((= 1 solitaire-stones)
402 (message "Yeah! You made it! Only the King is left!"))
404 (message "Sorry, no more possible moves."))
406 (message "There is one possible move."))
407 (t (message "There are %d possible moves." moves)))))
409 (defun solitaire-current-line ()
410 "Return the vertical position of point.
411 Seen in info on text lines."
412 (+ (count-lines (point-min) (point))
413 (if (= (current-column) 0) 1 0)
416 ;; And here's the spoiler:)
417 (defun solitaire-solve ()
418 "Spoil Solitaire by solving the game for you - nearly ...
419 ... stops with five stones left ;)"
421 (when (< solitaire-stones 32)
422 (error "Cannot solve game in progress"))
423 (let ((allmoves [up up S-down up left left S-right up up left S-down
424 up up right right S-left down down down S-up up
425 S-down down down down S-up left left down
426 S-right left left up up S-down right right right
427 S-left left S-right right right right S-left
428 right down down S-up down down left left S-right
429 up up up S-down down S-up up up up S-down up
430 right right S-left down right right down S-up
431 left left left S-right right S-left down down
432 left S-right S-up S-left S-left S-down S-right
433 up S-right left left])
434 ;; down down S-up left S-right
436 (solitaire-auto-eval nil))
437 (solitaire-center-point)
439 (when (memq op '(S-left S-right S-up S-down))
441 (execute-kbd-macro (vector op))
442 (when (memq op '(S-left S-right S-up S-down))
445 (solitaire-do-check))
449 ;;; solitaire.el ends here