1 ;;; chess.el --- Play chess in Emacs
3 ;; Copyright (C) 2001 John Wiegley <johnw@gnu.org>
5 ;; Emacs Lisp Archive Entry
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
15 ;; This file is not part of GNU Emacs.
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
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
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.
34 ;; Welcome to Emacs Chess, a chess playing module for GNU Emacs.
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.
42 ;; To just get a chessboard up, put the following in your .emacs file:
44 ;; (add-to-list 'load-list "<the path to Emacs Chess>")
46 ;; (autoload 'chess "chess" "Play a game of chess" t)
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.
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.
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.
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
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.
77 (require 'chess-display)
78 (require 'chess-engine)
79 (require 'chess-random)
80 (require 'chess-database)
84 "An Emacs chess playing program."
87 (defconst chess-version "2.0a12"
88 "The version of the Emacs chess program.")
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
95 :type '(choice symbol (repeat symbol))
98 (defcustom chess-default-modules
99 '((chess-sound chess-announce)
104 "Modules to be used when starting a chess session.
105 A sublist indicates a series of alternatives, if the first is not
107 These can do just about anything."
108 :type '(repeat (choice symbol (repeat symbol)))
111 (defcustom chess-default-engine
112 '(chess-crafty chess-gnuchess chess-phalanx)
113 "Default engine to be used when starting a chess session.
114 A list indicates a series of alternatives if the first engine is not
116 :type '(choice symbol (repeat symbol))
119 (defcustom chess-full-name (user-full-name)
120 "The full name to use when playing chess."
124 (defun chess--create-display (module game my-color disable-popup)
125 (let ((display (chess-display-create game module my-color)))
127 (chess-game-set-data game 'my-color my-color)
129 (chess-display-disable-popup display))
132 (defun chess--create-engine (module game response-handler ctor-args)
133 (let ((engine (apply 'chess-engine-create module game
134 response-handler ctor-args)))
136 ;; for the sake of engines which are ready to play now, and
137 ;; which don't need connect/accept negotiation (most
138 ;; computerized engines fall into this category), we need to
139 ;; let them know we're ready to begin
140 (chess-engine-command engine 'ready)
143 (defun chess-create-modules (module-list create-func &rest args)
145 (dolist (module module-list)
148 (if (setq object (apply create-func module args))
149 (push object objects))
150 ;; this module is actually a list, which means keep trying
151 ;; until we find one that works
153 (if (setq object (apply create-func (car module) args))
155 (push object objects)
157 (setq module (cdr module)))))))
160 (chess-message-catalog 'english
161 '((no-engines-found . "Could not find any chess engines to play against; install gnuchess!")))
164 (defun chess (&optional engine disable-popup engine-response-handler
165 &rest engine-ctor-args)
166 "Start a game of chess, playing against ENGINE (a module name)."
169 (if current-prefix-arg
172 (let ((str (read-string "Engine to play against: ")))
173 (if (> (length str) 0)
176 chess-default-engine)))
178 (let ((game (chess-game-create))
179 (my-color t) ; we start out as white always
182 ;; all these odd calls are so that `objects' ends up looking like:
183 ;; (ENGINE FIRST-DISPLAY...)
185 (setq objects (chess-create-modules (list chess-default-display)
186 'chess--create-display
187 game my-color disable-popup))
189 (mapc 'chess-display-update objects)
190 (chess-module-set-leader (car objects))
191 (chess-display-popup (car objects)))
193 (nconc objects (chess-create-modules chess-default-modules
194 'chess-module-create game))
196 (push (car (chess-create-modules (list (or engine chess-default-engine))
197 'chess--create-engine game
198 engine-response-handler
202 (unless (car objects)
203 (chess-message 'no-engines-found))
208 (defalias 'chess-session 'chess)
211 (defun chess-create-display ()
212 "Just make a display to use, letting chess.el decide the style."
213 (cadr (chess-session 'chess-none)))
216 (defun chess-create-display-object (perspective)
217 (car (chess-create-modules (list chess-default-display)
218 'chess--create-display
219 (chess-game-create) perspective nil)))
221 (defvar chess-puzzle-indices nil)
222 (defvar chess-puzzle-position nil)
223 (make-variable-buffer-local 'chess-puzzle-indices)
224 (make-variable-buffer-local 'chess-puzzle-position)
227 (defun chess-puzzle (file &optional index)
228 "Pick a random puzzle from FILE, and solve it against the default engine.
229 The spacebar in the display buffer is bound to `chess-puzzle-next',
230 making it easy to go on to the next puzzle once you've solved one."
231 (interactive "fRead chess puzzles from: ")
232 (let* ((database (chess-database-open 'chess-file file))
233 (objects (and database (chess-session)))
234 (display (cadr objects)))
236 (with-current-buffer display
237 ;; make sure the database is closed when the display is shutdown
238 (chess-game-add-hook (chess-display-game nil)
239 'chess-database-event-handler database)
240 (chess-game-set-data (chess-display-game nil) 'database database)
241 (define-key (current-local-map) [? ] 'chess-puzzle-next)
242 (let ((count (chess-database-count database)))
243 (setq chess-puzzle-indices (make-vector count nil))
245 (aset chess-puzzle-indices i i))
247 (shuffle-vector chess-puzzle-indices)
248 (setq chess-puzzle-position 0))
249 (chess-puzzle-next)))))
251 (chess-message-catalog 'english
252 '((bad-game-read . "Error reading game at position %d")
253 (end-of-puzzles . "There are no more puzzles in this collection")))
255 (defun chess-puzzle-next ()
256 "Play the next puzzle in the collection, selected randomly."
258 (let* ((game (chess-display-game nil))
259 (database (chess-game-data game 'database))
260 (index chess-puzzle-position)
262 (if (= index (length chess-puzzle-indices))
263 (chess-message 'end-of-puzzles)
264 (setq chess-puzzle-position (1+ chess-puzzle-position))
265 (if (null (setq next-game
266 (chess-database-read database
267 (aref chess-puzzle-indices index))))
268 (chess-error 'bag-game-read
269 (aref chess-puzzle-indices index))
270 (chess-display-set-game nil next-game 0)
271 (chess-game-set-data game 'my-color
272 (chess-pos-side-to-move (chess-game-pos game)))
273 (dolist (key '(database database-index database-count))
274 (chess-game-set-data game key (chess-game-data next-game key)))))))
276 (chess-message-catalog 'english
277 '((queen-would-take . "The queen would take your knight!")
278 (congratulations . "Congratulations!")))
280 (defun chess-tutorial-knight-1 (game ignore event &rest args)
282 (let ((position (chess-game-pos game)))
283 (if (null (chess-pos-search position ?p))
284 (chess-message 'congratulations)
285 (when (chess-search-position
286 position (car (chess-pos-search position ?N)) ?q)
287 (chess-game-run-hooks chess-module-game 'undo 1)
288 (chess-display-update nil)
289 (chess-error 'queen-would-take))))))
291 (defun chess-tutorial ()
293 (let* (chess-default-modules
294 (display (chess-create-display)))
295 (with-current-buffer display
296 (chess-game-set-start-position
297 (chess-display-game nil)
298 (chess-fen-to-pos "8/3p1p/2p3p/4q/2p3p/3p1p/8/N w - -"))
299 (chess-game-add-hook (chess-display-game nil) 'chess-tutorial-knight-1)
300 (setq chess-pos-always-white t)
301 (chess-display-popup nil)
302 (message "Goal: take all the pawns, without letting the queen take your knight"))))
306 ;;; chess.el ends here