1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;; Generate Fischer Random style positions
5 ;; Just call `chess-fischer-random-position' to generate such a
11 (defvar pieces-vector [?r ?n ?b ?q ?k ?b ?n ?r])
13 (defun chess-shuffle-vector (vector)
14 "Randomly permute the elements of VECTOR (all permutations equally likely)"
18 (len (length vector)))
20 (setq j (+ i (random (- len i))))
21 (setq temp (aref vector i))
22 (aset vector i (aref vector j))
28 (defun chess-fischer-random-position ()
29 "Generate a Fischer Random style position."
30 (let (pieces position)
31 (while (null position)
32 (setq pieces (chess-shuffle-vector pieces-vector))
33 (let (first-bishop first-rook king)
36 (let ((piece (aref pieces i)))
40 (if (= (mod i 2) first-bishop)
42 (setq first-bishop (mod i 2))))
51 (setq first-rook i))))))
52 (setq position (chess-pos-create)))))
54 ;; set the home row pieces
56 (chess-pos-set-piece position (chess-rf-to-index 0 i)
58 (chess-pos-set-piece position (chess-rf-to-index 7 i)
59 (upcase (aref pieces i))))
63 (provide 'chess-random)
65 ;;; chess-random.el ends here