perlquestion
cyocum
<p>I would like to thank you all in advance for helping me with this.</p>
<p>I am writing a Chess Engine in Perl. I know there are <a href="http://search.cpan.org/search?query=chess&mode=all">several </a> already on CPAN but they seem to have been abandoned and I was hoping to keep my skills up while I am in grad school.</p>
<p>My question is how do I go about parsing Chess algebra notation? I will give you a few examples. For instance, e4 means "move pawn on e2 to e4." Well this is easy enough but it gets more complicated. Nf3 means "move Knight on g1 to f3". As you can see the piece letter, in this case N for Knight, is not always required. Also, sometimes you get e4xd5 which means "pawn on e4 takes piece on d5". In the case of Rooks, you can get: Rad4, which means "move the Rook which is on column a and move it to d4" (this is in case both rooks can move to d4 legally).</p>
<p>Currently I have this: <code> my $piece;
my $col;
my $row;
my $capture;
#parse Nf4 where N is not always present
my @parsed = split //, $move;
if(scalar @parsed == 2) {
$col = pop @parsed;
$row = pop @parsed;
} elsif (scalar @parsed == 3) {
$piece = pop @parsed;
$col = pop @parsed;
$row = pop @parsed;
} elsif(scalar @parsed == 4) {
$piece = pop @parsed;
$capture = pop @parsed; #this is the capture symbol
$col = pop @parsed;
$row = pop @parsed;
} else {
die "not known algebra notation\n";
}
</code></p>
<p>It is horrible and not complete and I know it. I was thinking about using <a href="http://www.perlmonks.org/index.pl?node=split&go_button=Search">split </a>on this but because the first letter is not always the piece letter, it gets confusing.</p>
<p>Again, thanks and any help would be greatly appreciated!</p>