Jump to content
Sign in to follow this  
CodyBarrett

Tik Tak Toe

Recommended Posts

CodyBarrett

well.. im having troubles with the math part of this... getting whether or not there is a row of 3 x's or 3 o's

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Misc.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>

Opt ('GUIoneventmode',1)
$GUI = GUICreate ( 'Tik-Tak-Toe', 300, 340, -1, -1, -2138570616)
$Label = GUICtrlCreateLabel (@ScriptName, 20, 0, 280, 20, $SS_CENTER )
$Exit = GUICtrlCreateLabel ('x', 0, 0, 20, 20, $SS_CENTER )
$T_L = GUICtrlCreateButton ('', 0, 20, 100, 100)
$M_L = GUICtrlCreateButton ('', 0, 120, 100, 100)
$B_L = GUICtrlCreateButton ('', 0, 220, 100, 100)
$T_M = GUICtrlCreateButton ('', 100, 20, 100, 100)
$M_M = GUICtrlCreateButton ('', 100, 120, 100, 100)
$B_M = GUICtrlCreateButton ('', 100, 220, 100, 100)
$T_R = GUICtrlCreateButton ('', 200, 20, 100, 100)
$M_R = GUICtrlCreateButton ('', 200, 120, 100, 100)
$B_R = GUICtrlCreateButton ('', 200, 220, 100, 100)
$Replay = GUICtrlCreateButton ('Replay', 0, 320, 300, 20)
Dim $Array[9]
$Array[0] = $T_L
$Array[1] = $M_L
$Array[2] = $B_L
$Array[3] = $T_M
$Array[4] = $M_M
$Array[5] = $B_M
$Array[6] = $T_R
$Array[7] = $M_R
$Array[8] = $B_R

GUICtrlSetFont ($T_L, 50, 900, '', 'Courier New')
GUICtrlSetFont ($M_L, 50, 900, '', 'Courier New')
GUICtrlSetFont ($B_L, 50, 900, '', 'Courier New')
GUICtrlSetFont ($T_M, 50, 900, '', 'Courier New')
GUICtrlSetFont ($M_M, 50, 900, '', 'Courier New')
GUICtrlSetFont ($B_M, 50, 900, '', 'Courier New')
GUICtrlSetFont ($T_R, 50, 900, '', 'Courier New')
GUICtrlSetFont ($M_R, 50, 900, '', 'Courier New')
GUICtrlSetFont ($B_R, 50, 900, '', 'Courier New')
GUICtrlSetOnEvent ($Replay, '_Random_')
GUICtrlSetOnEvent ($T_L, '_Move_')
GUICtrlSetOnEvent ($T_M, '_Move_')
GUICtrlSetOnEvent ($T_R, '_Move_')
GUICtrlSetOnEvent ($M_L, '_Move_')
GUICtrlSetOnEvent ($M_M, '_Move_')
GUICtrlSetOnEvent ($M_R, '_Move_')
GUICtrlSetOnEvent ($B_L, '_Move_')
GUICtrlSetOnEvent ($B_M, '_Move_')
GUICtrlSetOnEvent ($B_R, '_Move_')
GUICtrlSetOnEvent ($Exit, '_exit_')
GUICtrlSetOnEvent ($Label, '_Win_Move_')
GUISetBkColor (0x000000, $GUI)
GUICtrlSetBkColor ($Label, 0x2e2e2e)
GUICtrlSetBkColor ($Exit, 0x2e2e2e)
GUICtrlSetColor ($Label, 0xFFFFFF)
GUICtrlSetColor ($Exit, 0xFFFFFF)
GUISetOnEvent ($GUI_EVENT_CLOSE,'_exit_',$GUI)
GUISetState()

 _Random_ ()

While 1
    Sleep (100)
WEnd

Func _Move_ ()
    $Button = @GUI_CTRLID
    GUICtrlSetData ($Button, 'O')
    GUICtrlSetState ($Button, $GUI_DISABLE)
    Dim $Guictrlread[9]
    For $i = 0 To 8
        $Guictrlread[$i] = GUICtrlRead ($Array[$i])
        If $Guictrlread[$i] = 'X' Then $Guictrlread[$i] = 1
        If $Guictrlread[$i] = 'O' Then $Guictrlread[$i] = -1
        If $Guictrlread[$i] = '' Then $Guictrlread[$i] = 0
    Next
    If $Guictrlread[0] + $Guictrlread[1] = 2 Or $Guictrlread[1] + $Guictrlread[2] = 2 Or $Guictrlread[2] + $Guictrlread[0] = 2 Then MsgBox (0,'','')
