| 1 | package Games::Minesweeper; | ||
| 2 | |||
| 3 | # http://txt.hello-penguin.com/6c206c05b150b767d55feb966a7654f6.txt | ||
| 4 | 1 | ▶ | BEGIN { $ENV{PERL_DL_NONLAZY} = 0; } |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use SDL (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use SDL::Mixer (); | ||
| 9 | use Gtk2 (); | ||
| 10 | use Gtk2::SimpleMenu (); | ||
| 11 | use AnyEvent (); | ||
| 12 | use File::HomeDir (); | ||
| 13 | |||
| 14 | use Data::Dumper; | ||
| 15 | |||
| 16 | our $VERSION = "0.5"; | ||
| 17 | our $custom_fix = 0; | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | Games::Minesweeper - another Minesweeper clone... | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | ################################################################################### | ||
| 26 | # do things only needed for single-binary version (par) | ||
| 27 | BEGIN { | ||
| 28 | if (%PAR::LibCache) { | ||
| 29 | @INC = grep ref, @INC; # weed out all paths except pars loader refs | ||
| 30 | |||
| 31 | my $root = $ENV{PAR_TEMP}; | ||
| 32 | |||
| 33 | while (my ($filename, $zip) = each %PAR::LibCache) { | ||
| 34 | for ($zip->memberNames) { | ||
| 35 | next unless /^root\/(.*)/; | ||
| 36 | $zip->extractMember ($_, "$root/$1") | ||
| 37 | unless -e "$root/$1"; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | unshift @INC, $root; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | BEGIN { | ||
| 46 | $ENV{GTK_RC_FILES} = "$ENV{PAR_TEMP}/share/themes/MS-Windows/gtk-2.0/gtkrc" | ||
| 47 | if %PAR::LibCache && $^O eq "MSWin32"; | ||
| 48 | } | ||
| 49 | |||
| 50 | unshift @INC, $ENV{PAR_TEMP}; | ||
| 51 | ################################################################################### | ||
| 52 | |||
| 53 | |||
| 54 | $SIG{CHLD} = 'IGNORE'; | ||
| 55 | |||
| 56 | my $frame; | ||
| 57 | my $watcher; | ||
| 58 | my ($l, $d, $w); | ||
| 59 | my ($mine, $mine_red, $mine_wrong, $mine_hidden, $mine_flag, @m); | ||
| 60 | my ($smiley_img, $smiley_happy_img, $smiley_ohno_img, $smiley_stress_img); | ||
| 61 | my ($smiley); | ||
| 62 | my ($field_width, $field_height, $field_mines) = (9, 9, 10); | ||
| 63 | my ($tile_width, $tile_height) = (16, 16); | ||
| 64 | my @mine_field; | ||
| 65 | my ($mine_count, $open); | ||
| 66 | my $audio = 0; | ||
| 67 | my $mc; | ||
| 68 | my $game_over = 0; | ||
| 69 | my $menu; | ||
| 70 | |||
| 71 | sub save_prefs () { | ||
| 72 | my $hd = my_home File::HomeDir; | ||
| 73 | my $rcfile = "$hd/.minesweeperrc\0"; | ||
| 74 | my $fh; | ||
| 75 | open $fh, ">", $rcfile | ||
| 76 | or do { warn "can't create $rcfile: $!\n"; return; }; | ||
| 77 | print $fh "$field_width $field_height $field_mines $audio\n"; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub load_prefs () { | ||
| 81 | my $hd = my_home File::HomeDir; | ||
| 82 | my $rcfile = "$hd/.minesweeperrc\0"; | ||
| 83 | my $fh; | ||
| 84 | open $fh, "<", $rcfile | ||
| 85 | or do { warn "can't open $rcfile: $!"; return; }; | ||
| 86 | my $line = <$fh>; | ||
| 87 | if(my ($w,$h, $m, $a) = $line =~ m/^(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/) { | ||
| 88 | $audio = !!$a; | ||
| 89 | $menu->get_widget ('/Game/Audio')->set_active ($audio); | ||
| 90 | $w = 9 if $w < 9; | ||
| 91 | $h = 9 if $h < 9; | ||
| 92 | $m = 3 if $m < 3; | ||
| 93 | ($field_width, $field_height, $field_mines) = ($w, $h, $m); | ||
| 94 | { | ||
| 95 | local $custom_fix = 1; | ||
| 96 | $menu->get_widget ('/Game/Custom...')->set_active (1); #d# | ||
| 97 | } | ||
| 98 | |||
| 99 | $menu->get_widget ('/Game/Beginner')->set_active (1) if $w == 9 && $h == 9 && $m == 10; | ||
| 100 | $menu->get_widget ('/Game/Intermediate')->set_active (1) if $w == 16 && $h == 16 && $m == 40; | ||
| 101 | $menu->get_widget ('/Game/Expert')->set_active (1) if $w == 30 && $h == 16 && $m == 99; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | sub IS_MINE () { 1 } | ||
| 106 | sub IS_OPEN () { 2 } | ||
| 107 | sub IS_FLAGGED () { 4 } | ||
| 108 | |||
| 109 | sub init_field() { | ||
| 110 | @mine_field = (); | ||
| 111 | for my $x (0..$field_width-1) { | ||
| 112 | for my $y (0..$field_height-1) { | ||
| 113 | $mine_field[$x][$y] = 0; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | my $cnt = 0; | ||
| 117 | while($cnt < $field_mines) { | ||
| 118 | my $x = int rand ($field_width); | ||
| 119 | my $y = int rand ($field_height); | ||
| 120 | if (!$mine_field[$x][$y]) { | ||
| 121 | $mine_field[$x][$y] = IS_MINE; | ||
| 122 | $cnt++; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | ## | ||
| 128 | ## 123 x-> | ||
| 129 | ## 4*5 y | ||
| 130 | ## 678 | | ||
| 131 | ## | ||
| 132 | sub count_mines ($$) { | ||
| 133 | my ($x, $y) = @_; | ||
| 134 | my $cnt = 0; | ||
| 135 | $cnt += $mine_field[$x-1][$y-1] & IS_MINE if $x > 0 && $y > 0; # 1 | ||
| 136 | $cnt += $mine_field[$x][$y-1] & IS_MINE if $y > 0; # 2 | ||
| 137 | $cnt += $mine_field[$x+1][$y-1] & IS_MINE if $x < $field_width -1 && $y >0; # 3 | ||
| 138 | $cnt += $mine_field[$x-1][$y] & IS_MINE if $x >0; # 4 | ||
| 139 | $cnt += $mine_field[$x+1][$y] & IS_MINE if $x < $field_width - 1; # 5 | ||
| 140 | $cnt += $mine_field[$x-1][$y+1] & IS_MINE if $x > 0 && $y < $field_height -1; # 6 | ||
| 141 | $cnt += $mine_field[$x][$y+1] & IS_MINE if $y < $field_height -1; # 7 | ||
| 142 | $cnt += $mine_field[$x+1][$y+1] & IS_MINE if $x < $field_width - 1 && $y < $field_height -1; # 8 | ||
| 143 | $cnt; | ||
| 144 | } | ||
| 145 | |||
| 146 | # find a data file using @INC | ||
| 147 | sub findfile { | ||
| 148 | my @files = @_; | ||
| 149 | file: | ||
| 150 | for (@files) { | ||
| 151 | for my $prefix (@INC) { | ||
| 152 | if (-f "$prefix/$_") { | ||
| 153 | $_ = "$prefix/$_"; | ||
| 154 | next file; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | die "$_: file not found in \@INC\n"; | ||
| 158 | } | ||
| 159 | wantarray ? @files : $files[0]; | ||
| 160 | } | ||
| 161 | |||
| 162 | my %sound; | ||
| 163 | my $mixer; | ||
| 164 | |||
| 165 | sub init_sound() { | ||
| 166 | $mixer = eval { SDL::Mixer->new(-frequency => 44100, -channels => 2, -size => 1024); }; | ||
| 167 | if ($@) { | ||
| 168 | warn "init_sound: $@"; | ||
| 169 | $audio = 0; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | sub load_sounds () { | ||
| 174 | init_sound; | ||
| 175 | return unless $audio; | ||
| 176 | for (qw/mouse_press mouse_release game_over win/) { | ||
| 177 | my $file; | ||
| 178 | $file = findfile "Games/Minesweeper/sounds/$_.wav" | ||
| 179 | or die "findfile $file failed: $!"; | ||
| 180 | $sound{$_} = new SDL::Sound ($file); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | sub play ($) { | ||
| 185 | my $snd = shift; | ||
| 186 | load_sounds unless exists $sound{$snd}; | ||
| 187 | $mixer->play_channel(-1, $sound{$snd}, 0); | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | sub about_dialog () { | ||
| 192 | show_about_dialog Gtk2 ($w, "program-name" => 'Minesweeper', | ||
| 193 | authors => [ 'Stefan Traby', ], | ||
| 194 | license => "This package is distributed under the same license as perl itself, i.e.\n". | ||
| 195 | "either the Artistic License (COPYING.Artistic) or the GPLv2 (COPYING.GNU).", | ||
| 196 | copyright => "(c) 2008 by St.Traby <stefan\@hello-penguin.com>", | ||
| 197 | website => 'http://oesiman.de', | ||
| 198 | version => "v$VERSION", | ||
| 199 | comments => "SDL version", | ||
| 200 | artists => [ "Andreas Zehender" ], | ||
| 201 | ); | ||
| 202 | 1; | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | sub custom_dialog () { | ||
| 207 | return if $custom_fix; | ||
| 208 | my $q = [ [ "Height:", $field_height ], | ||
| 209 | [ "Width:", $field_width ], | ||
| 210 | [ "Mines:", $field_mines ], | ||
| 211 | ]; | ||
| 212 | my $dialog = new Gtk2::Dialog("Customize", $w, 'modal', 'gtk-cancel' => 'cancel', OK => 'ok'); | ||
| 213 | $dialog->set_default_response ('ok'); | ||
| 214 | my @e; | ||
| 215 | for my $i (0..2) { | ||
| 216 | my $hb = new Gtk2::HBox; | ||
| 217 | my $l = new Gtk2::Label ($q->[$i][0]); | ||
| 218 | $e[$i] = new Gtk2::Entry; | ||
| 219 | $e[$i]->set_text ($q->[$i][1]); | ||
| 220 | $hb->add ($l); | ||
| 221 | $hb->add ($e[$i]); | ||
| 222 | $dialog->vbox->add ($hb); | ||
| 223 | } | ||
| 224 | $dialog->show_all; | ||
| 225 | my $response = $dialog->run; | ||
| 226 | $dialog->destroy; | ||
| 227 | return 1 unless $response eq "ok"; | ||
| 228 | my ($h, $w, $m) = map +($e[$_]->get_text), (0..2); | ||
| 229 | |||
| 230 | return if $h < 9 || $w < 9 || $m > $h*$w-10; | ||
| 231 | ($field_width, $field_height, $field_mines) = ($w, $h, $m); | ||
| 232 | restart(); | ||
| 233 | } | ||
| 234 | |||
| 235 | |||
| 236 | sub full_expose() { | ||
| 237 | my $update_rect = new Gtk2::Gdk::Rectangle (0, 0, $field_width*$tile_width, $field_height*$tile_height); | ||
| 238 | $d->window->invalidate_rect ($update_rect, 0); | ||
| 239 | } | ||
| 240 | |||
| 241 | sub draw_xy($$$;$) { | ||
| 242 | my ($x, $y, $img, $expose) = @_; | ||
| 243 | $img->copy_area(0, 0, $tile_width, $tile_height, $frame, $x*$tile_width, $y*$tile_height); | ||
| 244 | if ($expose) { | ||
| 245 | my $update_rect = new Gtk2::Gdk::Rectangle ($x*$tile_width, $y*$tile_height, $tile_width, $tile_height); | ||
| 246 | $d->window->invalidate_rect ($update_rect, 0); | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | sub open_all() { | ||
| 251 | for my $x (0..$field_width-1) { | ||
| 252 | for my $y (0..$field_height-1) { | ||
| 253 | my $f = $mine_field[$x][$y]; | ||
| 254 | next unless $f; | ||
| 255 | |||
| 256 | if ($f & IS_MINE) { | ||
| 257 | if ($f & IS_FLAGGED) { | ||
| 258 | draw_xy ($x, $y, $mine_flag, 0) | ||
| 259 | } else { # mine not flagged | ||
| 260 | if ($f & IS_OPEN) { | ||
| 261 | draw_xy ($x, $y, $mine_red, 0); | ||
| 262 | } else { | ||
| 263 | draw_xy ($x, $y, $mine, 0) | ||
| 264 | } | ||
| 265 | } | ||
| 266 | } else { # not a mine but open or flagged | ||
| 267 | draw_xy ($x, $y, $mine_wrong, 0) if $f & IS_FLAGGED; | ||
| 268 | } | ||
| 269 | } | ||
| 270 | } | ||
| 271 | full_expose; | ||
| 272 | } | ||
| 273 | |||
| 274 | |||
| 275 | sub cleanup_cb { | ||
| 276 | undef $watcher; | ||
| 277 | } | ||
| 278 | |||
| 279 | my $timer = 0; | ||
| 280 | sub timeout () { | ||
| 281 | $l->set_text(sprintf "%.4d ", ++$timer); | ||
| 282 | 1; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub stop_timer () { | ||
| 286 | undef $watcher; | ||
| 287 | $timer; | ||
| 288 | } | ||
| 289 | |||
| 290 | sub start_timer () { | ||
| 291 | $timer = 0; | ||
| 292 | $watcher = AnyEvent->timer (after => 1.0, interval => 1, cb => sub { timeout; }); | ||
| 293 | } | ||
| 294 | |||
| 295 | sub update_mine_count() { | ||
| 296 | $mc->set_text ( sprintf " %.3d", $mine_count); | ||
| 297 | } | ||
| 298 | |||
| 299 | sub expose_cb { | ||
| 300 | my ($w, $e) = @_; | ||
| 301 | #warn "expose: ".$e->area->x." ". $e->area->y." ".$e->area->width." ".$e->area->height; | ||
| 302 | $frame->render_to_drawable ($w->window, $w->style->black_gc, | ||
| 303 | $e->area->x, $e->area->y, | ||
| 304 | $e->area->x, $e->area->y, | ||
| 305 | $e->area->width, $e->area->height, | ||
| 306 | 'normal', | ||
| 307 | $e->area->x, $e->area->y); | ||
| 308 | 1; | ||
| 309 | } | ||
| 310 | |||
| 311 | |||
| 312 | sub around(&$$;$) { | ||
| 313 | my ($func, $x, $y, $data) = @_; | ||
| 314 | my $ret; | ||
| 315 | $ret = $func->($x-1, $y-1, $data) if $x > 0 && $y > 0; | ||
| 316 | $ret |= $func->($x, $y-1, $data) if $y > 0; | ||
| 317 | $ret |= $func->($x+1, $y-1, $data) if $x < $field_width -1 && $y >0; | ||
| 318 | $ret |= $func->($x-1, $y, $data) if $x >0; | ||
| 319 | $ret |= $func->($x+1, $y, $data) if $x < $field_width - 1; | ||
| 320 | $ret |= $func->($x-1, $y+1, $data) if $x > 0 && $y < $field_height -1; | ||
| 321 | $ret |= $func->($x, $y+1, $data) if $y < $field_height -1; | ||
| 322 | $ret |= $func->($x+1, $y+1, $data) if $x < $field_width - 1 && $y < $field_height -1; | ||
| 323 | $ret; | ||
| 324 | } | ||
| 325 | |||
| 326 | my @event; | ||
| 327 | my @undo; | ||
| 328 | |||
| 329 | sub button_press_cb { | ||
| 330 | return 1 if $game_over; | ||
| 331 | my ($w, $e) = @_; | ||
| 332 | play("mouse_press") if $audio; | ||
| 333 | my ($x, $y, $b) = (int $e->x / $tile_width, int $e->y / $tile_height, $e->button); | ||
| 334 | $event[$b] = [ $x, $y ]; | ||
| 335 | #warn "press x=$x y=$y b=$b mc=".count_mines($x, $y)."is_mine=".($mine_field[$x][$y] & IS_MINE)."\n"; | ||
| 336 | if($b == 3) { | ||
| 337 | return 1 if $mine_field[$x][$y] & IS_OPEN; | ||
| 338 | if ($mine_field[$x][$y] & IS_FLAGGED) { | ||
| 339 | $mine_field[$x][$y] &= ~IS_FLAGGED; | ||
| 340 | $mine_count++; | ||
| 341 | draw_xy ($x, $y, $mine_hidden, 1); | ||
| 342 | } else { | ||
| 343 | $mine_field[$x][$y] |= IS_FLAGGED; | ||
| 344 | $mine_count--; | ||
| 345 | draw_xy ($x, $y, $mine_flag, 1); | ||
| 346 | } | ||
| 347 | update_mine_count; | ||
| 348 | |||
| 349 | } elsif ($b == 2) { | ||
| 350 | $smiley->set_image ($smiley_stress_img); | ||
| 351 | @undo = (); | ||
| 352 | return 1 unless $mine_field[$x][$y] & IS_OPEN; | ||
| 353 | around ( sub { | ||
| 354 | my ($x, $y) = @_; | ||
| 355 | if ($mine_field[$x][$y] < 2) { # empty or nonflagged | ||
| 356 | draw_xy ($x, $y, $m[0], 1); | ||
| 357 | push @undo, sub { draw_xy ($x, $y, $mine_hidden, 1); }; | ||
| 358 | } | ||
| 359 | }, $x, $y); | ||
| 360 | } elsif ($b == 1) { | ||
| 361 | return 1 if $mine_field[$x][$y] & (IS_OPEN | IS_FLAGGED); | ||
| 362 | $smiley->set_image ($smiley_stress_img); | ||
| 363 | draw_xy ($x, $y, $m[0], 1); | ||
| 364 | if (!$watcher && $mine_field[$x][$y] & IS_MINE) { # first open field is not mine... | ||
| 365 | for(;;) { | ||
| 366 | my $nx = int rand ($field_width); | ||
| 367 | my $ny = int rand ($field_height); | ||
| 368 | if (!$mine_field[$nx][$ny]) { | ||
| 369 | $mine_field[$nx][$ny] = IS_MINE; | ||
| 370 | $mine_field[$x][$y] = 0; | ||
| 371 | last; | ||
| 372 | } | ||
| 373 | } | ||
| 374 | } | ||
| 375 | } | ||
| 376 | $watcher || start_timer; | ||
| 377 | 1; | ||
| 378 | } | ||
| 379 | |||
| 380 | |||
| 381 | |||
| 382 | my %visited; | ||
| 383 | |||
| 384 | sub deep_open2($$); | ||
| 385 | |||
| 386 | sub deep_open2 ($$) { | ||
| 387 | my ($x, $y) = @_; | ||
| 388 | $visited{$x,$y} = 1; | ||
| 389 | my $cnt = count_mines ($x, $y); | ||
| 390 | if (!$mine_field[$x][$y]) { # don't touch open fields and set mines... | ||
| 391 | $mine_field[$x][$y] = IS_OPEN; | ||
| 392 | draw_xy ($x, $y, $m[$cnt], 1); | ||
| 393 | $open++; | ||
| 394 | } | ||
| 395 | return if $cnt; | ||
| 396 | deep_open2 ($x+1, $y) if !$visited{$x+1,$y} && $x < $field_width -1; | ||
| 397 | deep_open2 ($x, $y+1) if !$visited{$x,$y+1} && $y < $field_height -1; | ||
| 398 | deep_open2 ($x-1, $y) if !$visited{$x-1,$y} && $x > 0; | ||
| 399 | deep_open2 ($x, $y-1) if !$visited{$x,$y-1} && $y > 0; | ||
| 400 | } | ||
| 401 | |||
| 402 | sub deep_open ($$) { | ||
| 403 | my ($x, $y) = @_; | ||
| 404 | %visited = (); | ||
| 405 | deep_open2 ($x, $y); | ||
| 406 | |||
| 407 | } | ||
| 408 | |||
| 409 | sub button_release_cb { | ||
| 410 | return 1 if $game_over; | ||
| 411 | my ($w, $e) = @_; | ||
| 412 | my ($x,$y, $b) = (int $e->x / $tile_width, int $e->y / $tile_height, $e->button); | ||
| 413 | #warn "release x=$x y=$y b=$b\n"; | ||
| 414 | |||
| 415 | play("mouse_release") if $audio; | ||
| 416 | |||
| 417 | # check if its the same tile else return... | ||
| 418 | if ($x != $event[$b][0] || $y != $event[$b][1]) { | ||
| 419 | draw_xy ($event[$b][0], $event[$b][1], $mine_hidden, 1) if $b == 1 && !$mine_field[$event[$b][0]][$event[$b][1]]; | ||
| 420 | if ($b == 2) { | ||
| 421 | $_->() for(@undo); | ||
| 422 | } | ||
| 423 | $smiley->set_image ($smiley_img); | ||
| 424 | return 1; | ||
| 425 | } | ||
| 426 | |||
| 427 | if ($b == 1) { | ||
| 428 | if ($mine_field[$x][$y] & IS_MINE) { | ||
| 429 | $mine_field[$x][$y] |= IS_OPEN; | ||
| 430 | stop_timer; | ||
| 431 | $game_over = 1; | ||
| 432 | $smiley->set_image ($smiley_ohno_img); | ||
| 433 | play ("game_over") if $audio; | ||
| 434 | open_all; | ||
| 435 | return 1; | ||
| 436 | } | ||
| 437 | deep_open ($x, $y); | ||
| 438 | $smiley->set_image ($smiley_img); | ||
| 439 | } elsif ($b == 2) { | ||
| 440 | return 1 unless @undo; | ||
| 441 | my $err = around (sub { | ||
| 442 | my ($x, $y) = @_; | ||
| 443 | my $m = $mine_field[$x][$y]; | ||
| 444 | return 0 unless $m; | ||
| 445 | if ($m & IS_MINE) { | ||
| 446 | return 0 if $m & IS_FLAGGED; | ||
| 447 | return 1; | ||
| 448 | } else { | ||
| 449 | return 1 if $m & IS_FLAGGED; | ||
| 450 | return 0; | ||
| 451 | } | ||
| 452 | }, $x, $y | ||
| 453 | ); | ||
| 454 | if ($err) { | ||
| 455 | around (sub { $mine_field[$_[0]][$_[1]] |= IS_OPEN; }, $x, $y); | ||
| 456 | stop_timer; | ||
| 457 | $game_over = 1; | ||
| 458 | $smiley->set_image ($smiley_ohno_img); | ||
| 459 | play ("game_over") if $audio; | ||
| 460 | open_all; | ||
| 461 | return 1; | ||
| 462 | } else { | ||
| 463 | around (sub { deep_open ($_[0], $_[1]) }, $x, $y); | ||
| 464 | } | ||
| 465 | |||
| 466 | } elsif ($b == 3) { | ||
| 467 | } | ||
| 468 | # check if solved... | ||
| 469 | if ($open == $field_width*$field_height-$field_mines) { | ||
| 470 | # we are finished, maybe not all mines are open. | ||
| 471 | for my $x (0..$field_width-1) { | ||
| 472 | for my $y (0..$field_height-1) { | ||
| 473 | $mine_field[$x][$y] |= IS_FLAGGED if $mine_field[$x][$y] & IS_MINE; | ||
| 474 | } | ||
| 475 | } | ||
| 476 | stop_timer; | ||
| 477 | $mine_count = 0; | ||
| 478 | update_mine_count; | ||
| 479 | play ('win') if $audio; | ||
| 480 | open_all; | ||
| 481 | $smiley->set_image ($smiley_happy_img); | ||
| 482 | $game_over = 1; | ||
| 483 | return 1; | ||
| 484 | } | ||
| 485 | |||
| 486 | $watcher or start_timer; | ||
| 487 | 1; | ||
| 488 | } | ||
| 489 | |||
| 490 | sub load_image { | ||
| 491 | my $path = findfile $_[0]; | ||
| 492 | new_from_file Gtk2::Image $path | ||
| 493 | or die "$path: $!"; | ||
| 494 | } | ||
| 495 | |||
| 496 | sub load_pixbuf { | ||
| 497 | my $path = findfile $_[0]; | ||
| 498 | new_from_file Gtk2::Gdk::Pixbuf $path | ||
| 499 | or die "$path: $!"; | ||
| 500 | } | ||
| 501 | |||
| 502 | sub load_images { | ||
| 503 | $mine = load_pixbuf "Games/Minesweeper/images/mine.png"; | ||
| 504 | $mine_wrong = load_pixbuf "Games/Minesweeper/images/mine-wrong.png"; | ||
| 505 | $mine_hidden = load_pixbuf "Games/Minesweeper/images/mine-hidden.png"; | ||
| 506 | $mine_flag = load_pixbuf "Games/Minesweeper/images/mine-flag.png"; | ||
| 507 | $mine_red = load_pixbuf "Games/Minesweeper/images/mine-red.png"; | ||
| 508 | @m = map +(load_pixbuf "Games/Minesweeper/images/mine-$_.png"), (0..8); | ||
| 509 | |||
| 510 | $smiley_img = load_image "Games/Minesweeper/images/smile.png"; | ||
| 511 | $smiley_happy_img = load_image "Games/Minesweeper/images/smile_happy.png"; | ||
| 512 | $smiley_ohno_img = load_image "Games/Minesweeper/images/smile_ohno.png"; | ||
| 513 | $smiley_stress_img = load_image "Games/Minesweeper/images/smile_stress.png"; | ||
| 514 | } | ||
| 515 | |||
| 516 | |||
| 517 | sub restart () { | ||
| 518 | stop_timer; | ||
| 519 | $l->set_text ('0000 '); | ||
| 520 | init_field; | ||
| 521 | my ($bw, $bh) = ($field_width*$tile_width, $field_height*$tile_height); | ||
| 522 | $d->set_size_request($bw, $bh); | ||
| 523 | $frame = Gtk2::Gdk::Pixbuf->new ('rgb', 1, 8, $bw, $bh); | ||
| 524 | for my $x (0..$field_width-1) { | ||
| 525 | for my $y (0..$field_height-1) { | ||
| 526 | draw_xy ($x, $y, $mine_hidden, 0); | ||
| 527 | } | ||
| 528 | } | ||
| 529 | full_expose; | ||
| 530 | $mine_count = $field_mines; | ||
| 531 | update_mine_count; | ||
| 532 | $open = 0; | ||
| 533 | $smiley->set_image ($smiley_img); | ||
| 534 | $game_over = 0; | ||
| 535 | 1; | ||
| 536 | } | ||
| 537 | |||
| 538 | sub new_minesweeper () { | ||
| 539 | $mine or load_images; | ||
| 540 | $w = Gtk2::Window->new ('toplevel'); | ||
| 541 | $w->set_resizable (0); | ||
| 542 | my $v = new Gtk2::VBox; | ||
| 543 | my $f1 = new Gtk2::Frame; | ||
| 544 | my $f2 = new Gtk2::Frame; | ||
| 545 | $d = new Gtk2::DrawingArea; | ||
| 546 | $smiley = new Gtk2::Button; | ||
| 547 | #$smiley->set_relief ('none'); | ||
| 548 | #$smiley->set_alignment (0.5, 0.5); | ||
| 549 | $smiley->set_image ($smiley_img); | ||
| 550 | |||
| 551 | my $menu_tree = [ | ||
| 552 | _Game => { | ||
| 553 | item_type => '<Branch>', | ||
| 554 | children => [ | ||
| 555 | _New => { callback => sub { restart; }, | ||
| 556 | accelerator => 'F2', | ||
| 557 | }, | ||
| 558 | Separator => { item_type => '<Separator>', | ||
| 559 | }, | ||
| 560 | _Beginner => { callback => sub { return unless $menu->get_widget ("/Game/Beginner")->get_active; | ||
| 561 | ($field_width, $field_height, $field_mines) = (9, 9, 10); restart; }, | ||
| 562 | item_type => '<RadioItem>', | ||
| 563 | groupid => 1, | ||
| 564 | }, | ||
| 565 | _Intermediate => { callback => sub { return unless $menu->get_widget ("/Game/Intermediate")->get_active; | ||
| 566 | ($field_width, $field_height, $field_mines) = (16, 16, 40); restart; }, | ||
| 567 | item_type => '<RadioItem>', | ||
| 568 | groupid => 1, | ||
| 569 | }, | ||
| 570 | _Expert => { callback => sub { return unless $menu->get_widget ("/Game/Expert")->get_active; | ||
| 571 | ($field_width, $field_height, $field_mines) = (30, 16, 99); restart; }, | ||
| 572 | item_type => '<RadioItem>', | ||
| 573 | groupid => 1, | ||
| 574 | }, | ||
| 575 | '_Custom...' => { callback => sub { return unless $menu->get_widget ("/Game/Custom...")->get_active; | ||
| 576 | custom_dialog; }, | ||
| 577 | item_type => '<RadioItem>', | ||
| 578 | groupid => 1, | ||
| 579 | }, | ||
| 580 | Separator => { item_type => '<Separator>', | ||
| 581 | }, | ||
| 582 | _Audio => { callback => sub { $audio = 0 + $menu->get_widget ('/Game/Audio')->get_active; }, | ||
| 583 | item_type => '<CheckItem>', | ||
| 584 | }, | ||
| 585 | Separator => { item_type => '<Separator>', | ||
| 586 | }, | ||
| 587 | E_xit => { callback => sub { save_prefs; main_quit Gtk2; }, | ||
| 588 | accelerator => '<Alt>X', | ||
| 589 | }, | ||
| 590 | ], | ||
| 591 | }, | ||
| 592 | "_?" => { | ||
| 593 | item_type => '<Branch>', | ||
| 594 | children => [ | ||
| 595 | _About => { callback => sub { about_dialog; }, | ||
| 596 | accelerator => 'F1', | ||
| 597 | } | ||
| 598 | ], | ||
| 599 | }, | ||
| 600 | ]; | ||
| 601 | |||
| 602 | $menu = new Gtk2::SimpleMenu (menu_tree => $menu_tree, | ||
| 603 | ); | ||
| 604 | $l = new Gtk2::Label ('0000 '); | ||
| 605 | $mc = new Gtk2::Label (' 000'); | ||
| 606 | $smiley->signal_connect (clicked => sub { restart; }); | ||
| 607 | $d->set_events ([ 'button_release_mask', 'button_press_mask', ]); #'pointer_motion_mask' ]); | ||
| 608 | $d->signal_connect (expose_event => \&expose_cb); | ||
| 609 | $d->signal_connect (button_press_event => \&button_press_cb); | ||
| 610 | $d->signal_connect (button_release_event => \&button_release_cb); | ||
| 611 | $f2->set_border_width(5); | ||
| 612 | my $fixbox = new Gtk2::HBox; | ||
| 613 | my $fix1 = new Gtk2::Frame; | ||
| 614 | my $fix2 = new Gtk2::Frame; | ||
| 615 | $fix1->set_shadow_type ('none'); | ||
| 616 | $fix1->set_border_width (0); | ||
| 617 | $fix2->set_shadow_type ('none'); | ||
| 618 | $fix2->set_border_width (0); | ||
| 619 | $fixbox->pack_start ($fix1, 1, 1, 1); | ||
| 620 | $f2->add ($d); | ||
| 621 | $fixbox->pack_start ($f2, 0, 0, 0); | ||
| 622 | $fixbox->pack_start ($fix2, 1, 1, 1); | ||
| 623 | my $vb = new Gtk2::VBox; | ||
| 624 | my $hb = new Gtk2::HBox; | ||
| 625 | $hb->pack_start ($mc, 0, 0, 0); | ||
| 626 | $hb->pack_start ($smiley, 1, 0, 0); | ||
| 627 | $hb->pack_end ($l, 0, 0, 0); | ||
| 628 | $vb->add ($hb); | ||
| 629 | $vb->pack_start ($fixbox, 1, 0, 0); | ||
| 630 | $f1->add ($vb); | ||
| 631 | $v->add ($menu->{widget}); | ||
| 632 | $w->add_accel_group ($menu->{accel_group}); | ||
| 633 | $v->pack_start ($f1, 1, 0, 0); | ||
| 634 | $w->add ($v); | ||
| 635 | $w->signal_connect( destroy => sub { save_prefs; main_quit Gtk2; }); | ||
| 636 | $w->signal_connect( destroy => \&cleanup_cb); | ||
| 637 | $d->realize; | ||
| 638 | load_prefs; | ||
| 639 | restart; | ||
| 640 | $w; | ||
| 641 | } | ||
| 642 | 1; | ||