]> code.delx.au - gnu-emacs-elpa/blob - chess.el
Release 2.0.4
[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: 2.0.4
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 :link '(custom-manual "(chess)Top"))
91
92 (defconst chess-version "2.0.4"
93 "The version of the Emacs chess program.")
94
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
99 not available."
100 :type '(choice symbol (repeat symbol))
101 :group 'chess)
102
103 (defcustom chess-default-modules
104 '((chess-sound chess-announce)
105 ;;chess-autosave ml (2014-06-06): module not fully working
106 chess-clock
107 ;;chess-kibitz jww (2002-04-30): not fully supported yet
108 ;;chess-chat
109 )
110 "Modules to be used when starting a chess session.
111 A sublist indicates a series of alternatives, if the first is not
112 available.
113 These can do just about anything."
114 :type '(repeat (choice symbol (repeat symbol)))
115 :group 'chess)
116
117 (defcustom chess-default-engine
118 '(chess-crafty
119 chess-stockfish chess-glaurung chess-fruit
120 chess-gnuchess chess-phalanx
121 chess-ai)
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
124 available."
125 :type '(choice symbol (repeat symbol))
126 :group 'chess)
127
128 (defcustom chess-full-name (user-full-name)
129 "The full name to use when playing chess."
130 :type 'string
131 :group 'chess)
132
133 (and (fboundp 'font-lock-add-keywords)
134 (font-lock-add-keywords
135 'emacs-lisp-mode
136 '(("(\\(chess-error\\)\\>" 1 font-lock-warning-face)
137 ("(\\(chess-with-current-buffer\\)\\>" 1 font-lock-keyword-face))))
138
139 (defun chess--create-display (module game my-color disable-popup)
140 (let ((display (chess-display-create game module my-color)))
141 (when display
142 (chess-game-set-data game 'my-color my-color)
143 (if disable-popup
144 (chess-display-disable-popup display))
145 display)))
146
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)))
150 (when engine
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)
156 engine)))
157
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."
161 (let (objects)
162 (dolist (module module-list)
163 (let (object)
164 (if (symbolp module)
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
169 (while module
170 (if (setq object (condition-case nil
171 (apply create-func (car module) args)
172 (error nil)))
173 (progn
174 (push object objects)
175 (setq module nil))
176 (setq module (cdr module)))))))
177 (nreverse objects)))
178
179 (chess-message-catalog 'english
180 '((no-engines-found
181 . "Could not find any chess engines to play against; install gnuchess!")))
182
183 ;;;###autoload
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."
189 (interactive
190 (list
191 (if current-prefix-arg
192 (intern
193 (concat "chess-"
194 (let ((str (read-string "Engine to play against: ")))
195 (if (> (length str) 0)
196 str
197 "none"))))
198 chess-default-engine)))
199
200 (let ((game (chess-game-create))
201 (my-color t) ; we start out as white always
202 objects)
203
204 ;; all these odd calls are so that `objects' ends up looking like:
205 ;; (ENGINE FIRST-DISPLAY...)
206
207 (setq objects (chess-create-modules (list chess-default-display)
208 'chess--create-display
209 game my-color disable-popup))
210 (when (car objects)
211 (mapc 'chess-display-update objects)
212 (chess-module-set-leader (car objects))
213 (unless disable-popup
214 (chess-display-popup (car objects))))
215
216 (nconc objects (chess-create-modules chess-default-modules
217 'chess-module-create game))
218
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
224 engine-ctor-args)
225 ; (error nil))
226 ))
227 objects)
228
229 (unless (car objects)
230 (chess-message 'no-engines-found))
231
232 objects))
233
234 ;;;###autoload
235 (defalias 'chess-session 'chess)
236
237 ;;;###autoload
238 (define-key menu-bar-games-menu [chess] '(menu-item "Chess" chess :help "Play Chess"))
239
240 ;;;###autoload
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'."
245 (if modules-too
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))))
251
252 (provide 'chess)
253
254 ;;; chess.el ends here