Jump to content

Simple MineSweeper game


SxyfrG
 Share

Recommended Posts

Ok, this took around 2 hours to put together. Quite simple and easy to play :D

Any improvements, suggestions, code optimisation or w/e is welcome ;)

Download skinned version - 4Shared

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
Opt("GUIOnEventMode", 1)
Global $Button[13][13]
Global $MinesArray, $Game = False, $Game2 = False, $Timer, $Button2, $Debug = False;change this to true so you can debug and bugs :P

$GUI = GUICreate("Mine Shweepy Into Korna", 270, 325)

$Menu1 = GUICtrlCreateMenu("Game")
$New = GUICtrlCreateMenuItem("New Game" & @TAB & "(F2)", $Menu1)
GUICtrlCreateMenuItem("", $Menu1)
$n00b = GUICtrlCreateMenuItem("n00b", $Menu1, -1, 1)
$Easy = GUICtrlCreateMenuItem("Easy", $Menu1, -1, 1)
$Hard = GUICtrlCreateMenuItem("Hard", $Menu1, -1, 1)
$Extreme = GUICtrlCreateMenuItem("EXTREME", $Menu1, -1, 1)
GUICtrlCreateMenuItem("", $Menu1)
$Exit = GUICtrlCreateMenuItem("Exit", $Menu1)

$Difficulty = IniRead(@ScriptDir & "\Config.ini", "MineShweeper", "Difficulty", "Easy")
Switch $Difficulty
    Case "n00b"
        GUICtrlSetState($n00b, $GUI_CHECKED)
    Case "Easy"
        GUICtrlSetState($Easy, $GUI_CHECKED)
    Case "Hard"
        GUICtrlSetState($Hard, $GUI_CHECKED)
    Case "Extreme"
        GUICtrlSetState($Extreme, $GUI_CHECKED)
EndSwitch


$NewGame = GUICtrlCreateButton("New Game", (275 / 2) - (70 / 2), 5, 70, 30)
$MineLabel = GUICtrlCreateLabel("00", 30, 13, 40, 20)
$Time = GUICtrlCreateLabel("00:00", 220, 13, 40, 20)

GUISetState()

Dim $Accel[1][2]
$Accel[0][0] = "{F2}"
$Accel[0][1] = $NewGame
GUISetAccelerators($Accel, $GUI)

GUICtrlSetOnEvent($New, "NewGame")
GUICtrlSetOnEvent($NewGame, "NewGame")
GUICtrlSetOnEvent($Exit, "_Exit")
GUICtrlSetOnEvent($n00b, "DifficultyChange")
GUICtrlSetOnEvent($Easy, "DifficultyChange")
GUICtrlSetOnEvent($Hard, "DifficultyChange")
GUICtrlSetOnEvent($Extreme, "DifficultyChange")

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

While 1
    Sleep(500)
    If $Game Then GUICtrlSetData($Time, MsToSecs(TimerDiff($Timer) / 1000))
WEnd

;=================================================================================;

Func NewGame()
    Local $Mines = 0
    
    If Not $Debug Then
        $Difficulty = IniRead(@ScriptDir & "\Config.ini", "MineShweeper", "Difficulty", "Easy")
        Switch $Difficulty
            Case "n00b"
                $Mines = 5
                
            Case "Easy"
                $Mines = 10
                
            Case "Hard"
                $Mines = 20
                
            Case "EXTREME"
                $Mines = 30
        EndSwitch
    EndIf
    
    Global $MinesArray[$Mines + 1]
    
    For $i = 1 To $Mines
        $MinesArray[$i] = _Random(15, 158, $MinesArray)
    Next
    
    If $Game2 Then
        For $i = 1 To 12
            For $v = 1 To 12
                GUICtrlDelete($Button[$i][$v])
            Next
        Next
    EndIf
    
    Local $Left = 15, $Top = 50
    For $i = 1 To (245 / 20) Step 1;Width
        For $v = 1 To (255 / 20) Step 1;height
            $Button[$i][$v] = GUICtrlCreateButton("", $Left, $Top, 20, 20, $BS_FLAT)
            GUICtrlSetColor($Button[$i][$v], 0x000000)
            GUICtrlSetOnEvent($Button[$i][$v], "ButtonPress")
