2 ;;; chess.el --- Play chess in Emacs
4 ;; Copyright (C) 2001 John Wiegley <johnw@gnu.org>
6 ;; Emacs Lisp Archive Entry
10 ;; Author: John Wiegley <johnw@gnu.org>
11 ;; Maintainer: John Wiegley <johnw@gnu.org>
12 ;; Description: Play chess in Emacs
13 ;; URL: http://www.gci-net.com/~johnw/Emacs/packages/chess.tar.gz
14 ;; Compatibility: Emacs20, Emacs21, XEmacs21
16 ;; This file is not part of GNU Emacs.
18 ;; This is free software; you can redistribute it and/or modify it under
19 ;; the terms of the GNU General Public License as published by the Free
20 ;; Software Foundation; either version 2, or (at your option) any later
23 ;; This is distributed in the hope that it will be useful, but WITHOUT
24 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs; see the file COPYING. If not, write to the
30 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31 ;; MA 02111-1307, USA.
35 ;; Welcome to Emacs Chess, a chess playing module for GNU Emacs.
37 ;; This program will not play chess against you; it is not a chess
38 ;; computer. It can use a chess computer, however, to simulate your
39 ;; opponent's moves. This is decided when you choose your opponent.
40 ;; You must, of course, have that chess computer installed. See the
41 ;; top of chess-player.el for more information.
43 ;; To just get a chessboard up, put the following in your .emacs file:
45 ;; (add-to-list 'load-list "<the path to Emacs Chess>")
47 ;; (autoload 'chess "chess" "Play a game of chess" t)
49 ;; Now you can type `M-x chess', and play chess against anyone else in
50 ;; the room with you, without having to install anything more.
52 ;; Once this is working, the next thing to do is to customize
53 ;; `chess-use-modules'. This is a list of functionality modules used
54 ;; by chess.el to provide its functionality. You can enable or
55 ;; disable modules so that Emacs Chess better suites your tastes.
56 ;; Those modules in turn often have configuration variables, and
57 ;; appropriate documentation at the top of the related file.
59 ;; Emacs Chess is designed in a highly modular fashion, using loosely
60 ;; coupled modules that respond to events on the chess board. This
61 ;; makes it very easy for programmers to add their own types of
62 ;; displays, opponents, analysis programs, etc. See the documentation
63 ;; in chess-module.el to learn more.
65 ;; There is no documentation for this program other than what exists
66 ;; in the source files. This is because the source files aim at being
67 ;; self documenting, and as chess is such a simple game, most chess
68 ;; players aren't going to need to know much about this program in
71 ;; However, most people will probably be interested in reading the top
72 ;; of chess-display.el and chess-pgn.el, which describe the user
73 ;; interface commands available in each of those buffer types.
78 (require 'chess-display)
79 (require 'chess-engine)
83 "An Emacs chess playing program."
86 (defconst chess-version "2.0a7"
87 "The version of the Emacs chess program.")
89 (defcustom chess-default-display (if (display-graphic-p)
90 'chess-images 'chess-ics1)
91 "Default module set to be used when starting a chess session."
95 (defcustom chess-default-engine 'chess-gnuchess
96 "Default engine to be used when starting a chess session."
100 (defcustom chess-announce-moves t
101 "If non-nil, announce when your opponent makes a move.
102 This variable can also be a symbol which names a different announcing
103 module to use. This happens verbally if 'festival' is installed, or
104 if you have sound files installed and a sound play (see
105 chess-sound.el). Otherwise it just prints a message in your
106 minibuffer, which works well for Emacspeak users."
110 (defcustom chess-full-name (user-full-name)
111 "The full name to use when playing chess."
116 (defun chess (&optional engine disable-popup engine-response-handler
117 &rest engine-ctor-args)
118 "Start a game of chess, playing against ENGINE (a module name)."
121 (if current-prefix-arg
124 (let ((str (read-string "Engine to play against: ")))
125 (if (> (length str) 0)
128 chess-default-engine)))
130 (require chess-default-display)
131 (let* ((my-color t) ; we start out as white always
132 (display (chess-display-create chess-default-display my-color))
133 (game (chess-game-create)))
135 (when (and (eq chess-default-display 'chess-images)
136 (with-current-buffer display
137 (null chess-images-size)))
138 (message "Could not find suitable chess images; using ics1 display")
139 (chess-display-destroy display)
140 (require 'chess-ics1)
141 (setq display (chess-display-create 'chess-ics1 my-color)))
143 (chess-game-set-data game 'my-color my-color)
145 (chess-display-disable-popup display))
146 (chess-display-set-game display game)
147 (chess-display-set-main display)
149 (let ((engine-module (or engine chess-default-engine)))
150 (when (and engine-module (require engine-module nil t))
151 (let ((engine (apply 'chess-engine-create engine-module nil
153 (chess-engine-set-game* engine game)
154 ;; for the sake of engines which are ready to play now, and
155 ;; which don't need connect/accept negotiation (most
156 ;; computerized engines fall into this category), we need to
157 ;; let them know we're ready to begin
158 (chess-engine-command engine 'ready))
160 (when chess-announce-moves
161 (if (and (not (eq chess-announce-moves t))
162 (symbolp chess-announce-moves))
163 (let ((name (symbol-name chess-announce-moves)))
164 (require chess-announce-moves)
165 (if (funcall (intern (concat name "-available-p")))
166 (funcall (intern (concat name "-for-game")) game)))
167 (require 'chess-sound)
168 (if (chess-sound-available-p)
169 (chess-sound-for-game game)
170 (require 'chess-announce)
171 (if (chess-announce-available-p)
172 (chess-announce-for-game game)))))))
174 (chess-display-update display t)
176 (cons display engine)))
179 (defun chess-read-pgn (&optional file)
180 "Read and display a PGN game after point."
182 (if (or file (not (search-forward "[Event" nil t)))
183 (setq file (read-file-name "Read a PGN game from file: ")))
186 (let ((game (chess-pgn-to-game)))
188 (require chess-default-display)
189 (chess-display-set-game
190 (chess-display-create chess-default-display
191 (chess-game-side-to-move game)) game))))
193 (defvar chess-puzzle-locations nil)
195 (defun chess-puzzle-next ()
196 "Play the next puzzle in the collection, selected randomly."
198 (if chess-puzzle-locations
199 (chess-puzzle (aref chess-puzzle-locations 0))))
202 (defun chess-puzzle (file)
203 "Pick a random puzzle from FILE, and solve it against the default engine.
204 The spacebar in the display buffer is bound to `chess-puzzle-next',
205 making it easy to go on to the next puzzle once you've solved one."
206 (interactive "fRead chess puzzles from: ")
208 (with-current-buffer (find-file-noselect file)
209 (when (or (null chess-puzzle-locations)
210 (not (equal file (aref chess-puzzle-locations 0))))
212 (goto-char (point-min))
213 (while (search-forward "[Event" nil t)
214 (push (point) locations))
215 (setq chess-puzzle-locations (vector file locations nil nil)))
217 (goto-char (nth (random (length (aref chess-puzzle-locations 1)))
218 (aref chess-puzzle-locations 1)))
219 (let ((game (chess-pgn-to-game)))
221 (require chess-default-display)
222 (let ((puzzle-display
223 (or (and (buffer-live-p (aref chess-puzzle-locations 2))
224 (aref chess-puzzle-locations 2))
225 (chess-display-create chess-default-display
226 (chess-game-side-to-move game)))))
227 (chess-display-set-game puzzle-display game)
228 (aset chess-puzzle-locations 2 puzzle-display)
229 ;; setup spacebar as a convenient way to jump to the next puzzle
230 (with-current-buffer puzzle-display
231 (define-key (current-local-map) [? ] 'chess-puzzle-next)))
232 (require chess-default-engine)
234 (or (and (buffer-live-p (aref chess-puzzle-locations 3))
235 (aref chess-puzzle-locations 3))
236 (chess-engine-create chess-default-engine))))
237 (chess-engine-set-game puzzle-engine game)
238 (aset chess-puzzle-locations 3 puzzle-engine)))))))
242 ;;; chess.el ends here