Algebraic notation is relative to the current board position. So, you have to parse it backwards.
The last character may or may not be a '+', indicating check. (I'm going to assume that there is no analysis on the game, so I will ignore !, !!, ?, ??, ?!, and !?.) So, we have to see if that exists. If it does, we may or may not want to verify that, after the move, a state of check exists.
After we look for the '+', the last two characters are guaranteed to be the destination square in the form /[a-h][1-8]/.
Any remaining characters are the specification for the piece and/or starting-square coordinates. (Remember, we're still parsing the string in reverse.)
- If there is nothing there, look for a pawn that could make it to this square.
- If there is an 'x', that means that there is a capture. (This notation, while legal, isn't common.)
- If there is a K, Q, R, N, or B, look for the appropriate piece that could make it to this square. There should be only one of that piece type that can legally make this move.
- If there is [a-h1-8]+, that means that there is either more than one officer of that piece type that can make this move or it is a pawn capture (ed5 is a legal notation). Remember, Qa2b3 means that there are three queens, potentially at a2, a4, and b4. All of them can move to b3.
The key is parsing it backwards.
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.