#!/bin/sh - # the next line restarts using wish \ exec wish "$0" "$@" ##### # The minesweeper game. ported to Tcl/Tk. # P Kern, pkern at utcc.utoronto.ca, 99/02/18 set usage " -beginner beginner level (8x8 40 mines). -intermediate medium level (16x16 60 mines). -expert expert level (30x16 99 mines). -mammoth huge board (56x40 396 mines). -x x dimension of board. -y y dimension of board. -mines number of mines. -ratio ratio of mines to board squares. -seed seed for random numbers." ##### # initial settings. bind . exit # standard parameters. set modes(beg,ident) Beginner set modes(beg,XSize) 8 set modes(beg,YSize) 8 set modes(beg,Mines) 10 set modes(int,ident) Intermediate set modes(int,XSize) 16 set modes(int,YSize) 16 set modes(int,Mines) 40 set modes(exp,ident) Expert set modes(exp,XSize) 30 set modes(exp,YSize) 16 set modes(exp,Mines) 99 set modes(mam,ident) Mammoth set modes(mam,XSize) 56 set modes(mam,YSize) 40 set modes(mam,Mines) 396 # user-defined board uses intermediate values as default. set modes(usr,ident) Custom set modes(usr,XSize) 16 set modes(usr,YSize) 16 set modes(usr,Mines) 40 set board(Seed) -1 set board(ratio) -1.0 set board(custom) 0 proc setmode { mode } { global board modes switch -glob -- $mode { -beg* - -int* - -exp* - -mam* - -usr* { set m [ string range $mode 1 3 ] set board(XSize) $modes($m,XSize) set board(YSize) $modes($m,YSize) set board(Mines) $modes($m,Mines) } default { return 0 } } return 1 } # "intermediate" is default. setmode "-int" # parse command-line arguments. set ac 0 foreach arg $argv { incr ac if {[ setmode $arg ] != 0} { continue } set field "" switch -glob -- $arg { -x { set field XSize } -y { set field YSize } -mines { set field Mines } -ratio { set board(ratio) [ lindex $argv $ac] } -seed { set board(Seed) [ lindex $argv $ac] } -* { puts stderr "$argv0 options: $usage"; exit 0 } } if {$field != ""} { set board($field) [ lindex $argv $ac ] set board(custom) 1 } } if {$board(custom) > 0} { # save custom choices. foreach field { XSize YSize Mines } { set modes(usr,$field) $board($field) } } ##### # define bitmaps. # # smiley-face bitmaps: smiley, shades, croak. # image create bitmap smiley -background yellow -data " #define smiley_width 26 #define smiley_height 26 static unsigned char smiley_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0x03, 0x06, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x20, 0x00, 0x20, 0x00, 0x10, 0x00, 0x40, 0x00, 0x10, 0x00, 0x40, 0x00, 0x08, 0x00, 0x80, 0x00, 0x08, 0x06, 0x83, 0x00, 0x04, 0x06, 0x03, 0x01, 0x04, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x01, 0x44, 0x00, 0x10, 0x01, 0x84, 0x00, 0x08, 0x01, 0x08, 0x03, 0x86, 0x00, 0x08, 0xfc, 0x81, 0x00, 0x10, 0x00, 0x40, 0x00, 0x10, 0x00, 0x40, 0x00, 0x20, 0x00, 0x20, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x00, 0x03, 0x06, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}; " -maskdata " #define smiley_width 26 #define smiley_height 26 static unsigned char smiley_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0xff, 0x07, 0x00, 0xc0, 0xff, 0x1f, 0x00, 0xe0, 0xff, 0x3f, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xf8, 0xff, 0xff, 0x00, 0xf8, 0xff, 0xff, 0x00, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xf8, 0xff, 0xff, 0x00, 0xf8, 0xff, 0xff, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xe0, 0xff, 0x3f, 0x00, 0xc0, 0xff, 0x1f, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}; " image create bitmap shades -background yellow -data " #define shades_width 26 #define shades_height 26 static unsigned char shades_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0x03, 0x06, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x20, 0x00, 0x20, 0x00, 0x10, 0x00, 0x40, 0x00, 0x10, 0x00, 0x40, 0x00, 0x08, 0x00, 0x80, 0x00, 0xc8, 0xff, 0x9f, 0x00, 0xe4, 0xdf, 0x3f, 0x01, 0x94, 0x8f, 0x4f, 0x01, 0x0c, 0x07, 0x87, 0x01, 0x04, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x01, 0x44, 0x00, 0x10, 0x01, 0x84, 0x00, 0x08, 0x01, 0x08, 0x03, 0x86, 0x00, 0x08, 0xfc, 0x81, 0x00, 0x10, 0x00, 0x40, 0x00, 0x10, 0x00, 0x40, 0x00, 0x20, 0x00, 0x20, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x00, 0x03, 0x06, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}; " -maskdata " #define smiley_width 26 #define smiley_height 26 static unsigned char smiley_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0xff, 0x07, 0x00, 0xc0, 0xff, 0x1f, 0x00, 0xe0, 0xff, 0x3f, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xf8, 0xff, 0xff, 0x00, 0xf8, 0xff, 0xff, 0x00, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xf8, 0xff, 0xff, 0x00, 0xf8, 0xff, 0xff, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xe0, 0xff, 0x3f, 0x00, 0xc0, 0xff, 0x1f, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}; " image create bitmap croak -background yellow -data " #define croak_width 26 #define croak_height 26 static unsigned char croak_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0x03, 0x06, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x20, 0x00, 0x20, 0x00, 0x10, 0x00, 0x40, 0x00, 0x90, 0x88, 0x48, 0x00, 0x08, 0x05, 0x85, 0x00, 0x08, 0x02, 0x82, 0x00, 0x04, 0x05, 0x05, 0x01, 0x84, 0x88, 0x08, 0x01, 0x04, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x01, 0x04, 0xfc, 0x01, 0x01, 0x04, 0xa3, 0x06, 0x01, 0x88, 0xa0, 0x8a, 0x00, 0x48, 0xa0, 0x92, 0x00, 0x10, 0x20, 0x42, 0x00, 0x10, 0xc0, 0x41, 0x00, 0x20, 0x00, 0x20, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x00, 0x03, 0x06, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}; " -maskdata " #define smiley_width 26 #define smiley_height 26 static unsigned char smiley_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0xff, 0x07, 0x00, 0xc0, 0xff, 0x1f, 0x00, 0xe0, 0xff, 0x3f, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xf8, 0xff, 0xff, 0x00, 0xf8, 0xff, 0xff, 0x00, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xfc, 0xff, 0xff, 0x01, 0xf8, 0xff, 0xff, 0x00, 0xf8, 0xff, 0xff, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xf0, 0xff, 0x7f, 0x00, 0xe0, 0xff, 0x3f, 0x00, 0xc0, 0xff, 0x1f, 0x00, 0x00, 0xff, 0x07, 0x00, 0x00, 0xfc, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}; " # # flag bitmap. # image create bitmap flag -background red -data " #define flag_width 12 #define flag_height 12 static unsigned char flag_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x01, 0xe0, 0x01, 0xf8, 0x03, 0xf8, 0x03}; " -maskdata " #define flag_width 12 #define flag_height 12 static unsigned char flag_bits[] = { 0x80, 0x00, 0xc0, 0x00, 0xf0, 0x00, 0xf8, 0x00, 0xf0, 0x00, 0xc0, 0x00, 0x80, 0x00, 0x80, 0x00, 0xe0, 0x01, 0xe0, 0x01, 0xf8, 0x03, 0xf8, 0x03}; " # # blank bitmap. # set blankxbm " #define blank_width 12 #define blank_height 12 static unsigned char blank_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; " image create bitmap blank -data $blankxbm # # question mark bitmap. # image create bitmap qmark -foreground blue -data " #define huh_width 12 #define huh_height 12 static unsigned char huh_bits[] = { 0xf0, 0x00, 0xf8, 0x01, 0x0c, 0x03, 0x0c, 0x03, 0x80, 0x01, 0xc0, 0x00, 0x60, 0x00, 0x60, 0x00, 0x60, 0x00, 0x00, 0x00, 0x60, 0x00, 0x60, 0x00}; " # # mine bitmaps: mine, wrong. # image create bitmap mine -data " #define mine_width 12 #define mine_height 12 static unsigned char mine_bits[] = { 0x00, 0x00, 0x42, 0x08, 0xf4, 0x05, 0xf8, 0x03, 0xec, 0x07, 0xec, 0x07, 0xfe, 0x0f, 0xfc, 0x07, 0xfc, 0x07, 0xf8, 0x03, 0xf4, 0x05, 0x42, 0x08}; " image create bitmap wrong -background red -data " #define wrong_width 12 #define wrong_height 12 static unsigned char wrong_bits[] = { 0x00, 0x00, 0x40, 0x00, 0xf0, 0x01, 0xf0, 0x00, 0x64, 0x06, 0x0c, 0x07, 0x9e, 0x0f, 0x0c, 0x07, 0x64, 0x06, 0xf0, 0x00, 0xf0, 0x01, 0x40, 0x00}; " -maskdata " #define wrong_width 12 #define wrong_height 12 static unsigned char wrong_bits[] = { 0x00, 0x00, 0x42, 0x0c, 0xf6, 0x07, 0xfc, 0x03, 0xec, 0x07, 0xfc, 0x07, 0xfe, 0x0f, 0xfc, 0x07, 0xfc, 0x07, 0xfc, 0x03, 0xf6, 0x07, 0x42, 0x0c}; " # # digit bitmaps: 0 thru 9. # set numb(0) " #define 0_width 12 #define 0_height 12 static unsigned char 0_bits[] = { 0xf0, 0x01, 0xf8, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0xf8, 0x03, 0xf0, 0x01}; " set numb(1) " #define 1_width 12 #define 1_height 12 static unsigned char 1_bits[] = { 0xe0, 0x00, 0xe0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xf0, 0x03, 0xf0, 0x03}; " set numb(2) " #define 2_width 12 #define 2_height 12 static unsigned char 2_bits[] = { 0xf8, 0x03, 0xf8, 0x03, 0x18, 0x03, 0x18, 0x03, 0x00, 0x03, 0x80, 0x03, 0xc0, 0x01, 0xe0, 0x00, 0x70, 0x00, 0x38, 0x00, 0xf8, 0x03, 0xf8, 0x03}; " set numb(3) " #define 3_width 12 #define 3_height 12 static unsigned char 3_bits[] = { 0xf8, 0x03, 0xf8, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0xf0, 0x03, 0xf0, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0xf8, 0x03, 0xf8, 0x03}; " set numb(4) " #define 4_width 12 #define 4_height 12 static unsigned char 4_bits[] = { 0x18, 0x00, 0x18, 0x00, 0x98, 0x01, 0x98, 0x01, 0x98, 0x01, 0x98, 0x01, 0xf8, 0x03, 0xf8, 0x03, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01}; " set numb(5) " #define 5_width 12 #define 5_height 12 static unsigned char 5_bits[] = { 0xf8, 0x03, 0xf8, 0x03, 0x18, 0x00, 0x18, 0x00, 0x18, 0x00, 0xf8, 0x01, 0xf0, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0xf8, 0x03, 0xf8, 0x01}; " set numb(6) " #define 6_width 12 #define 6_height 12 static unsigned char 6_bits[] = { 0xf8, 0x03, 0xf8, 0x03, 0x18, 0x00, 0x18, 0x00, 0x18, 0x00, 0xf8, 0x03, 0xf8, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0xf8, 0x03, 0xf8, 0x03}; " set numb(7) " #define 7_width 12 #define 7_height 12 static unsigned char 7_bits[] = { 0xf8, 0x03, 0xf8, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x80, 0x03, 0xc0, 0x01, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00, 0xc0, 0x00}; " set numb(8) " #define 8_width 12 #define 8_height 12 static unsigned char 8_bits[] = { 0xf8, 0x03, 0xf8, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0xf8, 0x03, 0xf8, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0xf8, 0x03, 0xf8, 0x03}; " set numb(9) " #define 9_width 12 #define 9_height 12 static unsigned char 9_bits[] = { 0xf8, 0x03, 0xf8, 0x03, 0x18, 0x03, 0x18, 0x03, 0x18, 0x03, 0xf8, 0x03, 0xf8, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0xf8, 0x03, 0xf8, 0x03}; " # # "register" the bitmaps. # image create bitmap 0 -data $blankxbm #image create bitmap 0 -data $numb(0) -foreground #646464 image create bitmap 1 -data $numb(1) -foreground #0000ff image create bitmap 2 -data $numb(2) -foreground #00c850 image create bitmap 3 -data $numb(3) -foreground #ff0000 image create bitmap 4 -data $numb(4) -foreground #0000af image create bitmap 5 -data $numb(5) -foreground #ff00ff image create bitmap 6 -data $numb(6) -foreground #00c8c8 image create bitmap 7 -data $numb(7) -foreground #b400b4 image create bitmap 8 -data $numb(8) -foreground #000000 # show all mines. proc reveal {} { global board Map foreach coord $Map(Coords) { set j [ lindex $coord 0 ] set i [ lindex $coord 1 ] .game.field.y$j.x$i configure -relief flat -image $board(type,$j,$i) set f [ lsearch $Map(Flags) $coord ] if { $f > -1 } { set Map(Flags) [ lreplace $Map(Flags) $f $f ] } } # show mistakes. foreach coord $Map(Flags) { set j [ lindex $coord 0 ] set i [ lindex $coord 1 ] .game.field.y$j.x$i configure -relief flat -image wrong } } # finished. show results. proc done { type } { after cancel timer .game.status.butn configure -image $type reveal } # step on a square. proc step { x y } { global board # disable buttonclicks. bind .game.field.y$y.x$x