Generate Chess960 starting position

From Rosetta Code
Revision as of 07:21, 1 May 2014 by Grondilu (talk | contribs) (new draft)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Generate Chess960 starting position is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Chess960 is a variant of chess created by world champion Bobby Fisher. Unlike other variant of the game, Chess960 does not require a different material, but instead relies on a random initial position, with a few constraints:

  • as in the standard chess game, all height pawns must be placed on the second rank.
  • pieces must stand on the first rank as in the standard game, in random column order but with the two following constraints:
    • the bishops must be placed on opposite color squares ;
    • the King must be between two rooks ;

With those constraints there are 960 possible starting positions, thus the name of the variant.

The purpose of this task is to write a program that randomly generates a Chess960 initial position. You will show the result as the first rank displayed with Chess symbols in Unicode.

Perl 6

<lang perl6>my Set $squares = set ^8;

$squares = $squares (-) set my @knights = $squares.pick(2); $squares = $squares (-) set my @bishops = $squares.list.grep(* % 2).pick, $squares.list.grep(* %% 2).pick; $squares = $squares (-) set my $queen = $squares.pick; my ($king, @rooks) = $squares.list[1, 0, 2];

my @squares;

@squares[$king, $queen, @rooks, @bishops, @knights] =

< ♔ ♕ ♖ ♖ ♗ ♗ ♘ ♘ >;

say @squares;</lang>

Output:
♗ ♖ ♔ ♕ ♘ ♗ ♖ ♘