Jump to content

Tic Tac Toe problem


Jex
 Share

Recommended Posts

My problem is that if a second client connects to the server, the first one disconnects. ( but I'm not really sure that happens ) I've created a very simple console to observe what is going on and wrong. So here are the results:

Server initialized.

392 connected. ; first client

400 connected. ; second client

!Ready send -1 ; first client. after 2 client connected sending ready message to all clients ( here first socket disconnecting i think )

!Ready send 400 ; second client.

!Player-2 send -1 ; first client. roll for who start first and send command

!Player-1 send 400 ; second client.

I'm used this server http://www.autoitscript.com/forum/index.ph...c=19369&hl= and edited for my game.

Without that problem everything working i think and ready for play :)

Client :

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

Global $MainSocket, $Server, $Port, $Nick, $Nick2, $Turn, $Shape, $Shape2, $Winner, $Point1, $Point2, $Moves, $Games, $Player, $Me, $Opponent
Global $Box[4][4]
Global $Button[4][4]
Global $Checkwin[9]
Global $Connected = 0
Local $MaxLength = 512

$Form1 = GUICreate("Tic Tac Toe", 217, 359, 193, 115)
$Group1 = GUICtrlCreateGroup("Login", 16, 224, 185, 121)
$Input1 = GUICtrlCreateInput(@IPAddress1, 72, 244, 105, 21)
$Input2 = GUICtrlCreateInput(5902, 72, 276, 105, 21)
$Label2 = GUICtrlCreateLabel("IP :", 32, 252, 20, 17)
$Label3 = GUICtrlCreateLabel("Port :", 32, 284, 29, 17)
$Connect = GUICtrlCreateButton("Connect !", 32, 308, 147, 25, 0)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button[1][1] = GUICtrlCreateButton("", 36, 17, 49, 49, 0)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
$Button[1][2] = GUICtrlCreateButton("", 84, 17, 49, 49, 0)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
$Button[1][3] = GUICtrlCreateButton("", 132, 17, 49, 49, 0)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
$Button[2][1] = GUICtrlCreateButton("", 36, 65, 49, 49, 0)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
$Button[2][2] = GUICtrlCreateButton("", 84, 65, 49, 49, 0)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
$Button[2][3] = GUICtrlCreateButton("", 132, 65, 49, 49, 0)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
$Button[3][1] = GUICtrlCreateButton("", 36, 113, 49, 49, 0)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
$Button[3][2] = GUICtrlCreateButton("", 84, 113, 49, 49, 0)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
$Button[3][3] = GUICtrlCreateButton("", 132, 113, 49, 49, 0)
GUICtrlSetFont(-1, 24, 800, 0, "MS Sans Serif")
$Label4 = GUICtrlCreateLabel("Status :", 24, 176, 192, 17)
$Label5 = GUICtrlCreateLabel("Games : 0  You : 0  Opponent : 0", 24, 200, 192, 17)
GUISetState()

For $i = 1 To 3
    For $j = 1 To 3
        $Box[$i][$j] = 0
    Next
Next

While 1
    Checktcp()
    $msg = GUIGetMsg()
    Switch $msg
        Case $Connect
            TCPStartup()
            Check()
            $MainSocket = TCPConnect($Server, $Port)
            If $MainSocket = -1 Then
                GUICtrlSetData($Label4, "Status : Unable to connect.")
            Else
                GUICtrlSetData($Label4, "Status : Connecting...")
                TCPSend($MainSocket, "!Connected")
            EndIf
        Case $Button[1][1]
            Checkbutton(1, 1)
        Case $Button[1][2]
            Checkbutton(1, 2)
        Case $Button[1][3]
            Checkbutton(1, 3)
        Case $Button[2][1]
            Checkbutton(2, 1)
        Case $Button[2][2]
            Checkbutton(2, 2)
        Case $Button[2][3]
            Checkbutton(2, 3)
        Case $Button[3][1]
            Checkbutton(3, 1)
        Case $Button[3][2]
            Checkbutton(3, 2)
        Case $Button[3][3]
            Checkbutton(3, 3)
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func Checkbutton($row, $column)
    TCPSend($MainSocket, "!Button-" & $Player & "-" & $row & "-" & $column)
EndFunc  ;==>Checkbutton

Func Click($Player, $row, $column)
    If $Player = $Me Then
        If $Box[$row][$column] = 0 And $Turn = True Then
            GUICtrlSetData($Button[$row][$column], $Shape)
            GUICtrlSetState($Button[$row][$column], $GUI_DISABLE)
            $Box[$row][$column] = 1
            Checkwin()
            Checkturn()
        EndIf
    ElseIf $Player = $Opponent Then
        If $Box[$row][$column] = 0 And $Turn = False Then
            GUICtrlSetData($Button[$row][$column], $Shape2)
            GUICtrlSetState($Button[$row][$column], $GUI_DISABLE)
            $Box[$row][$column] = 2
            Checkwin()
            Checkturn()
        EndIf
    EndIf
