Minesweeper

By Firstmate on Jul 26, 2009

It's minesweeper. Pretty simple, I'm sure we've all played it. Has nearly every feature as the windows one. Right click, flag

Crappy graphics though

To start: /mines [# of mines] [s | m | l] s(small), m(medium), large(large)

alias mines {
  set %m.mines $iif($1 < 40,$1,15)
  set %m.size $iif($istok(s m l,$2,32),$2,m)
  set %m.time 0
  set %m.gameOn 1
  window -pdCk0 @mines 1 1 $replace(%m.size,s,155,m,260,l,470) $replace(%m.size,s,220,m,320,l,320)
  drawfill -r @mines $rgb(255,255,255) $rgb(255,255,255) 1 1
  .timermtime 0 1 m.time
  if ($hget(mines)) { hfree mines }
  hadd -m mines mines %m.mines
  hadd -m mines size %m.size
  m.drawMap
  m.makeMines
}
menu @mines {
  uclick: {
    ;Show mine
    if ($getdot(@mines,$mouse.x,$mouse.y) == 8421504 && %m.gameOn == 1) {
      var %xy = $m.getCoord($mouse.x,$mouse.y)
      tokenize 32 %xy
      ;Check if cell is flagged.
      if (!$hget(mines,$+(flag.,$1,.,$2))) {
        if ($hget(mines,$+(cell.,$1,.,$2)) == m) { m.gameOver }
        else if ($hget(mines,$+(cell.,$1,.,$2)) == 0) { 
          drawrect -fr @mines $rgb(192,192,192) 1 $calc(6+(($1 -1)*15)) $calc(46+(($2 -1)*15)) 14 14
          ;Show nearby clear ones?
        }
        else {
          if ($hget(mines,$+(cell.,$1,.,$2))) { drawtext -r @mines $rgb(0,0,0) Tahoma 12 $calc(9+ (($1 -1) *15)) $calc(45+ (($2 -1) *15)) $hget(mines,$+(cell.,$1,.,$2)) }
          hdel mines $+(cell.,$1,.,$2) 
        }
      }
    }
    if ($inellipse($mouse.x,$mouse.y,$calc(($window(@mines).w /2) -20),5,25,25)) {
      ;new Game
      window -c @mines
      mines $hget(mines,mines) $hget(mines,size)
    }
  }
  rclick: {
    ;Show flag/question mark/none
    if ($getdot(@mines,$mouse.x,$mouse.y) == 8421504 || $getdot(@mines,$mouse.x,$mouse.y) == 255 && %m.gameOn == 1) {
      var %xy = $m.getCoord($mouse.x,$mouse.y)
      tokenize 32 %xy
      ;Flag is there already
      if ($hget(mines,$+(flag.,$1,.,$2)) && $istok(0 1 2 3 4 5 6 m,$hget(mines,$+(cell.,$1,.,$2)),32)) {
        hdel mines $+(flag.,$1,.,$2)
        drawfill -r @mines $rgb(128,128,128) $rgb(0,0,0) $calc((($1 -1) * 15) + 9) $calc((($2 -1) * 15) + 49)
        drawline -r @mines $rgb(255,255,255) 1 $calc(6+(($1 -1)*15)) $calc(46+(($2 -1)*15)) $calc(19+(($1 -1)*15)) $calc(46+(($2 -1)*15))
        drawline -r @mines $rgb(255,255,255) 1 $calc(6+(($1 -1)*15)) $calc(46+(($2 -1)*15)) $calc(6+(($1 -1)*15)) $calc(59+(($2 -1)*15))

        inc %m.mines
        drawrect -rf @mines $rgb(0,0,0) 1 5 5 40 25
        drawtext -r @mines $rgb(142,0,0) Tahoma 18 6 6 %m.mines
      }
      ;Add flag
      else {
        hadd mines $+(flag.,$1,.,$2) 1
        drawrect -fer @mines $rgb(255,0,0) 1 $calc(10+(($1 -1)*15)) $calc(50+(($2 -1)*15)) 5 5

        dec %m.mines
        drawrect -rf @mines $rgb(0,0,0) 1 5 5 40 25
        drawtext -r @mines $rgb(142,0,0) Tahoma 18 6 6 %m.mines
      }
    }
  }
}
alias m.time {
  inc %m.time
  if (%m.time == 1000) {
    ;game over
    .timermtime off
  }
  else {
    drawrect -rf @mines $rgb(0,0,0) 1 $calc($window(@mines).w - 55) 5 40 25
    drawtext -r @mines $rgb(142,0,0) Tahoma 18 $calc($window(@mines).w - 50) 6 %m.time
  }
}
alias m.drawMap {
  ;draw top
  ;Flags left
  drawrect -rf @mines $rgb(0,0,0) 1 5 5 40 25
  drawtext -r @mines $rgb(142,0,0) Tahoma 18 6 6 %m.mines
  ;Time
  drawrect -rf @mines $rgb(0,0,0) 1 $calc($window(@mines).w - 55) 5 40 25
  ;New Game
  drawrect -ref @mines $rgb(255,255,0) 1 $calc(($window(@mines).w /2) -20) 5 25 25
  drawrect -ref @mines $rgb(0,0,0) 1 $calc(($window(@mines).w /2) -15) 12 5 5
  drawrect -ref @mines $rgb(0,0,0) 1 $calc(($window(@mines).w /2) -5) 12 5 5
  drawrect -re @mines $rgb(0,0,0) 1 $calc(($window(@mines).w /2) -10) 20 5 5

  drawline -r @mines $rgb(0,0,0) 1 0 35 $window(@mines).w 35

  var %x 5, %y 45, %w $replace(%m.size,s,9,m,16,l,30), %h $replace(%m.size,s,9,m,16,l,16)
  var %i 1
  while (%i <= %w) {
    var %j 1
    while (%j <= %h) {
      ;draw cells
      drawrect -r @mines $rgb(0,0,0) 1 $calc(((%i -1) * 15) + %x) $calc(((%j -1) * 15) + %y) 15 15
      drawfill -r @mines $rgb(128,128,128) $rgb(0,0,0) $calc(((%i -1) * 15) + %x + 1) $calc(((%j -1) * 15) + %y + 1)
      drawline -r @mines $rgb(255,255,255) 1 $calc(6+((%i -1)*15)) $calc(46+((%j -1)*15)) $calc(19+((%i -1)*15)) $calc(46+((%j -1)*15))
      drawline -r @mines $rgb(255,255,255) 1 $calc(6+((%i -1)*15)) $calc(46+((%j -1)*15)) $calc(6+((%i -1)*15)) $calc(59+((%j -1)*15))
      hadd -m mines $+(cell.,%i,.,%j) 0
      hadd -m mines cells $hget(mines,cells) $+(%i,.,%j)
      inc %j
    }
    inc %i
  }
}
alias m.makeMines {
  var %zyx %m.mines
  while (%m.mines > 0) {
    var %xy = $m.getRandMineCoord
    tokenize 32 %xy 
    hadd mines $+(cell.,$1,.,$2) m
    var %z $hget(mines,cells)
    hdel mines cells
    hadd mines cells $remove(%z,$+($1,.,$2))

    ;increment surrounding mines
    ;(-1,-1) (0,-1) (+1,-1)
    ;(-1,0) (x,y) (+1,0)
    ;(-1,+1) (0,+1) (+1,+1) 
    if ($hget(mines,$+(cell.,$calc($1 -1),.,$calc($2 -1))) != m) { hinc mines $+(cell.,$calc($1 -1),.,$calc($2 -1)) }
    if ($hget(mines,$+(cell.,$1,.,$calc($2 -1))) != m) { hinc mines $+(cell.,$1,.,$calc($2 -1)) }
    if ($hget(mines,$+(cell.,$calc($1 +1),.,$calc($2 -1))) != m) { hinc mines $+(cell.,$calc($1 +1),.,$calc($2 -1)) }

    if ($hget(mines,$+(cell.,$calc($1 +1),.,$2)) != m) { hinc mines $+(cell.,$calc($1 +1),.,$2) }

    if ($hget(mines,$+(cell.,$calc($1 +1),.,$calc($2 +1))) != m) { hinc mines $+(cell.,$calc($1 +1),.,$calc($2 +1)) }
    if ($hget(mines,$+(cell.,$1,.,$calc($2 +1))) != m) { hinc mines $+(cell.,$1,.,$calc($2 +1)) }
    if ($hget(mines,$+(cell.,$calc($1 -1),.,$calc($2 +1))) != m) { hinc mines $+(cell.,$calc($1 -1),.,$calc($2 +1)) }

    if ($hget(mines,$+(cell.,$calc($1 -1),.,$2)) != m) { hinc mines $+(cell.,$calc($1 -1),.,$2) }
    dec %m.mines
  }
  set %m.mines %zyx
}
alias m.getRandMineCoord {
  var %x $hget(mines,cells)
  return $replace($gettok(%x,$r(1,$numtok(%x,32)),32),$chr(46),$chr(32))
}
alias m.getCoord {
  return $ceil($calc(($1 -5) /15)) $ceil($calc(($2 -45) /15))
}
alias m.gameOver {
  set %m.gameOn 0
  .timermtime off
  ;show Mines
  var %x $hfind(mines,cell.*,0,w), %p
  while (%x > 0) {
    %p = $hfind(mines,cell.*,%x,w)
    if ($hget(mines,%p) == m) { 
      tokenize 32 $gettok(%p,2,46) $gettok(%p,3,46)
      drawrect -fer @mines $rgb(0,0,0) 1 $calc(10+(($1 -1)*15)) $calc(50+(($2 -1)*15)) 5 5
    }
    dec %x
  }
  drawrect -ref @mines $rgb(255,255,0) 1 $calc(($window(@mines).w /2) -20) 5 25 25
  drawrect -ref @mines $rgb(0,0,0) 1 $calc(($window(@mines).w /2) -15) 12 5 5
  drawrect -ref @mines $rgb(0,0,0) 1 $calc(($window(@mines).w /2) -5) 12 5 5
  drawline -r @mines $rgb(0,0,0) 1 $calc(($window(@mines).w /2) -15) 23 $calc(($window(@mines).w /2)) 23
}
on 1:CLOSE:@mines: {
  unset %m.*
  .timermtime off
}
Sign in to comment.
asakura   -  Aug 02, 2009

i always wanted to make a mine sweeper just dint know how ): but very nice! :D 9/10

 Respond  
