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 (let (pieces position)
30 (while (null position)
31 (setq pieces (chess-shuffle-vector pieces-vector))
32 (let (first-bishop first-rook king)
35 (let ((piece (aref pieces i)))
39 (if (= (mod i 2) first-bishop)
41 (setq first-bishop (mod i 2))))
50 (setq first-rook i))))))
51 (setq position (chess-pos-create)))))
53 ;; set the home row pieces
55 (chess-pos-set-piece position (chess-rf-to-index 0 i)
57 (chess-pos-set-piece position (chess-rf-to-index 7 i)
58 (upcase (aref pieces i))))
62 (provide 'chess-random)
64 ;;; chess-random.el ends here