EndFunc  ;==>Click

Func Check()
    $Server = GUICtrlRead($Input1)
    $Port = GUICtrlRead($Input2)
EndFunc  ;==>Check

Func Checktcp()
    $Data = TCPRecv($MainSocket, $MaxLength)
    If StringInStr($Data, "!Player") Then
        If $Connected = 2 Then
            $Data = StringSplit($Data, "-")
            $Player = $Data[2]
            $Me = $Player
            If $Me = 1 Then
                $Opponent = 2
                $Shape = "X"
                $Shape2 = "O"
                $Turn = True
            Else
                $Opponent = 1
                $Shape = "O"
                $Shape2 = "X"
                $Turn = False
            EndIf
            Checkturn2()
            $Connected = 3
        EndIf
    ElseIf $Data = "!Ready" Then
        If $Connected = 1 Then
            GUICtrlSetData($Label4, "Status : Your Opponent ready.")
            $Connected = 2
        EndIf
    ElseIf $Data = "!Bye" Then
        MsgBox(16, "Session Ended", "Connection Terminated.")
        Exit
    ElseIf $Data = "!Connected" Then
        If $Connected = 0 Then
            GUICtrlSetData($Label4, "Status : Waiting for Opponent.")
            $Connected = 1
        EndIf
    ElseIf StringInStr($Data, "!Button") Then
        $Data = StringSplit($Data, "-")
        Click($Data[2], $Data[3], $Data[4])
    EndIf
EndFunc  ;==>Checktcp

Func Checkwin()
    For $j = 1 To 8
        $Checkwin[$j] = 0
    Next
    For $j = 1 To 3
        $Checkwin[1] += $Box[1][$j]
        $Checkwin[2] += $Box[2][$j]
        $Checkwin[3] += $Box[3][$j]
        $Checkwin[4] += $Box[$j][1]
        $Checkwin[5] += $Box[$j][2]
        $Checkwin[6] += $Box[$j][3]
    Next
    $Checkwin[7] = $Box[1][1] + $Box[2][2] + $Box[3][3]
    $Checkwin[8] = $Box[1][3] + $Box[2][2] + $Box[3][1]
    For $i = 1 To 8
        If $Checkwin[$i] = 3 Then
            $Winner = 1
            GUICtrlSetData($Label4, "Status : Winner You !")
            $Point1 = $Point1 + 1
            GUICtrlSetData($Label5, "Games : " & $Games & "  You : " & $Point1 & "  Opponent : " & $Point2)
            Reset()
        ElseIf $Checkwin[$i] = 6 Then
            $Winner = 1
            GUICtrlSetData($Label4, "Status : Winner Opponent !")
            $Point2 = $Point2 + 1
            GUICtrlSetData($Label5, "Games : " & $Games & "  You : " & $Point1 & "  Opponent : " & $Point2)
            Reset()
        EndIf
    Next
EndFunc  ;==>Checkwin

Func Reset()
    Sleep(1000)
    For $i = 1 To 3
        For $j = 1 To 3
            $Box[$i][$j] = 0
            GUICtrlSetData($Button[$i][$j], "")
            GUICtrlSetState($Button[$i][$j], $GUI_ENABLE)
        Next
    Next
    If $Shape = "X" Then
        $Shape = "O"
        $Shape2 = "X"
        $Turn = False
    Else
        $Shape = "X"
        $Shape2 = "O"
        $Turn = True
    EndIf
    $Games += 1
    $Moves = 0
    $Winner = 0
EndFunc  ;==>Reset

Func Checkturn()
    If $Winner <> 1 Then
        $Moves += 1
        If $Moves = 9 Then
            GUICtrlSetData($Label4, "Status : Draw...")
            Reset()
        Else
            If $Turn = True Then
                $Turn = False
                GUICtrlSetData($Label4, "Status : Opponent Turn!  (  " & $Shape2 & "  )")
            Else
                $Turn = True
                GUICtrlSetData($Label4, "Status : Your Turn!  (  " & $Shape & "  )")
            EndIf
        EndIf
    EndIf
EndFunc  ;==>Checkturn

Func Checkturn2()
    If $Turn = True Then
        GUICtrlSetData($Label4, "Status : Your Turn!  (  " & $Shape & "  )")
    Else
        GUICtrlSetData($Label4, "Status : Opponent Turn!  (  " & $Shape2 & "  )")
    EndIf
