1 ;;; chess.el --- Play chess in Emacs
3 ;; Copyright (C) 2001, 2014 Free Software Foundation, Inc.
5 ;; Emacs Lisp Archive Entry
8 ;; Package-Requires: ((cl-lib "0.5"))
10 ;; Author: John Wiegley <johnw@gnu.org>
11 ;; Maintainer: Mario Lang <mlang@delysid.org>
12 ;; Description: Play chess in Emacs
13 ;; URL: https://github.com/jwiegley/emacs-chess/
14 ;; Compatibility: Emacs24
16 ;; This program is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; This program is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
31 ;; Welcome to Emacs Chess, a chess playing module for GNU Emacs.
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 on one of the internet chess servers, type `M-x chess-ics'.
43 ;; If you'd like to view or edit Portable Game Notation (PGN) files,
44 ;; `chess-pgn-mode' provides a text-mode derived mode which can display the
45 ;; chess position at point.
47 ;; If you are new to Chess, `M-x chess-tutorial' provides a simple knight
48 ;; movement exercise to get you started, and `M-x chess-puzzle' can be used
49 ;; to solve puzzle collections.
51 ;; There are some different types of chessboard display modules available.
52 ;; * A simple character based display (chess-plain).
53 ;; * A more verbose ASCII chessboard (chess-ics1).
54 ;; * A graphical chessboard display which uses images (chess-images).
56 ;; The variable `chess-default-display' controls which display modules
57 ;; are tried when a chessboard should be displayed. By default, chess-images
58 ;; is tried first. If Emacs is not running in a graphical environment,
59 ;; chess-ics1 is used instead. To enable the chess-plain display module,
60 ;; customize `chess-default-display' accordingly.
62 ;; Once this is working, the next thing to do is to customize
63 ;; `chess-default-modules'. This is a list of functionality modules used
64 ;; by chess.el to provide additional functionality. You can enable or
65 ;; disable modules so that Emacs Chess better suites your tastes.
66 ;; Those modules in turn often have configuration variables, and
67 ;; appropriate documentation at the top of the related file.
69 ;; Emacs Chess is designed in a highly modular fashion, using loosely
70 ;; coupled modules that respond to events on the chess board. This
71 ;; makes it very easy for programmers to add their own types of
72 ;; displays, opponents, analysis programs, etc. See the documentation
73 ;; in chess-module.el to learn more.
75 ;; Most people will probably also be interested in reading the top
76 ;; of chess-display.el and chess-pgn.el, which describe the user
77 ;; interface commands available in each of those buffer types.
82 (require 'chess-display)
83 (require 'chess-engine)
86 "An Emacs chess playing program."
89 (defconst chess-version "2.0b6"
90 "The version of the Emacs chess program.")
92 (defcustom chess-default-display
93 '(chess-images chess-ics1 chess-plain)
94 "Default display to be used when starting a chess session.
95 A list indicates a series of alternatives if the first display is
97 :type '(choice symbol (repeat symbol))
100 (defcustom chess-default-modules
101 '((chess-sound chess-announce)
104 ;;chess-kibitz jww (2002-04-30): not fully supported yet
107 "Modules to be used when starting a chess session.
108 A sublist indicates a series of alternatives, if the first is not
110 These can do just about anything."
111 :type '(repeat (choice symbol (repeat symbol)))
114 (defcustom chess-default-engine
116 chess-stockfish chess-glaurung chess-fruit
117 chess-gnuchess chess-phalanx
119 "Default engine to be used when starting a chess session.
120 A list indicates a series of alternatives if the first engine is not
122 :type '(choice symbol (repeat symbol))
125 (defcustom chess-full-name (user-full-name)
126 "The full name to use when playing chess."
130 (and (fboundp 'font-lock-add-keywords)
131 (font-lock-add-keywords
133 '(("(\\(chess-error\\)\\>" 1 font-lock-warning-face)
134 ("(\\(chess-with-current-buffer\\)\\>" 1 font-lock-keyword-face))))
136 (defun chess--create-display (module game my-color disable-popup)
137 (let ((display (chess-display-create game module my-color)))
139 (chess-game-set-data game 'my-color my-color)
141 (chess-display-disable-popup display))
144 (defun chess--create-engine (module game response-handler ctor-args)
145 (let ((engine (apply 'chess-engine-create module game
146 response-handler ctor-args)))
148 ;; for the sake of engines which are ready to play now, and
149 ;; which don't need connect/accept negotiation (most
150 ;; computerized engines fall into this category), we need to
151 ;; let them know we're ready to begin
152 (chess-engine-command engine 'ready)
155 (defun chess-create-modules (module-list create-func &rest args)
156 "Create modules from MODULE-LIST with CREATE-FUNC and ARGS.
157 If an element of MODULE-LIST is a sublist, treat it as alternatives."
159 (dolist (module module-list)
162 (if (setq object (apply create-func module args))
163 (push object objects))
164 ;; this module is actually a list, which means keep trying
165 ;; until we find one that works
167 (if (setq object (condition-case nil
168 (apply create-func (car module) args)
171 (push object objects)
173 (setq module (cdr module)))))))
176 (chess-message-catalog 'english
178 . "Could not find any chess engines to play against; install gnuchess!")))
181 (defun chess (&optional engine disable-popup engine-response-handler
182 &rest engine-ctor-args)
183 "Start a game of chess, playing against ENGINE (a module name).
184 With prefix argument, prompt for the engine to play against.
185 Otherwise use `chess-default-engine' to determine the engine."
188 (if current-prefix-arg
191 (let ((str (read-string "Engine to play against: ")))
192 (if (> (length str) 0)
195 chess-default-engine)))
197 (let ((game (chess-game-create))
198 (my-color t) ; we start out as white always
201 ;; all these odd calls are so that `objects' ends up looking like:
202 ;; (ENGINE FIRST-DISPLAY...)
204 (setq objects (chess-create-modules (list chess-default-display)
205 'chess--create-display
206 game my-color disable-popup))
208 (mapc 'chess-display-update objects)
209 (chess-module-set-leader (car objects))
210 (unless disable-popup
211 (chess-display-popup (car objects))))
213 (nconc objects (chess-create-modules chess-default-modules
214 'chess-module-create game))
216 (push (unless (eq engine 'none)
217 (car ;(condition-case nil
218 (chess-create-modules (list (or engine chess-default-engine))
219 'chess--create-engine game
220 engine-response-handler
226 (unless (car objects)
227 (chess-message 'no-engines-found))
232 (defalias 'chess-session 'chess)
235 (defun chess-create-display (perspective &optional modules-too)
236 "Create a display, letting the user's customization decide the style.
237 If MODULES-TOO is non-nil, also create and associate the modules
238 listed in `chess-default-modules'."
240 (let ((display (cadr (chess-session 'none))))
241 (chess-display-set-perspective* display perspective))
242 (car (chess-create-modules (list chess-default-display)
243 'chess--create-display
244 (chess-game-create) perspective nil))))
248 ;;; chess.el ends here