Jump to content

Another Tic Tac Toe Game


crzftx
 Share

Recommended Posts

I made this script about a year ago. I thought I posted this already, but I didn't find it with Search. So here it is.

Rules: O goes three times, then X twice. X can go in the same box in the same turn, but O cannot. If X gains a box, his turn is over. In order to gain a box, a 3-in-a-row is needed in that box. To win, a three-in-a-row of gained boxes is necessary.

#include<GUIConstants.au3>
#NoTrayIcon

Global $Big[3][3],$Small[9][9],$Previous[2]
Global $Number,$Letter,$Name[2],$Score[2],$Temp

GUICreate("Tic Tac Toe Squared",630,555)
GUISetFont(28,400,0,"Arial Black")
For $z = 0 To 8
    For $y = 0 To 8
        GUICtrlCreateButton("",5+60*$z+5*Int($z/3),5+60*$y+5*Int($y/3),55,55,$SS_CENTER)
        $Small[$z][$y] = 45*$z+23*$y+34
        $Big[Int($z/3)][Int($y/3)] = 23*$z+2*$y+23
    Next
Next
GUISetFont(10)
GUICtrlCreateGraphic(0,0)
GUICtrlSetGraphic(-1,$GUI_GR_COLOR,0,0)
GUICtrlSetGraphic(-1,$GUI_GR_RECT,5,180,545,10)
GUICtrlSetGraphic(-1,$GUI_GR_RECT,5,365,545,10)
GUICtrlSetGraphic(-1,$GUI_GR_RECT,180,5,10,545)
GUICtrlSetGraphic(-1,$GUI_GR_RECT,365,5,10,545)
GUICtrlCreateButton("Undo",565,420,60,40,$SS_CENTER)
GUICtrlCreateButton("End Turn",565,465,60,40,$SS_CENTER)
GUICtrlCreateButton("Restart",565,510,60,40,$SS_CENTER)
RegRead("HKEY_CURRENT_USER\Software\Nathan\TicTacToe","Name 1")
If Not @error Then $Temp = MsgBox(4,"Load","Would you like to load the saved data?")
If $Temp = 6 Then
    $Name[0] = GUICtrlCreateLabel(RegRead("HKEY_CURRENT_USER\Software\Nathan\TicTacToe","Name 0"),550,15,80,20,$SS_CENTER)
    $Score[0] = GUICtrlCreateLabel(RegRead("HKEY_CURRENT_USER\Software\Nathan\TicTacToe","Score 0"),560,35,60,20,$SS_CENTER)
    $Name[1] = GUICtrlCreateLabel(RegRead("HKEY_CURRENT_USER\Software\Nathan\TicTacToe","Name 1"),550,70,80,20,$SS_CENTER)
    $Score[1] = GUICtrlCreateLabel(RegRead("HKEY_CURRENT_USER\Software\Nathan\TicTacToe","Score 1"),560,90,60,20,$SS_CENTER)
Else
    $Name[0] = GUICtrlCreateLabel(InputBox("Player 1","Enter the name of Player 1.","Player 1"," M10",100,65),550,15,80,20,$SS_CENTER)
    $Score[0] = GUICtrlCreateLabel(0,560,35,60,20,$SS_CENTER)
    $Name[1] = GUICtrlCreateLabel(InputBox("Player 2","Enter the name of Player 2.","Player 2"," M10",100,65),550,70,80,20,$SS_CENTER)
    $Score[1] = GUICtrlCreateLabel(0,560,90,60,20,$SS_CENTER)
EndIf
$Letter = GUICtrlCreateLabel("O",560,250,60,60,$SS_CENTER)
GUICtrlSetFont(-1,35,1200,0,"Arial Black")
$Number = GUICtrlCreateLabel(1,603,292,20,20,$SS_CENTER)
GUICtrlSetBkColor(-1,$GUI_BKCOLOR_TRANSPARENT)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            $Temp = MsgBox(515,"Save","Would you like to save before exiting?")
            If $Temp = 6 Then
                RegWrite("HKEY_CURRENT_USER\Software\Nathan\TicTacToe","Name 0","REG_SZ",GUICtrlRead($Name[0]))
                RegWrite("HKEY_CURRENT_USER\Software\Nathan\TicTacToe","Name 1","REG_SZ",GUICtrlRead($Name[1]))
                RegWrite("HKEY_CURRENT_USER\Software\Nathan\TicTacToe","Score 0","REG_SZ",GUICtrlRead($Score[0]))
                RegWrite("HKEY_CURRENT_USER\Software\Nathan\TicTacToe","Score 1","REG_SZ",GUICtrlRead($Score[1]))
            EndIf
            If $Temp > 2 Then Exit
        Case 3 To 83
            Check_Small()
        Case 85
            MsgBox(48,"Nothing","Under Development.")
        Case 86
            GUICtrlSetData($Number,1)
            If GUICtrlRead($Letter) = "O" Then
                GUICtrlSetData($Letter,"X")
            Else
                GUICtrlSetData($Letter,"O")
            EndIf
            $Previous[0] = -1
            $Previous[1] = -1
        Case 87
            $Temp = MsgBox(515,"Restart","Would you like to switch positions?")
            If $Temp = 6 Then
                $Temp = GUICtrlRead($Name[0])
                GUICtrlSetData($Name[0],GUICtrlRead($Name[1]))
                GUICtrlSetData($Name[1],$Temp)
                $Temp = GUICtrlRead($Score[0])
                GUICtrlSetData($Score[0],GUICtrlRead($Score[1]))
                GUICtrlSetData($Score[1],$Temp)
            EndIf
            If $Temp > 2 Then Clear_Screen()
    EndSwitch