;ConsoleWrite($i&@TAB&$v&@LF&$Button[$i][$v]&@LF)
            $Top += 20
        Next
        $Left += 20
        $Top = 50
    Next
    
    Global $Button2 = _2Dto1D($Button)
    
    GUICtrlSetData($MineLabel, $Mines)
    $Game = True
    $Game2 = True
    $Timer = TimerInit()
EndFunc;==>NewGame

;=================================================================================;

Func _Random($Min, $Max, ByRef $Array)
    Local $Exit__
    $Random = Random($Min, $Max, 1)
    If _SearchArray($Random, $Array) <> 0 Then
        While $Exit__ = False
            $Random = Random($Min, $Max, 1)
            If _SearchArray($Random, $Array) <> 0 Then $Exit__ = True
        WEnd
    EndIf
    
    Return $Random
EndFunc;==>_Random

;=================================================================================;

Func _SearchArray($sSearch, ByRef $Array, $cons = False);Modified function for internal use
    If $cons Then ConsoleWrite("!================================================!" & @LF)
    Local $Dump, $Bin[UBound($Array) + 1], $a = 0, $b = 0
    For $Dump In $Array
        $a += 1
        If $cons Then ConsoleWrite($sSearch & @TAB & $Dump & @LF)
        If $Dump = $sSearch Then;If a match is found
            $b += 1
            $Bin[$b] = $a
        EndIf
    Next
    If $cons Then ConsoleWrite("!================================================!" & @LF)
    
    Local $Return[$b + 1]
    $Return[0] = $b
    
    
    Return $b;$Return
EndFunc;==>_SearchArray

;=================================================================================;

Func ButtonPress()
    If $Game And GUICtrlRead(@GUI_CtrlId) = "" Then
        If _SearchArray(@GUI_CtrlId, $MinesArray) = 1 Then;clicked on a mine!
            If Not $Debug Then
                For $i = 1 To 12
                    For $v = 1 To 12
                        If _SearchArray($Button[$i][$v], $MinesArray) = 1 Then
                            GUICtrlSetBkColor($Button[$i][$v], 0x000000)
                            GUICtrlSetColor($Button[$i][$v], 0xff0000)
                            GUICtrlSetData($Button[$i][$v], Chr(164))
                        EndIf
                    Next
                Next
            Else
                GUICtrlSetBkColor(@GUI_CtrlId, 0x000000)
                GUICtrlSetColor(@GUI_CtrlId, 0xff0000)
                GUICtrlSetData(@GUI_CtrlId, Chr(64))
            EndIf
            
            If Not $Debug Then
                $Game = False
                MsgBox(48, "OMGEH", "Dude, you clicked on a MINE!!!", 10, $GUI)
            EndIf
        Else
            Local $Check, $Colour
            $Check = _Search9Boxes(@GUI_CtrlId)
            Switch $Check
                Case 0
                    $Colour = 0x000000
                Case 1 To 2
                    $Colour = 0x0000ff
                Case 3 To 4
                    $Colour = 0x0ca500
                Case 5 To 6
                    $Colour = 0x009cff
                Case 7 To 8
                    $Colour = 0xff0000
            EndSwitch
            
            GUICtrlSetColor(@GUI_CtrlId, $Colour)
            GUICtrlSetData(@GUI_CtrlId, $Check)
;GUICtrlSetState(@GUI_CtrlId, $GUI_DISABLE)
        EndIf
        
        Local $CheckMines = 0
        
        For $i = 1 To 12
            For $v = 1 To 12
                If GUICtrlRead($Button[$i][$v]) = "" Then $CheckMines += 1
            Next
        Next
        
        ConsoleWrite($CheckMines & @LF)
        
        If $CheckMines = GUICtrlRead($MineLabel) Then
            MsgBox(48, "OMGAWESOME", "HOLY CRAP YOU WON!!!", 10, $GUI)
            $Game = False
        EndIf
    EndIf
