Chess-Plisco

 view release on metacpan or  search on metacpan

scripts/gen-positions.pl  view on Meta::CPAN

use strict;
use v5.10;
use List::Util qw(shuffle);

use Chess::Plisco qw(:all);
use Chess::Plisco::Macro;

sub generate_board;
sub empty_pos;
sub print_positions;

my @files = @ARGV;

my @endgames;

foreach my $file (@files) {
	$file =~ s{.*/}{};
	$file =~ s{\..*}{};
	push @endgames, $file;
}

foreach my $i (1 .. 6000) {
	my $idx = int(rand(scalar @endgames));
	my $endgame = $endgames[$idx];
	my $pos;

	my %indexes = (
		K => CP_POS_KINGS,
		Q => CP_POS_QUEENS,
		R => CP_POS_ROOKS,
		B => CP_POS_BISHOPS,
		N => CP_POS_KNIGHTS,
		P => CP_POS_PAWNS,
	);
	while (!$pos) {
		my $new = empty_pos;
		my @board = generate_board $endgame;

		foreach my $shift (0 .. 63) {
			my $piece = $board[$shift] or next;
			my $index = $indexes{uc $piece};
			my $bb = 1 << $shift;
			$new->[$index] |= $bb;
			if ($piece ge 'a') {
				$new->[CP_POS_BLACK_PIECES] |= $bb;
			} else {
				$new->[CP_POS_WHITE_PIECES] |= $bb;
			}
		}

		eval {
			my $fen = $new->toFEN;
			$pos = Chess::Plisco->new($fen);
		};
		if ($pos) {
			my $legal = $pos->__legalityCheck;
			undef $pos if !$legal;
		}
	}

	print_positions $pos;
}

sub generate_board {
	my ($endgame) = @_;



( run in 1.181 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )