Sign in to follow this  
Followers 0
z0mgItsJohn

Online Rock, Paper, Scissors

3 posts in this topic

#1 ·  Posted (edited)

As the title says, I've wrote a nice little online Rock, Paper, Scissors game.

Server.au3:

#Include <GUIConstantsEx.Au3>
#Include <WindowsConstants.Au3>
#Include <Misc.Au3>
#Include <GUIEdit.Au3>
#Include <INet.Au3>
#NoTrayIcon
Opt ('GUIOnEventMode', 1)

Global $STS = 0, $Client = -1, $Server = -1, $cToggle = 0, $Weapon = 0, $cWeapon = 0
Global $Server_Wins = 0, $Client_Wins = 0

$Main = GUICreate ('Rock, Paper, Scissors - Player #1', 300, 225)
GUISetOnEvent ($GUI_EVENT_CLOSE, '_Exit')
GUISetBkColor (0x000000)
GUICtrlCreateGroup ('Settings', 5, 1, 173, 68)
GUICtrlSetColor (-1, 0xFFFFFF)
$ConStr = GUICtrlCreateInput (@IpAddress1 & ':1234', 12, 18, 125, 20, 1)
$sToggle = GUICtrlCreateButton ('Start', 142, 18, 30, 21)
GUICtrlSetOnEvent ($sToggle, '_sToggle')
$sMatch = GUICtrlCreateButton ('Start Match', 12, 43, 75, 20)
GUICtrlCreateGroup ('Game Console', 5, 70, 290, 150)
GUICtrlSetColor (-1, 0xFFFFFF)
GUICtrlSetOnEvent ($sMatch, '_sMatch')
GUICtrlSetState ($sMatch, $GUI_DISABLE)
$Console = GUICtrlCreateEdit ('', 12, 88, 276, 125, 2103360)
GUICtrlSetFont ($Console, 9, '','','Lucida Sans Unicode')
GUISetState (@SW_SHOW)

_Log ('Online Rock, Paper, Scissors v2.0')
_Log ('Made by : John O.')

$Child = GUICreate ('Rock, Paper, or Scissors?', 247, 66, -1, -1, -1, 128)
GUISetOnEvent ($GUI_EVENT_CLOSE, '_cToggle')
GUISetBkColor (0x000000)
GUICtrlCreateButton ('', 5, 5, 76, 56, 32896)
GUICtrlSetImage (-1, '1.bmp')
GUICtrlSetOnEvent (-1, '_1')
GUICtrlCreateButton ('', 85, 5, 76, 56, 32896)
GUICtrlSetImage (-1, '2.bmp')
GUICtrlSetOnEvent (-1, '_2')
GUICtrlCreateButton ('', 166, 5, 76, 56, 32896)
GUICtrlSetImage (-1, '3.bmp')
GUICtrlSetOnEvent (-1, '_3')
GUISetState (@SW_HIDE, $Child)

While 1
    If $STS = 1 Then
        If $Client = -1 Then
            Do
                $Client = TcpAccept ($Server)
            Until $Client <> -1
            _Log ('Client has connected.')
            GUICtrlSetState ($sMatch, $GUI_ENABLE)
        Else
            $Recv = TcpRecv ($Client, 1000)
            If @Error Then
                TcpCloseSocket ($Client)
                $Client = -1
                $Server_Wins = 0
                $Client_Wins = 0
                $Weapon = 0
                $cWeapon = 0
                _Log ('Client has disconnected.')
                GUICtrlSetState ($sMatch, $GUI_DISABLE)
            ElseIf StringInStr ($Recv, 'cWeapon:') Then
                $cWeapon = StringReplace ($Recv, 'cWeapon:','')
                If $Weapon == 'Rock' Or $Weapon == 'Paper' Or $Weapon == 'Scissors' Then _Process ()
            EndIf
        EndIf
    EndIf
    Sleep (25)
WEnd

Func _Process ()
    If $cWeapon == 'Rock' And $Weapon == 'Rock' Then
        $Result = 'Rock = Rock, there is a tie!'
    EndIf
    If $cWeapon == 'Rock' And $Weapon == 'Paper' Then
        $Result = 'Paper beats Rock, player #1 wins!'
        $Server_Wins = $Server_Wins + 1
    EndIf
    If $cWeapon == 'Rock' And $Weapon == 'Scissors' Then
        $Result = 'Rock beats Scissors, player #2 wins!'
        $Client_Wins = $Client_Wins + 1
    EndIf
    If $cWeapon == 'Paper' And $Weapon == 'Rock' Then
        $Result = 'Paper beats Rock, player #2 wins!'
        $Client_Wins = $Client_Wins + 1
    EndIf
    If $cWeapon == 'Paper' And $Weapon == 'Paper' Then
        $Result = 'Paper = Paper, there is a tie!'
    EndIf
    If $cWeapon == 'Paper' And $Weapon == 'Scissors' Then
        $Result = 'Scissors beats Paper, player #1 wins!'
        $Server_Wins = $Server_Wins + 1
    EndIf
    If $cWeapon == 'Scissors' And $Weapon == 'Rock' Then
        $Result = 'Rock beats Scissors, player #1 wins!'
        $Server_Wins = $Server_Wins + 1
    EndIf
    If $cWeapon == 'Scissors' And $Weapon == 'Paper' Then
        $Result = 'Scissors beats Paper, player #2 wins!'
        $Client_Wins = $Client_Wins + 1
    EndIf
    If $cWeapon == 'Scissors' And $Weapon == 'Scissors' Then
        $Result = 'Scissors = Scissors, there is a tie!'
    EndIf
    _Log ($Result & @CRLF & 'The match has ended.' & @CRLF & "Player #1's wins : " & $Server_Wins & ", Player #2's wins : " & $Client_Wins)
    TcpSend ($Client, 'Result:' & $Result & @CRLF & 'The match has ended.' & @CRLF & "Player #1's wins : " & $Server_Wins & ", Player #2's wins : " & $Client_Wins)
    $Weapon = 0
    $cWeapon = 0
    GUICtrlSetState ($sMatch, $GUI_ENABLE)
