Generate Chess960 starting position: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Java}}: Left some old code in the comments)
m (→‎{{header|Java}}: remove a line changing to a do/while)
Line 17: Line 17:
import java.util.Collections;
import java.util.Collections;
import java.util.List;
import java.util.List;



public class Chess960{
public class Chess960{
private static List<Character> pieces = Arrays.asList('R','B','N','Q','K','N','B','R');
private static List<Character> pieces = Arrays.asList('R','B','N','Q','K','N','B','R');

public static List<Character> generateFirstRank(){
public static List<Character> generateFirstRank(){
do{
Collections.shuffle(pieces);
while(!check(pieces.toString().replaceAll("[\\[\\],\\s]", ""))){ //List.toString adds some human stuff, remove that
Collections.shuffle(pieces);
Collections.shuffle(pieces);
}while(!check(pieces.toString().replaceAll("[\\[\\],\\s]", ""))); //List.toString adds some human stuff, remove that
}
return pieces;
return pieces;
}
}

private static boolean check(String rank){
private static boolean check(String rank){
if(!rank.matches(".*R.*K.*R.*")) return false; //king between rooks
if(!rank.matches(".*R.*K.*R.*")) return false; //king between rooks
Line 32: Line 35:
return true;
return true;
}
}

public static void main(String[] args){
public static void main(String[] args){
for(int i = 0; i < 10; i++){
for(int i = 0; i < 10; i++){

Revision as of 18:32, 1 May 2014

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 variants 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 (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)

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 or with the letters King Queen Rook Bishop kNight.

Java

Works with: Java version 1.5+

Regex inspired by Python, prints ten examples. <lang java5>import java.util.Arrays; import java.util.Collections; import java.util.List;


public class Chess960{ private static List<Character> pieces = Arrays.asList('R','B','N','Q','K','N','B','R');

public static List<Character> generateFirstRank(){ do{ Collections.shuffle(pieces); }while(!check(pieces.toString().replaceAll("[\\[\\],\\s]", ""))); //List.toString adds some human stuff, remove that

return pieces; }

private static boolean check(String rank){ if(!rank.matches(".*R.*K.*R.*")) return false; //king between rooks if(!rank.matches(".*B(..|....|......|)B.*")) return false; //all possible ways bishops can be placed return true; }

public static void main(String[] args){ for(int i = 0; i < 10; i++){ System.out.println(generateFirstRank()); } } }</lang>

Output:
[R, N, K, N, R, B, B, Q]
[B, B, Q, R, N, K, N, R]
[R, K, Q, N, N, R, B, B]
[N, B, B, N, R, K, Q, R]
[R, Q, B, B, K, N, N, R]
[R, K, B, Q, N, B, N, R]
[N, N, R, K, Q, B, B, R]
[R, N, K, Q, N, B, B, R]
[N, R, B, K, Q, B, N, R]
[N, Q, N, R, K, B, B, R]

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] =

< K Q R R B B N N >;

say @squares;</lang>

Output:
Q N B R K B R N

Python

Python: Indexing

This uses indexing rather than regexps. Rooks and bishops are in upper and lower case to start with so they can be individually indexed to apply the constraints. This would lead to some duplication of start positions if not for the use of a set comprehension to uniquify the, (upper-cased), start positions.

<lang python>>>> from itertools import permutations >>> pieces = 'KQRrBbNN' >>> starts = sorted({.join(p).upper() for p in permutations(pieces)

                    if p.index('B') % 2 != p.index('b') % 2 		# Bishop constraint
                    and ( p.index('r') < p.index('K') < p.index('R')	# King constraint	
                          or p.index('R') < p.index('K') < p.index('r') ) })

>>> starts[0] 'BBNNQRKR' >>> starts[-1] 'RQNNKRBB' >>> len(starts) 960 >>> </lang>

Python: Regexp

This uses regexps to filter permutations of the start position pieces rather than indexing. <lang python>>>> import re >>> pieces = 'KQRRBBNN' >>> starts2 = sorted({p for p in (.join(q) for q in permutations(pieces))

                     if re.search(r'B(|..|....|......)B', p)
                     and re.search(r'R.*K.*R', p)})

>>> starts2[0] 'BBNNQRKR' >>> starts2[-1] 'RQNNKRBB' >>> len(starts2) 960 >>> </lang>