CIL Toolkit: code snippets: move generation

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

CIL Toolkit: code snippets: loading from a FEN save file

Post by sje »

The "lf" (load FEN) command is the inverse of the "sf" (save FEN) command:

Code: Select all

(defun icp-do-lf (my-cpc)
  "Handle the ICP lf command."
  (if (/= (cpc-cmdtknc my-cpc) 2)
    (icp-user-error my-cpc "Need exactly one load file pathname parameter")
    (let ((load-stream (open (second (cpc-cmdtkns my-cpc)) :direction :input)))
      (if (not load-stream)
        (icp-user-error my-cpc "Can't open load file for input")
        (let ((text-line (read-line load-stream nil nil)))
          (if (not text-line)
            (icp-user-error my-cpc "Can't read load file")
            (let ((pos (calc-pos-from-str text-line)))
              (if (not pos)
                (icp-user-error my-cpc "Invalid FEN specification")
                (progn
                  (load-cpc-pos pos my-cpc)
                  (icp-display-colorized-pos my-cpc)))))
          (close load-stream)))))
  (values))
There is still a bit of lower level code to be written until the "lp" (load PGN) command can be implemented.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

CIL Toolkit: code snippets: dribble activation/passivation

Post by sje »

Simple stuff for activating or deactivating the dribble output feature:

Code: Select all

(defun icp-do-ad (my-cpc)
  "Handle the ICP ad command."
  (unless (cpc-stdrib my-cpc)
    (setf (cpc-stdrib my-cpc) (open (cpc-dfpstr my-cpc) :direction :output)))
  (values))

(defun icp-do-pd (my-cpc)
  "Handle the ICP pd command."
  (when (cpc-stdrib my-cpc)
    (close (cpc-stdrib my-cpc))
    (setf (cpc-stdrib my-cpc) nil))
  (values))
For simplicity, the ICP log file name is hard coded to be "ICP.log" and the dribble file name (when dribbling is activated) is "ICP.drb".

For the xboard command processor, the names are "xboard.log" and "xboard.drb" respectively. For the UCI command processor, the names are "UCI.log" and "UCI.drb".

In all cases the prior log file is overwritten, as is the dribble file. To assist with diagnosing the expected crashes, both the log and dribble output streams are flushed after each newline output.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

CIL Toolkit: code snippets: j'doube

Post by sje »

Poor typists and bad chessplayers agree: every chess program needs a move take back feature:

Code: Select all

(defun icp-do-tb (my-cpc)
  "Handle the ICP tb command."
  (if (zero? (cpc-push-count my-cpc))
    (icp-user-error my-cpc "No move to take back")
    (progn
      (retreat-cpc my-cpc)
      (icp-display-colorized-pos my-cpc))))
The "retreat-cpc" routine is just like the "advance-cpc" routine except that it travels backward in time.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

CIL Toolkit: code snippets: not entirely useless

Post by sje »

The simplest ICP command:

Code: Select all

(defun icp-do-noop (my-cpc)
  "Handle the ICP noop command."
  (declare (ignore my-cpc))
  (values))
The "noop" command actually has a purpose: it's used to hold comments in command batch files. That's all it's good for.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

CIL Toolkit: code snippets: setting up a new game

Post by sje »

The "ng" (new game) command works like the "ef" (enter FEN) position set up command, except that the (unspecified) position is always the initial array.

Code: Select all

(defun icp-do-ng (my-cpc)
  "Handle the ICP ng command."
  (load-cpc-pos (create-initial-array-pos) my-cpc)
  (icp-display-colorized-pos my-cpc))
User avatar
hgm
Posts: 28356
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: CIL Toolkit: code snippets: setting up a new game

Post by hgm »

Could you make a Chess program in LISP as small as micro-Max (< 2000 characters)?

Then this thread would not have to be so long... :P
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: CIL Toolkit: code snippets: setting up a new game

Post by sje »

hgm wrote:Could you make a Chess program in LISP as small as micro-Max (< 2000 characters)?

Then this thread would not have to be so long... :P
Be careful with what you write, lest I start a thread about a chess program written in COBOL. :shock:
User avatar
mhull
Posts: 13447
Joined: Wed Mar 08, 2006 9:02 pm
Location: Dallas, Texas
Full name: Matthew Hull

Re: CIL Toolkit: code snippets: setting up a new game

Post by mhull »

sje wrote:
hgm wrote:Could you make a Chess program in LISP as small as micro-Max (< 2000 characters)?

Then this thread would not have to be so long... :P
Be careful with what you write, lest I start a thread about a chess program written in COBOL. :shock:
Or befunge.
Matthew Hull
User avatar
Zach Wegner
Posts: 1922
Joined: Thu Mar 09, 2006 12:51 am
Location: Earth

Re: CIL Toolkit: code snippets: setting up a new game

Post by Zach Wegner »

Or even Whitespace. Then H.G.'s condition of only counting non-whitespace characters results in every program having zero size! :lol:

My personal vote for best esoteric language: PATH. I never imagined before that a programming language might not be read left-right, top-bottom.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

CIL Toolkit: code snippets: win/lose/draw accumulator

Post by sje »

The win/lose/draw accumulator is a structure that is used mostly for the construction and interpretation of opening book files:

Code: Select all

;;; The win/lose/draw accumulator structure

(defstruct
  (wldacc
    (:print-function
      (lambda (my-wldacc my-stream my-level)
        (declare (ignore my-level))
        (encode-wldacc my-stream my-wldacc))))
  (win-count  0 :type fixnum)  ; Count of wins
  (lose-count 0 :type fixnum)  ; Count of losses
  (draw-count 0 :type fixnum)) ; Count of draws

(defun clone-wldacc (my-wldacc)
  "Return a clone of a win/lose/draw accumulator structure."
  (make-wldacc
    :win-count  (wldacc-win-count  my-wldacc)
    :lose-count (wldacc-lose-count my-wldacc)
    :draw-count (wldacc-draw-count my-wldacc)))

(defun flip-wldacc (my-wldacc)
  "Return a color reversed win/lose/draw accumulator."
  (make-wldacc
    :win-count  (wldacc-lose-count my-wldacc)
    :lose-count (wldacc-win-count  my-wldacc)
    :draw-count (wldacc-draw-count my-wldacc)))

(defun calc-total-wldacc (my-wldacc)
  "Return the total result count of a win/lose/draw accumulator."
  (+
    (wldacc-win-count  my-wldacc)
    (wldacc-lose-count my-wldacc)
    (wldacc-draw-count my-wldacc)))

(defun encode-wldacc (my-stream my-wldacc)
  "Encode a win/lose/draw accumulator on a stream."
  (fmt-brack-l my-stream)
  (put-string my-stream "W:")
  (put-integer my-stream (wldacc-win-count  my-wldacc))
  (blank my-stream)
  (put-string my-stream "L:")
  (put-integer my-stream (wldacc-lose-count my-wldacc))
  (blank my-stream)
  (put-string my-stream "D:")
  (put-integer my-stream (wldacc-draw-count my-wldacc))
  (fmt-brack-r my-stream)
  (values))