Sign in to follow this  
Followers 0
MSLx Fanboy

Tic-Tac-Toe

11 posts in this topic

#1 ·  Posted (edited)

A fellow student in my Cisco networking class challenged me to write a 'program' to play tic-tac-toe, I told him I would do it in a script :( I did wrote the entire thing in just over an hour, now I just have to find him and start playing :(

If anyone notices a better way to handle variables, let me know...I have been experimenting with variable scopes, just want to make sure they're right.

Edit: Ran the tidy program, and changed a few argument variable names

Edit^2: Removed the code from post, scroll down, source and exe are attached in next post I made

Edited by MSLx Fanboy

Writing AutoIt scripts since
_DateAdd("d", -2, _NowCalcDate())

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

wel it doesnt work -.-

could you plz post a compiled version?

well this is working:

#include <GuiConstants.au3>
Global $turn, $mark, $player[2][9], $winner

Func NextPlayer($label)
    If $turn = 1 Then
        $turn = 2
        $mark = "O"
    Else
        $turn = 1
        $mark = "X"
    EndIf
    GUICtrlSetData($label, "Player " & $turn & "'s Turn")
EndFunc ;==>NextPlayer

Func Check($activegui)
    For $x = 0 To 1
        If $player[$x][0] = 1 And $player[$x][1] = 1 And $player[$x][2] = 1 Then $winner = $x + 1
        If $player[$x][3] = 1 And $player[$x][4] = 1 And $player[$x][5] = 1 Then $winner = $x + 1
        If $player[$x][6] = 1 And $player[$x][7] = 1 And $player[$x][8] = 1 Then $winner = $x + 1
        If $player[$x][0] = 1 And $player[$x][3] = 1 And $player[$x][6] = 1 Then $winner = $x + 1
        If $player[$x][1] = 1 And $player[$x][4] = 1 And $player[$x][7] = 1 Then $winner = $x + 1
        If $player[$x][2] = 1 And $player[$x][5] = 1 And $player[$x][8] = 1 Then $winner = $x + 1
        If $player[$x][0] = 1 And $player[$x][4] = 1 And $player[$x][8] = 1 Then $winner = $x + 1
        If $player[$x][2] = 1 And $player[$x][4] = 1 And $player[$x][6] = 1 Then $winner = $x + 1
    Next
    If $winner = 1 Or $winner = 2 Then
        $newgame = MsgBox(4, "Winner!", "And the winner is: Player " & $winner & "!" & @CRLF & @CRLF & "New Game?")
        If $newgame = 6 Then
            GUIDelete($activegui)
            NewGame()
        Else
            Exit
        EndIf
    EndIf
EndFunc ;==>Check

Func NewGame()
    For $x = 0 To 1
        For $y = 0 To 8
            $player[$x][$y] = 0
        Next
    Next
    $winner = 0
    $turn = 0
    $maingui = GUICreate("Tic-Tac-Toe", 145, 220, (@DesktopWidth - 145) / 2, (@DesktopHeight - 200) / 2, $WS_DLGFRAME)
;GUICtrlCreateButton(
    Dim $Button_1_1 = GUICtrlCreateButton("", 15, 30, 25, 25)
    Dim $Button_1_2 = GUICtrlCreateButton("", 60, 30, 25, 25)
    Dim $Button_1_3 = GUICtrlCreateButton("", 105, 30, 25, 25)
    Dim $Button_2_1 = GUICtrlCreateButton("", 15, 60, 25, 25)
    Dim $Button_2_2 = GUICtrlCreateButton("", 60, 60, 25, 25)
    Dim $Button_2_3 = GUICtrlCreateButton("", 105, 60, 25, 25)
    Dim $Button_3_1 = GUICtrlCreateButton("", 15, 90, 25, 25)
    Dim $Button_3_2 = GUICtrlCreateButton("", 60, 90, 25, 25)
    Dim $Button_3_3 = GUICtrlCreateButton("", 105, 90, 25, 25)
    Dim $Who_Goes = GUICtrlCreateLabel("Player 1's Turn", 15, 120, 115, 25)
    Dim $Exit = GUICtrlCreateButton("Exit", 60, 150, 30, 25)
    GUICtrlSetStyle($Who_Goes, $SS_CENTER)
    GUISetState()
    While 1
        NextPlayer($Who_Goes)
        Check($maingui)
        $nextturn = 0
        While Not $nextturn
            $msg = GUIGetMsg()
            Select
                Case $msg = $GUI_EVENT_CLOSE Or $msg = $Exit
                    Exit
                Case $msg = $Button_1_1
                    GUICtrlSetData($Button_1_1, $mark)
                    GUICtrlSetState($Button_1_1, $GUI_DISABLE)
                    $player[$turn - 1][0] = 1
                    $nextturn = 1
                Case $msg = $Button_1_2
                    GUICtrlSetData($Button_1_2, $mark)
                    GUICtrlSetState($Button_1_2, $GUI_DISABLE)
                    $player[$turn - 1][1] = 1
                    $nextturn = 1
                Case $msg = $Button_1_3
                    GUICtrlSetData($Button_1_3, $mark)
                    GUICtrlSetState($Button_1_3, $GUI_DISABLE)
                    $player[$turn - 1][2] = 1
                    $nextturn = 1
                Case $msg = $Button_2_1
                    GUICtrlSetData($Button_2_1, $mark)
                    GUICtrlSetState($Button_2_1, $GUI_DISABLE)
                    $player[$turn - 1][3] = 1
                    $nextturn = 1
                Case $msg = $Button_2_2
                    GUICtrlSetData($Button_2_2, $mark)
                    GUICtrlSetState($Button_2_2, $GUI_DISABLE)
                    $player[$turn - 1][4] = 1
                    $nextturn = 1
                Case $msg = $Button_2_3
                    GUICtrlSetData($Button_2_3, $mark)
                    GUICtrlSetState($Button_2_3, $GUI_DISABLE)
                    $player[$turn - 1][5] = 1
                    $nextturn = 1
                Case $msg = $Button_3_1
                    GUICtrlSetData($Button_3_1, $mark)
                    GUICtrlSetState($Button_3_1, $GUI_DISABLE)
                    $player[$turn - 1][6] = 1
                    $nextturn = 1
                Case $msg = $Button_3_2
                    GUICtrlSetData($Button_3_2, $mark)
                    GUICtrlSetState($Button_3_2, $GUI_DISABLE)
                    $player[$turn - 1][7] = 1
                    $nextturn = 1
                Case $msg = $Button_3_3
                    GUICtrlSetData($Button_3_3, $mark)
                    GUICtrlSetState($Button_3_3, $GUI_DISABLE)
                    $player[$turn - 1][8] = 1
                    $nextturn = 1
            EndSelect
        WEnd
        
    WEnd
EndFunc ;==>NewGame

Func HotKeyQuit()
    Exit
EndFunc ;==>HotKeyQuit

HotKeySet("^z", "HotKeyQuit")
NewGame()

a great script!

Edited by Analritter

[font="Verdana"]In work:[list=1][*]InstallIt[*]New version of SpaceWar[/list] [/font]

Share this post


Link to post
Share on other sites

Edited script again (see original post). Fixed scope of the $winner variable.

I don't quite understand analritter, did it work or not?

Attachments: Source Code and Executable (no decomp allowed)

Tic_Tac_Toe.zip


Writing AutoIt scripts since
_DateAdd("d", -2, _NowCalcDate())

Share this post


Link to post
Share on other sites

Nice Game. How about an AI for one player games. Or would that be one too many If staments :( .


.

Share this post


Link to post
Share on other sites

Another student made that suggestion to me today. I'm thinking just using a Random(), and then adding another array to directly track which buttons have been chosen already. Just do a while loop to generate a random button number until it finds one that isn't pressed


Writing AutoIt scripts since
_DateAdd("d", -2, _NowCalcDate())

Share this post


Link to post
Share on other sites

Another student made that suggestion to me today.  I'm thinking just using a Random(), and then adding another array to directly track which buttons have been chosen already.  Just do a while loop to generate a random button number until it finds one that isn't pressed

<{POST_SNAPBACK}>

You would also want to see if there are any two of the other players selections next to each other that would be a win next turn to make sure the AI plays the block.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

That's a tiny bit past what I'm willing to do for this at the moment, but I might have the inclination in the near future (sometime next week maybe) to put that in.

Right now the computer is just guessing...in other words, its fairly impossible not to win, unless you're just really unlucky.

I'm having a little trouble with the GUICtrlSendMsg() function. Never used it before, so I don't know what parameters to send other than the ControlID...help file doesn't really explain that part well

Func BotTurn()
    Local $numgen = Random(0, 8, 1)
    If $buttons[$numgen] = 1 Then
        BotTurn()
    Else
        GUICtrlSendMsg($CtrlButtons[$numgen], "", "", "")
    EndIf
EndFunc ;==>BotTurn

I just want GUIGetMsg() to return with the ID of that $CtrlButtons[$numgen]

Edit: Who knew that a game of tic-tac-toe needs more than 150 lines of code (153, and still things to fix and add :()

Edited by MSLx Fanboy

Writing AutoIt scripts since
_DateAdd("d", -2, _NowCalcDate())

Share this post


Link to post
Share on other sites

it didnt work...

i had to replace "False" with "0" and "true" with "1"


[font="Verdana"]In work:[list=1][*]InstallIt[*]New version of SpaceWar[/list] [/font]

Share this post


Link to post
Share on other sites

All done! Yea! It's over with!

Oh, as for the bot, its not an 'intelligent' bot, however, with the small amount of buttons available, it is still a real possibility to end with the computer winning, or a draw. I might work on an intelligent bot in the future, however, that would require a lot of checking of buttons, and who owns them, which would be quite a task for such a simple game.

Enjoy

Tic_Tac_Toe.au3


Writing AutoIt scripts since
_DateAdd("d", -2, _NowCalcDate())

Share this post


Link to post
Share on other sites

so now its near perfect....

it only should be more intelligent...


[font="Verdana"]In work:[list=1][*]InstallIt[*]New version of SpaceWar[/list] [/font]

Share this post


Link to post
Share on other sites

Off to build my first AI program/script ever :(

Only in time my friend.


Writing AutoIt scripts since
_DateAdd("d", -2, _NowCalcDate())

Share this post


Link to post
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
Sign in to follow this  
Followers 0