App-SweeperBot

 view release on metacpan or  search on metacpan

lib/App/SweeperBot.pm  view on Meta::CPAN


=cut

sub flag_mines {
	my ($this, $game_state, @flag_these) = @_;

	foreach my $square (@flag_these) {
		my ($x,$y) = @$square;

		# Skip to the next square if we have record that this
		# has already been flagged (earlier this iteration).
		next if $game_state->[$x][$y] eq "flag";

		$this->press($x,$y,"{RIGHTCLICK}");
		$game_state->[$x][$y] = "flag";
	}

	return;
}

=begin deprecated

# This code is left here as a mathom, but isn't used anymore.
# Generally we want to call flag_mines() to flag mines, or
# stomp() to stomp on a square.

sub mark_adjacent {
	my ($this, $x, $y) = @_;
	$this->press($x-1,$y-1,"{RIGHTCLICK}");
	$this->press($x  ,$y-1,"{RIGHTCLICK}");
	$this->press($x+1,$y-1,"{RIGHTCLICK}");

	$this->press($x-1,$y  ,"{RIGHTCLICK}");
	$this->press($x+1,$y  ,"{RIGHTCLICK}");

	$this->press($x-1,$y+1,"{RIGHTCLICK}");
	$this->press($x  ,$y+1,"{RIGHTCLICK}");
	$this->press($x+1,$y+1,"{RIGHTCLICK}");

}

=end deprecated

=head2 game_over

	if (my $state = $sweeperbot->game_over) {
		print $state > 0 ? "We won!\n" : "We lost!\n";
	}

Checks to see if the game is over by looking at the minesweeper smiley.
Returns C<1> for game over due to a win, C<-1> for game over due to
a loss, and false if the game has not finished.

=cut

# Is the game over (we hit a mine)? 
# Returns -1 if game is over and we lost, 0 if not over, 1 if over and we won
sub game_over {
    # Capture game button and determine its sig
    # Game button is always at (x,56). X-value must be determined by 
    # calculation using formula: x=w/2-11
    # Size is 26x26
    our($l,$t,$w);

    # If we don't know where our smiley lives, then go find it.
    if (not $Smiley_offset) {
        for (my $i = MINIMUM_TOP_DRESSING; $i <= MAXIMUM_TOP_DRESSING; $i++) {

	    $Smiley_offset = $i;

            warn "Searching $Smiley_offset pixels down for smiley\n" if DEBUG;

	    my $smiley = CaptureRect(
		$l+$w/2 - 11,
		$Smiley_offset + $t,
		SMILEY_LENGTH,
		SMILEY_LENGTH,
	    );

            my $sig = $smiley->Get('signature');

	    if (exists $smiley_type{$sig}) {
		return $smiley_type{$sig};
	    }
	}

	# Oh no!  We couldn't find our smiley!

	die "Smiley not found on gameboard!\n";
    }

    # my $smiley=CaptureRect($l+$w/2-11,$t+56,26,26);
    # my $smiley=CaptureRect($l+$w/2-11, $t+64, SMILEY_LENGTH, SMILEY_LENGTH);
    # my $smiley=CaptureRect($l+$w/2-11,$t+75,26,26);

    my $smiley = CaptureRect(
	$l+$w/2 - 11,
	$Smiley_offset + $t,
	SMILEY_LENGTH,
	SMILEY_LENGTH,
    );


    my $sig = $smiley->Get("signature");

    if (exists $smiley_type{$sig}) {
	return $smiley_type{$sig};
    }

    die "I don't know what the smiley means\n$sig\n";

}

=head2 make_move


	$sweeperbot->make_move($game_state);

Given a game state, determines the next move(s) that should be made,
and makes them.  By default this uses a very simple process:



( run in 0.983 second using v1.01-cache-2.11-cpan-df04353d9ac )