]> code.delx.au - gnu-emacs-elpa/blob - chess.el
* chess.el: Use new "Version: 0" convention.
[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: 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 ;; chess.el is an Emacs Lisp library and several clients on top of the
34 ;; underlying library functionality for performing various activities
35 ;; related to the game of chess.
36 ;;
37 ;; Type `M-x chess', and play chess against one of the default engine modules.
38 ;;
39 ;; Type `C-u M-x chess' to select a specific engine.
40 ;; You can play against various external chess computer programs, like
41 ;; crafty, fruit, glaurung, gnuchess, phalanx, sjeng and stockfish.
42 ;; There is also an Emacs based chess computer module (ai) which does not
43 ;; require any external programs. However, the internal AI is not very strong.
44 ;;
45 ;; To play on one of the internet chess servers, type `M-x chess-ics'.
46 ;;
47 ;; If you'd like to view or edit Portable Game Notation (PGN) files,
48 ;; `chess-pgn-mode' provides a text-mode derived mode which can display the
49 ;; chess position at point.
50 ;;
51 ;; To improve your chessaility, `M-x chess-tutorial' provides a simple knight
52 ;; movement exercise to get you started, and `M-x chess-puzzle' can be used
53 ;; to solve puzzle collections in EPD or PGN format.
54 ;;
55 ;; There are some different types of chessboard display modules available.
56 ;; * A simple character based display (chess-plain).
57 ;; * A more verbose ASCII chessboard (chess-ics1).
58 ;; * A graphical chessboard display which uses images (chess-images).
59 ;;
60 ;; The variable `chess-default-display' controls which display modules
61 ;; are tried when a chessboard should be displayed. By default, chess-images
62 ;; is tried first. If Emacs is not running in a graphical environment,
63 ;; chess-ics1 is used instead. To enable the chess-plain display module,
64 ;; customize `chess-default-display' accordingly.
65 ;;
66 ;; Once this is working, the next thing to do is to customize
67 ;; `chess-default-modules'. This is a list of functionality modules used
68 ;; by chess.el to provide additional functionality. You can enable or
69 ;; disable modules so that Emacs Chess better suites your tastes.
70 ;; Those modules in turn often have configuration variables, and
71 ;; appropriate documentation at the top of the related file.
72 ;;
73 ;; Emacs Chess is designed in a highly modular fashion, using loosely
74 ;; coupled modules that respond to events on the chess board. This
75 ;; makes it very easy for programmers to add their own types of
76 ;; displays, opponents, analysis programs, etc. See the documentation
77 ;; in chess-module.el to learn more.
78 ;;
79 ;; Most people will probably also be interested in reading the top
80 ;; of chess-display.el and chess-pgn.el, which describe the user
81 ;; interface commands available in each of those buffer types.
82
83 ;;; Code:
84
85 (require 'chess-game)
86 (require 'chess-display)
87 (require 'chess-engine)
88
89 (defgroup chess nil
90 "An Emacs chess playing program."
91 :group 'games)
92
93 (defconst chess-version "2.0b6"
94 "The version of the Emacs chess program.")
95
96 (defcustom chess-default-display
97 '(chess-images chess-ics1 chess-plain)
98 "Default display to be used when starting a chess session.
99 A list indicates a series of alternatives if the first display is
100 not available."
101 :type '(choice symbol (repeat symbol))
102 :group 'chess)
103
104 (defcustom chess-default-modules
105 '((chess-sound chess-announce)
106 chess-autosave
107 chess-clock
108 ;;chess-kibitz jww (2002-04-30): not fully supported yet
109 ;;chess-chat
110 )
111 "Modules to be used when starting a chess session.
112 A sublist indicates a series of alternatives, if the first is not
113 available.
114 These can do just about anything."
115 :type '(repeat (choice symbol (repeat symbol)))
116 :group 'chess)
117
118 (defcustom chess-default-engine
119 '(chess-crafty
120 chess-stockfish chess-glaurung chess-fruit
121 chess-gnuchess chess-phalanx
122 chess-ai)
123 "Default engine to be used when starting a chess session.
124 A list indicates a series of alternatives if the first engine is not
125 available."
126 :type '(choice symbol (repeat symbol))
127 :group 'chess)
128
129 (defcustom chess-full-name (user-full-name)
130 "The full name to use when playing chess."
131 :type 'string
132 :group 'chess)
133
134 (and (fboundp 'font-lock-add-keywords)
135 (font-lock-add-keywords
136 'emacs-lisp-mode
137 '(("(\\(chess-error\\)\\>" 1 font-lock-warning-face)
138 ("(\\(chess-with-current-buffer\\)\\>" 1 font-lock-keyword-face))))
139
140 (defun chess--create-display (module game my-color disable-popup)
141 (let ((display (chess-display-create game module my-color)))
142 (when display
143 (chess-game-set-data game 'my-color my-color)
144 (if disable-popup
145 (chess-display-disable-popup display))
146 display)))
147
148 (defun chess--create-engine (module game response-handler ctor-args)
149 (let ((engine (apply 'chess-engine-create module game
150 response-handler ctor-args)))
151 (when engine
152 ;; for the sake of engines which are ready to play now, and
153 ;; which don't need connect/accept negotiation (most
154 ;; computerized engines fall into this category), we need to
155 ;; let them know we're ready to begin
156 (chess-engine-command engine 'ready)
157 engine)))
158
159 (defun chess-create-modules (module-list create-func &rest args)
160 "Create modules from MODULE-LIST with CREATE-FUNC and ARGS.
161 If an element of MODULE-LIST is a sublist, treat it as alternatives."
162 (let (objects)
163 (dolist (module module-list)
164 (let (object)
165 (if (symbolp module)
166 (if (setq object (apply create-func module args))
167 (push object objects))
168 ;; this module is actually a list, which means keep trying
169 ;; until we find one that works
170 (while module
171 (if (setq object (condition-case nil
172 (apply create-func (car module) args)
173 (error nil)))
174 (progn
175 (push object objects)
176 (setq module nil))
177 (setq module (cdr module)))))))
178 (nreverse objects)))
179
180 (chess-message-catalog 'english
181 '((no-engines-found
182 . "Could not find any chess engines to play against; install gnuchess!")))
183
184 ;;;###autoload
185 (defun chess (&optional engine disable-popup engine-response-handler
186 &rest engine-ctor-args)
187 "Start a game of chess, playing against ENGINE (a module name).
188 With prefix argument, prompt for the engine to play against.
189 Otherwise use `chess-default-engine' to determine the engine."
190 (interactive
191 (list
192 (if current-prefix-arg
193 (intern
194 (concat "chess-"
195 (let ((str (read-string "Engine to play against: ")))
196 (if (> (length str) 0)
197 str
198 "none"))))
199 chess-default-engine)))
200
201 (let ((game (chess-game-create))
202 (my-color t) ; we start out as white always
203 objects)
204
205 ;; all these odd calls are so that `objects' ends up looking like:
206 ;; (ENGINE FIRST-DISPLAY...)
207
208 (setq objects (chess-create-modules (list chess-default-display)
209 'chess--create-display
210 game my-color disable-popup))
211 (when (car objects)
212 (mapc 'chess-display-update objects)
213 (chess-module-set-leader (car objects))
214 (unless disable-popup
215 (chess-display-popup (car objects))))
216
217 (nconc objects (chess-create-modules chess-default-modules
218 'chess-module-create game))
219
220 (push (unless (eq engine 'none)
221 (car ;(condition-case nil
222 (chess-create-modules (list (or engine chess-default-engine))
223 'chess--create-engine game
224 engine-response-handler
225 engine-ctor-args)
226 ; (error nil))
227 ))
228 objects)
229
230 (unless (car objects)
231 (chess-message 'no-engines-found))
232
233 objects))
234
235 ;;;###autoload
236 (defalias 'chess-session 'chess)
237
238 ;;;###autoload
239 (defun chess-create-display (perspective &optional modules-too)
240 "Create a display, letting the user's customization decide the style.
241 If MODULES-TOO is non-nil, also create and associate the modules
242 listed in `chess-default-modules'."
243 (if modules-too
244 (let ((display (cadr (chess-session 'none))))
245 (chess-display-set-perspective* display perspective))
246 (car (chess-create-modules (list chess-default-display)
247 'chess--create-display
248 (chess-game-create) perspective nil))))
249
250 (provide 'chess)
251
252 ;;; chess.el ends here