]> code.delx.au - gnu-emacs-elpa/blobdiff - chess.el
chess-perft.el: move generator testing.
[gnu-emacs-elpa] / chess.el
index 0d765f8530bf958ca5e51ce31e60a4b6ce3ca34a..541715db52562c8e7496c93ba4ef8f44adf67c4d 100644 (file)
--- a/chess.el
+++ b/chess.el
@@ -7,10 +7,10 @@
 ;; Version: 2.0
 ;; Keywords: games
 ;; Author: John Wiegley <johnw@gnu.org>
-;; Maintainer: John Wiegley <johnw@gnu.org>
+;; Maintainer: Mario Lang <mlang@delysid.org>
 ;; Description: Play chess in Emacs
-;; URL: http://www.gci-net.com/~johnw/Emacs/packages/chess.tar.gz
-;; Compatibility: Emacs20, Emacs21, XEmacs21
+;; URL: http://emacs-chess.sourceforge.net/
+;; Compatibility: Emacs21
 
 ;; This file is not part of GNU Emacs.
 
@@ -41,7 +41,7 @@
 ;;
 ;; To just get a chessboard up, put the following in your .emacs file:
 ;;
-;;   (add-to-list 'load-list "<the path to Emacs Chess>")
+;;   (add-to-list 'load-path "<the path to Emacs Chess>")
 ;;
 ;;   (autoload 'chess "chess" "Play a game of chess" t)
 ;;
 (require 'chess-game)
 (require 'chess-display)
 (require 'chess-engine)
-(require 'chess-pgn)
 
 (defgroup chess nil
   "An Emacs chess playing program."
   :group 'games)
 
-(defconst chess-version "2.0a8"
+(defconst chess-version "2.0b6"
   "The version of the Emacs chess program.")
 
-(defcustom chess-default-displays
-  '((chess-images chess-ics1 chess-plain)
-    (chess-sound chess-announce))
-  "Default displays to be used when starting a chess session.
-This is a list of display modules, all of which will be invoked.  If
-any entry is itself a list, then it specifies a series of alternatives
-if the first modules were not available.
-Note: The very first display is marked the 'main' display, which will
-popup on significant events (unless `chess-display-popup' in nil);
-also, killing this main display will cause all related chess buffers
-to be killed."
+(defcustom chess-default-display
+  '(chess-images chess-ics1 chess-plain)
+  "Default display to be used when starting a chess session.
+A list indicates a series of alternatives if the first display is
+not available."
+  :type '(choice symbol (repeat symbol))
+  :group 'chess)
+
+(defcustom chess-default-modules
+  '((chess-sound chess-announce)
+    chess-autosave
+    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 '(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 engines are not
+A list indicates a series of alternatives if the first engine is not
 available."
   :type '(choice symbol (repeat symbol))
   :group 'chess)
@@ -112,32 +122,62 @@ available."
   :type 'string
   :group 'chess)
 
-(defun chess--create-display (module game my-color first disable-popup)
-  (if (require module nil t)
-      (let ((display (chess-display-create game module my-color first)))
-       (when display
-         (chess-game-set-data game 'my-color my-color)
-         (if disable-popup
-             (chess-display-disable-popup display))
-         (chess-display-update display t)
-         display))))
+(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)
+  (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 (chess-engine-create game module
-                                        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)
+       (if (symbolp module)
+           (if (setq object (apply create-func module args))
+               (push object objects))
+         ;; this module is actually a list, which means keep trying
+         ;; until we find one that works
+         (while module
+           (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
@@ -149,111 +189,54 @@ available."
                     "none"))))
       chess-default-engine)))
 
-  (let ((my-color t)                   ; we start out as white always
-       (game (chess-game-create))
-       (first t)
+  (let ((game (chess-game-create))
+       (my-color t)                    ; we start out as white always
        objects)
 
-    (dolist (module chess-default-displays)
-      (let (display)
-       (if (symbolp module)
-           (setq display (chess--create-display module game my-color
-                                                first disable-popup))
-         ;; this module is actually a list, which means keep trying
-         ;; until we find one that works
-         (while module
-           (if (setq display (chess--create-display (car module) game
-                                                    my-color first
-                                                    disable-popup))
-               (setq module nil)
-             (setq module (cdr module)))))
-       (if display
-           (push display objects)))
-      (setq first nil))
-
-    (let ((module (or engine chess-default-engine)))
-      (if (symbolp module)
-         (push (chess--create-engine module game
-                                     engine-response-handler
-                                     engine-ctor-args)
-               objects)
-       (let (engine)
-         (while module
-           (setq engine (chess--create-engine (car module) game
-                                              engine-response-handler
-                                              engine-ctor-args))
-           (if engine
-               (progn
-                 (push engine objects)
-                 (setq module nil))
-             (setq module (cdr module))))
-         (unless engine
-           (push nil objects)))))
+    ;; all these odd calls are so that `objects' ends up looking like:
+    ;;   (ENGINE FIRST-DISPLAY...)
+
+    (setq objects (chess-create-modules (list chess-default-display)
+                                       'chess--create-display
+                                       game my-color disable-popup))
+    (when (car objects)
+      (mapc 'chess-display-update objects)
+      (chess-module-set-leader (car objects))
+      (unless disable-popup
+       (chess-display-popup (car objects))))
+
+    (nconc objects (chess-create-modules chess-default-modules
+                                        '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))
 
-(defalias 'chess-session 'chess)
-
 ;;;###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)))
-    (when game
-      (require chess-default-display)
-      (chess-display-create game chess-default-display
-                           (chess-game-side-to-move game)))))
-
-(defvar chess-puzzle-locations nil)
-
-(defun chess-puzzle-next ()
-  "Play the next puzzle in the collection, selected randomly."
-  (interactive)
-  (if chess-puzzle-locations
-      (chess-puzzle (aref chess-puzzle-locations 0))))
+(defalias 'chess-session 'chess)
 
 ;;;###autoload
-(defun chess-puzzle (file)
-  "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: ")
-  (save-excursion
-    (with-current-buffer (find-file-noselect file)
-      (when (or (null chess-puzzle-locations)
-               (not (equal file (aref chess-puzzle-locations 0))))
-       (let (locations)
-         (goto-char (point-min))
-         (while (search-forward "[Event" nil t)
-           (push (point) locations))
-         (setq chess-puzzle-locations (vector file locations nil nil)))
-       (random t))
-      (goto-char (nth (random (length (aref chess-puzzle-locations 1)))
-                     (aref chess-puzzle-locations 1)))
-      (let ((game (chess-pgn-to-game)))
-       (when game
-         (require chess-default-display)
-         (let (puzzle-display)
-           (if (buffer-live-p (aref chess-puzzle-locations 2))
-               (progn
-                 (setq puzzle-display (aref chess-puzzle-locations 2))
-                 (chess-display-set-game puzzle-display game))
-             (setq puzzle-display
-                   (chess-display-create game chess-default-display
-                                         (chess-game-side-to-move game))))
-           (aset chess-puzzle-locations 2 puzzle-display)
-           ;; setup spacebar as a convenient way to jump to the next puzzle
-           (with-current-buffer puzzle-display
-             (define-key (current-local-map) [? ] 'chess-puzzle-next)))
-         (require chess-default-engine)
-         (aset chess-puzzle-locations 3
-               (or (and (buffer-live-p (aref chess-puzzle-locations 3))
-                        (aref chess-puzzle-locations 3))
-                   (chess-engine-create game chess-default-engine))))))))
+(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)