This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
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()