]> code.delx.au - gnu-emacs-elpa/blob - chess.el
temporarily disable the condition-case in the main startup function, some strange...
[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
80 (defgroup chess nil
81 "An Emacs chess playing program."
82 :group 'games)
83
84 (defconst chess-version "2.0b4"
85 "The version of the Emacs chess program.")
86
87 (defcustom chess-default-display
88 '(chess-images chess-ics1 chess-plain)
89 "Default display to be used when starting a chess session.
90 A list indicates a series of alternatives if the first display is
91 not available."
92 :type '(choice symbol (repeat symbol))
93 :group 'chess)
94
95 (defcustom chess-default-modules
96 '((chess-sound chess-announce)
97 chess-autosave
98 chess-clock
99 chess-opening
100 ;;chess-kibitz jww (2002-04-30): not fully supported yet
101 ;;chess-chat
102 )
103 "Modules to be used when starting a chess session.
104 A sublist indicates a series of alternatives, if the first is not
105 available.
106 These can do just about anything."
107 :type '(repeat (choice symbol (repeat symbol)))
108 :group 'chess)
109
110 (defcustom chess-default-engine
111 '(chess-crafty chess-gnuchess chess-phalanx chess-ai)
112 "Default engine to be used when starting a chess session.
113 A list indicates a series of alternatives if the first engine is not
114 available."
115 :type '(choice symbol (repeat symbol))
116 :group 'chess)
117
118 (defcustom chess-full-name (user-full-name)
119 "The full name to use when playing chess."
120 :type 'string
121 :group 'chess)
122
123 (defun chess--create-display (module game my-color disable-popup)
124 (let ((display (chess-display-create game module my-color)))
125 (when display
126 (chess-game-set-data game 'my-color my-color)
127 (if disable-popup
128 (chess-display-disable-popup display))
129 display)))
130
131 (defun chess--create-engine (module game response-handler ctor-args)
132 (let ((engine (apply 'chess-engine-create module game
133 response-handler ctor-args)))
134 (when engine
135 ;; for the sake of engines which are ready to play now, and
136 ;; which don't need connect/accept negotiation (most
137 ;; computerized engines fall into this category), we need to
138 ;; let them know we're ready to begin
139 (chess-engine-command engine 'ready)
140 engine)))
141
142 (defun chess-create-modules (module-list create-func &rest args)
143 "Create modules from MODULE-LIST with CREATE-FUNC and ARGS.
144 If an element of MODULE-LIST is a sublist, treat it as alternatives."
145 (let (objects)
146 (dolist (module module-list)
147 (let (object)
148 (if (symbolp module)
149 (if (setq object (apply create-func module args))
150 (push object objects))
151 ;; this module is actually a list, which means keep trying
152 ;; until we find one that works
153 (while module
154 (if (setq object (apply create-func (car module) args))
155 (progn
156 (push object objects)
157 (setq module nil))
158 (setq module (cdr module)))))))
159 (nreverse objects)))
160
161 (chess-message-catalog 'english
162 '((no-engines-found . "Could not find any chess engines to play against; install gnuchess!")))
163
164 ;;;###autoload
165 (defun chess (&optional engine disable-popup engine-response-handler
166 &rest engine-ctor-args)
167 "Start a game of chess, playing against ENGINE (a module name)."
168 (interactive
169 (list
170 (if current-prefix-arg
171 (intern
172 (concat "chess-"
173 (let ((str (read-string "Engine to play against: ")))
174 (if (> (length str) 0)
175 str
176 "none"))))
177 chess-default-engine)))
178
179 (let ((game (chess-game-create))
180 (my-color t) ; we start out as white always
181 objects)
182
183 ;; all these odd calls are so that `objects' ends up looking like:
184 ;; (ENGINE FIRST-DISPLAY...)
185
186 (setq objects (chess-create-modules (list chess-default-display)
187 'chess--create-display
188 game my-color disable-popup))
189 (when (car objects)
190 (mapc 'chess-display-update objects)
191 (chess-module-set-leader (car objects))
192 (unless disable-popup
193 (chess-display-popup (car objects))))
194
195 (nconc objects (chess-create-modules chess-default-modules
196 'chess-module-create game))
197
198 (push (unless (eq engine 'none)
199 (car ;(condition-case nil
200 (chess-create-modules (list (or engine chess-default-engine))
201 'chess--create-engine game
202 engine-response-handler
203 engine-ctor-args)
204 ;(error nil))
205 ))
206 objects)
207
208 (unless (car objects)
209 (chess-message 'no-engines-found))
210
211 objects))
212
213 ;;;###autoload
214 (defalias 'chess-session 'chess)
215
216 ;;;###autoload
217 (defun chess-create-display (perspective &optional modules-too)
218 "Create a display, letting the user's customization decide the style.
219 If MODULES-TOO is non-nil, also create and associate the modules
220 listed in `chess-default-modules'."
221 (if modules-too
222 (let ((display (cadr (chess-session 'none))))
223 (chess-display-set-perspective* display perspective))
224 (car (chess-create-modules (list chess-default-display)
225 'chess--create-display
226 (chess-game-create) perspective nil))))
227
228 (provide 'chess)
229
230 ;;; chess.el ends here