Problem with Arrays (TicTacToe)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randi

    Problem with Arrays (TicTacToe)

    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.ScaleW idth / 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).Visi ble = 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(I ndex 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).Capt ion = ""
    Call ItoXY(i, X, Y)
    status(X, Y) = 0
    Next i
    player = 1
    End Sub




  • Karl S

    #2
    Re: Problem with Arrays (TicTacToe)

    Kelsey,

    Declare an integer variable such as intMovesTaken in the Declarations
    section.
    Since the only way to get into the "FindWinner " Subroutine is a valid move,
    then just increment the intMovesTaken variable at the beginning.

    intMovesTaken = intMovesTaken + 1

    and;
    since the only way to hit the "End Sub" in that routine is if there is no
    winner, simply check the intMovesTaken for a value of 9 (all blocks filled)
    as in the following code.


    If intMovesTaken = 9 Then ' we got here with all nine blocks filled in and
    no winner
    MsgBox "It was a tie!"
    ' Reset counts and clear board...
    End If


    "Randi" <RSaddler@stny. rr.com> wrote in message
    news:ehwob.1135 73$Hs.110343@tw ister.nyroc.rr. com...[color=blue]
    > Hi,
    > I have this assignment to make a Tic Tac Toe game, I believe I got the[/color]
    whoe[color=blue]
    > thing done. I set up 9 lbl through the use of an array, I can check to[/color]
    see[color=blue]
    > if there is a winner. But I don't know what to do if there is a tie.[/color]
    Below[color=blue]
    > is my code, if you can follow it. I would really appreciate any help.
    >
    > Thanks,
    > Kelsey[/color]

    [SNIP]


    Comment

    Working...