EndFunc;==>ButtonPress

;=================================================================================;

Func DifficultyChange()
    ConsoleWrite(@GUI_CtrlId & @TAB & $n00b & @TAB & $Easy & @TAB & $Hard & @TAB & $Extreme & @LF)
    Switch @GUI_CtrlId
        Case $n00b
            IniWrite(@ScriptDir & "\Config.ini", "MineShweeper", "Difficulty", "n00b")
            
        Case $Easy
            IniWrite(@ScriptDir & "\Config.ini", "MineShweeper", "Difficulty", "Easy")
            
        Case $Hard
            IniWrite(@ScriptDir & "\Config.ini", "MineShweeper", "Difficulty", "Hard")
            
        Case $Extreme
            IniWrite(@ScriptDir & "\Config.ini", "MineShweeper", "Difficulty", "EXTREME")
    EndSwitch
    
    If $Game Then NewGame()
EndFunc;==>DifficultyChange

;=================================================================================;

Func MsToSecs($TimeInMs)
    Local $Mins, $Secs
    If $TimeInMs > 60 Then
        $Mins += 1
        $TimeInMs -= 60
        While $TimeInMs > 60
            $Mins += 1
            $TimeInMs -= 60
        WEnd
    Else
        $Mins = "00"
    EndIf
    
    If StringLen($Mins) = 1 Then $Mins = "0" & $Mins
    
    $Secs = Round($TimeInMs, 0)
    If StringLen($Secs) = 1 Then $Secs = "0" & $Secs
    
    Return $Mins & ":" & $Secs
EndFunc;==>MsToSecs

;=================================================================================;

Func _2Dto1D($Array)
    Local $Array2[12 * 12 + 1]
    Local $Counter = 1
    
    For $i = 1 To 12
        For $v = 1 To 12
            $Array2[$Counter] = $Array[$i][$v]
            $Counter += 1
        Next
    Next
    
    Return $Array2
EndFunc;==>_2Dto1D

;=================================================================================;

Func _Search9Boxes($ID)
    Local $X, $Y
    For $i = 1 To 12
        For $v = 1 To 12
            If $Button[$i][$v] = $ID Then
                ConsoleWrite($ID & @TAB & $Button[$i][$v] & @LF)
                $X = $i
                $Y = $v
                ConsoleWrite("+ " & $X & @TAB & $Y & @LF)
            EndIf
        Next
    Next
    ConsoleWrite($i & @TAB & $v & @LF)
    ConsoleWrite("-================-" & @LF)
    Local $BoxesToSearch
    
    If $X = 1 Or $X = 12 Or $Y = 1 Or $Y = 12 Then;search 3 or 5 boxes for a mine
        If ($Y = 1 Or $Y = 12) And ($X = 1 Or $X = 12) Then;search 3 boxes on a corner
            Local $BoxesToSearch[4]
            If $Y = 1 And $X = 1 Then;top left
                $BoxesToSearch[1] = $Button[$X + 1][$Y]
                $BoxesToSearch[2] = $Button[$X + 1][$Y + 1]
                $BoxesToSearch[3] = $Button[$X][$Y + 1]
            ElseIf $Y = 1 And $X = 12 Then;top right
                $BoxesToSearch[1] = $Button[$X - 1][$Y]
                $BoxesToSearch[2] = $Button[$X - 1][$Y + 1]
                $BoxesToSearch[3] = $Button[$X][$Y + 1]
            ElseIf $Y = 12 And $X = 1 Then;bottom left
                $BoxesToSearch[1] = $Button[$X + 1][$Y]
                $BoxesToSearch[2] = $Button[$X + 1][$Y - 1]
                $BoxesToSearch[3] = $Button[$X][$Y - 1]
            ElseIf $Y = 12 And $X = 12 Then;bottom right
                $BoxesToSearch[1] = $Button[$X - 1][$Y]
                $BoxesToSearch[2] = $Button[$X - 1][$Y - 1]
                $BoxesToSearch[3] = $Button[$X][$Y - 1]
            EndIf
        Else;search 5 boxes
            Local $BoxesToSearch[6]
            If $X = 1 And $Y <> 1 And $Y <> 12 Then;left side
