I have always been a minesweeper junky... I played for 1/2 an hour before looking at your code <G>

I think you have already partially solved this problem with your mine placement magic, walk through the grid and use the logic you used for incrementing the number of mines next to a particular location to see what the surrounding values are...

some comments on the rest of your code if I may :)
that format is waaaaay ugly :) you could replace it with a sub like this-
sub draw_grid { print "# Wacky Fun Minesweeper!!!!\n"; for my $x (0..9) { print $x, ' '; for my $y (1..9) { if ($x == 0){ print $y, ' '; } else { print $blankgrid[$x][$y], ' '; } } print "\n"; } }

now instead of using write call draw_grid();

you can initialise both grids in the one double loop- (for modularity I would place the grid initialisation and the mine placement code in subroutines as well)

for my $x (1..9) { for my $y (1..9) { $blankgrid[$x][$y] = "_"; $grid[$x][$y] = 0; } }

your mainloop has a variable $minehit that you never use and I would replace $empty with something like $untested_squares so your main while loop becomes while ($untested_squares) { which IMO is nicer to read :)

Further optimisation left to the monastry gurus... I am going to go play some more :-)

--
my $chainsaw = 'Perl';


In reply to Re: Need help with a minesweeper game by greenFox
in thread Need help with a minesweeper game by PsionicMan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.