Jump to content

Dots & Boxes Game


SublimePorte
 Share

Recommended Posts

This is my first real attempt to actually write something that's useable. It's a little game that's usually played with pen & paper called dots and boxes. My code is very messy and probably quite illogical to most, so please feel free to offer "better coding tips".

Enjoy.

Screenshot attached

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.8.1
 Author:         SublimePorte (sultan@islamic.caliphate.info)

 Script Function:
    Boxes & Dots Game.

#ce ----------------------------------------------------------------------------

#include <GUIConstants.au3>
#Include <GuiIPAddress.au3>
#Include <GuiStatusBar.au3>

$Form1 = GUICreate("Boxes", 600, 540)

Dim $Buttons[8][8][2][4]
Dim $mainsock
Dim $connsock = -1
Dim $btnCAN
Dim $btnOK
Dim $Form2 = 0
Dim $Form3 = 0
Dim $myturn = 0
Dim $us = 0
Dim $them = 0
Dim $F2_Button1

$w = 0
$x = 0
$y = 20
$z = 25
$wid = 5
$hei = 60

For $m = 0 To 1
    If $m = 0 Then
        $p = 7
        $q = 8
    Else
        $p = 8
        $q = 7
    EndIf
    for $n = 1 to $p
        For $o = 1 to $q
            If $m = 1 Then 
                $y1 = $y + 5
                $z1 = $z - 5
                $Buttons[$w][$x][$m][0] = GUICtrlCreateGraphic($y1, $z1, $hei, $wid)
                GUICtrlSetBkColor(-1, 0xffffff)
                $Buttons[$w][$x][$m][2] = $y1
                $Buttons[$w][$x][$m][3] = $z1
            Else
                $Buttons[$w][$x][$m][0] = GUICtrlCreateGraphic($y, $z, $wid, $hei)
                GUICtrlSetBkColor(-1, 0xffffff)
                $Buttons[$w][$x][$m][2] = $y
                $Buttons[$w][$x][$m][3] = $z
            Endif
            $x += 1
            $y += 65
        Next
        $max = $Buttons[$w][$x-1][1][0]
        $x = 0
        $w += 1
        $o = 1
        If $m = 1 Then
            $y = 20
            $z += 65
        Else
            $y = 20
            $z += 65
        EndIf
    Next
    $w = 0
    $y = 20
    $z = 25
Next

$srvbtn = GUICtrlCreateButton("Be Server", 500, 20, 70, 25)
$cltbtn = GUICtrlCreateButton("Be Client", 500, 50, 70, 25)
$lblturn = GUICtrlCreateLabel("Opponent's Turn", 500, 90, 80, 25)
GUICtrlSetColor(-1, 0xFF0000)
GUICtrlCreateLabel("Us:", 500, 115, 50, 25)
GUICtrlSetColor(-1, 0xFF0000)
$lblusscore = GUICtrlCreateLabel("0", 540, 115, 50, 25)
GUICtrlCreateLabel("Them:", 500, 135, 50, 25)
GUICtrlSetColor(-1, 0x0000FF)
$lblthemscore = GUICtrlCreateLabel("0", 540, 135, 80, 25)
$Status = _GUICtrlStatusBarCreate($Form1, 600, "Status Bar")
_GUICtrlStatusBarSetText($Status, "Net Status:  Not Connected")

$Form3 = GUICreate("Details for connection", 160, 130, -1, -1, $DS_MODALFRAME)
GUICtrlCreateLabel("IP Address", 10, 5, 60, 20)
$srvip = _GUICtrlIpAddressCreate ($Form3, 10, 20, 125, 30, $WS_THICKFRAME)
$btnOK = GUICtrlCreateButton("Ok", 12, 65, 60, 25)
$btnCAN = GUICtrlCreateButton("Cancel", 75, 65, 60, 25)

GUISetState(@SW_SHOW, $Form1)

