Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #46220

Rosetta: Generate Chess960 starting position

From "WJ" <w_a_x_man@yahoo.com>
Newsgroups comp.lang.forth
Subject Rosetta: Generate Chess960 starting position
Date 2016-03-26 21:43 +0000
Organization A noiseless patient Spider
Message-ID <nd6vpc$59k$1@dont-email.me> (permalink)

Show all headers | View raw


the bishops must be placed on opposite color squares (i.e.
they must be an odd number of spaces apart or there must be an
even number of spaces between them)

the King must be between two rooks (with any number of other
pieces between them all)

OCaml:

let set_diff a b = List.filter (fun x -> not (List.mem x b)) a ;;

let remaining used = set_diff [0;1;2;3;4;5;6;7] used ;;

let position = Bytes.make 8 ' ' ;;
let table = Hashtbl.create 2000 ;;
for k = 1 to 6 do
  for r1 = 0 to k - 1 do
    for r2 = k + 1 to 7 do
      for b1 = 0 to 6 do
        if not (List.mem b1 [k;r1;r2]) then
          for b2 = b1 + 1 to 7 do
            if not (List.mem b2 [k;r1;r2]) && ((b2-b1) land 1) = 1 then
              List.iter
                (fun q ->
                  let used = [k;r1;r2;b1;b2;q] in
                    List.iter2 (fun i p -> Bytes.set position i p)
                      (used @ remaining used)
                      ['k';'r';'r';'b';'b';'q';'n';'n'] ;
                    Hashtbl.replace table (Bytes.copy position) true)
                (remaining [k;r1;r2;b1;b2])
          done
      done
    done
  done
done ;;

let array = Array.of_list (List.rev
  (Hashtbl.fold (fun k v acc -> k::acc) table []));;
let size = Array.length array in
Printf.printf "There are %d positions.\n" size ;
print_endline "Here are a few:\n" ;
Array.iter print_endline (Array.sub array 0 5);
print_endline " ...";
Array.iter print_endline (Array.sub array (size - 5) 5);
Printf.printf "\nThe traditional position is found at index %d.\n"
  (let rec loop i = if array.(i) = "rnbqkbnr" then i else loop (i+1)
   in loop 0) ;;


There are 960 positions.
Here are a few:

rkbrnbnq
bqnrnkrb
rkrbnqbn
bbqnnrkr
rnqbbkrn
 ...
nbnqrkbr
nnbrkbqr
nqrbbnkr
nnrqbbkr
rnknrqbb

The traditional position is found at index 505.



Will anybody do this in ANS Forth?


-- 
[A]n unholy alliance of leftists, capitalists, and Zionist supremacists has
schemed to promote immigration and miscegenation with the deliberate aim of
breeding us out of existence in our own homelands....  [T]he real aim stays the
same: the biggest genocide in human history....  --- Nick Griffin
(https://www.youtube.com/watch?v=K0hD7IffTJs)

Back to comp.lang.forth | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Rosetta: Generate Chess960 starting position "WJ" <w_a_x_man@yahoo.com> - 2016-03-26 21:43 +0000
  Re: Rosetta: Generate Chess960 starting position Paul Rubin <no.email@nospam.invalid> - 2016-03-26 16:11 -0700
    Re: Rosetta: Generate Chess960 starting position hughaguilar96@gmail.com - 2016-03-26 21:03 -0700

csiph-web