Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Symbolic: code example

Author: Steven Edwards

Date: 11:02:51 01/23/04

Go up one level in this thread


On January 22, 2004 at 22:58:00, Robert Hyatt wrote:
>On January 22, 2004 at 10:03:55, Steven Edwards wrote:

>>;;;; emp.lsp: Enumerate movepaths
>>
>>(defun emp (theFEN theDepth)
>> "Enumerate distinct movepaths from a FEN string to a depth"
>> (cond
>>  ((not (string? theFEN))
>>    "Error: first argument must be a FEN string")
>>  ((not (and (integer? theDepth) (nonnegative? theDepth)))
>>    "Error: second argument must be a nonnegative integer")
>>  (t
>>   (let ((thePos (PosFromFEN theFEN)))
>>    (if (null? thePos)
>>     "Error: invalid position"
>>     (emp-aux thePos theDepth))))))
>>
>>(defun emp-aux (thePos theDepth)
>> "Enumerate distinct movepaths from a position to a depth"
>> (cond
>>  ((= theDepth 0) 1)
>>  ((= theDepth 1) (length (Generate thePos)))
>>  (t
>>   (let ((theSum 0) (theEnv nil) (theMove nil) (theML (Generate thePos)))
>>    (dolist (theMove theML)
>>     (Execute theMove thePos theEnv)
>>     (incf theSum (emp-aux thePos (1- theDepth)))
>>     (Retract theMove thePos theEnv))
>>    theSum))))
>
>you know, when I was first introduced to Lisp a _long_ time ago, I looked
>the person right in the eye and said "that is a write-only language."

No, APL is a write-only langage.  BYTE 's APL theme issue of long ago had an APL
chess program that was all of three paragraphs long.

>Thank goodness vi supports the % key. :)

Emacs is the One True Way; also it is in fact a Lisp rogram with its own
interpreter and compiler.  Here's the Emacs chess-crafty.el (interface) source:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Play against crafty!
;;

(require 'chess-common)

(defgroup chess-crafty nil
  "The publically available chess engine 'crafty'."
  :group 'chess-engine)

(defcustom chess-crafty-path (or (executable-find "crafty")
				 (executable-find "wcrafty"))
  "The path to the crafty executable."
  :type 'file
  :group 'chess-crafty)

(defvar chess-crafty-evaluation nil)

(make-variable-buffer-local 'chess-crafty-evaluation)

(defvar chess-crafty-regexp-alist
  (list
   (cons (concat "\\(White\\|Black\\)\\s-*([0-9]+):\\s-+\\("
		 chess-algebraic-regexp "\\)\\s-*$")
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'move
		     (chess-engine-convert-algebraic (match-string 2) t)))))
   (cons "total evaluation\\.+\\s-+\\([-+0-9.]+\\)"
	 (function
	  (lambda ()
	    (setq chess-crafty-evaluation
		  (string-to-number (match-string 1))))))
   (cons "{\\(Black\\|White\\) resigns}"
	 (function
	  (lambda ()
	    (funcall chess-engine-response-handler 'resign))))
   (cons "\\(Illegal move\\|unrecognized/illegal command\\):\\s-*\\(.*\\)"
	 (function
	  (lambda ()
	    (error (match-string 1)))))
   (cons "command not legal now"
	 (function
	  (lambda ()
	    (error (match-string 0)))))))

(defun chess-crafty-handler (game event &rest args)
  (unless chess-engine-handling-event
    (cond
     ((eq event 'initialize)
      (let ((proc (chess-common-handler game 'initialize "crafty")))
	(when (and proc (processp proc)
		   (eq (process-status proc) 'run))
	  (process-send-string proc (concat "display nogeneral\n"
					    "display nochanges\n"
					    "display noextstats\n"
					    "display nohashstats\n"
					    "display nomoves\n"
					    "display nonodes\n"
					    "display noply1\n"
					    "display nostats\n"
					    "display notime\n"
					    "display novariation\n"
					    "alarm off\n"
					    "ansi off\n"))
	  (setq chess-engine-process proc
		chess-engine-opponent-name "Crafty")
	  t)))

     ((eq event 'setup-pos)
      (chess-engine-send nil (format "setboard %s\n"
				     (chess-pos-to-string (car args)))))

     ((eq event 'evaluate)
      (setq chess-crafty-evaluation nil)
      (chess-engine-send nil "display general\nscore\ndisplay nogeneral\n")
      (let ((limit 50))
	(while (and (null chess-crafty-evaluation)
		    (> (setq limit (1- limit)) 0))
	  (sit-for 0 100 t))
	chess-crafty-evaluation))

     ((eq event 'setup-game)
      (let ((file (chess-with-temp-file
		      (insert (chess-game-to-string (car args)) ?\n))))
	(chess-engine-send nil (format "read %s\n" file))))

     ((eq event 'set-option)
      (cond
       ((eq (car args) 'resign)
	(if (cadr args)
	    (chess-engine-send nil "resign 9\n")
	  (chess-engine-send nil "resign -1\n")))
       ((eq (car args) 'ponder)
	(if (cadr args)
	    (chess-engine-send nil "ponder on\n")
	  (chess-engine-send nil "ponder off\n")))))

     (t
      (if (and (eq event 'undo)
	       (= 1 (mod (car args) 2)))
	  (error "Cannot undo until after crafty moves"))

      (apply 'chess-common-handler game event args)))))

(provide 'chess-crafty)

;;; chess-crafty.el ends here




This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.