Jump to content

Tic Tac Toe


Recommended Posts

My first real script that actually seems to work, please report bugs and have fun, I really hope someone has a way to make the AI better since it is just guessing at the moment, this is "Strip Tic Tac Toe" so the pictures aren't included if you want the actual images and sounds private message me.

Big Thanks goes to Enaiman, considering he was the only one interested in helping me, I was doubting this project but with him guiding me through it I learned a bit.

Updates...

- Code Shortened...

- Included Background Picture

Errors...

- Huge Bug... If you take your turn 5 times and win instead of winning it comes back as a tie because the game checks to see how many times you have played... need a better way to check if there is no more moves.

Posted Image

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $SS_REALSIZEIMAGE , $Square[99][99] , $Resource = @ScriptDir & "\Resource\"
$Wins = 0 
$Loses = 0 
$Ties = 0 
$Moves = 0
$Babe = 1
$GUI = GUICreate("Wins " & $Wins & " Loses " & $Loses & " Tied " & $Ties, 1056, 606)
GUISetBkColor(0x000000)
$Board = GUICtrlCreatePic($Resource & "Pictures\Board.bmp", 450 , 0, 606, 606, $SS_REALSIZEIMAGE)
$Square[1][1] = GUICtrlCreateButton("", 460, 10, 180, 180) 
$Square[1][2] = GUICtrlCreateButton("", 660, 10, 180, 180) 
$Square[1][3] = GUICtrlCreateButton("", 860, 10 , 180, 180) 
$Square[2][1] = GUICtrlCreateButton("", 460, 210, 180, 180) 
$Square[2][2] = GUICtrlCreateButton("", 660, 210, 180, 180)
$Square[2][3] = GUICtrlCreateButton("", 860, 210, 180, 180)
$Square[3][1] = GUICtrlCreateButton("", 460, 410, 180, 180)
$Square[3][2] = GUICtrlCreateButton("", 660, 410, 180, 180)
$Square[3][3] = GUICtrlCreateButton("", 860, 410, 180, 180)
$Strip = GUICtrlCreatePic($Resource & "Pictures\1.bmp", 0 , 0, 450, 600, $SS_REALSIZEIMAGE)
GUISetState(@SW_SHOW)
While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Square[1][1] 
            _Win()
            _Block(1 , $Square[1][1])
        Case $Square[1][2]
            _Win()
            _Block(1 , $Square[1][2])
        Case $Square[1][3]
            _Win()
            _Block(1 , $Square[1][3])
        Case $Square[2][1]
            _Win()
            _Block(1 , $Square[2][1])
        Case $Square[2][2]
            _Win()
            _Block(1 , $Square[2][2])
        Case $Square[2][3]
            _Win()
            _Block(1 , $Square[2][3])
        Case $Square[3][1]
            _Win()
            _Block(1 , $Square[3][1])
        Case $Square[3][2]
            _Win()
            _Block(1 , $Square[3][2])
        Case $Square[3][3]
            _Win()
            _Block(1 , $Square[3][3])
    EndSwitch
    _Win()
WEnd

