]> code.delx.au - gnu-emacs-elpa/blobdiff - chess.el
Release 2.0.4
[gnu-emacs-elpa] / chess.el
index e99e6976d834c7ed666acee99c372091ae45f1ad..96d23f97d27317a7b3ee929261a5722fa491f291 100644 (file)
--- a/chess.el
+++ b/chess.el
@@ -1,56 +1,69 @@
-;;; chess.el --- Play chess in Emacs
+;;; chess.el --- Play chess in GNU Emacs
 
-;; Copyright (C) 2001 John Wiegley <johnw@gnu.org>
+;; Copyright (C) 2001, 2014 Free Software Foundation, Inc.
 
-;; Emacs Lisp Archive Entry
-;; Filename: chess.el
-;; Version: 2.0
-;; Keywords: games
 ;; Author: John Wiegley <johnw@gnu.org>
-;; Maintainer: John Wiegley <johnw@gnu.org>
-;; Description: Play chess in Emacs
-;; URL: http://www.gci-net.com/~johnw/Emacs/packages/chess.tar.gz
-;; Compatibility: Emacs20, Emacs21, XEmacs21
+;; Maintainer: Mario Lang <mlang@delysid.org>
+;; Version: 2.0.4
+;; Package-Requires: ((cl-lib "0.5"))
+;; Keywords: games
+;; Compatibility: Emacs24
 
-;; This file is not part of GNU Emacs.
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
 
-;; This is free software; you can redistribute it and/or modify it under
-;; the terms of the GNU General Public License as published by the Free
-;; Software Foundation; either version 2, or (at your option) any later
-;; version.
-;;
-;; This is distributed in the hope that it will be useful, but WITHOUT
-;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-;; for more details.
-;;
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
-;; MA 02111-1307, USA.
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
 ;; Welcome to Emacs Chess, a chess playing module for GNU Emacs.
 ;;
-;; This program will not play chess against you; it is not a chess
-;; computer.  It can use a chess computer, however, to simulate your
-;; opponent's moves.  This is decided when you choose your opponent.
-;; You must, of course, have that chess computer installed.  See the
-;; top of chess-player.el for more information.
+;; chess.el is an Emacs Lisp library and several clients on top of the
+;; underlying library functionality for performing various activities
+;; related to the game of chess.
 ;;
-;; To just get a chessboard up, put the following in your .emacs file:
+;; Type `M-x chess', and play chess against one of the default engine modules.
 ;;
-;;   (add-to-list 'load-list "<the path to Emacs Chess>")
+;; Type `C-u M-x chess' to select a specific engine.
+;; You can play against various external chess computer programs, like
+;; crafty, fruit, glaurung, gnuchess, phalanx, sjeng and stockfish.
+;; There is also an Emacs based chess computer module (ai) which does not
+;; require any external programs.  However, the internal AI is not very strong.
 ;;
