chess perl program questions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting chess perl program questions
# 1  
Old 06-30-2007
chess perl program questions

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 }
# 2  
Old 06-30-2007
while posting question, I hit the wrong button and submitted.. anyway

2) on line 20), why reverse it???

20 foreach my $i (reverse (0..7)) { #Row
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl program get a response before the program quits

I created a program, so a kid can practice there math on it. It dispenses varies math problems and the kid must input an answer. I also want it to grade the work they have done, but I can't find the best place for it to print out the grade. I have: if ( $response =~ m/^/ ) { $user_wants_to_quit... (1 Reply)
Discussion started by: germany1517
1 Replies

2. Shell Programming and Scripting

Perl Debug Stepping Answering Questions

I am new to perl and want to get a little better understanding of debugging code in perl. I have a perl script that has questions to be answered like: he following PERL modules are recommended: Crypt::DES Crypt::PasswdMD5 IO::Pty Net::Write::Layer2 String::CRC32 Attempt to install... (0 Replies)
Discussion started by: metallica1973
0 Replies

3. UNIX for Dummies Questions & Answers

Two questions. First one; What are the ways in which a program starts to run.

This is my first post here so hello everyone! I know that a command of the programs name can start a program and clicking on a icon in GUI can as well as a startup shell script but how do I educate myself of the method that starts an application? Does the GUI run a script? What are the ways/ way... (2 Replies)
Discussion started by: theKbStockpiler
2 Replies

4. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

5. Shell Programming and Scripting

Perl newbie questions!

Hi, So I started to learn perl a few days ago, and I have some problems... One of my problems... #!C:\Perl64\bin\perl.exe -w use LWP::Simple; print "Content-Type: Text/Plain\n\n"; sub pagelinks { return @all = get($_) =~ /href\s*=\s*"?(+)/gis; } @a =... (5 Replies)
Discussion started by: byte1918
5 Replies

6. Shell Programming and Scripting

How to answer the Questions in Perl .

I need to write a script to telnet or ssh a device the execute some commands in device then copy the output in a file, I wrote the script but I faced one issue, when you execute some commands the device asked me a Question, for example : device # copy run tftp device # Source filename ? ... (3 Replies)
Discussion started by: DarkSoul
3 Replies

7. Shell Programming and Scripting

Perl questions - more

More questions for Perl on Windows (again I apologize its on windows... ) 1. How can I check free disk space on a drive in windows using perl command in a script? 2. How can I check processes running using perl command (as I would normally be able to see in task manager for example) 3. I... (7 Replies)
Discussion started by: frustrated1
7 Replies

8. Shell Programming and Scripting

perl questions

New to perl and a few questions - usually used to shell scripting but have to write a script to check a few things on windows server and need help.. 1. Check for file missing in sequence.. ie. list of file in directory as follows: file0001.txt file0002.txt file0004.txt file0005.txt... (7 Replies)
Discussion started by: frustrated1
7 Replies

9. Shell Programming and Scripting

Perl: answering automatically to install questions

Hi everybody, I have been looking for an answer to this issue both on google and on the forum, but I couldn't find anything. please help me :eek: As part of an automated (in perl) install of Solaris 9, I would like to be able to answer automaticaly to the question the installer asks.... (2 Replies)
Discussion started by: zaap
2 Replies

10. Shell Programming and Scripting

bash and Perl interaction questions

hi. i´m working in bash and am trying to create a Perl daemon that controls bash´s behavior. this is actually in preparation for a later project i´ll be working on. basically, i´m looking for a way to have the Perl daemon tell bash what to do. i already have a small daemon that simply prints... (2 Replies)
Discussion started by: deryk
2 Replies
Login or Register to Ask a Question