eyiezinc   -  Jul 28, 2009

awesome! good Firstmate 9/10

 Respond  
Firstmate   -  Jul 27, 2009

mhm, Yeah I fixed that just recently (updated code)

I'll also take your suggestion in about indenting the blocks. I included a feature to show empty blocks. I still am going to add the part to show the cells on the border.

 Respond  
mountaindew   -  Jul 27, 2009

Nice job, I'm nasty at minesweeper :)

I just wish that the numbered tiles were indented in like the blank spaces, because they kind of blend in with the unclicked blocks.

Edit: I did /mines 20 m and looks like it's a little buggy

Image

(There's some dots above the board and where I lost, the numbers were wrong.)

 Respond  
Firstmate   -  Jul 27, 2009

The_Almight_Duelist, I am working on the second thing, it's kinda weird.

Are there any errors when right-clicking.

 Respond  
The_Almighty_Duelist   -  Jul 27, 2009

awsome script, but i can't right-click for some reason :( and when i click on an empty spot, it doesn't open up all other empty spots beside it.

other than that, amazing! ^_^

 Respond  
Ayon   -  Jul 27, 2009

i take my hat off for this one Firstmate!

 Respond  
blitzz   -  Jul 27, 2009

Wow..:)

 Respond  
_Daniel_   -  Jul 26, 2009

awesome!

 Respond  
FordLawnmower   -  Jul 26, 2009

Cool script Firstmate :)

 Respond  
knoeki   -  Jul 26, 2009

wow, awesome! nice job. :_)

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.