Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Hashsweeper, my first Perl program

by ChOas (Curate)
on Nov 13, 2000 at 17:10 UTC ( #41289=CUFP: print w/replies, xml ) Need Help??

Hi ;))

I was just browsing some of my old directories here,
and found my first 'do-it-myself-before-I-make-good-use-
of-Perl-program' ...

Its hashweeper, written the day after my two day perl
course, oh, sweet memories

Usage: X Y <ENTER></UL> X Y M <ENTER> for marking a hash(mine;))) or again for de-markin +g ;))<BR>
For your enjoyement, including the original debugging info
#!/usr/local/bin/perl -w use strict; sub CleanBoard($$); my @Board; for(my $i=0;$i<=21;++$i) { for(my $j=0;$j<=21;++$j) { $Board[$i][$j]={'Public'=>'*','Private'=>0}; }; }; sub InitBoard() { for(my $i=0;$i<50;++$i) { my($RandX,$RandY)=(rand(20)+1,rand(20)+1); redo if ($Board[$RandX][$RandY]{'Private'}==9); $Board[$RandX][$RandY]{'Private'}=9; }; for(my $j=1;$j<=20;++$j) { for(my $i=1;$i<=20;++$i) { next if ($Board[$i][$j]{'Private'}==9); for(my $y=$j-1;$y<=$j+1;++$y) { for(my $x=$i-1;$x<=$i+1;++$x) { ++$Board[$i][$j]{'Private'} if ($Board[$x][$y]{'Private'}==9); }; }; }; }; }; sub PrintBoard() { for(my $i=1;$i<=20;++$i) { print "\n$i\t"; for(my $j=1;$j<=20;++$j) { print "$Board[$i][$j]{'Public'} "; }; }; print "\nPlease give X and Y: "; }; sub CleanBoard($$) { my($x,$y)=@_; return if($Board[$x][$y]{'Public'} eq 'M'); return if(($x<1)||($x>20)||($y<1)||($y>20)); return if($Board[$x][$y]{'Private'}==9); return if($Board[$x][$y]{'Public'} ne '*'); $Board[$x][$y]{'Public'}=$Board[$x][$y]{'Private'}; return if($Board[$x][$y]{'Private'}!=0); print "I'm now in CleanBoard with X: $x, and Y: $y\n"; CleanBoard($x-1,$y); CleanBoard($x+1,$y); CleanBoard($x,$y-1); CleanBoard($x,$y+1); CleanBoard($x-1,$y-1); CleanBoard($x-1,$y+1); CleanBoard($x+1,$y-1); CleanBoard($x+1,$y+1); }; sub PlayGame() { while(<>) { chomp; my($y,$x,$Mark)=split; exit if($y eq "q"); if (($Mark)&&(lc($Mark) eq 'm')) { print "Got a mark!\n"; if ($Board[$x][$y]{'Public'} eq '*') { print "It even is a good one!!!\n"; $Board[$x][$y]{'Public'}='M'; } elsif ($Board[$x][$y]{'Public'} eq 'M') { $Board[$x][$y]{'Public'}='*'; }; PrintBoard(); next; }; if ($Board[$x][$y]{'Private'}==9) { print "BOOOOOOOOM!!!!!!!\n"; exit; }; CleanBoard($x,$y); PrintBoard(); }; }; InitBoard(); PrintBoard(); PlayGame();

Oh, don't worry... I got better at Perl ;)))

Rgrds,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://41289]
Approved by root
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-06-18 06:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (109 votes). Check out past polls.

    Notices?