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