While 1
    $msg = GUIGetMsg(1)
    Select
        Case $msg[0] = $GUI_EVENT_CLOSE And $msg[1] = $Form1 
            Exit            
        Case $msg[0] > 1 And $msg[0] < $max + 1
            If _IsSpent($msg[0]) Or $myturn < 1 Then
                Msgbox(0, "Test", "Invalid move or not your move.")
                ContinueLoop
            EndIf
            $myturn = 0
            _Spent($msg[0], 0)
            _SendMove($msg[0])
        Case $msg[0] = $srvbtn
            If GUICtrlRead($srvbtn) = "Stop Server" Then
                GUICtrlSetData($srvbtn, "Be Server")
                TCPCloseSocket($mainsock)
                TCPShutdown()
                _GUICtrlStatusBarSetText($Status, "Net Status:  Not Connected")
                GUICtrlSetState($cltbtn, $GUI_ENABLE)
            Else
                GUICtrlSetData($srvbtn, "Stop Server")
                _BeServer()
            EndIf
        Case $msg[0] = $cltbtn
            If GUICtrlRead($cltbtn) = "Disconnect" Then
                TCPCloseSocket($mainsock)
                TCPShutdown()
                GUICtrlSetData($cltbtn, "Be Client")
                _GUICtrlStatusBarSetText($Status, "Net Status:  Not Connected")
            Else
                GUISetState(@SW_SHOW, $Form3)
            EndIf           
            
        Case $msg[0] = $btnCAN
            GUISetState(@SW_HIDE, $Form3)
        Case $msg[0] = $btnOK
            _BeClient()
            If $mainsock = -1 Then
                Msgbox(0, "Connection Failure!", "Connection has failed to: " & _GUICtrlIpAddressGet($srvip) & @CRLF & "Check the address and try again.")
            Else
                $myturn = 1
                GUICtrlSetData($cltbtn, "Disconnect")
                _GUICtrlStatusBarSetText($Status, "Net Status:  Connected")
                GUICtrlSetState($srvbtn, $GUI_DISABLE)
            EndIf
    EndSelect
    _PollConn()
    If $myturn > 0 And GUICtrlRead($lblturn) = "Opponent's Turn" Then
        GUICtrlSetData($lblturn, "Your Turn")
        GUICtrlSetColor($lblturn, 0x00FF55)
    ElseIf $myturn = 0 And GUICtrlRead($lblturn) = "Your Turn" Then
        GUICtrlSetData($lblturn, "Opponent's Turn")
        GUICtrlSetColor($lblturn, 0xFF0000) 
    EndIf
WEnd

Func _Spent($idx, $remote)
    $colour = 0xFF0000
    If $remote > 0 Then $colour = 0x0000FF
    GUICtrlSetBkColor($idx, $colour)
    For $k = 0 To 1
        For $i = 0 to Ubound($Buttons) - 1
            For $j = 0 To UBound($Buttons, 2) - 1
                if $Buttons[$i][$j][$k][0] = $idx Then $Buttons[$i][$j][$k][1] = 1
            Next
        Next
    Next
    _IsWinner($idx, $remote)
EndFunc

Func _IsSpent($idx)
    For $k = 0 To 1
        For $i = 0 to Ubound($Buttons) - 1
            For $j = 0 To UBound($Buttons, 2) - 1
                if $Buttons[$i][$j][$k][0] = $idx Then
                    If $Buttons[$i][$j][$k][1] = 1 Then Return 1
                EndIf
            Next
        Next
    Next
    Return 0
EndFunc