EndFunc

Func _1 ()
    $Weapon = 'Rock'
    _Log ('You have selected : ' & $Weapon)
    _Log ('Waiting for results...')
    _cToggle ()
    If $cWeapon == 'Rock' Or $cWeapon == 'Paper' Or $cWeapon == 'Scissors' Then _Process ()
EndFunc

Func _2 ()
    $Weapon = 'Paper'
    _Log ('You have selected : ' & $Weapon)
    _Log ('Waiting for results...')
    _cToggle ()
    If $cWeapon == 'Rock' Or $cWeapon == 'Paper' Or $cWeapon == 'Scissors' Then _Process ()
EndFunc

Func _3 ()
    $Weapon = 'Scissors'
    _Log ('You have selected : ' & $Weapon)
    _Log ('Waiting for results...')
    _cToggle ()
    If $cWeapon == 'Rock' Or $cWeapon == 'Paper' Or $cWeapon == 'Scissors' Then _Process ()
EndFunc

Func _sMatch ()
    _Log ('The match has started.' & @CRLF & 'Choose your weapon.')
    TcpSend ($Client, 'sMatch')
    _cToggle ()
    GUICtrlSetState ($sMatch, $GUI_DISABLE)
EndFunc

Func _cToggle ()
    If $cToggle = 0 Then
        GUISetState (@SW_SHOW, $Child)
        $cToggle = 1
    Else
        GUISetState (@SW_HIDE, $Child)
        $cToggle = 0
    EndIf
EndFunc

Func _sToggle ()
    If $STS = 0 Then
        TcpStartUp ()
        $rConStr = GUICtrlRead ($ConStr)
        $rConStr = StringSplit ($rConStr, ':')
        If $rConStr[0] = 2 Then
            $Server = TcpListen ($rConStr[1], $rConStr[2])
            If @Error Or $Server = -1 Then
                TcpShutdown ()
                _Log ('Error : Cannot start the server.')
                $Server = -1
                Return @Error
            EndIf
        Else
            TcpShutdown ()
            _Log ('Error : Cannot start the server.')
            Return @Error
        EndIf
        _Log ('Global Server -> ' & _GetIP () & ':' & $rConStr[2])
        _Log ('Waiting for client...')
        GUICtrlSetState ($ConStr, $GUI_DISABLE)
        GUICtrlSetData ($sToggle, 'Stop')
        $STS = 1
    Else
        TcpCloseSocket ($Server)
        TcpCloseSocket ($Client)
        TcpShutdown ()
        _Log ('The server has stopped.')
        GUICtrlSetState ($ConStr, $GUI_ENABLE)
        GUICtrlSetState ($sMatch, $GUI_DISABLE)
        GUICtrlSetData ($sToggle, 'Start')
        $Server = -1
        $Client = -1
        $STS = 0
        $Server_Wins = 0
        $Client_Wins = 0
    EndIf
EndFunc

Func _Log ($Data)
    GUICtrlSetData ($Console, GUICtrlRead ($Console) & $Data & @CRLF)
    _GUICtrlEdit_LineScroll ($Console, 0, _GUICtrlEdit_GetLineCount ($Console) - 1)
EndFunc

Func _Exit ()
    Exit
EndFunc

Client.au3:

#Include <GUIConstantsEx.Au3>
#Include <WindowsConstants.Au3>
#Include <Misc.Au3>
#Include <GUIEdit.Au3>
#NoTrayIcon
Opt ('GUIOnEventMode', 1)

Global $_sToggle = 0, $cToggle = 0, $Server = -1

