I took your advice and tried it backwards. As you will notice, I did this in a step by step fashion. I am sure that there are much more effient ways to do this. Also, it does not yet support the edge cases that you are talking about (that includes promotion). I just wanted to layout a bit more code for general commments and improvments. Also, thanks for all the help. I really appreciate it!
use strict;
use warnings;
my $move = "Qe4++";
#reverse as per dragonchild's suggestion
my @parsed = reverse split //, $move;
print join('', @parsed) . "\n";
my $check;
my $row;
my $col;
my $piece;
#convert the letters into appropriate numbers
#there is probably a faster way to to this
my %col_convert = (a => 0,
b => 1,
c => 2,
d => 3,
e => 4,
f => 5,
g => 6,
h => 7);
foreach my $elem (@parsed) {
#check for, well, check
if($elem eq '+') {
#start the process to see if this is a checkmate
my $nextElem = shift @parsed;
print "nextElem $nextElem" . "\n";
#if it is then print
if($nextElem eq '+') {
print "Checkmate\n";
next;
} else {
#otherwise put it back on the stack for the next
#iteration
unshift @parsed, $nextElem;
}
#then print check in this instance
print "Check\n";
}elsif($elem =~ m/[1-8]/) {
$row = $elem;
print "Row: $row\n";
}elsif($elem =~ /[a-h]/) {
#convert to number
$col = $col_convert{$elem};
print "Col: $col\n";
}elsif($elem =~ m/x/) {
#this is a capture to that square
print "Capture on $col $row\n";
}elsif($elem =~ /[KQRNB]/) {
$piece = $elem;
print "Piece to move is $piece\n";
} else {
die "not a valid move\n";
}
}
-
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.
|