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."
91 (defconst chess-version "2.0b6"
92 "The version of the Emacs chess program.")
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
99 :type '(choice symbol (repeat symbol))
102 (defcustom chess-default-modules
103 '((chess-sound chess-announce)
106 ;;chess-kibitz jww (2002-04-30): not fully supported yet
109 "Modules to be used when starting a chess session.
110 A sublist indicates a series of alternatives, if the first is not
112 These can do just about anything."
113 :type '(repeat (choice symbol (repeat symbol)))
116 (defcustom chess-default-engine
118 chess-stockfish chess-glaurung chess-fruit
119 chess-gnuchess chess-phalanx
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
124 :type '(choice symbol (repeat symbol))
127 (defcustom chess-full-name (user-full-name)
128 "The full name to use when playing chess."
132 (and (fboundp 'font-lock-add-keywords)
133 (font-lock-add-keywords
135 '(("(\\(chess-error\\)\\>" 1 font-lock-warning-face)
136 ("(\\(chess-with-current-buffer\\)\\>" 1 font-lock-keyword-face))))
138 (defun chess--create-display (module game my-color disable-popup)
139 (let ((display (chess-display-create game module my-color)))
141 (chess-game-set-data game 'my-color my-color)
143 (chess-display-disable-popup display))
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)))
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)
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."
161 (dolist (module module-list)
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
169 (if (setq object (condition-case nil
170 (apply create-func (car module) args)
173 (push object objects)
175 (setq module (cdr module)))))))
178 (chess-message-catalog 'english
180 . "Could not find any chess engines to play against; install gnuchess!")))
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."
190 (if current-prefix-arg
193 (let ((str (read-string "Engine to play against: ")))
194 (if (> (length str) 0)
197 chess-default-engine)))
199 (let ((game (chess-game-create))
200 (my-color t) ; we start out as white always
203 ;; all these odd calls are so that `objects' ends up looking like:
204 ;; (ENGINE FIRST-DISPLAY...)
206 (setq objects (chess-create-modules (list chess-default-display)
207 'chess--create-display
208 game my-color disable-popup))
210 (mapc 'chess-display-update objects)
211 (chess-module-set-leader (car objects))
212 (unless disable-popup
213 (chess-display-popup (car objects))))
215 (nconc objects (chess-create-modules chess-default-modules
216 'chess-module-create game))
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
228 (unless (car objects)
229 (chess-message 'no-engines-found))
234 (defalias 'chess-session 'chess)
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'."
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))))
250 ;;; chess.el ends here