Func _Babe()
    If $Babe = 23 Then
        $Babe = 1
        GUICtrlDelete($Strip) 
        $Strip = GUICtrlCreatePic($Resource & "Pictures\" & $Babe & ".bmp", 0 , 0, 450, 600, $SS_REALSIZEIMAGE)
        SoundPlay($Resource & "Sounds\End.mp3")
        MsgBox(0 , "Game Over" , "You Won")
    Else
        $Babe = $Babe +1
        GUICtrlDelete($Strip) 
        $Strip = GUICtrlCreatePic($Resource & "Pictures\" & $Babe & ".bmp", 0 , 0, 450, 600, $SS_REALSIZEIMAGE)
    EndIf
EndFunc

Func _Block($Player , $Block)
    If $Player = 1 Then
    $Moves = $Moves  +1
    GUICtrlSetState($Block , $GUI_DISABLE)
    GUICtrlSetData($Block , "X")
    GUICtrlSetFont($Block, 30, 800, 0, "Times New Roman")
    If $Moves = 5 Then Return _ClearBoard(3)
    _Computer()
    ElseIf $Player = 2 Then
    GUICtrlSetState($Block , $GUI_DISABLE)
    GUICtrlSetData($Block , "O")
    GUICtrlSetFont($Block, 30, 800, 0, "Times New Roman")
Else        
    MsgBox(0 , "Error" , "Block Error")
EndIf
EndFunc

Func _Computer()
    Do 
        $Num = Random(1 , 3 , 1)
        $Letter = Random(1 , 3 , 1)
    Until GUICtrlRead($Square[$Num][$Letter]) = "" 
    _Block(2 , $Square[$Num][$Letter])
EndFunc

Func _ClearBoard($Winner)
    If $Winner = 1 Then
        $Wins = $Wins +1
        WinSetTitle($GUI , "" , "Wins " & $Wins & " Loses " & $Loses & " Tied " & $Ties)
        SoundPlay($Resource & "Sounds\Good.mp3")
        _Babe()
    ElseIf $Winner = 2  Then
        $Loses = $Loses +1
        WinSetTitle($GUI , "" , "Wins " & $Wins & " Loses " & $Loses & " Tied " & $Ties)
        SoundPlay($Resource & "Sounds\Bad.mp3")
    ElseIf $Winner = 3 Then
        $Ties = $Ties +1
        WinSetTitle($GUI , "" , "Wins " & $Wins & " Loses " & $Loses & " Tied " & $Ties)
        SoundPlay($Resource & "Sounds\Draw.mp3")
    Else
        MsgBox(0 , "Error" , "Winner Error")
    EndIf
    $Moves = 0
    GUICtrlSetData($Square[1][1] , "")
    GUICtrlSetData($Square[1][2] , "")
    GUICtrlSetData($Square[1][3] , "")
    GUICtrlSetData($Square[2][1] , "")
    GUICtrlSetData($Square[2][2] , "")
    GUICtrlSetData($Square[2][3] , "")
    GUICtrlSetData($Square[3][1] , "")
    GUICtrlSetData($Square[3][2] , "")
    GUICtrlSetData($Square[3][3] , "")
    GUICtrlSetState($Square[1][1] , $GUI_ENABLE)
    GUICtrlSetState($Square[1][2] , $GUI_ENABLE)
    GUICtrlSetState($Square[1][3] , $GUI_ENABLE)
    GUICtrlSetState($Square[2][1] , $GUI_ENABLE)
    GUICtrlSetState($Square[2][2] , $GUI_ENABLE)
    GUICtrlSetState($Square[2][3] , $GUI_ENABLE)
    GUICtrlSetState($Square[3][1] , $GUI_ENABLE)
    GUICtrlSetState($Square[3][2] , $GUI_ENABLE)
    GUICtrlSetState($Square[3][3] , $GUI_ENABLE)
EndFunc

Func _Win()
    If GUICtrlRead($Square[1][1]) = "X" And GUICtrlRead($Square[1][2]) = "X" And GUICtrlRead($Square[1][3]) = "X" Then Return _ClearBoard(1)
    If GUICtrlRead($Square[2][1]) = "X" And GUICtrlRead($Square[2][2]) = "X" And GUICtrlRead($Square[2][3]) = "X" Then Return _ClearBoard(1)
    If GUICtrlRead($Square[3][1]) = "X" And GUICtrlRead($Square[3][2]) = "X" And GUICtrlRead($Square[3][3]) = "X" Then Return _ClearBoard(1)
    If GUICtrlRead($Square[1][1]) = "X" And GUICtrlRead($Square[2][1]) = "X" And GUICtrlRead($Square[3][1]) = "X" Then Return _ClearBoard(1)
    If GUICtrlRead($Square[1][2]) = "X" And GUICtrlRead($Square[2][2]) = "X" And GUICtrlRead($Square[3][2]) = "X" Then Return _ClearBoard(1)
    If GUICtrlRead($Square[1][3]) = "X" And GUICtrlRead($Square[2][3]) = "X" And GUICtrlRead($Square[3][3]) = "X" Then Return _ClearBoard(1)
    If GUICtrlRead($Square[1][1]) = "X" And GUICtrlRead($Square[2][2]) = "X" And GUICtrlRead($Square[3][3]) = "X" Then Return _ClearBoard(1)
    If GUICtrlRead($Square[3][1]) = "X" And GUICtrlRead($Square[2][2]) = "X" And GUICtrlRead($Square[1][3]) = "X" Then Return _ClearBoard(1)
    
    If GUICtrlRead($Square[1][1]) = "O" And GUICtrlRead($Square[1][2]) = "O" And GUICtrlRead($Square[1][3]) = "O" Then Return _ClearBoard(2)
    If GUICtrlRead($Square[2][1]) = "O" And GUICtrlRead($Square[2][2]) = "O" And GUICtrlRead($Square[2][3]) = "O" Then Return _ClearBoard(2)
    If GUICtrlRead($Square[3][1]) = "O" And GUICtrlRead($Square[3][2]) = "O" And GUICtrlRead($Square[3][3]) = "O" Then Return _ClearBoard(2)
    If GUICtrlRead($Square[1][1]) = "O" And GUICtrlRead($Square[2][1]) = "O" And GUICtrlRead($Square[3][1]) = "O" Then Return _ClearBoard(2)    
    If GUICtrlRead($Square[1][2]) = "O" And GUICtrlRead($Square[2][2]) = "O" And GUICtrlRead($Square[3][2]) = "O" Then Return _ClearBoard(2)
    If GUICtrlRead($Square[1][3]) = "O" And GUICtrlRead($Square[2][3]) = "O" And GUICtrlRead($Square[3][3]) = "O" Then Return _ClearBoard(2)
    If GUICtrlRead($Square[1][1]) = "O" And GUICtrlRead($Square[2][2]) = "O" And GUICtrlRead($Square[3][3]) = "O" Then Return _ClearBoard(2)
    If GUICtrlRead($Square[3][1]) = "O" And GUICtrlRead($Square[2][2]) = "O" And GUICtrlRead($Square[1][3]) = "O" Then Return _ClearBoard(2)
EndFunc
Edited by JellyFish666
Link to comment
Share on other sites

The game is very easy, but your script is very nice. :)

Thanks that means a lot to me and ill continue to make it smarter, I hope a Autoit expert can help me make it smarter :)

