Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Why are you making it so complicated for yourself? All you want to do is build a string that corresponds to the move. This means that you're attempting to build a state representation by parsing another state representation, one character at a time. The following may or may not be complete, but it's a good start. (I didn't do castling, leaving that as an exercise for the reader. I'd probably special-case it.)
package ChessNot; use strict; use warnings; $|++; my %is_row = map { $_ => 1 } ( 1 .. 8 ); my %is_col = do { my $i = 1; map { $_ => $i++ } ( 'a' .. 'h' ) }; my %is_piece = ( P => 'Pawn', B => 'Bishop', N => 'Knight', R => 'Rook', Q => 'Queen', K => 'King', ); sub parse { my $move = shift; my @parse = split //, $move; my ($row, $col); my $piece = 'Pawn'; my $action = 'moves to'; my $have_capture = 0; my $start_row; my $start_col; my $check = ''; while (my $elem = pop @parse) { $is_row{ $elem } && do { ($row ? $start_row : $row) = $elem; next; }; $is_col{ $elem } && do { ($col ? $start_col : $col) = $is_col{ $elem }; next; }; $is_piece{ $elem } && do { $piece = $is_piece{ $elem }; next; }; $elem eq 'x' && do { $action = 'captures'; $have_capture = 1; next; }; $elem eq '+' && do { $check .= $check ? 'mate' : ', check'; next; }; } my $start_piece = ''; if ($start_row || $start_col) { $start_piece = join ', ', map { $_ || '?' } $start_row, $start +_col; $start_piece = "($start_piece) "; } return "$piece ${start_piece}$action ($row, $col)$check"; } 1; __END__

And the test code ...

#!/usr/local/bin/perl use strict; use warnings; $|++; use Test::More no_plan => 1; use_ok( 'ChessNot' ); my @tests = ( [ 'e4', "Pawn moves to (4, 5)" ], [ 'Pe4', "Pawn moves to (4, 5)" ], [ 'Be4', "Bishop moves to (4, 5)" ], [ 'Re4', "Rook moves to (4, 5)" ], [ 'Ne4', "Knight moves to (4, 5)" ], [ 'Qe4', "Queen moves to (4, 5)" ], [ 'Ke4', "King moves to (4, 5)" ], [ 'Nxe4', "Knight captures (4, 5)" ], [ 'dxe4', "Pawn (?, 4) captures (4, 5)" ], [ 'Ndxe4', "Knight (?, 4) captures (4, 5)" ], [ 'N5xe4', "Knight (5, ?) captures (4, 5)" ], [ 'Nf5xe4', "Knight (5, 6) captures (4, 5)" ], [ 'e4+', "Pawn moves to (4, 5), check" ], [ 'e4++', "Pawn moves to (4, 5), checkmate" ], [ 'dxe4++', "Pawn (?, 4) captures (4, 5), checkmate" ], ); foreach my $test (@tests) { my ($move, $expectation) = @$test; my $result = ChessNot::parse( $move ); ok( $result eq $expectation, "$move => $expectation ($result)" ); }
</reamore>

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

I shouldn't have to say this, but any code, unless otherwise stated, is untested


In reply to Re: Re: Re: Parsing Chess Algebra Notation by dragonchild
in thread 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found