Hi,
I have this assignment to make a Tic Tac Toe game, I believe I got the whoe
thing done. I set up 9 lbl through the use of an array, I can check to see
if there is a winner. But I don't know what to do if there is a tie. Below
is my code, if you can follow it. I would really appreciate any help.
Thanks,
Kelsey
Option Explicit
Dim player As Integer
Dim status(1 To 3, 1 To 3) As Integer
Private Sub Form_Load()
Dim X As Integer, Y As Integer, i As Integer
Dim W As Single, H As Single
picBoard.Height = picBoard.Width
W = picBoard.ScaleWidth / 3
H = W
lblMark(1).Move 0, 0, W, H
For i = 2 To 9
Load lblMark(i)
Call ItoXY(i, X, Y)
lblMark(i).Move (Y - 1) * W, (X - 1) * H
lblMark(i).Visible = True
Next i
player = 1
End Sub
Private Sub ItoXY(i As Integer, X As Integer, Y As Integer)
X = Int((i - 1) / 3) + 1
Y = (i - 1) Mod 3 + 1
End Sub
Private Function XYtoI(X As Integer, Y As Integer) As Integer
XYtoI = (X - 1) * 3 + Y
End Function
Private Sub FindWinner()
Dim ThePlayer As Integer
If status(1, 1) Then
ThePlayer = status(1, 1)
If status(2, 2) = ThePlayer And status(3, 3) = ThePlayer Then
MsgBox lblMark(XYtoI(1, 1)).Caption _
& " is the winner!", , "Winner"
MsgBox "test", vbYesNo, "Test"
Exit Sub
End If
End If
Dim i As Integer
For i = 1 To 3
If status(i, 1) Then
ThePlayer = status(i, 1)
If status(i, 2) = ThePlayer And status(i, 3) = ThePlayer Then
MsgBox lblMark(XYtoI(i, 1)).Caption _
& "is the winner!", , "Winner"
Exit Sub
End If
End If
Next i
For i = 1 To 3
If status(1, i) Then
ThePlayer = status(1, i)
If status(2, i) = ThePlayer And status(3, i) = ThePlayer Then
MsgBox lblMark(XYtoI(1, i)).Caption _
& "is the winner!", , "Winner"
Exit Sub
End If
End If
Next i
If status(1, 3) Then
ThePlayer = status(1, 3)
If status(2, 2) = ThePlayer And status(3, 1) = ThePlayer Then
MsgBox lblMark(XYtoI(3, 1)).Caption _
& " is the winner!", , "Winner"
If MsgBox("Do you want to end the current game?", _
vbYesNo, "Start a New Game?") = 6 Then
Call InitPlayGround
Else: End
End If
Exit Sub
End If
End If
End Sub
Private Sub lblMark_Click(Index As Integer)
Dim X As Integer, Y As Integer, imove As Integer
Call ItoXY(Index, X, Y)
If status(X, Y) Then
Beep
Exit Sub
End If
status(X, Y) = player
Select Case player
Case 1
lblMark(Index).Caption = "X"
Case -1
lblMark(Index).Caption = "O"
End Select
player = -player
Call FindWinner
End Sub
Private Sub InitPlayGround()
Dim i As Integer
Dim X As Integer, Y As Integer
' Erase any playing grid symbols
For i = 1 To 9
lblMark(i).Caption = ""
Call ItoXY(i, X, Y)
status(X, Y) = 0
Next i
player = 1
End Sub