Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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?
[holli]: you know, monk days are fun and all. but too many of them are bad for your health
[1nickt]: ...modern car. Suggests using TLA+ or other software modeling language to autogenerate code.
1nickt is not sure more and more layers separating the programmer from the program is the right way to go...
[holli]: happy one, shmem ;)
[1nickt]: I had an interview couople of weeks ago, heard from a principal engineer that a friend of his who works at The Google told him that the self-teaching software that determines the dynamic pricing for their adverts...
[1nickt]: ... has become so complex that there is no one at the Goole who knows what it does.
[marto]: William Gibson was right
[moritz]: a classical problem with machine learning
[1nickt]: moritz isn;t the difference today that the machine learning algos are in charge of most stuffs?
[moritz]: depends on what you mean by "most stuff"

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (13)
As of 2017-11-21 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (301 votes). Check out past polls.

    Notices?