Ill also try to find use friendly pictures as well such as flowers or something :)

Link to comment
Share on other sites

Thanks that means a lot to me and ill continue to make it smarter, I hope a Autoit expert can help me make it smarter :)

Ill also try to find use friendly pictures as well such as flowers or something :)

Anyway, the code is ok with new updates ( Included Background Picture, Code Shortened ) and work fine. That's enought for moment. :)

When the words fail... music speaks.

Link to comment
Share on other sites

I just wrote a simple tic-tac-toe game for you to study, it has a "perfect" bot that is very hard to beat :)

#include <GUIConstantsEx.au3>

Global $board[9][2]
; [x][0]= CtrlId
; [x][1]= flag (1=X 2=O)
Global $p1, $p2
Opt("GUIOnEventMode", 1)
GUICreate("Tic Tac Toe", 170, 170)
$y = 10
$x = 10
For $i = 0 To UBound($board) - 1 ; Draw board
    $board[$i][0] = GUICtrlCreateButton(" ", $x, $y, 50, 50)
    GUICtrlSetOnEvent(-1, "_HumanTurn")
    If $x >= 70 Then
        $y += 50
        $x = 10
    Else
        $x += 50
    EndIf

Next

GUISetOnEvent($GUI_EVENT_CLOSE, "close")
GUISetState()

Do
    Sleep(10)
Until False

Func _HumanTurn() ; fired when pressing a button
    For $i = 0 To UBound($board) - 1
        If @GUI_CtrlId = $board[$i][0] And $board[$i][1] = 0 Then ; If the clicked button was empty
            GUICtrlSetData(@GUI_CtrlId, "X")
            $board[$i][1] = 1
            $result = _CheckWin($board, 1)
            If $result Then
                _Win(1)
                Return
            EndIf
            _BotTurn()
            Return
        EndIf
    Next
    
EndFunc   ;==>_HumanTurn