;ConsoleWrite("++OGM"&@LF)
                $BoxesToSearch[1] = $Button[$X][$Y - 1]
                $BoxesToSearch[2] = $Button[$X][$Y + 1]
                $BoxesToSearch[3] = $Button[$X + 1][$Y - 1]
                $BoxesToSearch[4] = $Button[$X + 1][$Y]
                $BoxesToSearch[5] = $Button[$X + 1][$Y + 1]
            ElseIf $X = 12 And $Y <> 1 And $Y <> 12 Then;right side
;ConsoleWrite("1!OGM"&@LF)
                $BoxesToSearch[1] = $Button[$X][$Y - 1]
                $BoxesToSearch[2] = $Button[$X][$Y + 1]
                $BoxesToSearch[3] = $Button[$X - 1][$Y - 1]
                $BoxesToSearch[4] = $Button[$X - 1][$Y]
                $BoxesToSearch[5] = $Button[$X - 1][$Y + 1]
            ElseIf $X <> 1 And $X <> 12 And $Y = 1 Then;top
;ConsoleWrite("OGM"&@LF)
                $BoxesToSearch[1] = $Button[$X + 1][$Y]
                $BoxesToSearch[2] = $Button[$X - 1][$Y]
                $BoxesToSearch[3] = $Button[$X - 1][$Y + 1]
                $BoxesToSearch[4] = $Button[$X][$Y + 1]
                $BoxesToSearch[5] = $Button[$X + 1][$Y + 1]
            ElseIf $X <> 1 And $X <> 12 And $Y = 12 Then;bottom
;ConsoleWrite("OMG"&@LF)
                $BoxesToSearch[1] = $Button[$X + 1][$Y]
                $BoxesToSearch[2] = $Button[$X - 1][$Y]
                $BoxesToSearch[3] = $Button[$X - 1][$Y - 1]
                $BoxesToSearch[4] = $Button[$X][$Y - 1]
                $BoxesToSearch[5] = $Button[$X + 1][$Y - 1]
            EndIf
        EndIf
    Else;search 8 boxes
        Local $BoxesToSearch[9]
        $BoxesToSearch[1] = $Button[$X + 1][$Y - 1]
        $BoxesToSearch[2] = $Button[$X + 1][$Y]
        $BoxesToSearch[3] = $Button[$X + 1][$Y + 1]
        $BoxesToSearch[4] = $Button[$X][$Y - 1]
        $BoxesToSearch[5] = $Button[$X][$Y + 1]
        $BoxesToSearch[6] = $Button[$X - 1][$Y - 1]
        $BoxesToSearch[7] = $Button[$X - 1][$Y]
        $BoxesToSearch[8] = $Button[$X - 1][$Y + 1]
    EndIf
    
    Local $Search = 0
    For $i = 1 To UBound($BoxesToSearch) - 1
        $Search += _SearchArray($BoxesToSearch[$i], $MinesArray, False)
    Next
    
    If $Search = 0 Then
        For $i = 1 To UBound($BoxesToSearch) - 1
;ButtonPress2($BoxesToSearch[$i])
            If _Search9Boxes2($BoxesToSearch[$i]) = 0 Then
                GUICtrlSetData($BoxesToSearch[$i], 0)
            EndIf
        Next
    EndIf
    
    Return $Search
EndFunc;==>_Search9Boxes

Func _Search9Boxes2($ID)
    Local $X, $Y
    For $i = 1 To 12
        For $v = 1 To 12
            If $Button[$i][$v] = $ID Then
;ConsoleWrite($ID & @TAB & $Button[$i][$v] & @LF)
                $X = $i
                $Y = $v
;ConsoleWrite($X & @TAB & $Y & @LF)
            EndIf
        Next
    Next
