1 ;;; chess.el --- Play chess in GNU Emacs
3 ;; Copyright (C) 2001, 2014 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Maintainer: Mario Lang <mlang@delysid.org>
8 ;; Package-Requires: ((cl-lib "0.5"))
10 ;; Compatibility: Emacs24
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.
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.
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/>.
27 ;; Welcome to Emacs Chess, a chess playing module for GNU Emacs.
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.
33 ;; Type `M-x chess', and play chess against one of the default engine modules.
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.
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'.
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.
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.
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).
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.
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.
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.
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.
84 (require 'chess-display)
85 (require 'chess-engine)
88 "An Emacs chess playing program."
90 :link '(custom-manual "(chess)Top"))
92 (defconst chess-version "2.0.4"
93 "The version of the Emacs chess program.")
95 (defcustom chess-default-display
96 '(chess-images chess-ics1 chess-plain)
97 "Default display to be used when starting a chess session.
98 A list indicates a series of alternatives if the first display is
100 :type '(choice symbol (repeat symbol))
103 (defcustom chess-default-modules
104 '((chess-sound chess-announce)
105 ;;chess-autosave ml (2014-06-06): module not fully working
107 ;;chess-kibitz jww (2002-04-30): not fully supported yet
110 "Modules to be used when starting a chess session.
111 A sublist indicates a series of alternatives, if the first is not
113 These can do just about anything."
114 :type '(repeat (choice symbol (repeat symbol)))
117 (defcustom chess-default-engine
119 chess-stockfish chess-glaurung chess-fruit
120 chess-gnuchess chess-phalanx
122 "Default engine to be used when starting a chess session.
123 A list indicates a series of alternatives if the first engine is not
125 :type '(choice symbol (repeat symbol))
128 (defcustom chess-full-name (user-full-name)
129 "The full name to use when playing chess."
133 (and (fboundp 'font-lock-add-keywords)
134 (font-lock-add-keywords
136 '(("(\\(chess-error\\)\\>" 1 font-lock-warning-face)
137 ("(\\(chess-with-current-buffer\\)\\>" 1 font-lock-keyword-face))))
139 (defun chess--create-display (module game my-color disable-popup)
140 (let ((display (chess-display-create game module my-color)))
142 (chess-game-set-data game 'my-color my-color)
144 (chess-display-disable-popup display))
147 (defun chess--create-engine (module game response-handler ctor-args)
148 (let ((engine (apply 'chess-engine-create module game
149 response-handler ctor-args)))
151 ;; for the sake of engines which are ready to play now, and
152 ;; which don't need connect/accept negotiation (most
153 ;; computerized engines fall into this category), we need to
154 ;; let them know we're ready to begin
155 (chess-engine-command engine 'ready)
158 (defun chess-create-modules (module-list create-func &rest args)
159 "Create modules from MODULE-LIST with CREATE-FUNC and ARGS.
160 If an element of MODULE-LIST is a sublist, treat it as alternatives."
162 (dolist (module module-list)
165 (if (setq object (apply create-func module args))
166 (push object objects))
167 ;; this module is actually a list, which means keep trying
168 ;; until we find one that works
170 (if (setq object (condition-case nil
171 (apply create-func (car module) args)
174 (push object objects)
176 (setq module (cdr module)))))))
179 (chess-message-catalog 'english
181 . "Could not find any chess engines to play against; install gnuchess!")))
184 (defun chess (&optional engine disable-popup engine-response-handler
185 &rest engine-ctor-args)
186 "Start a game of chess, playing against ENGINE (a module name).
187 With prefix argument, prompt for the engine to play against.
188 Otherwise use `chess-default-engine' to determine the engine."
191 (if current-prefix-arg
194 (let ((str (read-string "Engine to play against: ")))
195 (if (> (length str) 0)
198 chess-default-engine)))
200 (let ((game (chess-game-create))
201 (my-color t) ; we start out as white always
204 ;; all these odd calls are so that `objects' ends up looking like:
205 ;; (ENGINE FIRST-DISPLAY...)
207 (setq objects (chess-create-modules (list chess-default-display)
208 'chess--create-display
209 game my-color disable-popup))
211 (mapc 'chess-display-update objects)
212 (chess-module-set-leader (car objects))
213 (unless disable-popup
214 (chess-display-popup (car objects))))
216 (nconc objects (chess-create-modules chess-default-modules
217 'chess-module-create game))
219 (push (unless (eq engine 'none)
220 (car ;(condition-case nil
221 (chess-create-modules (list (or engine chess-default-engine))
222 'chess--create-engine game
223 engine-response-handler
229 (unless (car objects)
230 (chess-message 'no-engines-found))
235 (defalias 'chess-session 'chess)
238 (define-key menu-bar-games-menu [chess] '(menu-item "Chess" chess :help "Play Chess"))
241 (defun chess-create-display (perspective &optional modules-too)
242 "Create a display, letting the user's customization decide the style.
243 If MODULES-TOO is non-nil, also create and associate the modules
244 listed in `chess-default-modules'."
246 (let ((display (cadr (chess-session 'none))))
247 (chess-display-set-perspective* display perspective))
248 (car (chess-create-modules (list chess-default-display)
249 'chess--create-display
250 (chess-game-create) perspective nil))))
254 ;;; chess.el ends here