Jump to content

Recommended Posts

Posted

Hello

I was wondering can someone tell me whats wrong with this code, and also are all my computer block checks correct.

#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <WindowsConstants.au3>

Global $TacBlock[10] , $Player = "X" , $Computer = "O" , $WinnerIs , $WinnerMark
$TicTac = GUICreate("Tic Tac", 150 , 150)
$TacBlock[1] = GUICtrlCreateButton("", 0, 0, 50, 50)
$TacBlock[2] = GUICtrlCreateButton("", 50, 0, 50, 50)
$TacBlock[3] = GUICtrlCreateButton("", 100, 0, 50, 50)
$TacBlock[4] = GUICtrlCreateButton("", 0, 50, 50, 50)
$TacBlock[5] = GUICtrlCreateButton("", 50, 50, 50, 50)
$TacBlock[6] = GUICtrlCreateButton("", 100, 50, 50, 50)
$TacBlock[7] = GUICtrlCreateButton("", 0, 100, 50, 50)
$TacBlock[8] = GUICtrlCreateButton("", 50, 100, 50, 50)
$TacBlock[9] = GUICtrlCreateButton("", 100, 100, 50, 50)
GUISetState(@SW_SHOW)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $TacBlock[1]
            _Player($TacBlock[1])
            _SmartComputer()
        Case $TacBlock[2]
            _Player($TacBlock[2])
            _SmartComputer()
        Case $TacBlock[3]
            _Player($TacBlock[3])
            _SmartComputer()
        Case $TacBlock[4]
            _Player($TacBlock[4])
            _SmartComputer()
        Case $TacBlock[5]
            _Player($TacBlock[5])
            _SmartComputer()
        Case $TacBlock[6]
            _Player($TacBlock[6])
            _SmartComputer()
        Case $TacBlock[7]
            _Player($TacBlock[7])
            _SmartComputer()
        Case $TacBlock[8]
            _Player($TacBlock[8])
            _SmartComputer()
        Case $TacBlock[9]
            _Player($TacBlock[9])
            _SmartComputer()
    EndSwitch
WEnd

Func _Player($PlayerBlock)
    If GUICtrlRead($PlayerBlock) = "" Then
        GUICtrlSetData($PlayerBlock , $Player)
        GUICtrlSetState($PlayerBlock , $GUI_DISABLE)
    EndIf
    _CheckWin($Player)
EndFunc

Func _SmartComputer()
    If GUICtrlRead($TacBlock[1]) <> "" And GUICtrlRead($TacBlock[2]) <> "" Then 
        _ComputerBlock($TacBlock[3])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[1]) <> "" And GUICtrlRead($TacBlock[3]) <> "" Then 
        _ComputerBlock($TacBlock[2])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[3]) <> "" And GUICtrlRead($TacBlock[2]) <> "" Then 
        _ComputerBlock($TacBlock[1])
        _CheckWin($Computer)
    EndIf

    If GUICtrlRead($TacBlock[4]) <> "" And GUICtrlRead($TacBlock[5]) <> "" Then 
        _ComputerBlock($TacBlock[6])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[4]) <> "" And GUICtrlRead($TacBlock[6]) <> "" Then 
        _ComputerBlock($TacBlock[5])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[6]) <> "" And GUICtrlRead($TacBlock[5]) <> "" Then 
        _ComputerBlock($TacBlock[4])
        _CheckWin($Computer)
    EndIf

    If GUICtrlRead($TacBlock[7]) <> "" And GUICtrlRead($TacBlock[8]) <> "" Then 
        _ComputerBlock($TacBlock[9])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[7]) <> "" And GUICtrlRead($TacBlock[9]) <> "" Then
        _ComputerBlock($TacBlock[8])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[9]) <> "" And GUICtrlRead($TacBlock[8]) <> "" Then 
        _ComputerBlock($TacBlock[7])
        _CheckWin($Computer)
    EndIf

    If GUICtrlRead($TacBlock[1]) <> "" And GUICtrlRead($TacBlock[4]) <> "" Then 
        _ComputerBlock($TacBlock[7])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[1]) <> "" And GUICtrlRead($TacBlock[7]) <> "" Then 
        _ComputerBlock($TacBlock[4])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[4]) <> "" And GUICtrlRead($TacBlock[7]) <> "" Then 
        _ComputerBlock($TacBlock[1])
        _CheckWin($Computer)
    EndIf

    If GUICtrlRead($TacBlock[2]) <> "" And GUICtrlRead($TacBlock[5]) <> "" Then 
        _ComputerBlock($TacBlock[8])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[2]) <> "" And GUICtrlRead($TacBlock[8]) <> "" Then 
        _ComputerBlock($TacBlock[5])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[5]) <> "" And GUICtrlRead($TacBlock[8]) <> "" Then 
        _ComputerBlock($TacBlock[2])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[3]) <> "" And GUICtrlRead($TacBlock[6]) <> "" Then 
        _ComputerBlock($TacBlock[9])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[3]) <> "" And GUICtrlRead($TacBlock[9]) <> "" Then 
        _ComputerBlock($TacBlock[6])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[6]) <> "" And GUICtrlRead($TacBlock[9]) <> "" Then 
        _ComputerBlock($TacBlock[3])
        _CheckWin($Computer)
    EndIf

    If GUICtrlRead($TacBlock[1]) <> "" And GUICtrlRead($TacBlock[5]) <> "" Then 
        _ComputerBlock($TacBlock[9])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[1]) <> "" And GUICtrlRead($TacBlock[9]) <> "" Then 
        _ComputerBlock($TacBlock[5])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[9]) <> "" And GUICtrlRead($TacBlock[5]) <> "" Then 
        _ComputerBlock($TacBlock[1])
        _CheckWin($Computer)
    EndIf

    If GUICtrlRead($TacBlock[3]) <> "" And GUICtrlRead($TacBlock[5]) <> "" Then 
        _ComputerBlock($TacBlock[7])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[3]) <> "" And GUICtrlRead($TacBlock[7]) <> "" Then 
        _ComputerBlock($TacBlock[5])
        _CheckWin($Computer)
    EndIf
    
    If GUICtrlRead($TacBlock[7]) <> "" And GUICtrlRead($TacBlock[5]) <> "" Then 
        _ComputerBlock($TacBlock[3])
        _CheckWin($Computer)
    EndIf
    
    