Func _IsWinner($idx, $remote)
    $colour = 0xFFCCCC
    $filename = "\avatar.jpg"
    If $remote > 0 Then
        $colour = 0xCCCCFF
        $filename = "\avatar2.jpg"
    EndIf
    For $k = 0 To 1
        For $i = 0 to Ubound($Buttons) - 1
            For $j = 0 To UBound($Buttons, 2) - 1
                if $Buttons[$i][$j][$k][0] = $idx Then
                    If $k = 0 Then
                        If $j > 0 Then
                            If _IsSpent($Buttons[$i][$j-1][$k][0]) And _
                               _IsSpent($Buttons[$i][$j-1][$k+1][0]) And _
                               _IsSpent($Buttons[$i+1][$j-1][$k+1][0]) Then
                                If $remote > 0 Then
                                    $myturn = 0
                                    $them += 1
                                    GUICtrlSetData($lblthemscore, $them)
                                Else
                                    $myturn = 1
                                    $us += 1
                                    GUICtrlSetData($lblusscore, $us)
                                EndIf
                                If FileExists(@ScriptDir & $filename) Then
                                    GUICtrlCreatePic(@ScriptDir & $filename, $Buttons[$i][$j][$k][2] - 60, $Buttons[$i][$j][$k][3], 60, 60)
                                Else
                                    GUICtrlCreateGraphic($Buttons[$i][$j][$k][2] - 60, $Buttons[$i][$j][$k][3], 60, 60)
                                    GUICtrlSetBkColor(-1, $colour)
                                EndIf
                            EndIf
                        EndIf
                        If $j < UBound($Buttons, 2) - 1 Then
                            If _IsSpent($Buttons[$i][$j+1][$k][0]) And _
                               _IsSpent($Buttons[$i][$j][$k+1][0]) And _
                               _IsSpent($Buttons[$i+1][$j][$k+1][0]) Then
                                If $remote > 0 Then
                                    $myturn = 0
                                    $them += 1
                                    GUICtrlSetData($lblthemscore, $them)
                                Else
                                    $myturn = 1
                                    $us += 1
                                    GUICtrlSetData($lblusscore, $us)
                                EndIf
                                If FileExists(@ScriptDir & $filename) Then
                                    GUICtrlCreatePic(@ScriptDir & $filename, $Buttons[$i][$j][$k][2] + 5, $Buttons[$i][$j][$k][3], 60, 60)
                                Else
                                    GUICtrlCreateGraphic($Buttons[$i][$j][$k][2] + 5, $Buttons[$i][$j][$k][3], 60, 60)
                                    GUICtrlSetBkColor(-1, $colour)
                                EndIf
                            EndIf
                        EndIf
                    Else
                        If $i > 0 Then
                            If _IsSpent($Buttons[$i-1][$j][$k][0]) And _
                               _IsSpent($Buttons[$i-1][$j][$k-1][0]) And _
                               _IsSpent($Buttons[$i-1][$j+1][$k-1][0]) Then
                                If $remote > 0 Then
                                    $myturn = 0
                                    $them += 1
                                    GUICtrlSetData($lblthemscore, $them)
                                Else
                                    $myturn = 1
                                    $us += 1
                                    GUICtrlSetData($lblusscore, $us)
                                EndIf
                                If FileExists(@ScriptDir & $filename) Then
                                    GUICtrlCreatePic(@ScriptDir & $filename, $Buttons[$i][$j][$k][2], $Buttons[$i][$j][$k][3] - 60, 60, 60)
                                Else
                                    GUICtrlCreateGraphic($Buttons[$i][$j][$k][2], $Buttons[$i][$j][$k][3] - 60, 60, 60)
                                    GUICtrlSetBkColor(-1, $colour)  
                                EndIf
                            EndIf
                        EndIf
                        If $i < UBound($Buttons, 2) - 1 Then
                            If _IsSpent($Buttons[$i+1][$j][$k][0]) And _
                               _IsSpent($Buttons[$i][$j][$k-1][0]) And _
                               _IsSpent($Buttons[$i][$j+1][$k-1][0]) Then
                                If $remote > 0 Then
                                    $myturn = 0
                                    $them += 1
                                    GUICtrlSetData($lblthemscore, $them)
                                Else
                                    $myturn = 1
                                    $us += 1
                                    GUICtrlSetData($lblusscore, $us)
                                EndIf
                                If FileExists(@ScriptDir & $filename) Then
                                    GUICtrlCreatePic(@ScriptDir & $filename, $Buttons[$i][$j][$k][2], $Buttons[$i][$j][$k][3] + 5, 60, 60)
                                Else
                                    GUICtrlCreateGraphic($Buttons[$i][$j][$k][2], $Buttons[$i][$j][$k][3] + 5, 60, 60)
                                    GUICtrlSetBkColor(-1, $colour)  
                                EndIf
                            EndIf
                        EndIf                       
                    EndIf
                EndIf
            Next
        Next
    Next
    If $us + $them = 49 Then
        If $us > $them Then
            Msgbox(0, "Congratulations!", "You have won!!!")
        Else
            MsgBox(0, "Not to worry!", "Better luck next time")
        EndIf
    EndIf       
EndFunc

Func _BeServer()
    TCPStartup()
    $mainsock = TCPListen(TCPNameToIP(@ComputerName), 65433)
    If $mainsock = -1 Then Exit
    _GUICtrlStatusBarSetText($Status, "Net Status:  Waiting For Connections... (IP: " & TCPNameToIP(@ComputerName) & ")")
    GUICtrlSetState($cltbtn, $GUI_DISABLE)
    Do
        $gmsg = GUIGetMsg(1)
        If $gmsg[0] = $srvbtn Then
            GUICtrlSetData($srvbtn, "Be Server")
            _GUICtrlStatusBarSetText($Status, "Net Status:  Not Connected")
            GUICtrlSetState($cltbtn, $GUI_ENABLE)
            TCPCloseSocket($mainsock)
            Return
        EndIf
        $connsock = TCPAccept($mainsock)
    Until $connsock <> -1
    _GUICtrlStatusBarSetText($Status, "Net Status:  Connected")
    GUISwitch($Form1)
EndFunc

Func _BeClient()
    TCPStartup()
    $mainsock = TCPConnect(_GUICtrlIpAddressGet($srvip), 65433)
    GUISetState(@SW_HIDE, $Form3)
    GUISwitch($Form1)
EndFunc

Func _PollConn()
    If $connsock <> -1 Then 
        $mesg = TCPRecv($connsock, 2048)
    Else
        $mesg = TCPRecv($mainsock, 2048)
    EndIf
    If $mesg <> "" Then
        $myturn = 1
        _Spent($mesg, 1)
    EndIf
EndFunc
    
Func _SendMove($idx)
    If $connsock <> -1 Then
        TCPSend($connsock, $idx)
    Else
        TCPSend($mainsock, $idx)
    EndIf
EndFunc

post-21308-1190603925_thumb.jpg

Edited by SublimePorte
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...