How to resolve the algorithm Generate Chess960 starting position step by step in the Raku programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Generate Chess960 starting position step by step in the Raku programming language
Table of Contents
Problem Statement
Chess960 is a variant of chess created by world champion Bobby Fischer. Unlike other variants of the game, Chess960 does not require a different material, but instead relies on a random initial position, with a few constraints:
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 can randomly generate any one of the 960 Chess960 initial positions. You will show the result as the first rank displayed using either the chess symbols in Unicode (♔♕♖♗♘), the letters King Queen Rook Bishop kNight, or the corresponding letters in a language other than English.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Generate Chess960 starting position step by step in the Raku programming language
Source code in the raku programming language
repeat until m/ '♗' [..]* '♗' / { $_ = < ♖ ♖ ♖ ♕ ♗ ♗ ♘ ♘ >.pick(*).join }
s:2nd['♖'] = '♔';
say .comb;
sub chess960 {
.subst(:nth(2), /'♜'/, '♚') given
first rx/ '♝' [..]* '♝' /,
< ♛ ♜ ♜ ♜ ♝ ♝ ♞ ♞ >.pick(*).join xx *;
}
say chess960;
constant chess960 =
< ♛ ♜ ♜ ♜ ♝ ♝ ♞ ♞ >.permutations».join.unique.grep( / '♝' [..]* '♝' / )».subst(:nth(2), /'♜'/, '♚');
.say for chess960;
constant chess960 = gather for 0..3 -> $q {
(my @q = <♜ ♚ ♜>).splice($q, 0, '♛');
for 0 .. @q -> $n1 {
(my @n1 = @q).splice($n1, 0, '♞');
for $n1 ^.. @n1 -> $n2 {
(my @n2 = @n1).splice($n2, 0, '♞');
for 0 .. @n2 -> $b1 {
(my @b1 = @n2).splice($b1, 0, '♝');
for $b1+1, $b1+3 ...^ * > @b1 -> $b2 {
(my @b2 = @b1).splice($b2, 0, '♝');
take @b2.join;
}
}
}
}
}
CHECK { note "done compiling" }
note +chess960;
say chess960.pick;
subset Pos960 of Int where { $_ ~~ ^960 };
sub chess960(Pos960 $position = (^960).pick) {
# We remember the remainder used to place first bishop in order to place the
# second
my $b1;
# And likewise remember the chosen combination for knights between those
# placements
my @n;
# Piece symbols and positioning rules in order. Start with the position
# number. At each step, divide by the divisor; the quotient becomes the
# dividend for the next step. Feed the remainder into the specified code block
# to get a space number N, then place the piece in the Nth empty space left in
# the array.
my @rules = (
#divisor, mapping function, piece
( 4, { $b1 = $_; 2 * $_ + 1 }, '♝' ),
( 4, { 2 * $_ - ($_ > $b1 ?? 1 !! 0) }, '♝' ),
( 6, { $_ }, '♛' ),
(10, { @n = combinations(5,2)[$_]; @n[0] }, '♞' ),
( 1, { @n[1]-1 }, '♞' ),
( 1, { 0 }, '♜' ),
( 1, { 0 }, '♚' ),
( 1, { 0 }, '♜' )
);
# Initial array, using '-' to represent empty spaces
my @array = «-» xx 8;
# Working value that starts as the position number but is divided by the
# divisor at each placement step.
my $p = $position;
# Loop over the placement rules
for @rules -> ($divisor, $block, $piece) {
# get remainder when divided by divisor
(my $remainder, $p) = $p.polymod($divisor);
# apply mapping function
my $space = $block($remainder);
# find index of the $space'th element of the array that's still empty
my $index = @array.kv.grep(-> $i,$v { $v eq '-' })[$space][0];
# and place the piece
@array[$index] = $piece;
}
return @array;
}
# demo code
say chess960(518); #standard optning position
say chess960; # (it happened to pick #300)
You may also check:How to resolve the algorithm Miller–Rabin primality test step by step in the Python programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Java programming language
You may also check:How to resolve the algorithm Optional parameters step by step in the Slate programming language
You may also check:How to resolve the algorithm Pinstripe/Display step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm Web scraping step by step in the R programming language