EndFunc

Func _Random_ ()
    GUICtrlSetData ($T_L, '')
    GUICtrlSetData ($M_L, '')
    GUICtrlSetData ($B_L, '')
    GUICtrlSetData ($T_M, '')
    GUICtrlSetData ($M_M, '')
    GUICtrlSetData ($B_M, '')
    GUICtrlSetData ($T_R, '')
    GUICtrlSetData ($M_R, '')
    GUICtrlSetData ($B_R, '')
    GUICtrlSetState ($T_L, $GUI_ENABLE)
    GUICtrlSetState ($M_L, $GUI_ENABLE)
    GUICtrlSetState ($B_L, $GUI_ENABLE)
    GUICtrlSetState ($T_M, $GUI_ENABLE)
    GUICtrlSetState ($M_M, $GUI_ENABLE)
    GUICtrlSetState ($B_M, $GUI_ENABLE)
    GUICtrlSetState ($T_R, $GUI_ENABLE)
    GUICtrlSetState ($M_R, $GUI_ENABLE)
    GUICtrlSetState ($B_R, $GUI_ENABLE)
    $Random = Random ($Array[0], $Array[8])
    GUICtrlSetData ($Random, 'X')
    GUICtrlSetState ($Random, $GUI_DISABLE) 
EndFunc

Func _Win_Move_ ()
    While _IsPressed ('01')
        $xy = MouseGetPos ()
        WinMove ($GUI, '', $xy[0], $xy[1])
    WEnd
EndFunc

Func _exit_ ()
    Exit
EndFunc

lol any ideas?

Share this post


Link to post
Share on other sites
Valuater

If memory serves, there are two Tic-Tac-Toe that were pretty good

search Example Scripts and you may find how it was done for those two

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
WideBoyDixon

I did start writing some generic game code based on:

[1] Static evaluater (to score a position)

[2] Legal move generator (i.e. create a list of legal moves for a specific board position)

[3] Minimax algorithm with alpha-beta pruning

However, I didn't get too far down the line with it although tic-tac-toe was going to be my first implementation. I may well go back to it one day in order to write a checkers/othello/chess(!) script.

WBD

Share this post


Link to post
Share on other sites
CodyBarrett

well.. i really don't want to hard code roughly 800,000 different scenarios.. and ill look into the the examplescripts.. ^_^

EDIT

looked through theexaample scripts... and i found a few.. lol

enaiman has made a TRUELY unbeatable.. i tested it... err..like 30 times and couldn't win once..

haha but i don't think i will use much or any of that code.. because i find that.. if you can't beat a game then it gets boring.. but if you can beat it even %10 of the time.. it gets you hopeing and it could get addicting.. so yeah ill do some more research

Edited by CodyBarrett

Share this post


Link to post
Share on other sites
CodyBarrett

ok i took the easy way out LMAO.. but this is also an example for people to get better at.. although i have one bug... (that i know of)

sometimes when it restarts... there is one X left over.. X=the computer.. so when it restarts there is two X's.. any ideas..

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Misc.au3>
#include <ButtonConstants.au3>
#include <StaticConstants.au3>

Opt ('GUIoneventmode',1)
$GUI = GUICreate ( 'Tik-Tak-Toe', 300, 340, -1, -1, -2138570616)
$Label = GUICtrlCreateLabel (@ScriptName, 20, 0, 280, 20, $SS_CENTER )
$Exit = GUICtrlCreateLabel ('x', 0, 0, 20, 20, $SS_CENTER )
$T_L = GUICtrlCreateButton ('', 0, 20, 100, 100)
$M_L = GUICtrlCreateButton ('', 0, 120, 100, 100)
$B_L = GUICtrlCreateButton ('', 0, 220, 100, 100)
$T_M = GUICtrlCreateButton ('', 100, 20, 100, 100)
$M_M = GUICtrlCreateButton ('', 100, 120, 100, 100)
$B_M = GUICtrlCreateButton ('', 100, 220, 100, 100)
$T_R = GUICtrlCreateButton ('', 200, 20, 100, 100)
$M_R = GUICtrlCreateButton ('', 200, 120, 100, 100)
$B_R = GUICtrlCreateButton ('', 200, 220, 100, 100)
$Replay = GUICtrlCreateButton ('Replay', 0, 320, 300, 20)
Global $Array[9]
Global $Guictrlread[9]
$Array[0] = $T_L
$Array[1] = $M_L
$Array[2] = $B_L
$Array[3] = $T_M
$Array[4] = $M_M
$Array[5] = $B_M
$Array[6] = $T_R
$Array[7] = $M_R
$Array[8] = $B_R
$Turn = 0
$Status = ''
For $i = 0 To 8
    GUICtrlSetFont ($Array[$i], 50, 900, '', 'Courier New')
    GUICtrlSetOnEvent ($Array[$i], '_UserMove_')