EndFunc

Func _ComputerBlock($PCBlock)
    If GUICtrlRead($PCBlock) = "" Then
        GUICtrlSetData($PCBlock , $Computer)
        GUICtrlSetState($PCBlock , $GUI_DISABLE)
    EndIf
EndFunc

Func _CheckWin($WinnerMark)
    If GUICtrlRead($TacBlock[1]) And GUICtrlRead($TacBlock[2]) And GUICtrlRead($TacBlock[3]) = $WinnerMark Then _ShowWin($TacBlock[1] , $TacBlock[2] , $TacBlock[3] , $WinnerMark)
    If GUICtrlRead($TacBlock[4]) And GUICtrlRead($TacBlock[5]) And GUICtrlRead($TacBlock[6]) = $WinnerMark Then _ShowWin($TacBlock[4] , $TacBlock[5] , $TacBlock[6] , $WinnerMark)
    If GUICtrlRead($TacBlock[7]) And GUICtrlRead($TacBlock[8]) And GUICtrlRead($TacBlock[9]) = $WinnerMark Then _ShowWin($TacBlock[7] , $TacBlock[8] , $TacBlock[9] , $WinnerMark)
    If GUICtrlRead($TacBlock[1]) And GUICtrlRead($TacBlock[4]) And GUICtrlRead($TacBlock[7]) = $WinnerMark Then _ShowWin($TacBlock[1] , $TacBlock[4] , $TacBlock[7] , $WinnerMark)
    If GUICtrlRead($TacBlock[2]) And GUICtrlRead($TacBlock[5]) And GUICtrlRead($TacBlock[8]) = $WinnerMark Then _ShowWin($TacBlock[2] , $TacBlock[5] , $TacBlock[8] , $WinnerMark)
    If GUICtrlRead($TacBlock[3]) And GUICtrlRead($TacBlock[6]) And GUICtrlRead($TacBlock[9]) = $WinnerMark Then _ShowWin($TacBlock[3] , $TacBlock[6] , $TacBlock[9] , $WinnerMark)
    If GUICtrlRead($TacBlock[1]) And GUICtrlRead($TacBlock[5]) And GUICtrlRead($TacBlock[9]) = $WinnerMark Then _ShowWin($TacBlock[1] , $TacBlock[5] , $TacBlock[9] , $WinnerMark)
    If GUICtrlRead($TacBlock[3]) And GUICtrlRead($TacBlock[5]) And GUICtrlRead($TacBlock[7]) = $WinnerMark Then _ShowWin($TacBlock[3] , $TacBlock[5] , $TacBlock[7] , $WinnerMark)
EndFunc

Func _ShowWin($WinBlock1 , $WinBlock2 , $WinBlock3 ,$Winner)
    For $Disable = 1 To 9 Step +1
        GUICtrlSetState($TacBlock[$Disable] , $GUI_DISABLE)
    Next
    GUICtrlSetBkColor($WinBlock1, 0x00FF00)
    GUICtrlSetBkColor($WinBlock2, 0x00FF00)
    GUICtrlSetBkColor($WinBlock3, 0x00FF00)
    MsgBox(0 , "Winner" , "Winner is " & $Winner)
EndFunc
Posted

The _CheckWin doesn't check if the input are equivalent in that x = x or o = o. You need also to check if GUICtrlRead($TacBlock[1]) = 'x' And ....

I think this kind of tedious code is not ideal, you can do it much better by preforming a recursive function with logic behind it ;] google it.

Also, after the is a successful _CheckWin, you need to return from the function or preform the MsgBox only after returning from the function, in my opinion at least.

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
×
×
  • Create New...