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