;ConsoleWrite($i & @TAB & $v & @LF)
    Local $BoxesToSearch
    
    If $X = 1 Or $X = 12 Or $Y = 1 Or $Y = 12 Then;search 3 or 5 boxes for a mine
        If ($Y = 1 Or $Y = 12) And ($X = 1 Or $X = 12) Then;search 3 boxes on a corner
            Local $BoxesToSearch[4]
            If $Y = 1 And $X = 1 Then;top left
                $BoxesToSearch[1] = $Button[$X + 1][$Y]
                $BoxesToSearch[2] = $Button[$X + 1][$Y + 1]
                $BoxesToSearch[3] = $Button[$X][$Y + 1]
            ElseIf $Y = 1 And $X = 12 Then;top right
                $BoxesToSearch[1] = $Button[$X - 1][$Y]
                $BoxesToSearch[2] = $Button[$X - 1][$Y + 1]
                $BoxesToSearch[3] = $Button[$X][$Y + 1]
            ElseIf $Y = 12 And $X = 1 Then;bottom left
                $BoxesToSearch[1] = $Button[$X + 1][$Y]
                $BoxesToSearch[2] = $Button[$X + 1][$Y - 1]
                $BoxesToSearch[3] = $Button[$X][$Y - 1]
            ElseIf $Y = 12 And $X = 12 Then;bottom right
                $BoxesToSearch[1] = $Button[$X - 1][$Y]
                $BoxesToSearch[2] = $Button[$X - 1][$Y - 1]
                $BoxesToSearch[3] = $Button[$X][$Y - 1]
            EndIf
        Else;search 5 boxes
            Local $BoxesToSearch[6]
            If $X = 1 And $Y <> 1 And $Y <> 12 Then;left side
;ConsoleWrite("++OGM"&@LF)
                $BoxesToSearch[1] = $Button[$X][$Y - 1]
                $BoxesToSearch[2] = $Button[$X][$Y + 1]
                $BoxesToSearch[3] = $Button[$X + 1][$Y - 1]
                $BoxesToSearch[4] = $Button[$X + 1][$Y]
                $BoxesToSearch[5] = $Button[$X + 1][$Y + 1]
            ElseIf $X = 12 And $Y <> 1 And $Y <> 12 Then;right side
;ConsoleWrite("1!OGM"&@LF)
                $BoxesToSearch[1] = $Button[$X][$Y - 1]
                $BoxesToSearch[2] = $Button[$X][$Y + 1]
                $BoxesToSearch[3] = $Button[$X - 1][$Y - 1]
                $BoxesToSearch[4] = $Button[$X - 1][$Y]
                $BoxesToSearch[5] = $Button[$X - 1][$Y + 1]
            ElseIf $X <> 1 And $X <> 12 And $Y = 1 Then;top
;ConsoleWrite("OGM"&@LF)
                $BoxesToSearch[1] = $Button[$X + 1][$Y]
                $BoxesToSearch[2] = $Button[$X - 1][$Y]
                $BoxesToSearch[3] = $Button[$X - 1][$Y + 1]
                $BoxesToSearch[4] = $Button[$X][$Y + 1]
                $BoxesToSearch[5] = $Button[$X + 1][$Y + 1]
            ElseIf $X <> 1 And $X <> 12 And $Y = 12 Then;bottom
;ConsoleWrite("OMG"&@LF)
                $BoxesToSearch[1] = $Button[$X + 1][$Y]
                $BoxesToSearch[2] = $Button[$X - 1][$Y]
                $BoxesToSearch[3] = $Button[$X - 1][$Y - 1]
                $BoxesToSearch[4] = $Button[$X][$Y - 1]
                $BoxesToSearch[5] = $Button[$X + 1][$Y - 1]
            EndIf
        EndIf
    Else;search 8 boxes
        Local $BoxesToSearch[9]
        $BoxesToSearch[1] = $Button[$X + 1][$Y - 1]
        $BoxesToSearch[2] = $Button[$X + 1][$Y]
        $BoxesToSearch[3] = $Button[$X + 1][$Y + 1]
        $BoxesToSearch[4] = $Button[$X][$Y - 1]
        $BoxesToSearch[5] = $Button[$X][$Y + 1]
        $BoxesToSearch[6] = $Button[$X - 1][$Y - 1]
        $BoxesToSearch[7] = $Button[$X - 1][$Y]
        $BoxesToSearch[8] = $Button[$X - 1][$Y + 1]
    EndIf
    
    Local $Search = 0
    For $i = 1 To UBound($BoxesToSearch) - 1
        $Search += _SearchArray($BoxesToSearch[$i], $MinesArray, False)
    Next
    
    Return $Search
