]> code.delx.au - gnu-emacs-elpa/blob - chess.el
whitespace fix
[gnu-emacs-elpa] / chess.el
1 ;;; chess.el --- Play chess in Emacs
2
3 ;; Copyright (C) 2001 John Wiegley <johnw@gnu.org>
4
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: chess.el
7 ;; Version: 2.0
8 ;; Keywords: games
9 ;; Author: John Wiegley <johnw@gnu.org>
10 ;; Maintainer: John Wiegley <johnw@gnu.org>
11 ;; Description: Play chess in Emacs
12 ;; URL: http://www.gci-net.com/~johnw/Emacs/packages/chess.tar.gz
13 ;; Compatibility: Emacs20, Emacs21, XEmacs21
14
15 ;; This file is not part of GNU Emacs.
16
17 ;; This is free software; you can redistribute it and/or modify it under
18 ;; the terms of the GNU General Public License as published by the Free
19 ;; Software Foundation; either version 2, or (at your option) any later
20 ;; version.
21 ;;
22 ;; This is distributed in the hope that it will be useful, but WITHOUT
23 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 ;; for more details.
26 ;;
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs; see the file COPYING. If not, write to the
29 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 ;; MA 02111-1307, USA.
31
32 ;;; Commentary:
33
34 ;; Welcome to Emacs Chess, a chess playing module for GNU Emacs.
35 ;;
36 ;; This program will not play chess against you; it is not a chess
37 ;; computer. It can use a chess computer, however, to simulate your
38 ;; opponent's moves. This is decided when you choose your opponent.
39 ;; You must, of course, have that chess computer installed. See the
40 ;; top of chess-player.el for more information.
41 ;;
42 ;; To just get a chessboard up, put the following in your .emacs file:
43 ;;
44 ;; (add-to-list 'load-list "<the path to Emacs Chess>")
45 ;;
46 ;; (autoload 'chess "chess" "Play a game of chess" t)
47 ;;
48 ;; Now you can type `M-x chess', and play chess against anyone else in
49 ;; the room with you, without having to install anything more.
50 ;;
51 ;; Once this is working, the next thing to do is to customize
52 ;; `chess-use-modules'. This is a list of functionality modules used
53 ;; by chess.el to provide its functionality. You can enable or
54 ;; disable modules so that Emacs Chess better suites your tastes.
55 ;; Those modules in turn often have configuration variables, and
56 ;; appropriate documentation at the top of the related file.
57 ;;
58 ;; Emacs Chess is designed in a highly modular fashion, using loosely
59 ;; coupled modules that respond to events on the chess board. This
60 ;; makes it very easy for programmers to add their own types of
61 ;; displays, opponents, analysis programs, etc. See the documentation
62 ;; in chess-module.el to learn more.
63 ;;
64 ;; There is no documentation for this program other than what exists
65 ;; in the source files. This is because the source files aim at being
66 ;; self documenting, and as chess is such a simple game, most chess
67 ;; players aren't going to need to know much about this program in
68 ;; particular.
69 ;;
70 ;; However, most people will probably be interested in reading the top
71 ;; of chess-display.el and chess-pgn.el, which describe the user
72 ;; interface commands available in each of those buffer types.
73
74 ;;; Code:
75
76 (require 'chess-game)
77 (require 'chess-display)
78 (require 'chess-engine)
79 (require 'chess-random)
80 (require 'chess-database)
81 (require 'chess-file)
82
83 (defgroup chess nil
84 "An Emacs chess playing program."
85 :group 'games)
86
87 (defconst chess-version "2.0a12"
88 "The version of the Emacs chess program.")
89
90 (defcustom chess-default-display
91 '(chess-images chess-ics1 chess-plain)
92 "Default display to be used when starting a chess session.
93 A list indicates a series of alternatives if the first display is
94 not available."
95 :type '(choice symbol (repeat symbol))
96 :group 'chess)
97
98 (defcustom chess-default-modules
99 '((chess-sound chess-announce)
100 chess-autosave)
101 "Modules to be used when starting a chess session.
102 A sublist indicates a series of alternatives, if the first is not
103 available.
104 These can do just about anything."
105 :type '(repeat (choice symbol (repeat symbol)))
106 :group 'chess)
107
108 (defcustom chess-default-engine
109 '(chess-crafty chess-gnuchess chess-phalanx)
110 "Default engine to be used when starting a chess session.
111 A list indicates a series of alternatives if the first engine is not
112 available."
113 :type '(choice symbol (repeat symbol))
114 :group 'chess)
115
116 (defcustom chess-full-name (user-full-name)
117 "The full name to use when playing chess."
118 :type 'string
119 :group 'chess)
120
121 (defun chess--create-display (module game my-color disable-popup)
122 (when (require module nil t)
123 (let ((display (chess-display-create game module my-color)))
124 (when display
125 (chess-game-set-data game 'my-color my-color)
126 (if disable-popup
127 (chess-display-disable-popup display))
128 display))))
129
130 (defun chess--create-engine (module game response-handler ctor-args)
131 (let ((engine (apply 'chess-engine-create module game
132 response-handler ctor-args)))
133 (when engine
134 ;; for the sake of engines which are ready to play now, and
135 ;; which don't need connect/accept negotiation (most
136 ;; computerized engines fall into this category), we need to
137 ;; let them know we're ready to begin
138 (chess-engine-command engine 'ready)
139 engine)))
140
141 (defun chess-create-modules (module-list create-func &rest args)
142 (let (objects)
143 (dolist (module module-list)
144 (let (object)
145 (if (symbolp module)
146 (if (setq object (apply create-func module args))
147 (push object objects))
148 ;; this module is actually a list, which means keep trying
149 ;; until we find one that works
150 (while module
151 (if (setq object (apply create-func (car module) args))
152 (progn
153 (push object objects)
154 (setq module nil))
155 (setq module (cdr module)))))))
156 (nreverse objects)))
157
158 (chess-message-catalog 'english
159 '((no-engines-found . "Could not find any chess engines to play against; install gnuchess!")))
160
161 ;;;###autoload
162 (defun chess (&optional engine disable-popup engine-response-handler
163 &rest engine-ctor-args)
164 "Start a game of chess, playing against ENGINE (a module name)."
165 (interactive
166 (list
167 (if current-prefix-arg
168 (intern
169 (concat "chess-"
170 (let ((str (read-string "Engine to play against: ")))
171 (if (> (length str) 0)
172 str
173 "none"))))
174 chess-default-engine)))
175
176 (let ((game (chess-game-create))
177 (my-color t) ; we start out as white always
178 objects)
179
180 ;; all these odd calls are so that `objects' ends up looking like:
181 ;; (ENGINE FIRST-DISPLAY...)
182
183 (setq objects (chess-create-modules (list chess-default-display)
184 'chess--create-display
185 game my-color disable-popup))
186 (when (car objects)
187 (mapc 'chess-display-update objects)
188 (chess-module-set-leader (car objects))
189 (chess-display-popup (car objects)))
190
191 (nconc objects (chess-create-modules chess-default-modules
192 'chess-module-create game))
193
194 (push (car (chess-create-modules (list (or engine chess-default-engine))
195 'chess--create-engine game
196 engine-response-handler
197 engine-ctor-args))
198 objects)
199
200 (unless (car objects)
201 (chess-message 'no-engines-found))
202
203 objects))
204
205 ;;;###autoload
206 (defalias 'chess-session 'chess)
207
208 ;;;###autoload
209 (defun chess-create-display ()
210 "Just make a display to use, letting chess.el decide the style."
211 (cadr (chess-session 'chess-none)))
212
213 ;;;###autoload
214 (defun chess-create-display-object (perspective)
215 (car (chess-create-modules (list chess-default-display)
216 'chess--create-display
217 (chess-game-create) perspective nil)))
218
219 ;;;###autoload
220 (defun chess-read-pgn (&optional file)
221 "Read and display a PGN game after point."
222 (interactive "P")
223 (if (or file (not (search-forward "[Event " nil t)))
224 (setq file (read-file-name "Read a PGN game from file: ")))
225 (if file
226 (find-file file))
227 (let ((game (chess-pgn-to-game))
228 display)
229 (when game
230 (setq display (chess-create-display))
231 (chess-display-set-game display game))))
232
233 (defvar chess-puzzle-indices nil)
234 (defvar chess-puzzle-position nil)
235 (make-variable-buffer-local 'chess-puzzle-indices)
236 (make-variable-buffer-local 'chess-puzzle-position)
237
238 ;;;###autoload
239 (defun chess-puzzle (file &optional index)
240 "Pick a random puzzle from FILE, and solve it against the default engine.
241 The spacebar in the display buffer is bound to `chess-puzzle-next',
242 making it easy to go on to the next puzzle once you've solved one."
243 (interactive "fRead chess puzzles from: ")
244 (let* ((database (chess-database-open 'chess-file file))
245 (objects (and database (chess-session)))
246 (display (cadr objects)))
247 (when database
248 (with-current-buffer display
249 ;; make sure the database is closed when the display is shutdown
250 (chess-game-add-hook (chess-display-game nil)
251 'chess-database-event-handler database)
252 (chess-game-set-data (chess-display-game nil) 'database database)
253 (define-key (current-local-map) [? ] 'chess-puzzle-next)
254 (let ((count (chess-database-count database)))
255 (setq chess-puzzle-indices (make-vector count nil))
256 (dotimes (i count)
257 (aset chess-puzzle-indices i i))
258 (random t)
259 (shuffle-vector chess-puzzle-indices)
260 (setq chess-puzzle-position 0))
261 (chess-puzzle-next)))))
262
263 (chess-message-catalog 'english
264 '((bad-game-read . "Error reading game at position %d")
265 (end-of-puzzles . "There are no more puzzles in this collection")))
266
267 (defun chess-puzzle-next ()
268 "Play the next puzzle in the collection, selected randomly."
269 (interactive)
270 (let* ((game (chess-display-game nil))
271 (database (chess-game-data game 'database))
272 (index chess-puzzle-position)
273 next-game)
274 (if (= index (length chess-puzzle-indices))
275 (chess-message 'end-of-puzzles)
276 (setq chess-puzzle-position (1+ chess-puzzle-position))
277 (if (null (setq next-game
278 (chess-database-read database
279 (aref chess-puzzle-indices index))))
280 (chess-error 'bag-game-read
281 (aref chess-puzzle-indices index))
282 (chess-display-set-game nil next-game 0)
283 (chess-game-set-data game 'my-color
284 (chess-pos-side-to-move (chess-game-pos game)))
285 (dolist (key '(database database-index database-count))
286 (chess-game-set-data game key (chess-game-data next-game key)))))))
287
288 (provide 'chess)
289
290 ;;; chess.el ends here