Hello guys,
While going over the book, I ran into this chess program and I have few questions
1) on line 40), why is not $chessboard[$startx]->[$starty] ) ???
40 unless (defined $chessboard[$starty]->[$startx]) {
2)
20 foreach my $i (reverse (0..7)) { #Row
1 #!/usr/bin/perl -w
2 #
3 #
4
5
6 use strict;
7
8 my @chessboard;
9 my @back = qw(R N B Q K N B R);
10
11 foreach (0..7) {
12 $chessboard[0]->[$] = "W" . $back[$]; #white Back Row
13 $chessboard[1]->[$] = "WP" ; #white Pawns
14 $chessboard[6]->[$] = "BP" ; #Black Pawns
15 $chessboard[7]->[$] = "B" . $back[$]; #Black Back Row
16 }
17
18 while (1) {
19 # Print board
20 foreach my $i (reverse (0..7)) { #Row
21 foreach my $j (0..7) { #Column
22 if (defined $chessboard[$i]->[$j]) {
23 print $chessboard[$i]->[$j];
24 } elsif ( ($i % 2) == ($j % 2) ) {
25 print "..";
26 } else {
27 print " ";
28 }
29 print " "; #End of cell
30 }
31 print "\n"; #End of row
32 }
33
34 print "\nStarting square [x,y]: ";
35
36 my $move = <>;
37 last unless ($move =~ /^\s*([1-8]),([1-8])/);
38 my $startx = $1-1; my $starty = $2-1;
39
40 unless (defined $chessboard[$starty]->[$startx]) {
41 print "There nothing on that square\n";
42 next;
43 }
44
45 print "\nEnding square [x,y]: ";
46 $move = <>;
47
48 last unless ($move =~ /([1-8]),([1-8])/);
49 my $endx = $1-1; my $endy = $2-1;
50
51 # put starting square on ending square.
52 $chessboard[$endy]->[$endx] = $chessboard[$starty]->[$startx];
53
54 #remove from old square
55 undef $chessboard[$starty]->[$startx];
56 }