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!


In reply to Parsing Chess Algebra Notation by cyocum

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.