Next
GUICtrlSetOnEvent ($Replay, '_Random_')
GUICtrlSetOnEvent ($Exit, '_exit_')
GUICtrlSetOnEvent ($Label, '_Win_Move_')
GUISetBkColor (0x000000, $GUI)
GUICtrlSetBkColor ($Label, 0x2e2e2e)
GUICtrlSetBkColor ($Exit, 0x2e2e2e)
GUICtrlSetColor ($Label, 0xFFFFFF)
GUICtrlSetColor ($Exit, 0xFFFFFF)
GUICtrlSetFont ($Label, 10, 700, '', 'Trebuchet MS')
GUICtrlSetFont ($Exit, 10, 700, '', 'Trebuchet MS')
GUISetOnEvent ($GUI_EVENT_CLOSE,'_exit_',$GUI)
GUISetState()
 _Random_ ()
While 1
    For $i = 0 To 8
        $Guictrlread[$i] = GUICtrlRead ($Array[$i])
        If $Guictrlread[$i] = 'X' Then $Guictrlread[$i] = 1
        If $Guictrlread[$i] = 'O' Then $Guictrlread[$i] = -1
        If $Guictrlread[$i] = '' Then $Guictrlread[$i] = 0
    Next
        If $Guictrlread[0] + $Guictrlread[3] + $Guictrlread[6] = -3 Then _Win_ ()
        If $Guictrlread[1] + $Guictrlread[4] + $Guictrlread[7] = -3 Then _Win_ ()
        If $Guictrlread[2] + $Guictrlread[5] + $Guictrlread[8] = -3 Then _Win_ ()
        If $Guictrlread[0] + $Guictrlread[4] + $Guictrlread[8] = -3 Then _Win_ ()
        If $Guictrlread[2] + $Guictrlread[4] + $Guictrlread[6] = -3 Then _Win_ ()
        If $Guictrlread[0] + $Guictrlread[1] + $Guictrlread[2] = -3 Then _Win_ ()
        If $Guictrlread[3] + $Guictrlread[4] + $Guictrlread[5] = -3 Then _Win_ ()
        If $Guictrlread[6] + $Guictrlread[7] + $Guictrlread[8] = -3 Then _Win_ ()
        If $Guictrlread[0] + $Guictrlread[3] + $Guictrlread[6] = 3 Then _Lose_ ()
        If $Guictrlread[1] + $Guictrlread[4] + $Guictrlread[7] = 3 Then _Lose_ ()
        If $Guictrlread[2] + $Guictrlread[5] + $Guictrlread[8] = 3 Then _Lose_ ()
        If $Guictrlread[0] + $Guictrlread[4] + $Guictrlread[8] = 3 Then _Lose_ ()
        If $Guictrlread[2] + $Guictrlread[4] + $Guictrlread[6] = 3 Then _Lose_ ()
        If $Guictrlread[0] + $Guictrlread[1] + $Guictrlread[2] = 3 Then _Lose_ ()       
        If $Guictrlread[3] + $Guictrlread[4] + $Guictrlread[5] = 3 Then _Lose_ ()       
        If $Guictrlread[6] + $Guictrlread[7] + $Guictrlread[8] = 3 Then _Lose_ ()
        If $Turn = 9 Then  _Tie_ ()
        $UaTemp = _ArrayFindAll ($Guictrlread, '-1')
        $PaTemp = _ArrayFindAll ($Guictrlread, '1')
        If UBound ($UaTemp) >= UBound ($PaTemp) Then _PC_Move ()
        Sleep (100)
