]> code.delx.au - gnu-emacs-elpa/blob - chess.el
added message catalog support
[gnu-emacs-elpa] / chess.el
1 ;;; chess.el --- Play chess in Emacs
2
3 ;; Copyright (C) 2001 John Wiegley <johnw@gnu.org>
4
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: chess.el
7 ;; Version: 2.0
8 ;; Keywords: games
9 ;; Author: John Wiegley <johnw@gnu.org>
10 ;; Maintainer: John Wiegley <johnw@gnu.org>
11 ;; Description: Play chess in Emacs
12 ;; URL: http://www.gci-net.com/~johnw/Emacs/packages/chess.tar.gz
13 ;; Compatibility: Emacs20, Emacs21, XEmacs21
14
15 ;; This file is not part of GNU Emacs.
16
17 ;; This is free software; you can redistribute it and/or modify it under
18 ;; the terms of the GNU General Public License as published by the Free
19 ;; Software Foundation; either version 2, or (at your option) any later
20 ;; version.
21 ;;
22 ;; This is distributed in the hope that it will be useful, but WITHOUT
23 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 ;; for more details.
26 ;;
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs; see the file COPYING. If not, write to the
29 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 ;; MA 02111-1307, USA.
31
32 ;;; Commentary:
33
34 ;; Welcome to Emacs Chess, a chess playing module for GNU Emacs.
35 ;;
36 ;; This program will not play chess against you; it is not a chess
37 ;; computer. It can use a chess computer, however, to simulate your
38 ;; opponent's moves. This is decided when you choose your opponent.
39 ;; You must, of course, have that chess computer installed. See the
40 ;; top of chess-player.el for more information.
41 ;;
42 ;; To just get a chessboard up, put the following in your .emacs file:
43 ;;
44 ;; (add-to-list 'load-list "<the path to Emacs Chess>")
45 ;;
46 ;; (autoload 'chess "chess" "Play a game of chess" t)
47 ;;
48 ;; Now you can type `M-x chess', and play chess against anyone else in
49 ;; the room with you, without having to install anything more.
50 ;;
51 ;; Once this is working, the next thing to do is to customize
52 ;; `chess-use-modules'. This is a list of functionality modules used
53 ;; by chess.el to provide its functionality. You can enable or
54 ;; disable modules so that Emacs Chess better suites your tastes.
55 ;; Those modules in turn often have configuration variables, and
56 ;; appropriate documentation at the top of the related file.
57 ;;
58 ;; Emacs Chess is designed in a highly modular fashion, using loosely
59 ;; coupled modules that respond to events on the chess board. This
60 ;; makes it very easy for programmers to add their own types of
61 ;; displays, opponents, analysis programs, etc. See the documentation
62 ;; in chess-module.el to learn more.
63 ;;
64 ;; There is no documentation for this program other than what exists
65 ;; in the source files. This is because the source files aim at being
66 ;; self documenting, and as chess is such a simple game, most chess
67 ;; players aren't going to need to know much about this program in
68 ;; particular.
69 ;;
70 ;; However, most people will probably be interested in reading the top
71 ;; of chess-display.el and chess-pgn.el, which describe the user
72 ;; interface commands available in each of those buffer types.
73
74 ;;; Code:
75
76 (require 'chess-game)
77 (require 'chess-display)
78 (require 'chess-engine)
79 (require 'chess-pgn)
80
81 (defgroup chess nil
82 "An Emacs chess playing program."
83 :group 'games)
84
85 (defconst chess-version "2.0a8"
86 "The version of the Emacs chess program.")
87
88 (defcustom chess-default-display (if (display-graphic-p)
89 'chess-images 'chess-ics1)
90 "Default module set to be used when starting a chess session."
91 :type 'sexp
92 :group 'chess)
93
94 (defcustom chess-default-engine 'chess-gnuchess
95 "Default engine to be used when starting a chess session."
96 :type 'sexp
97 :group 'chess)
98
99 (defcustom chess-announce-moves t
100 "If non-nil, announce when your opponent makes a move.
101 This variable can also be a symbol which names a different announcing
102 module to use. This happens verbally if 'festival' is installed, or
103 if you have sound files installed and a sound play (see
104 chess-sound.el). Otherwise it just prints a message in your
105 minibuffer, which works well for Emacspeak users."
106 :type 'boolean
107 :group 'chess)
108
109 (defcustom chess-full-name (user-full-name)
110 "The full name to use when playing chess."
111 :type 'string
112 :group 'chess)
113
114 (chess-message-catalog 'english
115 '((no-images-fallback . "Could not find suitable chess images; using ics1 display")))
116
117 ;;;###autoload
118 (defun chess (&optional engine disable-popup engine-response-handler
119 &rest engine-ctor-args)
120 "Start a game of chess, playing against ENGINE (a module name)."
121 (interactive
122 (list
123 (if current-prefix-arg
124 (intern
125 (concat "chess-"
126 (let ((str (read-string "Engine to play against: ")))
127 (if (> (length str) 0)
128 str
129 "none"))))
130 chess-default-engine)))
131
132 (require chess-default-display)
133 (let* ((my-color t) ; we start out as white always
134 (game (chess-game-create))
135 (display (chess-display-create game chess-default-display
136 my-color)))
137
138 (when (and (eq chess-default-display 'chess-images)
139 (with-current-buffer display
140 (null chess-images-size)))
141 (chess-message 'no-images-fallback)
142 (chess-display-destroy display)
143 (require 'chess-ics1)
144 (setq display (chess-display-create game 'chess-ics1 my-color)))
145
146 (chess-game-set-data game 'my-color my-color)
147 (if disable-popup
148 (chess-display-disable-popup display))
149 (chess-display-set-main display)
150
151 (let ((engine-module (or engine chess-default-engine)))
152 (when (and engine-module (require engine-module nil t))
153 (let ((engine (apply 'chess-engine-create game engine-module nil
154 engine-ctor-args)))
155 ;; for the sake of engines which are ready to play now, and
156 ;; which don't need connect/accept negotiation (most
157 ;; computerized engines fall into this category), we need to
158 ;; let them know we're ready to begin
159 (chess-engine-command engine 'ready))
160
161 (when (and (not (eq engine-module 'chess-none))
162 chess-announce-moves)
163 (if (and (not (eq chess-announce-moves t))
164 (symbolp chess-announce-moves))
165 (let ((name (symbol-name chess-announce-moves)))
166 (require chess-announce-moves)
167 (if (funcall (intern (concat name "-available-p")))
168 (funcall (intern (concat name "-for-game")) game)))
169 (require 'chess-sound)
170 (if (chess-sound-available-p)
171 (chess-sound-for-game game)
172 (require 'chess-announce)
173 (if (chess-announce-available-p)
174 (chess-announce-for-game game)))))))
175
176 (chess-display-update display t)
177
178 (cons display engine)))
179
180 (defalias 'chess-session 'chess)
181
182 ;;;###autoload
183 (defun chess-read-pgn (&optional file)
184 "Read and display a PGN game after point."
185 (interactive "P")
186 (if (or file (not (search-forward "[Event" nil t)))
187 (setq file (read-file-name "Read a PGN game from file: ")))
188 (if file
189 (find-file file))
190 (let ((game (chess-pgn-to-game)))
191 (when game
192 (require chess-default-display)
193 (chess-display-create game chess-default-display
194 (chess-game-side-to-move game)))))
195
196 (defvar chess-puzzle-locations nil)
197
198 (defun chess-puzzle-next ()
199 "Play the next puzzle in the collection, selected randomly."
200 (interactive)
201 (if chess-puzzle-locations
202 (chess-puzzle (aref chess-puzzle-locations 0))))
203
204 ;;;###autoload
205 (defun chess-puzzle (file)
206 "Pick a random puzzle from FILE, and solve it against the default engine.
207 The spacebar in the display buffer is bound to `chess-puzzle-next',
208 making it easy to go on to the next puzzle once you've solved one."
209 (interactive "fRead chess puzzles from: ")
210 (save-excursion
211 (with-current-buffer (find-file-noselect file)
212 (when (or (null chess-puzzle-locations)
213 (not (equal file (aref chess-puzzle-locations 0))))
214 (let (locations)
215 (goto-char (point-min))
216 (while (search-forward "[Event" nil t)
217 (push (point) locations))
218 (setq chess-puzzle-locations (vector file locations nil nil)))
219 (random t))
220 (goto-char (nth (random (length (aref chess-puzzle-locations 1)))
221 (aref chess-puzzle-locations 1)))
222 (let ((game (chess-pgn-to-game)))
223 (when game
224 (require chess-default-display)
225 (let (puzzle-display)
226 (if (buffer-live-p (aref chess-puzzle-locations 2))
227 (progn
228 (setq puzzle-display (aref chess-puzzle-locations 2))
229 (chess-display-set-game puzzle-display game))
230 (setq puzzle-display
231 (chess-display-create game chess-default-display
232 (chess-game-side-to-move game))))
233 (aset chess-puzzle-locations 2 puzzle-display)
234 ;; setup spacebar as a convenient way to jump to the next puzzle
235 (with-current-buffer puzzle-display
236 (define-key (current-local-map) [? ] 'chess-puzzle-next)))
237 (require chess-default-engine)
238 (aset chess-puzzle-locations 3
239 (or (and (buffer-live-p (aref chess-puzzle-locations 3))
240 (aref chess-puzzle-locations 3))
241 (chess-engine-create game chess-default-engine))))))))
242
243 (provide 'chess)
244
245 ;;; chess.el ends here