$Main = GUICreate ('Rock, Paper, Scissors - Player #2', 300, 202)
GUISetOnEvent ($GUI_EVENT_CLOSE, '_Exit')
GUISetBkColor (0x000000)
GUICtrlCreateGroup ('Settings', 5, 1, 208, 45)
GUICtrlSetColor (-1, 0xFFFFFF)
$ConStr = GUICtrlCreateInput (@IpAddress1 & ':1234', 12, 18, 125, 20, 1)
$sToggle = GUICtrlCreateButton ('Connect', 142, 18, 65, 21)
GUICtrlSetOnEvent ($sToggle, '_sToggle')
GUICtrlCreateGroup ('Game Console', 5, 46, 290, 150)
GUICtrlSetColor (-1, 0xFFFFFF)
$Console = GUICtrlCreateEdit ('', 12, 63, 276, 125, 2103360)
GUICtrlSetFont ($Console, 9, '','','Lucida Sans Unicode')
GUISetState (@SW_SHOW)

_Log ('Online Rock, Paper, Scissors v2.0')
_Log ('Made by : John O.')

$Child = GUICreate ('Rock, Paper, or Scissors?', 247, 66, -1, -1, -1, 128)
GUISetOnEvent ($GUI_EVENT_CLOSE, '_cToggle')
GUISetBkColor (0x000000)
GUICtrlCreateButton ('', 5, 5, 76, 56, 32896)
GUICtrlSetImage (-1, '1.bmp')
GUICtrlSetOnEvent (-1, '_1')
GUICtrlCreateButton ('', 85, 5, 76, 56, 32896)
GUICtrlSetImage (-1, '2.bmp')
GUICtrlSetOnEvent (-1, '_2')
GUICtrlCreateButton ('', 166, 5, 76, 56, 32896)
GUICtrlSetImage (-1, '3.bmp')
GUICtrlSetOnEvent (-1, '_3')
GUISetState (@SW_HIDE, $Child)

While 1
    If $_sToggle = 1 Then
        $Recv = TcpRecv ($Server, 1000)
        If @Error Then
            _Log ('Disconnected from the server.')
            _sToggle ()
        ElseIf $Recv <> '' Then
            If $Recv = 'sMatch' Then
                _Log ('The match has started.' & @CRLF & 'Choose your weapon.')
                _cToggle ()
            ElseIf StringInStr ($Recv, 'Result:') Then
                $Recv = StringReplace ($Recv, 'Result:','')
                _Log ($Recv)
            EndIf
        EndIf
    EndIf
    Sleep (25)
WEnd

Func _cToggle ()
    If $cToggle = 0 Then
        GUISetState (@SW_SHOW, $Child)
        $cToggle = 1
    Else
        GUISetState (@SW_HIDE, $Child)
        $cToggle = 0
    EndIf
EndFunc

Func _sToggle ()
    If $_sToggle = 0 Then
        TcpStartUp ()
        $rConStr = GUICtrlRead ($ConStr)
        $rConStr = StringSplit ($rConStr, ':')
        If $rConStr[0] = 2 Then
            $Server = TcpConnect ($rConStr[1], $rConStr[2])
            If @Error Or $Server = -1 Then
                TcpShutdown ()
                _Log ('Error : Cannot connect to the server.')
                $Server = -1
                Return @Error
            EndIf
        Else
            TcpShutdown ()
            _Log ('Error : Cannot connect to the server.')
            Return @Error
        EndIf
        _Log ('You have connected to the server.')
        GUICtrlSetState ($ConStr, $GUI_DISABLE)
        GUICtrlSetData ($sToggle, 'Disconnect')
        $_sToggle = 1
    Else
        TcpCloseSocket ($Server)
        TcpShutdown ()
        GUICtrlSetState ($ConStr, $GUI_ENABLE)
        GUICtrlSetData ($sToggle, 'Connect')
        $Server = -1
        $_sToggle = 0
    EndIf
EndFunc

Func _1 ()
    $Weapon = 'Rock'
    _Log ('You have selected : ' & $Weapon)
    _Log ('Waiting for results...')
    _cToggle ()
    TcpSend ($Server, 'cWeapon:' & $Weapon)
EndFunc

Func _2 ()
    $Weapon = 'Paper'
    _Log ('You have selected : ' & $Weapon)
    _Log ('Waiting for results...')
    _cToggle ()
    TcpSend ($Server, 'cWeapon:' & $Weapon)
EndFunc

Func _3 ()
    $Weapon = 'Scissors'
    _Log ('You have selected : ' & $Weapon)
    _Log ('Waiting for results...')
    _cToggle ()
    TcpSend ($Server, 'cWeapon:' & $Weapon)
EndFunc

Func _Log ($Data)
    GUICtrlSetData ($Console, GUICtrlRead ($Console) & $Data & @CRLF)
    _GUICtrlEdit_LineScroll ($Console, 0, _GUICtrlEdit_GetLineCount ($Console) - 1)
EndFunc

Func _Exit ()
    Exit
EndFunc

You'll need the images :

http://www.mediafire.com/download.php?qm765d0cecw9u3b

Or if you want to download source files, complied version, and images :

http://www.mediafire.com/download.php?7cc2q8kb8ufb8lt

Hope you guys enjoy! :graduated:

- John

Edited by z0mgItsJohn

Latest Projects :- New & Improved TCP Chat

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Thanks! Also, you're pro. I've played against my self like 50 times and lost all of them... :graduated:

Edited by z0mgItsJohn

Latest Projects :- New & Improved TCP Chat

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