EndFunc  ;==>Checkturn2

Func OnAutoItExit()
    If $MainSocket <> -1 Then
        TCPSend($MainSocket, "!Bye")
        TCPCloseSocket($MainSocket)
    EndIf
    TCPShutdown()
EndFunc  ;==>OnAutoItExit

Server :

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

Global Const $Port = 5902
Global $MaxConc = 2
Global Const $MaxLength = 512
Global $ConnectedSocket[$MaxConc]
Global $CurrentSocket = 0
Global $Connection = 0
Local $Track = 0
Global Const $MaxConnection = ($MaxConc - 1)

For $Track = 0 To $MaxConnection Step 1
    $ConnectedSocket[$Track] = -1
Next

$Form1 = GUICreate("Tic Tac Toe", 602, 439, 193, 115)
$Edit = GUICtrlCreateEdit("", 8, 8, 585, 393, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_READONLY,$ES_WANTRETURN,$WS_HSCROLL,$WS_VSCROLL))
$Input1 = GUICtrlCreateInput("", 8, 408, 505, 21)
$Button1 = GUICtrlCreateButton("Send All", 520, 408, 75, 23, 0)
GUISetState(@SW_SHOW)

Global $MainSocket = TCPStartServer($Port, $MaxConc)
If @error <> 0 Then
    Console("Server unable to initialize.")
Else
    Console("Server initialized.")
EndIf

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $Button1
            $Input = GUICtrlRead($Input1)
            TCPSendMessageAll($MaxConnection, $Input)
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    $ConnectedSocket[$CurrentSocket] = TCPAccept($MainSocket)
    If $ConnectedSocket[$CurrentSocket] <> -1 Then
        $CurrentSocket = SocketSearch()
    EndIf
    If $Connection = 2 Then
        TCPSendMessageAll($MaxConnection, "!Ready")
        $Player1 = Random(1, 2)
        If $Player1 = 1 Then
            $Player1 = "!Player-1"
            $Player2 = "!Player-2"
        Else
            $Player1 = "!Player-2"
            $Player2 = "!Player-1"
        EndIf
        TCPSend($ConnectedSocket[0], $Player1)
        Console($Player1 & " send " & $ConnectedSocket[0])
        TCPSend($ConnectedSocket[1], $Player2)
        Console($Player2 & " send " & $ConnectedSocket[1])
        $Connection = 3
    EndIf
    $Track = 0
    For $Track = 0 To $MaxConnection Step 1
        If $ConnectedSocket[$Track] <> -1 Then
            $Data = TCPRecv($ConnectedSocket[$Track], $MaxLength)
            If $Data = "!Bye" Then
                TCPCloseSocket($ConnectedSocket[$Track])
                Console($ConnectedSocket[$Track] & " disconnected.")
                $ConnectedSocket[$Track] = -1
                $CurrentSocket = SocketSearch()
            ElseIf $Data = "!Connected" Then
                TCPSend($ConnectedSocket[$Track], "!Connected")
                Console($ConnectedSocket[$Track] & " connected.")
                $Connection = $Connection + 1
            ElseIf $Data <> "" Then
                TCPSendMessageAll($MaxConnection, $Data)
            EndIf
        EndIf
    Next
WEnd

Func Console($Dataa)
    $Editt = GUICtrlRead($Edit)
    If $Editt = "" Then
        GUICtrlSetData($Edit, $Dataa)
    Else
        GUICtrlSetData($Edit, $Editt & @CRLF & $Dataa)
    EndIf
EndFunc  ;==>Console

Func TCPSendMessageAll($ConnectionLimit, $Data)
    Local $Track = 0
    For $Track = 0 To $ConnectionLimit
        TCPSend($ConnectedSocket[$Track], $Data)
        Console($Data & " send " & $ConnectedSocket[$Track])
    Next
EndFunc  ;==>TCPSendMessageAll

Func TCPStartServer($Port, $MaxConnect = 1)
    Local $Socket
    $Socket = TCPStartup()
    Select
        Case $Socket = 0
            SetError(@error)
            Return -1
    EndSelect
    $Socket = TCPListen(@IPAddress1, $Port, $MaxConnect)
    Select
        Case $Socket = -1
            SetError(@error)
            Return 0
    EndSelect
    SetError(0)
    Return $Socket
EndFunc  ;==>TCPStartServer

Func SocketSearch()
    Local $Track = 0
    For $Track = 0 To $MaxConnection Step 1
        If $ConnectedSocket[$Track] = -1 Then
            Return $Track
        Else
        ;
        EndIf
    Next
EndFunc  ;==>SocketSearch

Sorry for my bad English ;)

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