Code:
Dim score1 As Integer, score2 As Integer
Dim cur As String
Private Sub Form_Load()
ResetGame
p2.Enabled = False
cur = 1
End Sub
Private Sub img_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
If img(Index).Picture = imgX.Picture Or img(Index).Picture = imgO.Picture Then
Dim cur As String
Private Sub Form_Load()
ResetGame
p2.Enabled = False
cur = 1
End Sub
Private Sub img_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
If img(Index).Picture = imgX.Picture Or img(Index).Picture = imgO.Picture Then
Exit Sub
img(Index).Picture = Source.Picture
img(Index).Tag = Source.Tag
Dim winner As String
If checkwin <> "no" Then
If checkwin = "x" Then
winner = "Player 1"
score1 = score1 + 1
s1.Caption = score1
Else
winner = "Player 2"
score2 = score2 + 1
s2.Caption = score2
End If
MsgBox "The winner is " & winner, vbInformation, "Winner"
ResetGame
End If
If cur = 1 Then
cur = 2
p1.Enabled = False
p2.Enabled = True
Else
cur = 1
p1.Enabled = True
p2.Enabled = False
End If
End Sub
Private Sub ResetGame()
For i = 0 To 8
img(i).Tag = i
img(i).Picture = Nothing
Next i
End Sub
Private Function checkwin() As String
Dim a1 As String, a2 As String, a3 As String, b1 As String, b2 As String, b3 As String, _
c1 As String, c2 As String, c3 As String
a1 = img(0).Tag
a2 = img(1).Tag
a3 = img(2).Tag
b1 = img(3).Tag
b2 = img(4).Tag
b3 = img(5).Tag
c1 = img(6).Tag
c2 = img(7).Tag
c3 = img(8).Tag
If a1 <> "0" And a2 <> "1" And a3 <> "2" And _
b1 <> "3" And b2 <> "4" And b3 <> "5" And _
c1 <> "6" And c2 <> "7" And c3 <> "8" Then
MsgBox "Game Draw!", vbInformation, "Game Over"
ResetGame
End If
If a1 = a2 And a2 = a3 Then
checkwin = a1
ElseIf b1 = b2 And b2 = b3 Then
checkwin = b1
ElseIf c1 = c2 And c2 = c3 Then
checkwin = c1
ElseIf a1 = b1 And b1 = c1 Then
checkwin = a1
ElseIf a2 = b2 And b2 = c2 Then
checkwin = a2
ElseIf a3 = b3 And b3 = c3 Then
checkwin = a3
ElseIf a1 = b2 And b2 = c3 Then
checkwin = a1
ElseIf a3 = b2 And b2 = c1 Then
checkwin = a3
Else
checkwin = "no"
End If
End Function
img(Index).Picture = Source.Picture
img(Index).Tag = Source.Tag
Dim winner As String
If checkwin <> "no" Then
If checkwin = "x" Then
winner = "Player 1"
score1 = score1 + 1
s1.Caption = score1
Else
winner = "Player 2"
score2 = score2 + 1
s2.Caption = score2
End If
MsgBox "The winner is " & winner, vbInformation, "Winner"
ResetGame
End If
If cur = 1 Then
cur = 2
p1.Enabled = False
p2.Enabled = True
Else
cur = 1
p1.Enabled = True
p2.Enabled = False
End If
End Sub
Private Sub ResetGame()
For i = 0 To 8
img(i).Tag = i
img(i).Picture = Nothing
Next i
End Sub
Private Function checkwin() As String
Dim a1 As String, a2 As String, a3 As String, b1 As String, b2 As String, b3 As String, _
c1 As String, c2 As String, c3 As String
a1 = img(0).Tag
a2 = img(1).Tag
a3 = img(2).Tag
b1 = img(3).Tag
b2 = img(4).Tag
b3 = img(5).Tag
c1 = img(6).Tag
c2 = img(7).Tag
c3 = img(8).Tag
If a1 <> "0" And a2 <> "1" And a3 <> "2" And _
b1 <> "3" And b2 <> "4" And b3 <> "5" And _
c1 <> "6" And c2 <> "7" And c3 <> "8" Then
MsgBox "Game Draw!", vbInformation, "Game Over"
ResetGame
End If
If a1 = a2 And a2 = a3 Then
checkwin = a1
ElseIf b1 = b2 And b2 = b3 Then
checkwin = b1
ElseIf c1 = c2 And c2 = c3 Then
checkwin = c1
ElseIf a1 = b1 And b1 = c1 Then
checkwin = a1
ElseIf a2 = b2 And b2 = c2 Then
checkwin = a2
ElseIf a3 = b3 And b3 = c3 Then
checkwin = a3
ElseIf a1 = b2 And b2 = c3 Then
checkwin = a1
ElseIf a3 = b2 And b2 = c1 Then
checkwin = a3
Else
checkwin = "no"
End If
End Function