EndFunc;==>_Search9Boxes2

;=================================================================================;

Func _Exit()
    Exit
EndFunc;==>_Exit

;=================================================================================;
Edited by SxyfrG

My scripts:AppLauncherTRAY - Awesome app launcher that runs from the system tray NEW VERSION! | Run Length Encoding - VERY simple compression in pure autoit | Simple Minesweeper Game - Fun little game :)My website

Link to comment
Share on other sites

Very nice, Only thing I noticed different from the original was that if you click on a block and it has a number it should reveal all of those same numbers that come in contact with it, and all the same numbers that come in contact with those and so on. so for like yours with 10 mines, it should clear a majority of the screen when you click on a 0. And then you gotta work on the difficulty settings ;) and the smiley face dude at the top. I may have to look at this more when I have time for when I make an attempt at battleship.

Giggity

Link to comment
Share on other sites

Very nice, Only thing I noticed different from the original was that if you click on a block and it has a number it should reveal all of those same numbers that come in contact with it, and all the same numbers that come in contact with those and so on. so for like yours with 10 mines, it should clear a majority of the screen when you click on a 0. And then you gotta work on the difficulty settings ;) and the smiley face dude at the top. I may have to look at this more when I have time for when I make an attempt at battleship.

It was just a test to see if i could make a minesweeper-like game, which i kind of did :D

At the moment my script only checks the 8 boxes around the box you click on, but i'll try implement what you have suggested this afternoon when i get some time.

And what do you mean by work on the difficulty settings? That was a vague statement =\

My scripts:AppLauncherTRAY - Awesome app launcher that runs from the system tray NEW VERSION! | Run Length Encoding - VERY simple compression in pure autoit | Simple Minesweeper Game - Fun little game :)My website

Link to comment
Share on other sites

Works pretty good. It wouldn't be too hard to add right clicking support (I think). Also, you can die on your first click which I think is impossible in the original minesweeper.

Sounds good, i'll see if a WM_COMMAND message is sent when you right click a button and i'll get that working ;)

I'll also fix the problem with you dying on the first click :D

*EDIT*

Nope, no WM_COMMAND message is sent on right click :\

Edited by SxyfrG

My scripts:AppLauncherTRAY - Awesome app launcher that runs from the system tray NEW VERSION! | Run Length Encoding - VERY simple compression in pure autoit | Simple Minesweeper Game - Fun little game :)My website

Link to comment
Share on other sites

I just thought of something, what if you had a dude, and you had to guide him across the mine field. and he has a metal detector so when he's close to a mine it gives a system beep, and if there are more around it beeps louder and faster. call in minesweeper '08

Giggity

Link to comment
Share on other sites

I just thought of something, what if you had a dude, and you had to guide him across the mine field. and he has a metal detector so when he's close to a mine it gives a system beep, and if there are more around it beeps louder and faster. call in minesweeper '08

;) Sounds awesome, i already have a function that searches the 8 boxes around where you click, so it shouldn't be too hard to turn into that game. Only problem is i have exams on (plus revision) for the next 3 weeks so i won't really have time to work on it until they're all over, then i'll give it a shot ;)

In the mean time i'll be checking on this post everyday, so feel free to critique my script, make amendments or try what youknowho4eva suggested :D

My scripts:AppLauncherTRAY - Awesome app launcher that runs from the system tray NEW VERSION! | Run Length Encoding - VERY simple compression in pure autoit | Simple Minesweeper Game - Fun little game :)My website

Link to comment
Share on other sites

  • 3 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...