Jump to content

Wanna be "Unbeatable"


enaiman
 Share

Recommended Posts

I thought that would be better to make a thread here than "highjacking" the A.I. thread from "Help and Support"

The idea about making this came from that topic and I felt "challenged" to make this script.

Since Tic-Tac-Toe is impossible to be won if the players play it correctly (only a mistake can lead to victory) I thought I can make it this way.

Couple of users tested it and found situations where they won - I did my homework and took care of those patterns and improved this way my initial script.

I would be glad if there would be further testers and if I could get feedback on their findings (because my aim is to make it really unbeatable).

Here is the code (I'm sure there is some redundant code but I'll "refine it" during the next few days)

#include <GUIConstants.au3>
#include <ARRAY.AU3>

Global $turn = True;true means player false means ai
Global $grid[4][4]
Global $button[4][4]
Global $string_eval[9]
Global $content[9]
Global $moves = 0
Global $computer_win = 0
Global $total = 0
Global $playerwin = 0

$content[1] = "31,32,33"
$content[2] = "21,22,23"
$content[3] = "11,12,13"
$content[4] = "11,22,33"
$content[5] = "11,21,31"
$content[6] = "12,22,32"
$content[7] = "13,23,33"
$content[8] = "13,22,31"

$Form1 = GUICreate("Beat-me Tic-Tac-Toe", 326, 347, 193, 125)
GUICtrlCreateLabel("Computer Win", 10, 10, 71, 17)
$cwin = GUICtrlCreateLabel("00", 90, 10, 25, 17)
GUICtrlCreateLabel("Player Win", 140, 10, 55, 17)
$pwin = GUICtrlCreateLabel("00", 205, 10, 25, 17)
GUICtrlCreateLabel("Games", 250, 10, 37, 17)
$games = GUICtrlCreateLabel("00", 295, 10, 16, 17)
$Button[1][1] = GUICtrlCreateButton("", 44, 72, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[1][2] = GUICtrlCreateButton("", 134, 72, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[1][3] = GUICtrlCreateButton("", 224, 72, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[2][3] = GUICtrlCreateButton("", 224, 152, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[2][1] = GUICtrlCreateButton("", 44, 152, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[2][2] = GUICtrlCreateButton("", 134, 152, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[3][3] = GUICtrlCreateButton("", 224, 232, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[3][2] = GUICtrlCreateButton("", 134, 232, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[3][1] = GUICtrlCreateButton("", 44, 232, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Label1 = GUICtrlCreateLabel("Press any Button", 100, 40, 200, 17)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$reset = GUICtrlCreateButton("Reset", 112, 312, 97, 25, 0)
GUISetState(@SW_SHOW)
For $i = 1 to 3;column
    For $j = 1 to 3;row
        $grid[$i][$j] = 0
    Next
Next

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button [1][1]
            If $grid[1][1] = 0 And BitAND(GUICtrlGetState($button [1][1]),$GUI_ENABLE) Then
                _clicked (1,1)
            EndIf
         Case $button [1][2]
            If $grid[1][2] = 0 And BitAND(GUICtrlGetState($button [1][2]),$GUI_ENABLE) Then
                _clicked (1,2)
            EndIf

        Case $button [1][3]
            If $grid[1][3] = 0 And BitAND(GUICtrlGetState($button [1][3]),$GUI_ENABLE) Then
                _clicked (1,3)
            EndIf

        Case $button [2][1]
            If $grid[2][1] = 0 And BitAND(GUICtrlGetState($button [2][1]),$GUI_ENABLE) Then
                _clicked (2,1)
            EndIf

        Case $button [2][2]
            If $grid[2][2] = 0 And BitAND(GUICtrlGetState($button [2][2]),$GUI_ENABLE) Then
                _clicked (2,2)
            EndIf

        Case $button [2][3]
            If $grid[2][3] = 0 And BitAND(GUICtrlGetState($button [2][3]),$GUI_ENABLE) Then
                _clicked (2,3)
            EndIf

        Case $button [3][1]
            If $grid[3][1] = 0 And BitAND(GUICtrlGetState($button [3][1]),$GUI_ENABLE) Then
                _clicked (3,1)
            EndIf

        Case $button[3][2]
             If $grid[3][2] = 0 And BitAND(GUICtrlGetState($button [3][2]),$GUI_ENABLE) Then
                _clicked (3,2)
            EndIf

        Case $button [3][3]
            If $grid[3][3] = 0 And BitAND(GUICtrlGetState($button [3][3]),$GUI_ENABLE) Then
                _clicked (3,3)
            EndIf
        Case $reset
            _ResetGrid()
    EndSwitch
    If $moves = 9 Then
        Select
            Case $computer_win = 1
                GUICtrlSetData ($Label1, "Computer Wins")
                $moves = 0
            Case $playerwin = 1
                GUICtrlSetData ($Label1, "You Win - you shouldn't have.")
                $moves = 0
            Case $computer_win = 0 And $playerwin = 0
                GUICtrlSetData ($Label1, "Nobody won")
                $moves = 0
        EndSelect
    EndIf
WEnd

Func string_eval()
    For $j=1 To 8
        $string_eval[$j] = 0
    Next
    For $j=1 To 3
        $string_eval[1] +=  $grid[3][$j]
        $string_eval[2] +=  $grid[2][$j]
        $string_eval[3] +=  $grid[1][$j]
        $string_eval[5] +=  $grid[$j][1]
        $string_eval[6] +=  $grid[$j][2]
        $string_eval[7] +=  $grid[$j][3]
    Next
    $string_eval[4] =  $grid[1][1]+$grid[2][2]+$grid[3][3]
    $string_eval[8] =  $grid[1][3]+$grid[2][2]+$grid[3][1]
EndFunc

Func _clicked ($row, $column)
    If $moves = 9 Then Return
    Local $found_win = 0
        If $turn = true Then
            If BitAND(GUICtrlGetState($button [$row][$column]),$GUI_ENABLE) Then
                _SetButton($button[$row][$column], $grid[$row][$column], 5, "Thinking...", "X")
                $turn = False
                string_eval()
                For $i=1 To 8
                    If $string_eval[$i] = 15 Then   ;player won
                        $playerwin = 1
                        $moves = 9
                        Return
                    EndIf
                Next
                If $moves = 9 Then Return
            Else
                Return
            EndIf
        EndIf
        If $turn = False Then
            Select
                Case $moves = 1
                    If $grid[2][2] = 5 Then
                        _SetButton($button[1][1], $grid[1][1], 1, "Your Turn...")
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                    If $grid[2][2] = 0 Then
                        _SetButton($button[2][2], $grid[2][2], 1, "Your Turn...")
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                Case $moves >2
                    string_eval()
                    For $i=1 To 8
                        If $string_eval[$i] = 2 Then    ;go for win
                            $found_win = 1
                            $row_played = StringSplit($content[$i], ",")
                            For $k=1 To 3
                                $ids = StringSplit($row_played[$k], "")
                                If $grid[$ids[1]][$ids[2]] = 0 Then     ;3rd empty - play there
                                    GUICtrlSetData ($button[$ids[1]][$ids[2]], "O")
                                    GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable)
                                    $computer_win=1
                                    $grid[$ids[1]][$ids[2]] = 1
                                    $moves = 9
                                    Return
                                EndIf
                            Next
                        EndIf
                    Next
                    For $i=1 To 8
                        If $string_eval[$i] = 10 Then   ;2 in a row from player
                            $row_played = StringSplit($content[$i], ",")
                            For $k=1 To 3
                                $ids = StringSplit($row_played[$k], "")
                                If $grid[$ids[1]][$ids[2]] = 0 Then     ;3rd empty - play there
                                    _SetButton($button[$ids[1]][$ids[2]], $grid[$ids[1]][$ids[2]], 1, "Your Turn...")
                                    If $moves = 9 Then Return
                                    $turn = True
                                    Return
                                EndIf
                            Next
                        EndIf
                    Next
                    If $string_eval[4] = 11 Then
                        If $grid [1][1] = 5 And $grid [3][3] = 5 And $moves < 4 Then
                            _SetButton($button[1][2], $grid[1][2], 1, "Your Turn...")
                            If $moves = 9 Then Return
                            $turn = True
                            Return
                        EndIf
                        If $grid [2][2] = 5 And $grid [3][3] = 5 And $moves < 4 Then
                            _SetButton($button[1][3], $grid[1][3], 1, "Your Turn...")
                            If $moves = 9 Then Return
                            $turn = True
                            Return
                        EndIf
                    EndIf
                    If $grid [1][2] = 5 And $grid [3][3] = 5 And $moves < 4 Then
                        _SetButton($button[1][3], $grid[1][3], 1, "Your Turn...")
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                    If $grid [2][3] = 5 And $grid [3][2] = 5 And $moves < 4 Then
                        _SetButton($button[3][3], $grid[3][3], 1, "Your Turn...")
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                    If $grid [1][2] = 5 And $grid [3][1] = 5 And $moves < 4 Then
                        _SetButton($button[1][1], $grid[1][1], 1, "Your Turn...")
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                    For $x=1 To 8
                        If $string_eval[$x] <> 10 And $string_eval[$x] <> 2 Then    ;2 in a row from player
                            If $string_eval[$x] = 1 Then
                                $row_played = StringSplit($content[$x], ",")
                                For $k=1 To 3
                                    $ids = StringSplit($row_played[$k], "")
                                    If $grid[$ids[1]][$ids[2]] = 0 Then     ;3rd empty - play there
                                        _SetButton($button[$ids[1]][$ids[2]], $grid[$ids[1]][$ids[2]], 1, "Your Turn...")
                                        If $moves = 9 Then Return
                                        $turn = True
                                        Return
                                    EndIf
                                Next
                            ElseIf $string_eval[$x] = 5 Then
                                $row_played = StringSplit($content[$x], ",")
                                For $k=1 To 3
                                    $ids = StringSplit($row_played[$k], "")
                                    If $grid[$ids[1]][$ids[2]] = 0 Then     ;3rd empty - play there
                                        _SetButton($button[$ids[1]][$ids[2]], $grid[$ids[1]][$ids[2]], 1, "Your Turn...")
                                        If $moves = 9 Then Return
                                        $turn = True
                                        Return
                                    EndIf
                                Next
                            EndIf
                        EndIf
                    Next
            EndSelect
        EndIf
EndFunc

Func _SetButton($crt_pressed, ByRef $grid_pos, $grid_val, $label_txt, $label_but = "O")
    GUICtrlSetData ($crt_pressed, $label_but)
    GUICtrlSetState ($crt_pressed, $gui_disable)
    GUICtrlSetData ($Label1, $label_txt)
    $grid_pos = $grid_val
    $moves +=1
EndFunc

Func _ResetGrid()
    If $computer_win = 1 Then GUICtrlSetData($cwin, GUICtrlRead($cwin)+1)
    If $playerwin = 1 Then GUICtrlSetData($pwin, GUICtrlRead($pwin)+1)
    GUICtrlSetData ($Label1, "Your Turn")
    For $i = 1 to 3;column
        For $j = 1 to 3;row
            $grid[$i][$j] = 0
            GUICtrlSetState($button[$i][$j], $GUI_ENABLE)
            GUICtrlSetData($button[$i][$j], "")
        Next
    Next
    $moves = 0
    $turn = True
    $total +=1
    $computer_win = 0
    $playerwin = 0
    GUICtrlSetData($games, $total)
EndFunc

Thanks:

- Nahuel and Sardith for testing it and for their feedback

- sccrstvn93 for the starting interface and "grid" idea

- for all future testers

[06/01/2011] Code updated: made it shorter and corrected a bug discovered by Revolter.

Edited by enaiman

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

  • Replies 58
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

Well done :) I couldn't beat it...

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

Awesome! I can't beat it either :P

I must re-make mine. Valuater made it shorter and more effective... I want to add 'levels'. Like.. Unbeatable, Hard, Normal, Piece of cake :)

Your script is a great example of what a good and effective algorithm must look like. Wouldn't it be cool to combine our scripts and make them play with eath other? :) haha. Of course there's no way my bot can beat yours... yet ¬_¬

Link to comment
Share on other sites

Thank you all for testing - I really appreciate it ... and for the kind comments too :)

@Nahuel - I'm looking forward to your script and I'll be testing it also ... it would be a big challenge to add levels and to make the script to play it right. and about playing one against other ... it might be possible.. somehow :)

I have to think about something special for anyone able to beat it and to show me how ... have to think about that.

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

I like this version more:

#include <GUIConstants.au3>
#include <ARRAY.AU3>

Global $turn = True;true means player false means ai
Global $grid[4][4]
Global $button[4][4]
Global $string_eval[9]
Global $content[9]
Global $moves = 0
Global $computer_win = 0
Global $total = 0
Global $playerwin = 0

$content[1] = "31,32,33"
$content[2] = "21,22,23"
$content[3] = "11,12,13"
$content[4] = "11,22,33"
$content[5] = "11,21,31"
$content[6] = "12,22,32"
$content[7] = "13,23,33"
$content[8] = "13,22,31"

$Form1 = GUICreate("Beat-me Tic-Tac-Toe", 326, 347, 193, 125)
GUICtrlCreateLabel("Computer Win", 10, 10, 71, 17)
$cwin = GUICtrlCreateLabel("00", 90, 10, 25, 17)
GUICtrlCreateLabel("Player Win", 140, 10, 55, 17)
$pwin = GUICtrlCreateLabel("00", 205, 10, 25, 17)
GUICtrlCreateLabel("Games", 250, 10, 37, 17)
$games = GUICtrlCreateLabel("00", 295, 10, 16, 17)
$Button[1][1] = GUICtrlCreateButton("", 44, 72, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[1][2] = GUICtrlCreateButton("", 134, 72, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[1][3] = GUICtrlCreateButton("", 224, 72, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[2][3] = GUICtrlCreateButton("", 224, 152, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[2][1] = GUICtrlCreateButton("", 44, 152, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[2][2] = GUICtrlCreateButton("", 134, 152, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[3][3] = GUICtrlCreateButton("", 224, 232, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[3][2] = GUICtrlCreateButton("", 134, 232, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[3][1] = GUICtrlCreateButton("", 44, 232, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Label1 = GUICtrlCreateLabel("Press any Button", 100, 40, 200, 17)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$reset = GUICtrlCreateButton("Reset", 112, 312, 97, 25, 0)
GUISetState(@SW_SHOW)
For $i = 1 to 3;column
    For $j = 1 to 3;row
        $grid[$i][$j] = 0
    Next
Next

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button [1][1]
            If $grid[1][1] = 0 And BitAND(GUICtrlGetState($button [1][1]),$GUI_ENABLE) Then
                _clicked (1,1)
            EndIf
         Case $button [1][2]
            If $grid[1][2] = 0 And BitAND(GUICtrlGetState($button [1][2]),$GUI_ENABLE) Then
                _clicked (1,2)
            EndIf
           
        Case $button [1][3]
            If $grid[1][3] = 0 And BitAND(GUICtrlGetState($button [1][3]),$GUI_ENABLE) Then
                _clicked (1,3)
            EndIf
           
        Case $button [2][1]
            If $grid[2][1] = 0 And BitAND(GUICtrlGetState($button [2][1]),$GUI_ENABLE) Then
                _clicked (2,1)
            EndIf
       
        Case $button [2][2]
            If $grid[2][2] = 0 And BitAND(GUICtrlGetState($button [2][2]),$GUI_ENABLE) Then
                _clicked (2,2)
            EndIf
           
        Case $button [2][3]
            If $grid[2][3] = 0 And BitAND(GUICtrlGetState($button [2][3]),$GUI_ENABLE) Then
                _clicked (2,3)
            EndIf
           
        Case $button [3][1]
            If $grid[3][1] = 0 And BitAND(GUICtrlGetState($button [3][1]),$GUI_ENABLE) Then
                _clicked (3,1)
            EndIf
           
        Case $button[3][2]
             If $grid[3][2] = 0 And BitAND(GUICtrlGetState($button [3][2]),$GUI_ENABLE) Then
                _clicked (3,2)
            EndIf
           
        Case $button [3][3]
            If $grid[3][3] = 0 And BitAND(GUICtrlGetState($button [3][3]),$GUI_ENABLE) Then
                _clicked (3,3)
            EndIf
        Case $reset
            $playerwin += 1
            $computer_win = 0
            
            If $computer_win = 1 Then GUICtrlSetData($cwin, GUICtrlRead($cwin)+1)
            GUICtrlSetData($pwin, GUICtrlRead($pwin)+1)
           GuiCtrlSetData($Label1, "Piano_man wins!")
            For $i = 1 to 3;column
                For $j = 1 to 3;row
                    $grid[$i][$j] = 0
                    GUICtrlSetState($button[$i][$j], $GUI_ENABLE)
                    GUICtrlSetData($button[$i][$j], "")
                Next
            Next
            $moves = 0
            $turn = True
            $total +=1
            
            GUICtrlSetData($games, $total)
    EndSwitch
    If $moves = 9 And $computer_win = 0 Then
       GuiCtrlSetData($Label1, "Piano_man wins!")
       
        $moves = 0
    EndIf
WEnd

Func string_eval()
    For $j=1 To 8
        $string_eval[$j] = 0
    Next
    For $j=1 To 3
        $string_eval[1] +=  $grid[3][$j]
        $string_eval[2] +=  $grid[2][$j]
        $string_eval[3] +=  $grid[1][$j]
        $string_eval[5] +=  $grid[$j][1]
        $string_eval[6] +=  $grid[$j][2]
        $string_eval[7] +=  $grid[$j][3]
    Next
    $string_eval[4] =  $grid[1][1]+$grid[2][2]+$grid[3][3]
    $string_eval[8] =  $grid[1][3]+$grid[2][2]+$grid[3][1]
EndFunc

Func _clicked ($row, $column)
    If $moves = 9 Then Return
    Local $found_win = 0
        If $turn = true Then
            If BitAND(GUICtrlGetState($button [$row][$column]),$GUI_ENABLE) Then
                GUICtrlSetData ($button[$row][$column], "X")
                GUICtrlSetState ($button[$row][$column], $gui_disable)
                GUICtrlSetData ($Label1, "Thinking...")
                $grid[$row][$column] = 5
                $turn = False
                $moves +=1
                string_eval()
                For $i=1 To 8
                    If $string_eval[$i] = 15 Then  ;player won
                        $playerwin = 1
                        GUICtrlSetData ($Label1, "You Won")
                        Return
                    EndIf
                Next       
                If $moves = 9 Then Return
            Else
                Return
            EndIf
        EndIf
        If $turn = False Then
            Select
                Case $moves = 1
                    If $grid[2][2] = 5 Then
                        GUICtrlSetData ($button[1][1], "O")
                        GUICtrlSetState ($button[1][1], $gui_disable)
                        GUICtrlSetData ($Label1, "Your Turn...")
                        $grid[1][1] = 1
                        $moves +=1
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                    If $grid[2][2] = 0 Then
                        GUICtrlSetData ($button[2][2], "O")
                        GUICtrlSetState ($button[2][2], $gui_disable)
                        GUICtrlSetData ($Label1, "Your Turn...")
                        $grid[2][2] = 1
                        $moves +=1
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                Case $moves >2
                    string_eval()
                    For $i=1 To 8
                        If $string_eval[$i] = 2 Then   ;go for win
                            $found_win = 1
                            $row_played = StringSplit($content[$i], ",")
                            For $k=1 To 3
                                $ids = StringSplit($row_played[$k], "")
                                If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there
                                    GUICtrlSetData ($button[$ids[1]][$ids[2]], "O")
                                    GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable)
                                    GUICtrlSetData ($Label1, "Piano_man won!")
                                    $computer_win=0
                                    $grid[$ids[1]][$ids[2]] = 1
                                    Return
                                EndIf
                            Next
                        EndIf
                    Next       
                    For $i=1 To 8     
                        If $string_eval[$i] = 10 Then  ;2 in a row from player
                            $row_played = StringSplit($content[$i], ",")
                            For $k=1 To 3
                                $ids = StringSplit($row_played[$k], "")
                                If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there
                                    GUICtrlSetData ($button[$ids[1]][$ids[2]], "O")
                                    GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable)
                                    GUICtrlSetData ($Label1, "Your Turn...")
                                    $grid[$ids[1]][$ids[2]] = 1
                                    $moves +=1
                                    If $moves = 9 Then Return
                                    $turn = True
                                    Return
                                EndIf
                            Next
                        EndIf
                    Next
                    If $string_eval[4] = 11 Then
                        If $grid [1][1] = 5 And $grid [3][3] = 5 And $moves < 4 Then
                            GUICtrlSetData ($button[1][2], "O")
                            GUICtrlSetState ($button[1][2], $gui_disable)
                            GUICtrlSetData ($Label1, "Your Turn...")
                            $grid[1][2] = 1
                            $moves +=1
                            If $moves = 9 Then Return
                            $turn = True
                            Return
                        EndIf   
                        If $grid [2][2] = 5 And $grid [3][3] = 5 And $moves < 4 Then   
                            GUICtrlSetData ($button[1][3], "O")
                            GUICtrlSetState ($button[1][3], $gui_disable)
                            GUICtrlSetData ($Label1, "Your Turn...")
                            $grid[1][3] = 1
                            $moves +=1
                            If $moves = 9 Then Return
                            $turn = True
                            Return
                        EndIf
                    EndIf
                    If $grid [1][2] = 5 And $grid [3][3] = 5 And $moves < 4 Then   
                        GUICtrlSetData ($button[1][3], "O")
                        GUICtrlSetState ($button[1][3], $gui_disable)
                        GUICtrlSetData ($Label1, "Your Turn...")
                        $grid[1][3] = 1
                        $moves +=1
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                    If $grid [2][3] = 5 And $grid [3][2] = 5 And $moves < 4 Then
                        GUICtrlSetData ($button[3][3], "O")
                        GUICtrlSetState ($button[3][3], $gui_disable)
                        GUICtrlSetData ($Label1, "Your Turn...")
                        $grid[3][3] = 1
                        $moves +=1
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                    If $grid [1][2] = 5 And $grid [3][1] = 5 And $moves < 4 Then
                        GUICtrlSetData ($button[1][1], "O")
                        GUICtrlSetState ($button[1][1], $gui_disable)
                        GUICtrlSetData ($Label1, "Your Turn...")
                        $grid[1][1] = 1
                        $moves +=1
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                    For $x=1 To 8     
                        If $string_eval[$x] <> 10 And $string_eval[$x] <> 2 Then   ;2 in a row from player
                            If $string_eval[$x] = 1 Then
                                $row_played = StringSplit($content[$x], ",")
                                For $k=1 To 3
                                    $ids = StringSplit($row_played[$k], "")
                                    If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there
                                        GUICtrlSetData ($button[$ids[1]][$ids[2]], "O")
                                        GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable)
                                        GUICtrlSetData ($Label1, "Your Turn...")
                                        $grid[$ids[1]][$ids[2]] = 1
                                        $moves +=1
                                        If $moves = 9 Then Return
                                        $turn = True
                                        Return
                                    EndIf
                                Next
                            ElseIf $string_eval[$x] = 5 Then
                                $row_played = StringSplit($content[$x], ",")
                                For $k=1 To 3
                                    $ids = StringSplit($row_played[$k], "")
                                    If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there
                                        GUICtrlSetData ($button[$ids[1]][$ids[2]], "O")
                                        GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable)
                                        GUICtrlSetData ($Label1, "Your Turn...")
                                        $grid[$ids[1]][$ids[2]] = 1
                                        $moves +=1
                                        If $moves = 9 Then Return
                                        $turn = True
                                        Return
                                    EndIf
                                Next
                            EndIf
                        EndIf
                    Next
            EndSelect
        EndIf
EndFunc

Great work on yours though!

Edited by Piano_Man
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

@PianoMan - I guess I can "personalize" it to show that you won ... (just to add something to prove you're the user, a couple lines of code ... nothing impossible) ... but ... you have to beat it first to make it display "PianoMan won" :)

Glad you enjoyed it :)

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

Link to comment
Share on other sites

Lol. Thats pretty good. I havent beat it yet. I think you should add an option to make it play itself as well. And a sleep function so that it looks like a real game while its playing itself.

I also edited it so that it will ask for the players name and adress them directly. Hope you like it.

#include <GUIConstants.au3>
#include <ARRAY.AU3>

Global $turn = True;true means player false means ai
Global $grid[4][4]
Global $button[4][4]
Global $string_eval[9]
Global $content[9]
Global $moves = 0
Global $computer_win = 0
Global $total = 0
Global $playerwin = 0

$content[1] = "31,32,33"
$content[2] = "21,22,23"
$content[3] = "11,12,13"
$content[4] = "11,22,33"
$content[5] = "11,21,31"
$content[6] = "12,22,32"
$content[7] = "13,23,33"
$content[8] = "13,22,31"
$player = InputBox( "Name" ,"Please input your name.")
$Form1 = GUICreate("Beat-me Tic-Tac-Toe", 326, 347, 193, 125)
GUICtrlCreateLabel("Computer Win", 10, 10, 71, 17)
$cwin = GUICtrlCreateLabel("00", 90, 10, 25, 17)
GUICtrlCreateLabel( $player & " win", 140, 10, 55, 17)
$pwin = GUICtrlCreateLabel("00", 205, 10, 25, 17)
GUICtrlCreateLabel("Games", 250, 10, 37, 17)
$games = GUICtrlCreateLabel("00", 295, 10, 16, 17)
$Button[1][1] = GUICtrlCreateButton("", 44, 72, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[1][2] = GUICtrlCreateButton("", 134, 72, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[1][3] = GUICtrlCreateButton("", 224, 72, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[2][3] = GUICtrlCreateButton("", 224, 152, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[2][1] = GUICtrlCreateButton("", 44, 152, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[2][2] = GUICtrlCreateButton("", 134, 152, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[3][3] = GUICtrlCreateButton("", 224, 232, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[3][2] = GUICtrlCreateButton("", 134, 232, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Button[3][1] = GUICtrlCreateButton("", 44, 232, 57, 57, 0)
GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif")
$Label1 = GUICtrlCreateLabel("Press any Button", 100, 40, 200, 17)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$reset = GUICtrlCreateButton("Reset", 112, 312, 97, 25, 0)
GUISetState(@SW_SHOW)
For $i = 1 to 3;column
    For $j = 1 to 3;row
        $grid[$i][$j] = 0
    Next
Next

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button [1][1] 
            If $grid[1][1] = 0 And BitAND(GUICtrlGetState($button [1][1]),$GUI_ENABLE) Then
                _clicked (1,1)
            EndIf
         Case $button [1][2]
            If $grid[1][2] = 0 And BitAND(GUICtrlGetState($button [1][2]),$GUI_ENABLE) Then
                _clicked (1,2)
            EndIf
            
        Case $button [1][3]
            If $grid[1][3] = 0 And BitAND(GUICtrlGetState($button [1][3]),$GUI_ENABLE) Then
                _clicked (1,3)
            EndIf
           
        Case $button [2][1]
            If $grid[2][1] = 0 And BitAND(GUICtrlGetState($button [2][1]),$GUI_ENABLE) Then
                _clicked (2,1)
            EndIf
        
        Case $button [2][2]
            If $grid[2][2] = 0 And BitAND(GUICtrlGetState($button [2][2]),$GUI_ENABLE) Then
                _clicked (2,2)
            EndIf
            
        Case $button [2][3]
            If $grid[2][3] = 0 And BitAND(GUICtrlGetState($button [2][3]),$GUI_ENABLE) Then
                _clicked (2,3)
            EndIf
           
        Case $button [3][1]
            If $grid[3][1] = 0 And BitAND(GUICtrlGetState($button [3][1]),$GUI_ENABLE) Then
                _clicked (3,1)
            EndIf
            
        Case $button[3][2]
             If $grid[3][2] = 0 And BitAND(GUICtrlGetState($button [3][2]),$GUI_ENABLE) Then
                _clicked (3,2)
            EndIf
            
        Case $button [3][3]
            If $grid[3][3] = 0 And BitAND(GUICtrlGetState($button [3][3]),$GUI_ENABLE) Then
                _clicked (3,3)
            EndIf
        Case $reset
            If $computer_win = 1 Then GUICtrlSetData($cwin, GUICtrlRead($cwin)+1)
            If $playerwin = 1 Then GUICtrlSetData($pwin, GUICtrlRead($pwin)+1)
            GUICtrlSetData ($Label1, $player &"'s Turn")
            For $i = 1 to 3;column
                For $j = 1 to 3;row
                    $grid[$i][$j] = 0
                    GUICtrlSetState($button[$i][$j], $GUI_ENABLE)
                    GUICtrlSetData($button[$i][$j], "")
                Next
            Next
            $moves = 0
            $turn = True
            $total +=1
            $computer_win = 0
            $playerwin = 0
            GUICtrlSetData($games, $total)
    EndSwitch
    If $moves = 9 And $computer_win = 0 Then
        GUICtrlSetData ($Label1, "Nobody won")
        
        $moves = 0
    EndIf
WEnd

Func string_eval()
    For $j=1 To 8
        $string_eval[$j] = 0
    Next
    For $j=1 To 3
        $string_eval[1] +=  $grid[3][$j]
        $string_eval[2] +=  $grid[2][$j]
        $string_eval[3] +=  $grid[1][$j]
        $string_eval[5] +=  $grid[$j][1]
        $string_eval[6] +=  $grid[$j][2]
        $string_eval[7] +=  $grid[$j][3]
    Next
    $string_eval[4] =  $grid[1][1]+$grid[2][2]+$grid[3][3]
    $string_eval[8] =  $grid[1][3]+$grid[2][2]+$grid[3][1]
EndFunc

Func _clicked ($row, $column)
    If $moves = 9 Then Return
    Local $found_win = 0
        If $turn = true Then
            If BitAND(GUICtrlGetState($button [$row][$column]),$GUI_ENABLE) Then
                GUICtrlSetData ($button[$row][$column], "X")
                GUICtrlSetState ($button[$row][$column], $gui_disable)
                GUICtrlSetData ($Label1, "Thinking...")
                $grid[$row][$column] = 5
                $turn = False
                $moves +=1
                string_eval()
                For $i=1 To 8
                    If $string_eval[$i] = 15 Then  ;player won
                        $playerwin = 1
                        GUICtrlSetData ($Label1,$player & " won.")
                        Return
                    EndIf
                Next        
                If $moves = 9 Then Return
            Else
                Return
            EndIf
        EndIf
        If $turn = False Then
            Select
                Case $moves = 1 
                    If $grid[2][2] = 5 Then
                        GUICtrlSetData ($button[1][1], "O")
                        GUICtrlSetState ($button[1][1], $gui_disable)
                        GUICtrlSetData ($Label1, $player &"'s Turn...")
                        $grid[1][1] = 1
                        $moves +=1
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                    If $grid[2][2] = 0 Then
                        GUICtrlSetData ($button[2][2], "O")
                        GUICtrlSetState ($button[2][2], $gui_disable)
                        GUICtrlSetData ($Label1, $player &"'s Turn...")
                        $grid[2][2] = 1
                        $moves +=1
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                Case $moves >2 
                    string_eval()
                    For $i=1 To 8
                        If $string_eval[$i] = 2 Then   ;go for win
                            $found_win = 1
                            $row_played = StringSplit($content[$i], ",")
                            For $k=1 To 3
                                $ids = StringSplit($row_played[$k], "")
                                If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there
                                    GUICtrlSetData ($button[$ids[1]][$ids[2]], "O")
                                    GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable)
                                    GUICtrlSetData ($Label1, "I Won")
                                    $computer_win=1
                                    $grid[$ids[1]][$ids[2]] = 1
                                    Return
                                EndIf
                            Next
                        EndIf
                    Next        
                    For $i=1 To 8     
                        If $string_eval[$i] = 10 Then  ;2 in a row from player
                            $row_played = StringSplit($content[$i], ",")
                            For $k=1 To 3
                                $ids = StringSplit($row_played[$k], "")
                                If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there
                                    GUICtrlSetData ($button[$ids[1]][$ids[2]], "O")
                                    GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable)
                                    GUICtrlSetData ($Label1, $player &"'s Turn...")
                                    $grid[$ids[1]][$ids[2]] = 1
                                    $moves +=1
                                    If $moves = 9 Then Return
                                    $turn = True
                                    Return
                                EndIf
                            Next
                        EndIf
                    Next
                    If $string_eval[4] = 11 Then
                        If $grid [1][1] = 5 And $grid [3][3] = 5 And $moves < 4 Then
                            GUICtrlSetData ($button[1][2], "O")
                            GUICtrlSetState ($button[1][2], $gui_disable)
                            GUICtrlSetData ($Label1, $player &"'s Turn...")
                            $grid[1][2] = 1
                            $moves +=1
                            If $moves = 9 Then Return
                            $turn = True
                            Return
                        EndIf   
                        If $grid [2][2] = 5 And $grid [3][3] = 5 And $moves < 4 Then    
                            GUICtrlSetData ($button[1][3], "O")
                            GUICtrlSetState ($button[1][3], $gui_disable)
                            GUICtrlSetData ($Label1, $player &"'s Turn...")
                            $grid[1][3] = 1
                            $moves +=1
                            If $moves = 9 Then Return
                            $turn = True
                            Return
                        EndIf
                    EndIf
                    If $grid [1][2] = 5 And $grid [3][3] = 5 And $moves < 4 Then    
                        GUICtrlSetData ($button[1][3], "O")
                        GUICtrlSetState ($button[1][3], $gui_disable)
                        GUICtrlSetData ($Label1, $player &"'s Turn...")
                        $grid[1][3] = 1
                        $moves +=1
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                    If $grid [2][3] = 5 And $grid [3][2] = 5 And $moves < 4 Then
                        GUICtrlSetData ($button[3][3], "O")
                        GUICtrlSetState ($button[3][3], $gui_disable)
                        GUICtrlSetData ($Label1, $player &"'s Turn...")
                        $grid[3][3] = 1
                        $moves +=1
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                    If $grid [1][2] = 5 And $grid [3][1] = 5 And $moves < 4 Then
                        GUICtrlSetData ($button[1][1], "O")
                        GUICtrlSetState ($button[1][1], $gui_disable)
                        GUICtrlSetData ($Label1, $player &"'s Turn...")
                        $grid[1][1] = 1
                        $moves +=1
                        If $moves = 9 Then Return
                        $turn = True
                        Return
                    EndIf
                    For $x=1 To 8     
                        If $string_eval[$x] <> 10 And $string_eval[$x] <> 2 Then   ;2 in a row from player
                            If $string_eval[$x] = 1 Then
                                $row_played = StringSplit($content[$x], ",")
                                For $k=1 To 3
                                    $ids = StringSplit($row_played[$k], "")
                                    If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there
                                        GUICtrlSetData ($button[$ids[1]][$ids[2]], "O")
                                        GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable)
                                        GUICtrlSetData ($Label1, $player &"'s turn...")
                                        $grid[$ids[1]][$ids[2]] = 1
                                        $moves +=1
                                        If $moves = 9 Then Return
                                        $turn = True
                                        Return
                                    EndIf
                                Next
                            ElseIf $string_eval[$x] = 5 Then
                                $row_played = StringSplit($content[$x], ",")
                                For $k=1 To 3
                                    $ids = StringSplit($row_played[$k], "")
                                    If $grid[$ids[1]][$ids[2]] = 0 Then ;3rd empty - play there
                                        GUICtrlSetData ($button[$ids[1]][$ids[2]], "O")
                                        GUICtrlSetState ($button[$ids[1]][$ids[2]], $gui_disable)
                                        GUICtrlSetData ($Label1, $player &"'s Turn...")
                                        $grid[$ids[1]][$ids[2]] = 1
                                        $moves +=1
                                        If $moves = 9 Then Return
                                        $turn = True
                                        Return
                                    EndIf
                                Next
                            EndIf
                        EndIf
                    Next
            EndSelect
        EndIf
EndFunc
Spoiler

 

"If a vegetarian eats vegetables,What the heck does a humanitarian eat?"

"I hear voices in my head, but I ignore them and continue on killing."

"You have forced me to raise the indifference warning to beige, it's a beige alert people. As with all beige alerts please prepare to think about the possibility of caring."

An optimist says that giving someone power DOESN'T immediately turn them into a sadist. A pessimist says that giving someone power doesn't IMMEDIATELY turn them into a sadist.

 

 
Link to comment
Share on other sites

  • 10 months later...

Yeah ... it doesn't give any chances to "poor human" player :D

Been thinking a while ago that I could modify the script and add some difficulty levels; nothing too fancy just adding some "random" statements before AI's answer. Something like: beginner level will give AI 1 chance out of 5 to play the "best" move, Intermediate will give 1 chance out of 3 while the expert level will play always at its best.

Not sure when I'll come back to this but it will be sometime in the future ;)

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

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