Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I read On Interviewing Candidates and Games in Perl so i felt it would be interesting to try to code a simple MineSweeper.
#!/usr/bin/perl # Usage: ./mine.pl [dimension] + # # enter coordinates to check like "5 6" (y coordinate first) + # #if you want to place a flag where you think there is a mine use "x 3 +4" # use strict;
my $dimension = $ARGV[0] || 10; my ($count, %old, %mines); my $fill_char = '.'; my @row = map {$fill_char} 1..$dimension; my @matrix = map {[@row]} @row; my @control = ([-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1] ); sub print_matrix { my $i; unshift @_, [map {$_} 1..$dimension]; for (@_) { my @row = @$_; unshift @row, $i++ || ' '; for (@row) { printf("%2s ", $_) ; } print $/; } } sub initialize_fields { my $i; while ($i < $dimension) { my ($x, $y) = map {int rand $dimension} 1..2; my $mine = join ' ', $x, $y; $mines{$mine}++ || $i++; } } sub check_square { die "BOOOOMMMM!!!\n" if is_bomb( @_ ); cq( @_ ); } sub cq { my ($x, $y) = @_; return unless $matrix[$x][$y] eq $fill_char; my $value = value( @_ ); $matrix[$x][$y] = $value; unless ( $value ) { for ( @control ) { @_ = @$_; my @nc = ($x + $_[0], $y + $_[1]); next if out_of_range( @nc ); next if $matrix[ $nc[0] ][ $nc[1] ] ne $fill_char; cq( @nc ); } } $count++; } sub value { my ($x, $y) = @_; my $value; for ( @control ) { @_ = @$_; my @nc = ($x + $_[0], $y + $_[1]); next if out_of_range( @nc ); $value += is_bomb( @nc ); } return $value; } sub is_bomb { my $mine = join ' ', @_; return $mines{$mine} ? 1 : 0; } sub out_of_range { return grep { $_ < 0 or $_ >= $dimension } @_; } initialize_fields; while (1) { print_matrix(@matrix); print "YOU WON!!!\n" and exit if $count >= ($dimension ** 2 - $dim +ension); print "Insert (y, x) = "; $_ = <STDIN>; print $/; my ($command, $x, $y) = /^(x)?\s*(\d+)\D+(\d+)/i; $x--; $y--; print "x or y out of range!!!\n\n" and next if out_of_range($x, $y +); if ( $command ) { if ( $matrix[$x][$y] eq 'X' ) { my $mine = join ' ', $x, $y; $matrix[$x][$y] = $old{$mine}; } else { my $mine = join ' ', $x, $y; $old{$mine} = $matrix[$x][$y]; $matrix[$x][$y] = 'X'; } } else { check_square($x, $y); } }
Update: fixed exploitable bug.
Update (20020117): cuter command parsing.

$|=$_='1g2i1u1l2i4e2n0k',map{print"\7",chop;select$,,$,,$,,$_/7}m{..}g

In reply to YAMineSweeper by giulienk

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (7)
    As of 2014-11-22 21:30 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (125 votes), past polls