WEnd
Func _Lose_ ()
    $2nd_GUI = GUICreate ('', 300, 50, -1, -1, -2138570616)
    GUICtrlCreateLabel ('AI Wins...', 0, 0, 300, 50)
    GUICtrlSetBkColor (-1, 0x000000)
    GUICtrlSetFont (-1, 30, 400, '', 'Trebuchet MS')
    GUICtrlSetColor (-1, 0xFFFFFF)
    GUISetState ()
    Sleep (2000)
    GUIDelete ($2nd_GUI)
    _Random_ ()
EndFunc
Func _PC_Move ()
    
    $2nd_GUI = GUICreate ('', 300, 50, -1, -1, -2138570616)
    GUICtrlCreateLabel ('Please wait...', 0, 0, 300, 50)
    GUICtrlSetBkColor (-1, 0x000000)
    GUICtrlSetFont (-1, 30, 400, '', 'Trebuchet MS')
    GUICtrlSetColor (-1, 0xFFFFFF)
    GUISetState ()
    Sleep (1000)
    Do
        $i = Random (0, 9)
    Until GUICtrlRead ($Array[$i]) <> 'X' And GUICtrlRead ($Array[$i]) <> 'O'
    GUICtrlSetData ($Array[$i], 'X')
    GUICtrlSetState ($Array[$i], $GUI_DISABLE)
    $Turn += 1
    GUIDelete ($2nd_GUI)
    _MouseTrap ()
EndFunc
Func _UserMove_ ()
    $Button = @GUI_CTRLID
    GUICtrlSetData ($Button, 'O')
    GUICtrlSetState ($Button, $GUI_DISABLE)
    $Turn += 1
    _MouseTrap (MouseGetPos (0), MouseGetPos (1), MouseGetPos (0) + 1, MouseGetPos (1) + 1)
EndFunc
Func _Random_ ()
    For $i = 0 To 8
        GUICtrlSetData ($Array[$i], '')
        GUICtrlSetState ($Array[$i], $GUI_ENABLE)
    Next
    $Turn = 1
    $Random = Random (0, 8)
    GUICtrlSetData ($Array[$Random], 'X')
    GUICtrlSetState ($Array[$Random], $GUI_DISABLE)
    _MouseTrap ()
EndFunc
Func _Tie_ ()
    $2nd_GUI = GUICreate ('', 300, 50, -1, -1, -2138570616)
    GUICtrlCreateLabel ('Tie game...', 0, 0, 300, 50)
    GUICtrlSetBkColor (-1, 0x000000)
    GUICtrlSetFont (-1, 30, 400, '', 'Trebuchet MS')
    GUICtrlSetColor (-1, 0xFFFFFF)
    GUISetState ()
    Sleep (2000)
    GUIDelete ($2nd_GUI)
    _Random_ ()
EndFunc
Func _Win_Move_ ()
    While _IsPressed ('01')
        $xy = MouseGetPos ()
        WinMove ($GUI, '', $xy[0], $xy[1])
    WEnd
EndFunc
Func _Win_ ()
    $2nd_GUI = GUICreate ('', 300, 50, -1, -1, -2138570616)
    GUICtrlCreateLabel (@UserName & ' Wins...', 0, 0, 300, 50)
    GUICtrlSetBkColor (-1, 0x000000)
    GUICtrlSetFont (-1, 30, 400, '', 'Trebuchet MS')
    GUICtrlSetColor (-1, 0xFFFFFF)
    GUISetState ()
    Sleep (2000)
    GUIDelete ($2nd_GUI)
    _Random_ ()
EndFunc
Func _exit_ ()
    Exit
EndFunc

Share this post


Link to post
Share on other sites
FinalVersion

well.. i really don't want to hard code roughly 800,000 different scenarios.. and ill look into the the examplescripts.. ^_^

EDIT

looked through theexaample scripts... and i found a few.. lol

enaiman has made a TRUELY unbeatable.. i tested it... err..like 30 times and couldn't win once..

haha but i don't think i will use much or any of that code.. because i find that.. if you can't beat a game then it gets boring.. but if you can beat it even %10 of the time.. it gets you hopeing and it could get addicting.. so yeah ill do some more research

Can you pass me the one made by enaiman, searched the forums found nothing >.<

Share this post


Link to post
Share on other sites
CodyBarrett

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  

×