Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Parsing Chess Algebra Notation

by davidj (Priest)
on May 14, 2004 at 17:26 UTC ( [id://353424]=note: print w/replies, xml ) Need Help??


in reply to Parsing Chess Algebra Notation

If all you want to do is parse the individual moves (without validating, etc), the following regex will do the trick:

($p, $f, $r, $a, $d, $c) = $move =~ /([BKNPQR]?)([a-h]?)([0-9]?)([x=]?)([BKNPQR]|[a-h][1-8])([+#]?)/;

where the variables are as follows:
$move = the move being parsed
$p = the piece being moved
$f = the file being moved from
$r = the rank being moved from
$a = action (x = capture, '=' = promote)
$d = destination square (or promotion piece if $a = '=')
$c = check or mate
As an example, the move you gave, Nf4, will parse as follows:
$p = N, $f = "", $r = "", $a = "", $d = b4, $c = ""


hope this helps,
davidj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://353424]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 04:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found