]> code.delx.au - gnu-emacs-elpa/blob - chess.el
displays and engines now always have a single object associated with
[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 ;;;###autoload
115 (defun chess (&optional engine disable-popup engine-response-handler
116 &rest engine-ctor-args)
117 "Start a game of chess, playing against ENGINE (a module name)."
118 (interactive
119 (list
120 (if current-prefix-arg
121 (intern
122 (concat "chess-"
123 (let ((str (read-string "Engine to play against: ")))
124 (if (> (length str) 0)
125 str
126 "none"))))
127 chess-default-engine)))
128
129 (require chess-default-display)
130 (let* ((my-color t) ; we start out as white always
131 (game (chess-game-create))
132 (display (chess-display-create game chess-default-display
133 my-color)))
134
135 (when (and (eq chess-default-display 'chess-images)
136 (with-current-buffer display
137 (null chess-images-size)))
138 (message "Could not find suitable chess images; using ics1 display")
139 (chess-display-destroy display)
140 (require 'chess-ics1)
141 (setq display (chess-display-create game 'chess-ics1 my-color)))
142
143 (chess-game-set-data game 'my-color my-color)
144 (if disable-popup
145 (chess-display-disable-popup display))
146 (chess-display-set-main display)
147
148 (let ((engine-module (or engine chess-default-engine)))
149 (when (and engine-module (require engine-module nil t))
150 (let ((engine (apply 'chess-engine-create game engine-module nil
151 engine-ctor-args)))
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
158 (when chess-announce-moves
159 (if (and (not (eq chess-announce-moves t))
160 (symbolp chess-announce-moves))
161 (let ((name (symbol-name chess-announce-moves)))
162 (require chess-announce-moves)
163 (if (funcall (intern (concat name "-available-p")))
164 (funcall (intern (concat name "-for-game")) game)))
165 (require 'chess-sound)
166 (if (chess-sound-available-p)
167 (chess-sound-for-game game)
168 (require 'chess-announce)
169 (if (chess-announce-available-p)
170 (chess-announce-for-game game)))))))
171
172 (chess-display-update display t)
173
174 (cons display engine)))
175
176 (defalias 'chess-session 'chess)
177
178 ;;;###autoload
179 (defun chess-read-pgn (&optional file)
180 "Read and display a PGN game after point."
181 (interactive "P")
182 (if (or file (not (search-forward "[Event" nil t)))
183 (setq file (read-file-name "Read a PGN game from file: ")))
184 (if file
185 (find-file file))
186 (let ((game (chess-pgn-to-game)))
187 (when game
188 (require chess-default-display)
189 (chess-display-create game chess-default-display
190 (chess-game-side-to-move game)))))
191
192 (defvar chess-puzzle-locations nil)
193
194 (defun chess-puzzle-next ()
195 "Play the next puzzle in the collection, selected randomly."
196 (interactive)
197 (if chess-puzzle-locations
198 (chess-puzzle (aref chess-puzzle-locations 0))))
199
200 ;;;###autoload
201 (defun chess-puzzle (file)
202 "Pick a random puzzle from FILE, and solve it against the default engine.
203 The spacebar in the display buffer is bound to `chess-puzzle-next',
204 making it easy to go on to the next puzzle once you've solved one."
205 (interactive "fRead chess puzzles from: ")
206 (save-excursion
207 (with-current-buffer (find-file-noselect file)
208 (when (or (null chess-puzzle-locations)
209 (not (equal file (aref chess-puzzle-locations 0))))
210 (let (locations)
211 (goto-char (point-min))
212 (while (search-forward "[Event" nil t)
213 (push (point) locations))
214 (setq chess-puzzle-locations (vector file locations nil nil)))
215 (random t))
216 (goto-char (nth (random (length (aref chess-puzzle-locations 1)))
217 (aref chess-puzzle-locations 1)))
218 (let ((game (chess-pgn-to-game)))
219 (when game
220 (require chess-default-display)
221 (let (puzzle-display)
222 (if (buffer-live-p (aref chess-puzzle-locations 2))
223 (progn
224 (setq puzzle-display (aref chess-puzzle-locations 2))
225 (chess-display-set-game puzzle-display game))
226 (setq puzzle-display
227 (chess-display-create game chess-default-display
228 (chess-game-side-to-move game))))
229 (aset chess-puzzle-locations 2 puzzle-display)
230 ;; setup spacebar as a convenient way to jump to the next puzzle
231 (with-current-buffer puzzle-display
232 (define-key (current-local-map) [? ] 'chess-puzzle-next)))
233 (require chess-default-engine)
234 (aset chess-puzzle-locations 3
235 (or (and (buffer-live-p (aref chess-puzzle-locations 3))
236 (aref chess-puzzle-locations 3))
237 (chess-engine-create game chess-default-engine))))))))
238
239 (provide 'chess)
240
241 ;;; chess.el ends here