diff options
| author | John Wiegley <johnw@newartisans.com> | 2002-04-09 00:00:13 +0000 |
|---|---|---|
| committer | John Wiegley <johnw@newartisans.com> | 2002-04-09 00:00:13 +0000 |
| commit | 244bfdc122b7427b3f3f96bef00415e579aff67d (patch) | |
| tree | 5ef1f55145b06d4917ccf7ae414bfb724c4041be | |
| parent | 039172c36273647bca85f156c99894c678cd6e3f (diff) | |
Add support for generating randomized starting position, ala Fischer
Random.
| -rw-r--r-- | chess-display.el | 14 | ||||
| -rw-r--r-- | chess-engine.el | 10 | ||||
| -rw-r--r-- | chess-pgn.el | 1 | ||||
| -rw-r--r-- | chess-random.el | 62 |
4 files changed, 77 insertions, 10 deletions
diff --git a/chess-display.el b/chess-display.el index 9dc80a3..7c488d6 100644 --- a/chess-display.el +++ b/chess-display.el @@ -317,6 +317,7 @@ See `chess-display-type' for the different kinds of displays." (define-key map [?M] 'chess-display-manual-move) (define-key map [?@] 'chess-display-remote) (define-key map [? ] 'chess-display-pass) + (define-key map [?S] 'chess-display-shuffle) (define-key map [?<] 'chess-display-move-first) (define-key map [?,] 'chess-display-move-backward) @@ -475,6 +476,14 @@ Basically, it means we are playing, not editing or reviewing." (= 0 (chess-display-index nil))) (chess-game-run-hooks chess-display-game 'pass))) +(defun chess-display-shuffle () + "Generate a shuffled opening position." + (interactive) + (when (and (chess-display-active-p) + (= 0 (chess-display-index nil))) + (chess-game-set-start-position chess-display-game + (chess-fischer-random-position)))) + (defun chess-display-set-current (dir) "Change the currently displayed board. Direction may be - or +, to move forward or back, or t or nil to jump @@ -547,9 +556,8 @@ to the end or beginning." "Send the current board configuration to the user." (interactive) (if chess-display-game - (chess-game-set-plies - chess-display-game - (list (chess-ply-create (chess-display-position nil))))) + (chess-game-set-start-position chess-display-game + (chess-display-position nil))) (setq chess-display-edit-mode nil)) (defun chess-display-restore-board () diff --git a/chess-engine.el b/chess-engine.el index 419444b..1f4c9c9 100644 --- a/chess-engine.el +++ b/chess-engine.el @@ -68,9 +68,8 @@ (message "Your opponent has quit playing")) ((eq event 'setup) - (let* ((position (chess-fen-to-pos (car args))) - (ply (chess-ply-create position))) - (chess-game-set-plies (chess-engine-game nil) (list ply)))))) + (chess-game-set-start-position (chess-engine-game nil) + (chess-fen-to-pos (car args)))))) (defun chess-engine-create (module &optional user-handler &rest args) (let ((regexp-alist (intern-soft (concat (symbol-name module) @@ -189,10 +188,7 @@ (apply chess-engine-event-handler event args) (cond ((eq event 'shutdown) - (chess-engine-destroy engine)) - - ((eq event 'setup) - (chess-engine-set-game engine (car args)))))) + (chess-engine-destroy engine))))) (defun chess-engine-filter (proc string) "Filter for receiving text for an engine from an outside source." diff --git a/chess-pgn.el b/chess-pgn.el index 930edc5..bc06741 100644 --- a/chess-pgn.el +++ b/chess-pgn.el @@ -68,6 +68,7 @@ game (if fen (chess-fen-to-pos fen) (chess-pos-copy chess-starting-position)) t) + ;; set the starting position to the FEN string (list (chess-ply-create (chess-fen-to-pos fen)))))) game))) diff --git a/chess-random.el b/chess-random.el new file mode 100644 index 0000000..3155373 --- /dev/null +++ b/chess-random.el @@ -0,0 +1,62 @@ +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; +;; Generate Fischer Random style positions +;; +;; Just call `chess-fischer-random-position' to generate such a +;; position. +;; +;; $Revision$ + +(require 'chess-pos) + +(defvar pieces-vector [?r ?n ?b ?q ?k ?b ?n ?r]) + +(defun shuffle-vector (vector) + "Randomly permute the elements of VECTOR (all permutations equally likely)" + (let ((i 0) + j + temp + (len (length vector))) + (while (< i len) + (setq j (+ i (random (- len i)))) + (setq temp (aref vector i)) + (aset vector i (aref vector j)) + (aset vector j temp) + (setq i (1+ i)))) + vector) + +;;;###autoload +(defun chess-fischer-random-position () + (let (pieces position) + (while (null position) + (setq pieces (shuffle-vector pieces-vector)) + (let (first-bishop first-rook king) + (catch 'retry + (dotimes (i 8) + (let ((piece (aref pieces i))) + (cond + ((= ?b piece) + (if first-bishop + (if (= (mod i 2) first-bishop) + (throw 'retry t)) + (setq first-bishop (mod i 2)))) + ((= ?k piece) + (if (null first-rook) + (throw 'retry t)) + (setq king i)) + ((= ?r piece) + (if first-rook + (if (null king) + (throw 'retry t)) + (setq first-rook i)))))) + (setq position (chess-pos-create))))) + (dotimes (i 8) + (chess-pos-set-piece position (chess-rf-to-index 0 i) + (aref pieces i)) + (chess-pos-set-piece position (chess-rf-to-index 7 i) + (upcase (aref pieces i)))) + position)) + +(provide 'chess-random) + +;;; chess-random.el ends here |