Func _BotTurn()
    ; Loops through all empty squares
    For $i = 0 To UBound($board) - 1
        If $board[$i][1] = 0 Then ; Found a empty square
            ; Check if we win by checking
            $board[$i][1] = 2
            If _CheckWin($board, 2) Then ; We won :)
                GUICtrlSetData($board[$i][0], "O")
                _Win(2)
                Return
            EndIf
            $board[$i][1] = 0
        EndIf
    Next
    
    For $i = 0 To UBound($board) - 1
        If $board[$i][1] = 0 Then ; Found a empty square
            ; Check if someone else win by checking
            $board[$i][1] = 1
            If _CheckWin($board, 1) Then ; He wins if not stopping!
                GUICtrlSetData($board[$i][0], "O")
                $board[$i][1] = 2
                Return
            EndIf
            $board[$i][1] = 0
        EndIf
    Next
    
    ; If we have to this just place random
    $inc = 0
    Do
        $t = Random(0, 8, 1)
        $inc += 1
        If $inc > 100 Then
            _Win(0)
            Return
        EndIf
        
    Until $board[$t][1] = 0
    GUICtrlSetData($board[$t][0], "O")
    $board[$t][1] = 2
    
EndFunc   ;==>_BotTurn

Func _CheckWin($data, $player)
    If $player = 1 Then
        $product = 1
    Else
        $product = 8
    EndIf
    
    ; Simple stuff
    If $data[0][1] * $data[1][1] * $data[2][1] = $product Then Return True
    If $data[3][1] * $data[4][1] * $data[5][1] = $product Then Return True
    If $data[6][1] * $data[7][1] * $data[8][1] = $product Then Return True
    If $data[0][1] * $data[3][1] * $data[6][1] = $product Then Return True
    If $data[1][1] * $data[4][1] * $data[7][1] = $product Then Return True
    If $data[2][1] * $data[5][1] * $data[8][1] = $product Then Return True
    If $data[0][1] * $data[4][1] * $data[8][1] = $product Then Return True
    If $data[2][1] * $data[4][1] * $data[6][1] = $product Then Return True
EndFunc   ;==>_CheckWin


Func close()
    MsgBox(0,"Results:","Player 1: "&$p1&@CRLF&"Bot: "&$p2)
    Exit
EndFunc   ;==>close

Func _Win($who)
    If $who = 1 Then
        MsgBox(0, "Victory", "You won!")
        $p1 += 1
    ElseIf $who = 2 Then
        MsgBox(0, "Fail", "You lost...")
        $p2 += 1
    Else
        MsgBox(0, "Draw", "Nobody won")
    EndIf
    For $i = 0 To UBound($board) - 1
        $board[$i][1] = 0
        GUICtrlSetData($board[$i][0], " ")
    Next
EndFunc   ;==>_Win

I tried commenting some important parts.

Feel free to copy if there is something in there you like :)

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

I just wrote a simple tic-tac-toe game for you to study, it has a "perfect" bot that is very hard to beat :)

#include <GUIConstantsEx.au3>

Global $board[9][2]
; [x][0]= CtrlId
; [x][1]= flag (1=X 2=O)
Global $p1, $p2
Opt("GUIOnEventMode", 1)
GUICreate("Tic Tac Toe", 170, 170)
$y = 10
$x = 10
For $i = 0 To UBound($board) - 1 ; Draw board
    $board[$i][0] = GUICtrlCreateButton(" ", $x, $y, 50, 50)
    GUICtrlSetOnEvent(-1, "_HumanTurn")
    If $x >= 70 Then
        $y += 50
        $x = 10
    Else
        $x += 50
    EndIf

Next

GUISetOnEvent($GUI_EVENT_CLOSE, "close")
GUISetState()

Do
    Sleep(10)
Until False

Func _HumanTurn() ; fired when pressing a button
    For $i = 0 To UBound($board) - 1
        If @GUI_CtrlId = $board[$i][0] And $board[$i][1] = 0 Then ; If the clicked button was empty
            GUICtrlSetData(@GUI_CtrlId, "X")
            $board[$i][1] = 1
            $result = _CheckWin($board, 1)
            If $result Then
                _Win(1)
                Return
            EndIf
            _BotTurn()
            Return
        EndIf
    Next
    