-;;   (autoload 'chess "chess" "Play a game of chess" t)
+;; To play against another human on a different machine running GNU Emacs,
+;; type `C-u M-x chess RET network RET'.
+;; To play on one of the internet chess servers, type `M-x chess-ics'.
 ;;
-;; Now you can type `M-x chess', and play chess against anyone else in
-;; the room with you, without having to install anything more.
+;; If you'd like to view or edit Portable Game Notation (PGN) files,
+;; `chess-pgn-mode' provides a text-mode derived mode which can display the
+;; chess position at point.
+;;
+;; To improve your chessaility, `M-x chess-tutorial' provides a simple knight
+;; movement exercise to get you started, and `M-x chess-puzzle' can be used
+;; to solve puzzle collections in EPD or PGN format.
+;;
+;; There are some different types of chessboard display modules available.
+;; * A simple character based display (chess-plain).
+;; * A more verbose ASCII chessboard (chess-ics1).
+;; * A graphical chessboard display which uses images (chess-images).
+;;
+;; The variable `chess-default-display' controls which display modules
+;; are tried when a chessboard should be displayed.  By default, chess-images
+;; is tried first.  If Emacs is not running in a graphical environment,
+;; chess-ics1 is used instead.  To enable the chess-plain display module,
+;; customize `chess-default-display' accordingly.
 ;;
 ;; Once this is working, the next thing to do is to customize
-;; `chess-use-modules'.  This is a list of functionality modules used
-;; by chess.el to provide its functionality.  You can enable or
+;; `chess-default-modules'.  This is a list of functionality modules used
+;; by chess.el to provide additional functionality.  You can enable or
 ;; disable modules so that Emacs Chess better suites your tastes.
 ;; Those modules in turn often have configuration variables, and
 ;; appropriate documentation at the top of the related file.
 ;; displays, opponents, analysis programs, etc.  See the documentation
 ;; in chess-module.el to learn more.
 ;;
-;; There is no documentation for this program other than what exists
-;; in the source files.  This is because the source files aim at being
-;; self documenting, and as chess is such a simple game, most chess
-;; players aren't going to need to know much about this program in
-;; particular.
-;;
-;; However, most people will probably be interested in reading the top
+;; Most people will probably also be interested in reading the top
 ;; of chess-display.el and chess-pgn.el, which describe the user
 ;; interface commands available in each of those buffer types.
 
 (require 'chess-game)
 (require 'chess-display)
 (require 'chess-engine)
-(require 'chess-database)
-(require 'chess-file)
 
 (defgroup chess nil
   "An Emacs chess playing program."
-  :group 'games)
+  :group 'games
+  :link '(custom-manual "(chess)Top"))
 
-(defconst chess-version "2.0a8"
+(defconst chess-version "2.0.4"
   "The version of the Emacs chess program.")
 
 (defcustom chess-default-display
@@ -96,16 +102,23 @@ not available."
 
 (defcustom chess-default-modules
   '((chess-sound chess-announce)
-    chess-autosave)
+    ;;chess-autosave ml (2014-06-06): module not fully working
+    chess-clock
+    ;;chess-kibitz   jww (2002-04-30): not fully supported yet
+    ;;chess-chat
+    )
   "Modules to be used when starting a chess session.
 A sublist indicates a series of alternatives, if the first is not
 available.
 These can do just about anything."
-  :type '(choice symbol (repeat symbol))
+  :type '(repeat (choice symbol (repeat symbol)))
   :group 'chess)
 
 (defcustom chess-default-engine
-  '(chess-crafty chess-gnuchess chess-phalanx)
+  '(chess-crafty
+    chess-stockfish chess-glaurung chess-fruit
+    chess-gnuchess chess-phalanx
+    chess-ai)
   "Default engine to be used when starting a chess session.
 A list indicates a series of alternatives if the first engine is not
 available."
@@ -117,32 +130,34 @@ available."
   :type 'string
   :group 'chess)
 
+(and (fboundp 'font-lock-add-keywords)
+     (font-lock-add-keywords
+      'emacs-lisp-mode
+      '(("(\\(chess-error\\)\\>"              1 font-lock-warning-face)
+       ("(\\(chess-with-current-buffer\\)\\>" 1 font-lock-keyword-face))))
+
 (defun chess--create-display (module game my-color disable-popup)
-  (if (require module nil t)
-      (let ((display (chess-display-create game module my-color)))
-       (when display
-         (chess-game-set-data game 'my-color my-color)
-         (if disable-popup
-             (chess-display-disable-popup display))
-         display))))
-
-(defun chess--create-module (module game)
-  (and (require module nil t)
-       (chess-module-create module game)))
+  (let ((display (chess-display-create game module my-color)))
+    (when display
+      (chess-game-set-data game 'my-color my-color)
+      (if disable-popup
+         (chess-display-disable-popup display))
+      display)))
 
 (defun chess--create-engine (module game response-handler ctor-args)
-  (if (require module nil t)
-      (let ((engine (apply 'chess-engine-create module game
-                          response-handler ctor-args)))
-       (when engine
-         ;; for the sake of engines which are ready to play now, and
-         ;; which don't need connect/accept negotiation (most
-         ;; computerized engines fall into this category), we need to
-         ;; let them know we're ready to begin
-         (chess-engine-command engine 'ready)
-         engine))))
+  (let ((engine (apply 'chess-engine-create module game
+                      response-handler ctor-args)))
+    (when engine
+      ;; for the sake of engines which are ready to play now, and
+      ;; which don't need connect/accept negotiation (most
+      ;; computerized engines fall into this category), we need to
+      ;; let them know we're ready to begin
+      (chess-engine-command engine 'ready)
+      engine)))
 
 (defun chess-create-modules (module-list create-func &rest args)
+  "Create modules from MODULE-LIST with CREATE-FUNC and ARGS.
+If an element of MODULE-LIST is a sublist, treat it as alternatives."
   (let (objects)
     (dolist (module module-list)
       (let (object)
@@ -152,17 +167,25 @@ available."
          ;; this module is actually a list, which means keep trying
          ;; until we find one that works
          (while module
-           (if (setq object (apply create-func (car module) args))
+           (if (setq object (condition-case nil
+                                (apply create-func (car module) args)
+                              (error nil)))
                (progn
                  (push object objects)
                  (setq module nil))
              (setq module (cdr module)))))))
     (nreverse objects)))
 
+(chess-message-catalog 'english
+  '((no-engines-found
+     . "Could not find any chess engines to play against; install gnuchess!")))
+
 ;;;###autoload
 (defun chess (&optional engine disable-popup engine-response-handler
                        &rest engine-ctor-args)
-  "Start a game of chess, playing against ENGINE (a module name)."
+  "Start a game of chess, playing against ENGINE (a module name).
+With prefix argument, prompt for the engine to play against.
+Otherwise use `chess-default-engine' to determine the engine."
   (interactive
    (list
     (if current-prefix-arg
@@ -187,77 +210,44 @@ available."
     (when (car objects)
       (mapc 'chess-display-update objects)
       (chess-module-set-leader (car objects))
-      (chess-display-popup (car objects)))
+      (unless disable-popup
+       (chess-display-popup (car objects))))
 
     (nconc objects (chess-create-modules chess-default-modules
-                                        'chess--create-module game))
-
-    (push (car (chess-create-modules (list (or engine chess-default-engine))
-                                    'chess--create-engine game
-                                    engine-response-handler
-                                    engine-ctor-args))
+                                        'chess-module-create game))
+
+    (push (unless (eq engine 'none)
+           (car ;(condition-case nil
+                    (chess-create-modules (list (or engine chess-default-engine))
+                                          'chess--create-engine game
+                                          engine-response-handler
+                                          engine-ctor-args)
+                  ;  (error nil))
+           ))
          objects)
 
+    (unless (car objects)
+      (chess-message 'no-engines-found))
+
     objects))
 
+;;;###autoload
 (defalias 'chess-session 'chess)
 
-(defun chess-create-display ()
-  "Just make a display to use, letting chess.el decide the style."
-  (cadr (chess-session 'chess-none)))
-
-(defun chess-create-display-object (perspective)
-  (car (chess-create-modules (list chess-default-display)
-                            'chess--create-display
-                            (chess-game-create) perspective)))
-
 ;;;###autoload
-(defun chess-read-pgn (&optional file)
-  "Read and display a PGN game after point."
-  (interactive "P")
-  (if (or file (not (search-forward "[Event " nil t)))
-      (setq file (read-file-name "Read a PGN game from file: ")))
-  (if file
-      (find-file file))
-  (let ((game (chess-pgn-to-game))
-       display)
-    (when game
-      (setq display (chess-create-display))
-      (chess-display-set-game display game))))
+(define-key menu-bar-games-menu [chess] '(menu-item "Chess" chess :help "Play Chess"))
 
 ;;;###autoload
-(defun chess-puzzle (file &optional index)
-  "Pick a random puzzle from FILE, and solve it against the default engine.
-The spacebar in the display buffer is bound to `chess-puzzle-next',
-making it easy to go on to the next puzzle once you've solved one."
-  (interactive "fRead chess puzzles from: ")
-  (random t)
-  (let* ((database (chess-database-open 'chess-file file))
-        (objects (and database (chess-session)))
-        (display (cadr objects)))
-    (when database
-      (with-current-buffer display
-       ;; make sure the database is closed when the display is shutdown
-       (chess-game-add-hook (chess-display-game nil)
-                            'chess-database-event-handler database)
-       (chess-game-set-data (chess-display-game nil) 'database database)
-       (define-key (current-local-map) [? ] 'chess-puzzle-next)
-       (chess-puzzle-next)))))
-
-(defun chess-puzzle-next ()
-  "Play the next puzzle in the collection, selected randomly."
-  (interactive)
-  (let* ((game (chess-display-game nil))
-        (database (chess-game-data game 'database))
-        (index (random (chess-database-count database)))
-        (next-game (chess-database-read database index)))
-    (if (null next-game)
-       (error "Error reading game at position %d" index)
-      (chess-display-set-game nil next-game 0)
-      (chess-game-set-data game 'my-color
-                          (chess-pos-side-to-move (chess-game-pos game)))
-      (dolist (key '(database database-index database-count))
-       (chess-game-set-data game key (chess-game-data next-game key))))))
+(defun chess-create-display (perspective &optional modules-too)
+  "Create a display, letting the user's customization decide the style.
+If MODULES-TOO is non-nil, also create and associate the modules
+listed in `chess-default-modules'."
+  (if modules-too
+      (let ((display (cadr (chess-session 'none))))
+       (chess-display-set-perspective* display perspective))
+    (car (chess-create-modules (list chess-default-display)
+                              'chess--create-display
+                              (chess-game-create) perspective nil))))
 
 (provide 'chess)