WEnd

Func Check_Small()
    $Row1 = Int(($msg-3)/9)
    $Column1 = $msg-3-9*$Row1
    $Row = 3*Int($Row1/3)
    $Column = 3*Int($Column1/3)
    $First = $Row*9+$Column+3
    $Temp = GUICtrlRead($Letter)
    $Finished = False
    If $Temp = "O" Then
        If $First = $Previous[0] Or $First = $Previous[1] Then Return
        $Small[$Row1][$Column1] = 0
        If GUICtrlRead($Number) = 1 Then
            GUICtrlSetData($Number,2)
            $Previous[0] = $First
        ElseIf GUICtrlRead($Number) = 2 Then 
            GUICtrlSetData($Number,3)
            $Previous[1] = $First
        Else
            GUICtrlSetData($Number,1)
            GUICtrlSetData($Letter,"X")
            $Previous[0] = -1
            $Previous[1] = -1
        EndIf
    Else
        $Small[$Row1][$Column1] = 1
        If GUICtrlRead($Number) = 1 Then
            GUICtrlSetData($Number,2)
        Else
            GUICtrlSetData($Number,1)
            GUICtrlSetData($Letter,"O")
        EndIf
    EndIf
    GUICtrlSetData($msg,$Temp)
    GUICtrlSetState($msg,$GUI_DISABLE)
    For $z = 0 To 2
        If $Small[$Row+$z][$Column] = $Small[$Row+$z][$Column+1] And $Small[$Row+$z][$Column] = $Small[$Row+$z][$Column+2] Then $Finished = True
        If $Small[$Row][$Column+$z] = $Small[$Row+1][$Column+$z] And $Small[$Row][$Column+$z] = $Small[$Row+2][$Column+$z] Then $Finished = True
    Next
    If $Small[$Row][$Column] = $Small[$Row+1][$Column+1] And $Small[$Row][$Column] = $Small[$Row+2][$Column+2] Then $Finished = True
    If $Small[$Row+2][$Column] = $Small[$Row+1][$Column+1] And $Small[$Row+2][$Column] = $Small[$Row][$Column+2] Then $Finished = True
    If $Finished = True Then
        For $z = 0 To 8
            GUICtrlSetData($First+$z+6*Int($z/3),$Temp)
            GUICtrlSetState($First+$z+6*Int($z/3),$GUI_DISABLE)
        Next
        $Big[$Row/3][$Column/3] = $Temp
        Check_Big()
        If $Temp = "X" Then
            GUICtrlSetData($Number,1)
            GUICtrlSetData($Letter,"O")
        EndIf
    EndIf
EndFunc

Func Check_Big()
    $Finished = False
    For $z = 0 To 2
        If $Big[$z][1] = $Big[$z][2] And $Big[$z][1] = $Big[$z][0] Then $Finished = True
        If $Big[1][$z] = $Big[2][$z] And $Big[1][$z] = $Big[0][$z] Then $Finished = True
    Next
    If $Big[1][1] = $Big[2][2] And $Big[1][1] = $Big[0][0] Then $Finished = True
    If $Big[0][2] = $Big[1][1] And $Big[1][1] = $Big[2][0] Then $Finished = True
    If $Finished = True Then
        If $Temp = "O" Then
            $Temp = 0
        Else
            $Temp = 1
        EndIf
        GUICtrlSetData($Score[$Temp],GUICtrlRead($Score[$Temp])+1)
        $Temp = MsgBox(4,"Winner",GUICtrlRead($Name[$Temp])&" wins!"&@CRLF&"Would you like to switch positions?")
        If $Temp = 6 Then
            $Temp = GUICtrlRead($Name[0])
            GUICtrlSetData($Name[0],GUICtrlRead($Name[1]))
            GUICtrlSetData($Name[1],$Temp)
            $Temp = GUICtrlRead($Score[0])
            GUICtrlSetData($Score[0],GUICtrlRead($Score[1]))
            GUICtrlSetData($Score[1],$Temp)
        EndIf
        Clear_Screen()
    EndIf
EndFunc

Func Clear_Screen()
    For $z = 3 To 83
        GUICtrlSetData($z,"")
        GUICtrlSetState($z,$GUI_ENABLE)
    Next
    $Previous[0] = -1
    $Previous[1] = -1
    For $z = 0 To 8
        For $y = 0 To 8
            $Small[$z][$y] = 45*$z+23*$y+34
            $Big[Int($z/3)][Int($y/3)] = 23*$z+2*$y+23
        Next
    Next
    GUICtrlSetData($Number,1)
    GUICtrlSetData($Letter,"O")
EndFunc
Edited by crzftx
Link to comment
Share on other sites

I thought id use the registry to eliminate worrying about where to put the ini (and the ease of changing it). Where would anyone recommend putting it? Maybe just use @ScriptDir, since these are just simple examples?

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