EndFunc   ;==>_HumanTurn

Func _BotTurn()
    ; Loops through all empty squares
    For $i = 0 To UBound($board) - 1
        If $board[$i][1] = 0 Then ; Found a empty square
            ; Check if we win by checking
            $board[$i][1] = 2
            If _CheckWin($board, 2) Then ; We won :)
                GUICtrlSetData($board[$i][0], "O")
                _Win(2)
                Return
            EndIf
            $board[$i][1] = 0
        EndIf
    Next
    
    For $i = 0 To UBound($board) - 1
        If $board[$i][1] = 0 Then ; Found a empty square
            ; Check if someone else win by checking
            $board[$i][1] = 1
            If _CheckWin($board, 1) Then ; He wins if not stopping!
                GUICtrlSetData($board[$i][0], "O")
                $board[$i][1] = 2
                Return
            EndIf
            $board[$i][1] = 0
        EndIf
    Next
    
    ; If we have to this just place random
    $inc = 0
    Do
        $t = Random(0, 8, 1)
        $inc += 1
        If $inc > 100 Then
            _Win(0)
            Return
        EndIf
        
    Until $board[$t][1] = 0
    GUICtrlSetData($board[$t][0], "O")
    $board[$t][1] = 2
    
EndFunc   ;==>_BotTurn

Func _CheckWin($data, $player)
    If $player = 1 Then
        $product = 1
    Else
        $product = 8
    EndIf
    
    ; Simple stuff
    If $data[0][1] * $data[1][1] * $data[2][1] = $product Then Return True
    If $data[3][1] * $data[4][1] * $data[5][1] = $product Then Return True
    If $data[6][1] * $data[7][1] * $data[8][1] = $product Then Return True
    If $data[0][1] * $data[3][1] * $data[6][1] = $product Then Return True
    If $data[1][1] * $data[4][1] * $data[7][1] = $product Then Return True
    If $data[2][1] * $data[5][1] * $data[8][1] = $product Then Return True
    If $data[0][1] * $data[4][1] * $data[8][1] = $product Then Return True
    If $data[2][1] * $data[4][1] * $data[6][1] = $product Then Return True
EndFunc   ;==>_CheckWin


Func close()
    MsgBox(0,"Results:","Player 1: "&$p1&@CRLF&"Bot: "&$p2)
    Exit
EndFunc   ;==>close

Func _Win($who)
    If $who = 1 Then
        MsgBox(0, "Victory", "You won!")
        $p1 += 1
    ElseIf $who = 2 Then
        MsgBox(0, "Fail", "You lost...")
        $p2 += 1
    Else
        MsgBox(0, "Draw", "Nobody won")
    EndIf
    For $i = 0 To UBound($board) - 1
        $board[$i][1] = 0
        GUICtrlSetData($board[$i][0], " ")
    Next
EndFunc   ;==>_Win

I tried commenting some important parts.

Feel free to copy if there is something in there you like :)

means a lot to me and ill be sure to study your script, I am very stupid with theses kinds of scripts that have a AI, although I am not very good at Autoit to start with and if anyone else had idea's it wouldn't hurt I am learning everyday :)

[edit]

Very Nice script the AI is wonderful, I don't mean to be rude or anything but I got something against

Opt("GUIOnEventMode", 1)

lol don't even know why but I try not to use it as much as possible but I don't even like my Variables being renamed in my scripts for some reason I am a very weird coder.

I guess I don't like it because I have to code my GUI a way I am not used to...

@GUI_CtrlId

is helpful and so is your code and ill still learn a lot from it.

Edited by JellyFish666
Link to comment
Share on other sites

Very nice one monoceros :)

It plays well enough. The problem with random answers is the bot will make a wrong move thus allowing you to win.

Tic-Tac-Toe is a game which can't be won if the opponents know how to play. It will be always a tie.

I like the code - it is effective indeed.

Some time ago I've made myself a game like this and my purpose was to make it invincible. Took a couple tests and tries but finally I've improved the AI so it is unbeatable ...

:) ... I'm just thinking ... who will play a game with no chance of winning? ...

When my time will allow it, I'll maybe edit my script to add difficulty levels ... not sure when this will be though ...

