solitaire.el 14 KB

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