]> code.delx.au - gnu-emacs-elpa/blob - chess.el
Update commentary.
[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: Mario Lang <mlang@delysid.org>
11 ;; Description: Play chess in Emacs
12 ;; URL: https://github.com/jwiegley/emacs-chess/
13 ;; Compatibility: Emacs24
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 ;; Type `M-x chess', and play chess against the default engine module.
37 ;; Type `C-u M-x chess' to select a specific engine.
38 ;;
39 ;; You can play against various external chess computer programs.
40 ;; There is also an Emacs based chess computer module which does not require
41 ;; any external programs. However, the internal AI is not very strong.
42 ;;
43 ;; To play on one of the internet chess servers, type `M-x chess-ics'.
44 ;;
45 ;; Once this is working, the next thing to do is to customize
46 ;; `chess-default-modules'. This is a list of functionality modules used
47 ;; by chess.el to provide its functionality. You can enable or
48 ;; disable modules so that Emacs Chess better suites your tastes.
49 ;; Those modules in turn often have configuration variables, and
50 ;; appropriate documentation at the top of the related file.
51 ;;
52 ;; Emacs Chess is designed in a highly modular fashion, using loosely
53 ;; coupled modules that respond to events on the chess board. This
54 ;; makes it very easy for programmers to add their own types of
55 ;; displays, opponents, analysis programs, etc. See the documentation
56 ;; in chess-module.el to learn more.
57 ;;
58 ;; Most people will probably also be interested in reading the top
59 ;; of chess-display.el and chess-pgn.el, which describe the user
60 ;; interface commands available in each of those buffer types.
61
62 ;;; Code:
63
64 (require 'chess-game)
65 (require 'chess-display)
66 (require 'chess-engine)
67
68 (defgroup chess nil
69 "An Emacs chess playing program."
70 :group 'games)
71
72 (defconst chess-version "2.0b6"
73 "The version of the Emacs chess program.")
74
75 (defcustom chess-default-display
76 '(chess-images chess-ics1 chess-plain)
77 "Default display to be used when starting a chess session.
78 A list indicates a series of alternatives if the first display is
79 not available."
80 :type '(choice symbol (repeat symbol))
81 :group 'chess)
82
83 (defcustom chess-default-modules
84 '((chess-sound chess-announce)
85 chess-autosave
86 chess-clock
87 ;;chess-kibitz jww (2002-04-30): not fully supported yet
88 ;;chess-chat
89 )
90 "Modules to be used when starting a chess session.
91 A sublist indicates a series of alternatives, if the first is not
92 available.
93 These can do just about anything."
94 :type '(repeat (choice symbol (repeat symbol)))
95 :group 'chess)
96
97 (defcustom chess-default-engine
98 '(chess-crafty
99 chess-stockfish chess-glaurung chess-fruit
100 chess-gnuchess chess-phalanx
101 chess-ai)
102 "Default engine to be used when starting a chess session.
103 A list indicates a series of alternatives if the first engine is not
104 available."
105 :type '(choice symbol (repeat symbol))
106 :group 'chess)
107
108 (defcustom chess-full-name (user-full-name)
109 "The full name to use when playing chess."
110 :type 'string
111 :group 'chess)
112
113 (and (fboundp 'font-lock-add-keywords)
114 (font-lock-add-keywords
115 'emacs-lisp-mode
116 '(("(\\(chess-error\\)\\>" 1 font-lock-warning-face)
117 ("(\\(chess-with-current-buffer\\)\\>" 1 font-lock-keyword-face))))
118
119 (defun chess--create-display (module game my-color disable-popup)
120 (let ((display (chess-display-create game module my-color)))
121 (when display
122 (chess-game-set-data game 'my-color my-color)
123 (if disable-popup
124 (chess-display-disable-popup display))
125 display)))
126
127 (defun chess--create-engine (module game response-handler ctor-args)
128 (let ((engine (apply 'chess-engine-create module game
129 response-handler ctor-args)))
130 (when engine
131 ;; for the sake of engines which are ready to play now, and
132 ;; which don't need connect/accept negotiation (most
133 ;; computerized engines fall into this category), we need to
134 ;; let them know we're ready to begin
135 (chess-engine-command engine 'ready)
136 engine)))
137
138 (defun chess-create-modules (module-list create-func &rest args)
139 "Create modules from MODULE-LIST with CREATE-FUNC and ARGS.
140 If an element of MODULE-LIST is a sublist, treat it as alternatives."
141 (let (objects)
142 (dolist (module module-list)
143 (let (object)
144 (if (symbolp module)
145 (if (setq object (apply create-func module args))
146 (push object objects))
147 ;; this module is actually a list, which means keep trying
148 ;; until we find one that works
149 (while module
150 (if (setq object (condition-case nil
151 (apply create-func (car module) args)
152 (error nil)))
153 (progn
154 (push object objects)
155 (setq module nil))
156 (setq module (cdr module)))))))
157 (nreverse objects)))
158
159 (chess-message-catalog 'english
160 '((no-engines-found
161 . "Could not find any chess engines to play against; install gnuchess!")))
162
163 ;;;###autoload
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).
167 With prefix argument, prompt for the engine to play against.
168 Otherwise use `chess-default-engine' to determine the engine."
169 (interactive
170 (list
171 (if current-prefix-arg
172 (intern
173 (concat "chess-"
174 (let ((str (read-string "Engine to play against: ")))
175 (if (> (length str) 0)
176 str
177 "none"))))
178 chess-default-engine)))
179
180 (let ((game (chess-game-create))
181 (my-color t) ; we start out as white always
182 objects)
183
184 ;; all these odd calls are so that `objects' ends up looking like:
185 ;; (ENGINE FIRST-DISPLAY...)
186
187 (setq objects (chess-create-modules (list chess-default-display)
188 'chess--create-display
189 game my-color disable-popup))
190 (when (car objects)
191 (mapc 'chess-display-update objects)
192 (chess-module-set-leader (car objects))
193 (unless disable-popup
194 (chess-display-popup (car objects))))
195
196 (nconc objects (chess-create-modules chess-default-modules
197 'chess-module-create game))
198
199 (push (unless (eq engine 'none)
200 (car ;(condition-case nil
201 (chess-create-modules (list (or engine chess-default-engine))
202 'chess--create-engine game
203 engine-response-handler
204 engine-ctor-args)
205 ; (error nil))
206 ))
207 objects)
208
209 (unless (car objects)
210 (chess-message 'no-engines-found))
211
212 objects))
213
214 ;;;###autoload
215 (defalias 'chess-session 'chess)
216
217 ;;;###autoload
218 (defun chess-create-display (perspective &optional modules-too)
219 "Create a display, letting the user's customization decide the style.
220 If MODULES-TOO is non-nil, also create and associate the modules
221 listed in `chess-default-modules'."
222 (if modules-too
223 (let ((display (cadr (chess-session 'none))))
224 (chess-display-set-perspective* display perspective))
225 (car (chess-create-modules (list chess-default-display)
226 'chess--create-display
227 (chess-game-create) perspective nil))))
228
229 (provide 'chess)
230
231 ;;; chess.el ends here