Good Job JellyFish666 :)

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

Very nice one monoceros :)

It plays well enough. The problem with random answers is the bot will make a wrong move thus allowing you to win.

Tic-Tac-Toe is a game which can't be won if the opponents know how to play. It will be always a tie.

I like the code - it is effective indeed.

Some time ago I've made myself a game like this and my purpose was to make it invincible. Took a couple tests and tries but finally I've improved the AI so it is unbeatable ...

:) ... I'm just thinking ... who will play a game with no chance of winning? ...

When my time will allow it, I'll maybe edit my script to add difficulty levels ... not sure when this will be though ...

Good Job JellyFish666 :)

Thanks, still aways to go... one error and then I need to make it smarter.

Link to comment
Share on other sites

[edit]

Very Nice script the AI is wonderful, I don't mean to be rude or anything but I got something against

Opt("GUIOnEventMode", 1)

lol don't even know why but I try not to use it as much as possible but I don't even like my Variables being renamed in my scripts for some reason I am a very weird coder.

I guess I don't like it because I have to code my GUI a way I am not used to...

@GUI_CtrlId

is helpful and so is your code and ill still learn a lot from it.

You know it was good practise for me too, first game I ever made under one hour :)

The reason why I use OnEvent mode is tht I feel you get cleaner code and that you get better control over your main loop.

Check this examples to see what I mean:

; Using GUIGetMsg()
GUICreate("Progress",400,70)
$progress=GUICtrlCreateProgress(10,10,380,50)

GUICtrlSetLimit(-1,100)
GUISetSTate()
$inc=0
Do
    $msg=GUIGetMsg()
    $inc+=0.1
    GUICtrlSetData($progress,$inc)
    If $inc>=100 Then $inc=0
Until $msg=-3
GUISetState(@SW_HIDE)

; Using OnEvent
Opt("GuiOnEventMode",1)
GUICreate("Progress",400,70)
$progress=GUICtrlCreateProgress(10,10,380,50)
GUICtrlSetLimit(-1,100)
GUISetOnEvent(-3,"close")
GUISetSTate()
$inc=0
Do
    sleep(5)
    $inc+=0.1
    GUICtrlSetData($progress,$inc)
    If $inc>=100 Then $inc=0
Until False

Func close ()
    Exit
EndFunc

Try unfocus the winow that uses GUIGetMsg() and you will see that it will run at different speeds.

Just my 5 cents.

:)

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

I just wrote a simple tic-tac-toe game for you to study, it has a "perfect" bot that is very hard to beat :)

#include <GUIConstantsEx.au3>

Global $board[9][2]
; [x][0]= CtrlId
; [x][1]= flag (1=X 2=O)
Global $p1, $p2
Opt("GUIOnEventMode", 1)
GUICreate("Tic Tac Toe", 170, 170)
$y = 10
$x = 10
For $i = 0 To UBound($board) - 1 ; Draw board
    $board[$i][0] = GUICtrlCreateButton(" ", $x, $y, 50, 50)
    GUICtrlSetOnEvent(-1, "_HumanTurn")
    If $x >= 70 Then
        $y += 50
        $x = 10
    Else
        $x += 50
    EndIf

Next

GUISetOnEvent($GUI_EVENT_CLOSE, "close")
GUISetState()

Do
    Sleep(10)
Until False

Func _HumanTurn() ; fired when pressing a button
    For $i = 0 To UBound($board) - 1
        If @GUI_CtrlId = $board[$i][0] And $board[$i][1] = 0 Then ; If the clicked button was empty
            GUICtrlSetData(@GUI_CtrlId, "X")
            $board[$i][1] = 1
            $result = _CheckWin($board, 1)
            If $result Then
                _Win(1)
                Return
            EndIf
            _BotTurn()
            Return
        EndIf
    Next
    
EndFunc   ;==>_HumanTurn

