]> code.delx.au - gnu-emacs-elpa/blob - chess.el
Declare requirement for cl-lib.
[gnu-emacs-elpa] / chess.el
1 ;;; chess.el --- Play chess in Emacs
2
3 ;; Copyright (C) 2001, 2014 Free Software Foundation, Inc.
4
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: chess.el
7 ;; Version: 2.0
8 ;; Package-Requires: ((cl-lib "0.5"))
9 ;; Keywords: games
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
15
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.
20
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.
25
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/>.
28
29 ;;; Commentary:
30
31 ;; Welcome to Emacs Chess, a chess playing module for GNU Emacs.
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 on one of the internet chess servers, type `M-x chess-ics'.
42 ;;
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.
46 ;;
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.
50 ;;
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).
55 ;;
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.
61 ;;
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.
68 ;;
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.
74 ;;
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.
78
79 ;;; Code:
80
81 (require 'chess-game)
82 (require 'chess-display)
83 (require 'chess-engine)
84
85 (defgroup chess nil
86 "An Emacs chess playing program."
87 :group 'games)
88
89 (defconst chess-version "2.0b6"
90 "The version of the Emacs chess program.")
91
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
96 not available."
97 :type '(choice symbol (repeat symbol))
98 :group 'chess)
99
100 (defcustom chess-default-modules
101 '((chess-sound chess-announce)
102 chess-autosave
103 chess-clock
104 ;;chess-kibitz jww (2002-04-30): not fully supported yet
105 ;;chess-chat
106 )
107 "Modules to be used when starting a chess session.
108 A sublist indicates a series of alternatives, if the first is not
109 available.
110 These can do just about anything."
111 :type '(repeat (choice symbol (repeat symbol)))
112 :group 'chess)
113
114 (defcustom chess-default-engine
115 '(chess-crafty
116 chess-stockfish chess-glaurung chess-fruit
117 chess-gnuchess chess-phalanx
118 chess-ai)
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
121 available."
122 :type '(choice symbol (repeat symbol))
123 :group 'chess)
124
125 (defcustom chess-full-name (user-full-name)
126 "The full name to use when playing chess."
127 :type 'string
128 :group 'chess)
129
130 (and (fboundp 'font-lock-add-keywords)
131 (font-lock-add-keywords
132 'emacs-lisp-mode
133 '(("(\\(chess-error\\)\\>" 1 font-lock-warning-face)
134 ("(\\(chess-with-current-buffer\\)\\>" 1 font-lock-keyword-face))))
135
136 (defun chess--create-display (module game my-color disable-popup)
137 (let ((display (chess-display-create game module my-color)))
138 (when display
139 (chess-game-set-data game 'my-color my-color)
140 (if disable-popup
141 (chess-display-disable-popup display))
142 display)))
143
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)))
147 (when engine
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)
153 engine)))
154
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."
158 (let (objects)
159 (dolist (module module-list)
160 (let (object)
161 (if (symbolp module)
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
166 (while module
167 (if (setq object (condition-case nil
168 (apply create-func (car module) args)
169 (error nil)))
170 (progn
171 (push object objects)
172 (setq module nil))
173 (setq module (cdr module)))))))
174 (nreverse objects)))
175
176 (chess-message-catalog 'english
177 '((no-engines-found
178 . "Could not find any chess engines to play against; install gnuchess!")))
179
180 ;;;###autoload
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."
186 (interactive
187 (list
188 (if current-prefix-arg
189 (intern
190 (concat "chess-"
191 (let ((str (read-string "Engine to play against: ")))
192 (if (> (length str) 0)
193 str
194 "none"))))
195 chess-default-engine)))
196
197 (let ((game (chess-game-create))
198 (my-color t) ; we start out as white always
199 objects)
200
201 ;; all these odd calls are so that `objects' ends up looking like:
202 ;; (ENGINE FIRST-DISPLAY...)
203
204 (setq objects (chess-create-modules (list chess-default-display)
205 'chess--create-display
206 game my-color disable-popup))
207 (when (car objects)
208 (mapc 'chess-display-update objects)
209 (chess-module-set-leader (car objects))
210 (unless disable-popup
211 (chess-display-popup (car objects))))
212
213 (nconc objects (chess-create-modules chess-default-modules
214 'chess-module-create game))
215
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
221 engine-ctor-args)
222 ; (error nil))
223 ))
224 objects)
225
226 (unless (car objects)
227 (chess-message 'no-engines-found))
228
229 objects))
230
231 ;;;###autoload
232 (defalias 'chess-session 'chess)
233
234 ;;;###autoload
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'."
239 (if modules-too
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))))
245
246 (provide 'chess)
247
248 ;;; chess.el ends here