]> code.delx.au - gnu-emacs-elpa/blob - chess.el
*** no comment ***
[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-database)
80 (require 'chess-file)
81
82 (defgroup chess nil
83 "An Emacs chess playing program."
84 :group 'games)
85
86 (defconst chess-version "2.0a8"
87 "The version of the Emacs chess program.")
88
89 (defcustom chess-default-display
90 '(chess-images chess-ics1 chess-plain)
91 "Default display to be used when starting a chess session.
92 A list indicates a series of alternatives if the first display is
93 not available."
94 :type '(choice symbol (repeat symbol))
95 :group 'chess)
96
97 (defcustom chess-default-modules
98 '((chess-sound chess-announce)
99 chess-autosave)
100 "Modules to be used when starting a chess session.
101 A sublist indicates a series of alternatives, if the first is not
102 available.
103 These can do just about anything."
104 :type '(choice symbol (repeat symbol))
105 :group 'chess)
106
107 (defcustom chess-default-engine
108 '(chess-crafty chess-gnuchess chess-phalanx)
109 "Default engine to be used when starting a chess session.
110 A list indicates a series of alternatives if the first engine is not
111 available."
112 :type '(choice symbol (repeat symbol))
113 :group 'chess)
114
115 (defcustom chess-full-name (user-full-name)
116 "The full name to use when playing chess."
117 :type 'string
118 :group 'chess)
119
120 (defun chess--create-display (module game my-color disable-popup)
121 (if (require module nil t)
122 (let ((display (chess-display-create game module my-color)))
123 (when display
124 (chess-game-set-data game 'my-color my-color)
125 (if disable-popup
126 (chess-display-disable-popup display))
127 display))))
128
129 (defun chess--create-module (module game)
130 (and (require module nil t)
131 (chess-module-create module game)))
132
133 (defun chess--create-engine (module game response-handler ctor-args)
134 (if (require module nil t)
135 (let ((engine (apply 'chess-engine-create module game
136 response-handler ctor-args)))
137 (when engine
138 ;; for the sake of engines which are ready to play now, and
139 ;; which don't need connect/accept negotiation (most
140 ;; computerized engines fall into this category), we need to
141 ;; let them know we're ready to begin
142 (chess-engine-command engine 'ready)
143 engine))))
144
145 (defun chess-create-modules (module-list create-func &rest args)
146 (let (objects)
147 (dolist (module module-list)
148 (let (object)
149 (if (symbolp module)
150 (if (setq object (apply create-func module args))
151 (push object objects))
152 ;; this module is actually a list, which means keep trying
153 ;; until we find one that works
154 (while module
155 (if (setq object (apply create-func (car module) args))
156 (progn
157 (push object objects)
158 (setq module nil))
159 (setq module (cdr module)))))))
160 (nreverse objects)))
161
162 ;;;###autoload
163 (defun chess (&optional engine disable-popup engine-response-handler
164 &rest engine-ctor-args)
165 "Start a game of chess, playing against ENGINE (a module name)."
166 (interactive
167 (list
168 (if current-prefix-arg
169 (intern
170 (concat "chess-"
171 (let ((str (read-string "Engine to play against: ")))
172 (if (> (length str) 0)
173 str
174 "none"))))
175 chess-default-engine)))
176
177 (let ((game (chess-game-create))
178 (my-color t) ; we start out as white always
179 objects)
180
181 ;; all these odd calls are so that `objects' ends up looking like:
182 ;; (ENGINE FIRST-DISPLAY...)
183
184 (setq objects (chess-create-modules (list chess-default-display)
185 'chess--create-display
186 game my-color disable-popup))
187 (when (car objects)
188 (mapc 'chess-display-update objects)
189 (chess-module-set-leader (car objects))
190 (chess-display-popup (car objects)))
191
192 (nconc objects (chess-create-modules chess-default-modules
193 'chess--create-module game))
194
195 (push (car (chess-create-modules (list (or engine chess-default-engine))
196 'chess--create-engine game
197 engine-response-handler
198 engine-ctor-args))
199 objects)
200
201 objects))
202
203 (defalias 'chess-session 'chess)
204
205 (defun chess-create-display ()
206 "Just make a display to use, letting chess.el decide the style."
207 (cadr (chess-session 'chess-none)))
208
209 (defun chess-create-display-object (perspective)
210 (car (chess-create-modules (list chess-default-display)
211 'chess--create-display
212 (chess-mage-create) perspective)))
213
214 ;;;###autoload
215 (defun chess-read-pgn (&optional file)
216 "Read and display a PGN game after point."
217 (interactive "P")
218 (if (or file (not (search-forward "[Event " nil t)))
219 (setq file (read-file-name "Read a PGN game from file: ")))
220 (if file
221 (find-file file))
222 (let ((game (chess-pgn-to-game))
223 display)
224 (when game
225 (setq display (chess-create-display))
226 (chess-display-set-game display game))))
227
228 ;;;###autoload
229 (defun chess-puzzle (file &optional index)
230 "Pick a random puzzle from FILE, and solve it against the default engine.
231 The spacebar in the display buffer is bound to `chess-puzzle-next',
232 making it easy to go on to the next puzzle once you've solved one."
233 (interactive "fRead chess puzzles from: ")
234 (random t)
235 (let* ((database (chess-database-open 'chess-file file))
236 (objects (and database (chess-session)))
237 (display (cadr objects)))
238 (when database
239 (with-current-buffer display
240 ;; make sure the database is closed when the display is shutdown
241 (chess-game-add-hook (chess-display-game nil)
242 'chess-database-event-handler database)
243 (chess-game-set-data (chess-display-game nil) 'database database)
244 (define-key (current-local-map) [? ] 'chess-puzzle-next)
245 (chess-puzzle-next)))))
246
247 (defun chess-puzzle-next ()
248 "Play the next puzzle in the collection, selected randomly."
249 (interactive)
250 (let* ((game (chess-display-game nil))
251 (database (chess-game-data game 'database))
252 (index (random (chess-database-count database)))
253 (next-game (chess-database-read database index)))
254 (if (null next-game)
255 (error "Error reading game at position %d" index)
256 (chess-display-set-game nil next-game 0)
257 (chess-game-set-data game 'my-color
258 (chess-pos-side-to-move (chess-game-pos game)))
259 (dolist (key '(database database-index database-count))
260 (chess-game-set-data game key (chess-game-data next-game key))))))
261
262 (provide 'chess)
263
264 ;;; chess.el ends here