http://qs321.pair.com?node_id=350332

cyocum has asked for the wisdom of the Perl Monks concerning the following question:

I would like to thank you all in advance for helping me with this.

I am writing a Chess Engine in Perl. I know there are several 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.

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).

Currently I have this:

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"; }

It is horrible and not complete and I know it. I was thinking about using split on this but because the first letter is not always the piece letter, it gets confusing.

Again, thanks and any help would be greatly appreciated!