/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" MSH get-WmiMethodHelp (GWM) Update + format tips. "," More on Cryptograpy and MSH "," Get-credential and Decrypting a SecureString in MSH "," Atari Laptop (no MSH content) "," Sending Mail from MSH "," MSH More replacement script update (HotFix) "," Enhanced More Function for MSH "," Get driveletters for All drives (Providers) in MSH "," Math "," Exploring .NET types, classes and Enums from MSH "," ")

Saturday, November 19, 2005

 


MSH Minesweeper GUI game



I after, my Thow Dices in MSH (Sttt, it originaly started as Yath, but was downgraded on the way ;-)

and ofcource after seeing the famious Alien game in MSH (I won't add another link here, but I'm sure you can find it)

I was thinking about making a simple GUI game in MSH.
(also Snake crossed my mind before, but i think i did hear of a Monad team internal version of that !?)

So I could not realy think of a simle example game, for awile.

Then today minesweeper crossed my mind as easy to make without a GUI-editor for the form.
(think did see it as a Kix-forms example before)

so I came to the script below,

It it is very basic, (even the level parameters are in the script),
I already added a Menu to make it easy to add them there, I only added the Quit Item as an example, but it would be easy to extend with a "level" and "new" option for example.

b.t.w. notice I used the New .NET MenuStrip instead of the .net 1.1 Menu-style as I used here, MSH directory watcher with popup-balloon , as it makes you more fexible in make-ing the menu . (for example add the Smily as in the original ;-)

Also you will not lose if you click a Mine, but this is also easy to add in this Block (in the Function DoClear)

if ($map[$num].tag -eq "x") {
$map[$num].text = "x"
$map[$num].BackColor = "red"
}

Enjoy,

gr /\/\o\/\/


# Mines.msh 
# very basic MineSweeper GUI game in MSH
# /\/\o\/\/ 2005 

# Level Parameters :
$rows = 16
$Cols = 16
$Mines = 40

# Build Form 

$form = new-object System.Windows.Forms.form
$Form.text = "/\/\o\/\/'s MSH Minesweeper"

# Build Menu (Only Quit now, but easy to add Level Etc.) 

$MS = new-object System.Windows.Forms.MenuStrip
$Mi = new-object System.Windows.Forms.ToolStripMenuItem("&File")

$Msi1 = new-object System.Windows.Forms.ToolStripMenuItem("&Quit")
$msi1.add_Click({$form.close()}) 
$Mi.DropDownItems.Add($msi1)

$ms.Items.Add($mi)
$form.Controls.Add($ms)

# Needed here for An Other Thread error when buttoncolection added to form.

$Button = new-object System.Windows.Forms.Button
  
$form.width = ($rows * 25) + 20
$form.Height = ($Cols * 25) + 70   

# container for buttons that make up MineField

[System.Windows.Forms.Control[]]$Map = @()

# this Function is Used by the Button click event to clear a Field

Function DoClear {
  $num = $args[0]
  $C = $num % $rows
  $R = [math]::truncate($num/$rows)
  If ($map[$num].enabled -eq $true) {
    if ($map[$num].tag -eq "x") {
       $map[$num].text = "x"
       $map[$num].BackColor = "red"
    }
    Else {
      $map[$num].BackColor = "DarkGray"
      $map[$num].FlatStyle = 'Flat'
      $map[$num].Enabled = $false
      if ($map[$num].tag -ne 0) {
        $map[$num].text = $map[$num].tag
      }
      Else {
        if ($C -gt 0) {
          doClear ($num - 1 )
          if ($R -gt 0) {
            doClear (($num - $Cols) - 1)
          }
        }
        if ($C -lt ($Cols - 1)) {
          doClear ($num + 1)
          if ($R -gt 0) {
            doClear (($num - $Cols) + 1)
          }
        }
        if ($R -gt 0) {
          doClear ($num - $cols)
        }
        if ($R -lt ($rows -1)) {
          doClear ($num + $cols)
          if ($C -gt 0) {
            doClear (($num + $Cols) - 1)
          }
          if ($C -lt ($Cols - 1)) {
            doClear (($num + $Cols) + 1)
          }
        }
      }
    }
  }
}

# Create the MineField (array of Buttons)

for ($i = 0;$i -lt $rows;$i++) 
{
  $row = @()
  for ($j = 0;$j -lt $Cols;$j++) 
  {
    $Button = new-object System.Windows.Forms.Button
    $Button.width = 25
    $Button.Height = 25
    $button.top = ($i * 25) + 25
    $button.Left = $j * 25
    $button.Name = ($i * $cols) + $j
    $Button.tag = "0"
    $button.add_click({
      [int]$num = $this.name
      $this.name
      DoClear $num 
    })

    $button.font = new-object system.drawing.font('Microsoft Sans Serif',10,'bold')
    #$Button.ForeColor
    $Row += $Button
  }
  
  $Map += $row

}

# this Function is used to update the fields surrounding mines count

Function DoRaise{
  [int]$Cell = $args[0
  if ($map[$cell].tag -ne "x"){
    $n = [int]$map[$cell].tag
    $n += 1
    $map[$cell].tag = $n
  }
}

# throw some Mines 

$Random = new-object system.random([datetime]::now.Millisecond)
for ($i = 0 ; $i -lt $Mines) {
  $num = $random.next($map.count)
  if ( $map[$num].tag -ne "x") {
    $map[$num].tag = "x"
    $C = $num % $rows
    $R = [math]::truncate($num/$rows)

    if ($C -gt 0) {
      doRaise ($num - 1)
      if ($R -gt 0) {
        doRaise (($num - $Cols) - 1)
      }
    }
    if ($C -lt ($Cols - 1)) {
      doRaise ($num + 1)
      if ($R -gt 0) {
        doRaise (($num - $Cols) + 1)
      }
    }
    if ($R -gt 0) {
      doRaise ($num - $cols)

    }
    if ($R -lt ($rows -1)) {
      doRaise ($num + $cols)
      if ($C -gt 0) {
        doRaise (($num + $Cols) - 1)
      }
      if ($C -lt ($Cols -1)) {
       doRaise (($num + $Cols) + 1)
      }
    }
     
    $i++
  }
}

# hit the Road (....ehh, the Minefield)

$form.controls.addrange($map)
$form.topmost = $true 
$form.showdialog() 




Comments:
Blogger /\/\o\/\/
You need to load the forms library for this script to work.

otherwise it will give you a bunch of errors.

you can load the forms library like this :

[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

enjoy,

greetings /\/\o\/\/
 
Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?