Page 1
Page 2
Page 3
Page 4
So now we are connected, how do we deal
with playing the game. This is where the 'control-array'
comes in. If you click on the bottom left square (xdisp6)
the code is:-
Private Sub xdisp6_Click()
xdisp_click 6
End Sub
This carries out the xdisp_click procedure
and sends it the number of the image we clicked on. 'We'
in this case are the server, and are playing 'x'. We keep
an array of the positions (OXArray), initialised to 0's.
When we click on the image control we display the relevant
picture for our symbol 'o' or 'x', and populate the array
with our position. The array now reads:-
00000x000.
We then build up a string of 'x' or
'o' by scanning the array. We send back to the client, the
number of the image control we clicked on so that the client
can draw the symbol, by sending "OXCLICK6|". We
then check if we have 3 x's or 3 o's in a line.
The 'board' is laid out:
We check through the string, and check
for winning combinations. If we have xxx in positions 0,1
and 2 or 0, 4 and 8 etc., we are a winner.
Private Sub xdisp_click(index As Integer)
If Not bReady Or Not bMyGo Then Exit Sub
If Not bHasWon Then
If OXArray(index) = 0 Then
OXArray(index) = MySymbol
If MySymbol = "o" Then
xdisp(index).Picture = opic
Else
xdisp(index).Picture = xpic
End If
bMyGo = False
Dim XS As String
Dim OS As String
Dim i As Integer
For i = 0 To 8
If OXArray(i) = "x" Then
XS = XS & i
ElseIf OXArray(i) = "o" Then
OS = OS & i
End If
Next
WinSock1.SendData "OXCLICK" & _&
index & "|"
lstStatus.AddItem _&
"Sending Move...", 0
lstStatus.AddItem _*&
opponentName & "'s Go", 0
If InStr(XS, "0") > 0 _
And InStr(XS, "1") > 0 And _
InStr(XS, "2") > 0 Then _
Winner index
'
' See project for rest of code
If (Len(OS) + Len(XS)) = 9 Then _&
Its_A_Draw index
End If
End If
End Sub
If we are a winner, the code is:-
Private Sub Winner(index As Integer)
If Not bHasWon Then
bHasWon = True
WinSock1.SendData "OXWIN" & index & "|"
lstStatus.AddItem "You Win!!", 0
MyScore = MyScore + 1
lstStatus.AddItem "Scores are, You: " _
& MyScore & ", " & opponentName _
& ":" & opponentScore, 0
tmrReset.Enabled = True
If SoundOn Then
Lret = PlaySound(App.Path & _
"\woohoo.wav", 0, _
SND_ASYNC + SND_NODEFAULT _
+ SND_FILENAME)
End If
End If
End Sub
We send the data "OXWIN6|".
We also play Homer saying WooHoo. The client receives the
data and deals with it so...
If InStr(Wdata, "OXWIN") Then
bHasWon = True
bReady = False
bMyGo = True
opponentScore = opponentScore + 1
IndexClick = InStr(Wdata, "IN") + 2
IndexClick = Int(Mid(Wdata, IndexClick, 1))
OXArray(IndexClick) = opponentSymbol
If opponentSymbol = "x" Then
xdisp(IndexClick).Picture = xpic
Else
xdisp(IndexClick).Picture = opic
End If
lstStatus.AddItem opponentName & " Wins!!",
0
If SoundOn Then
Lret = PlaySound(App.Path & "\doh.wav", _
0, SND_ASYNC + SND_NODEFAULT _
+ SND_FILENAME)
End If
lstStatus.AddItem "Scores are, You: " & _
MyScore & ", " & opponentName & ":"
_
& opponentScore, 0
tmrReset.Enabled = True
Wdata = ""
Exit Sub
End If
Previous Page
Next Page