]> code.delx.au - gnu-emacs-elpa/blob - chess.el
Remove URL field as this is now the official repo.
[gnu-emacs-elpa] / chess.el
1 ;;; chess.el --- Play chess in GNU Emacs
2
3 ;; Copyright (C) 2001, 2014 Free Software Foundation, Inc.
4
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Maintainer: Mario Lang <mlang@delysid.org>
7 ;; Version: 0
8 ;; Package-Requires: ((cl-lib "0.5"))
9 ;; Keywords: games
10 ;; Compatibility: Emacs24
11
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Welcome to Emacs Chess, a chess playing module for GNU Emacs.
28 ;;
29 ;; chess.el is an Emacs Lisp library and several clients on top of the
30 ;; underlying library functionality for performing various activities
31 ;; related to the game of chess.
32 ;;
33 ;; Type `M-x chess', and play chess against one of the default engine modules.
34 ;;
35 ;; Type `C-u M-x chess' to select a specific engine.
36 ;; You can play against various external chess computer programs, like
37 ;; crafty, fruit, glaurung, gnuchess, phalanx, sjeng and stockfish.
38 ;; There is also an Emacs based chess computer module (ai) which does not
39 ;; require any external programs. However, the internal AI is not very strong.
40 ;;
41 ;; To play against another human on a different machine running GNU Emacs,
42 ;; type `C-u M-x chess RET network RET'.
43 ;; To play on one of the internet chess servers, type `M-x chess-ics'.
44 ;;
45 ;; If you'd like to view or edit Portable Game Notation (PGN) files,
46 ;; `chess-pgn-mode' provides a text-mode derived mode which can display the
47 ;; chess position at point.
48 ;;
49 ;; To improve your chessaility, `M-x chess-tutorial' provides a simple knight
50 ;; movement exercise to get you started, and `M-x chess-puzzle' can be used
51 ;; to solve puzzle collections in EPD or PGN format.
52 ;;
53 ;; There are some different types of chessboard display modules available.
54 ;; * A simple character based display (chess-plain).
55 ;; * A more verbose ASCII chessboard (chess-ics1).
56 ;; * A graphical chessboard display which uses images (chess-images).
57 ;;
58 ;; The variable `chess-default-display' controls which display modules
59 ;; are tried when a chessboard should be displayed. By default, chess-images
60 ;; is tried first. If Emacs is not running in a graphical environment,
61 ;; chess-ics1 is used instead. To enable the chess-plain display module,
62 ;; customize `chess-default-display' accordingly.
63 ;;
64 ;; Once this is working, the next thing to do is to customize
65 ;; `chess-default-modules'. This is a list of functionality modules used
66 ;; by chess.el to provide additional functionality. You can enable or
67 ;; disable modules so that Emacs Chess better suites your tastes.
68 ;; Those modules in turn often have configuration variables, and
69 ;; appropriate documentation at the top of the related file.
70 ;;
71 ;; Emacs Chess is designed in a highly modular fashion, using loosely
72 ;; coupled modules that respond to events on the chess board. This
73 ;; makes it very easy for programmers to add their own types of
74 ;; displays, opponents, analysis programs, etc. See the documentation
75 ;; in chess-module.el to learn more.
76 ;;
77 ;; Most people will probably also be interested in reading the top
78 ;; of chess-display.el and chess-pgn.el, which describe the user
79 ;; interface commands available in each of those buffer types.
80
81 ;;; Code:
82
83 (require 'chess-game)
84 (require 'chess-display)
85 (require 'chess-engine)
86
87 (defgroup chess nil
88 "An Emacs chess playing program."
89 :group 'games)
90
91 (defconst chess-version "2.0b6"
92 "The version of the Emacs chess program.")
93
94 (defcustom chess-default-display
95 '(chess-images chess-ics1 chess-plain)
96 "Default display to be used when starting a chess session.
97 A list indicates a series of alternatives if the first display is
98 not available."
99 :type '(choice symbol (repeat symbol))
100 :group 'chess)
101
102 (defcustom chess-default-modules
103 '((chess-sound chess-announce)
104 chess-autosave
105 chess-clock
106 ;;chess-kibitz jww (2002-04-30): not fully supported yet
107 ;;chess-chat
108 )
109 "Modules to be used when starting a chess session.
110 A sublist indicates a series of alternatives, if the first is not
111 available.
112 These can do just about anything."
113 :type '(repeat (choice symbol (repeat symbol)))
114 :group 'chess)
115
116 (defcustom chess-default-engine
117 '(chess-crafty
118 chess-stockfish chess-glaurung chess-fruit
119 chess-gnuchess chess-phalanx
120 chess-ai)
121 "Default engine to be used when starting a chess session.
122 A list indicates a series of alternatives if the first engine is not
123 available."
124 :type '(choice symbol (repeat symbol))
125 :group 'chess)
126
127 (defcustom chess-full-name (user-full-name)
128 "The full name to use when playing chess."
129 :type 'string
130 :group 'chess)
131
132 (and (fboundp 'font-lock-add-keywords)
133 (font-lock-add-keywords
134 'emacs-lisp-mode
135 '(("(\\(chess-error\\)\\>" 1 font-lock-warning-face)
136 ("(\\(chess-with-current-buffer\\)\\>" 1 font-lock-keyword-face))))
137
138 (defun chess--create-display (module game my-color disable-popup)
139 (let ((display (chess-display-create game module my-color)))
140 (when display
141 (chess-game-set-data game 'my-color my-color)
142 (if disable-popup
143 (chess-display-disable-popup display))
144 display)))
145
146 (defun chess--create-engine (module game response-handler ctor-args)
147 (let ((engine (apply 'chess-engine-create module game
148 response-handler ctor-args)))
149 (when engine
150 ;; for the sake of engines which are ready to play now, and
151 ;; which don't need connect/accept negotiation (most
152 ;; computerized engines fall into this category), we need to
153 ;; let them know we're ready to begin
154 (chess-engine-command engine 'ready)
155 engine)))
156
157 (defun chess-create-modules (module-list create-func &rest args)
158 "Create modules from MODULE-LIST with CREATE-FUNC and ARGS.
159 If an element of MODULE-LIST is a sublist, treat it as alternatives."
160 (let (objects)
161 (dolist (module module-list)
162 (let (object)
163 (if (symbolp module)
164 (if (setq object (apply create-func module args))
165 (push object objects))
166 ;; this module is actually a list, which means keep trying
167 ;; until we find one that works
168 (while module
169 (if (setq object (condition-case nil
170 (apply create-func (car module) args)
171 (error nil)))
172 (progn
173 (push object objects)
174 (setq module nil))
175 (setq module (cdr module)))))))
176 (nreverse objects)))
177
178 (chess-message-catalog 'english
179 '((no-engines-found
180 . "Could not find any chess engines to play against; install gnuchess!")))
181
182 ;;;###autoload
183 (defun chess (&optional engine disable-popup engine-response-handler
184 &rest engine-ctor-args)
185 "Start a game of chess, playing against ENGINE (a module name).
186 With prefix argument, prompt for the engine to play against.
187 Otherwise use `chess-default-engine' to determine the engine."
188 (interactive
189 (list
190 (if current-prefix-arg
191 (intern
192 (concat "chess-"
193 (let ((str (read-string "Engine to play against: ")))
194 (if (> (length str) 0)
195 str
196 "none"))))
197 chess-default-engine)))
198
199 (let ((game (chess-game-create))
200 (my-color t) ; we start out as white always
201 objects)
202
203 ;; all these odd calls are so that `objects' ends up looking like:
204 ;; (ENGINE FIRST-DISPLAY...)
205
206 (setq objects (chess-create-modules (list chess-default-display)
207 'chess--create-display
208 game my-color disable-popup))
209 (when (car objects)
210 (mapc 'chess-display-update objects)
211 (chess-module-set-leader (car objects))
212 (unless disable-popup
213 (chess-display-popup (car objects))))
214
215 (nconc objects (chess-create-modules chess-default-modules
216 'chess-module-create game))
217
218 (push (unless (eq engine 'none)
219 (car ;(condition-case nil
220 (chess-create-modules (list (or engine chess-default-engine))
221 'chess--create-engine game
222 engine-response-handler
223 engine-ctor-args)
224 ; (error nil))
225 ))
226 objects)
227
228 (unless (car objects)
229 (chess-message 'no-engines-found))
230
231 objects))
232
233 ;;;###autoload
234 (defalias 'chess-session 'chess)
235
236 ;;;###autoload
237 (defun chess-create-display (perspective &optional modules-too)
238 "Create a display, letting the user's customization decide the style.
239 If MODULES-TOO is non-nil, also create and associate the modules
240 listed in `chess-default-modules'."
241 (if modules-too
242 (let ((display (cadr (chess-session 'none))))
243 (chess-display-set-perspective* display perspective))
244 (car (chess-create-modules (list chess-default-display)
245 'chess--create-display
246 (chess-game-create) perspective nil))))
247
248 (provide 'chess)
249
250 ;;; chess.el ends here