Func _BotTurn()
    ; Loops through all empty squares
    For $i = 0 To UBound($board) - 1
        If $board[$i][1] = 0 Then ; Found a empty square
            ; Check if we win by checking
            $board[$i][1] = 2
            If _CheckWin($board, 2) Then ; We won :)
                GUICtrlSetData($board[$i][0], "O")
                _Win(2)
                Return
            EndIf
            $board[$i][1] = 0
        EndIf
    Next
    
    For $i = 0 To UBound($board) - 1
        If $board[$i][1] = 0 Then ; Found a empty square
            ; Check if someone else win by checking
            $board[$i][1] = 1
            If _CheckWin($board, 1) Then ; He wins if not stopping!
                GUICtrlSetData($board[$i][0], "O")
                $board[$i][1] = 2
                Return
            EndIf
            $board[$i][1] = 0
        EndIf
    Next
    
    ; If we have to this just place random
    $inc = 0
    Do
        $t = Random(0, 8, 1)
        $inc += 1
        If $inc > 100 Then
            _Win(0)
            Return
        EndIf
        
    Until $board[$t][1] = 0
    GUICtrlSetData($board[$t][0], "O")
    $board[$t][1] = 2
    
EndFunc   ;==>_BotTurn

Func _CheckWin($data, $player)
    If $player = 1 Then
        $product = 1
    Else
        $product = 8
    EndIf
    
    ; Simple stuff
    If $data[0][1] * $data[1][1] * $data[2][1] = $product Then Return True
    If $data[3][1] * $data[4][1] * $data[5][1] = $product Then Return True
    If $data[6][1] * $data[7][1] * $data[8][1] = $product Then Return True
    If $data[0][1] * $data[3][1] * $data[6][1] = $product Then Return True
    If $data[1][1] * $data[4][1] * $data[7][1] = $product Then Return True
    If $data[2][1] * $data[5][1] * $data[8][1] = $product Then Return True
    If $data[0][1] * $data[4][1] * $data[8][1] = $product Then Return True
    If $data[2][1] * $data[4][1] * $data[6][1] = $product Then Return True
EndFunc   ;==>_CheckWin


Func close()
    MsgBox(0,"Results:","Player 1: "&$p1&@CRLF&"Bot: "&$p2)
    Exit
EndFunc   ;==>close

Func _Win($who)
    If $who = 1 Then
        MsgBox(0, "Victory", "You won!")
        $p1 += 1
    ElseIf $who = 2 Then
        MsgBox(0, "Fail", "You lost...")
        $p2 += 1
    Else
        MsgBox(0, "Draw", "Nobody won")
    EndIf
    For $i = 0 To UBound($board) - 1
        $board[$i][1] = 0
        GUICtrlSetData($board[$i][0], " ")
    Next
EndFunc   ;==>_Win

I tried commenting some important parts.

Feel free to copy if there is something in there you like :)

The code work fine but the game is easy. You can make the bot to select the middle square if this is empty, otherwise I win the game everytime.

When the words fail... music speaks.

Link to comment
Share on other sites

You know it was good practise for me too, first game I ever made under one hour :)

The reason why I use OnEvent mode is tht I feel you get cleaner code and that you get better control over your main loop.

Check this examples to see what I mean:

; Using GUIGetMsg()
GUICreate("Progress",400,70)
$progress=GUICtrlCreateProgress(10,10,380,50)

GUICtrlSetLimit(-1,100)
GUISetSTate()
$inc=0
Do
    $msg=GUIGetMsg()
    $inc+=0.1
    GUICtrlSetData($progress,$inc)
    If $inc>=100 Then $inc=0
Until $msg=-3
GUISetState(@SW_HIDE)

; Using OnEvent
Opt("GuiOnEventMode",1)
GUICreate("Progress",400,70)
$progress=GUICtrlCreateProgress(10,10,380,50)
GUICtrlSetLimit(-1,100)
GUISetOnEvent(-3,"close")
GUISetSTate()
$inc=0
Do
    sleep(5)
    $inc+=0.1
    GUICtrlSetData($progress,$inc)
    If $inc>=100 Then $inc=0
Until False

Func close ()
    Exit
EndFunc

Try unfocus the winow that uses GUIGetMsg() and you will see that it will run at different speeds.

Just my 5 cents.

:)

wow may be I should give it a chance it is just I suck at coding and using "on event" means I have to code a little differently.
Link to comment
Share on other sites

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...