I'm doing tic tac toe. I am planning to show the scores of the users and computers using the same form. So, for each time the user or the computer loop, i need to add a timer to it so that it can update the scores. Refer to the source code at Private Sub CheckForWin(), i suggest to add a variable to store the scores for X and O,using:
(at line 419)
If XsInARow = 3 Then
player1=player1+1
Call XWins

If OsInARow = 3 Then
player2=player2+1
Call OWins

However i still thinking on how to add a timer to this function. Or like this?
If XsInARow = 3 Then
Call anyfunction() within the Private Sub Timer1_Timer() to calculate the scores?

Option Explicit
Dim StartSquare As Integer, EndSquare As Integer, Direction As Integer
Dim XO(2) As String
Dim Blank As Integer
Dim CaseCounter As Integer
Dim XsInARow As Integer, OsInARow As Integer
Dim HumanPlayingAsX As Boolean
Dim HumanMovedAlready As Boolean, CompMovedAlready As Boolean
Dim GameOver As Boolean
Dim SquareValue(8) As Integer
Dim RandChoice(8) As Integer
Dim WinLine As Integer
Dim TieGame As Boolean
Dim strCPU As String
Dim strHuman As String
Dim px As Integer
Dim po As Integer

Private Sub Form_KeyPress(KeyAscii As Integer)
  On Error Resume Next
  Call Square_Click(Chr$(KeyAscii))
  On Error GoTo 0
  End Sub

Private Sub Form_Load()
Dim Index As Integer

Cls
Randomize

'Clears squares and any red line marking a win on the last game.
For Index = 0 To 8
Square(Index).Caption = ""
SquareValue(Index) = 0
Next Index

For Index = 0 To 7
Win(Index).Visible = False
Next Index

'Keeps Human as playing same type (X or O) as he did in
'previous game. The default side is X.
If HumanX.Value = 1 Then
    HumanPlayingAsX = True
    strCPU = "O"
    strHuman = "X"
Else
    HumanPlayingAsX = False
    strCPU = "X"
    strHuman = "O"
End If

'Defaults Human as not having selected a square yet
HumanMovedAlready = False
CompMovedAlready = False

'CheckBox for choosing side and
'who plays first are visible.
'Human defaults to being first.
'Displays Intelligence level of Computer.
HumanX.Visible = True
HumanFirst.Visible = True
HumanFirst.Value = 1
Call IntelligenceLevelCase

'These variables are true only if the game ends with a win or a cat's game.
GameOver = False
TieGame = False

'Changes the PlayAgain command button caption back to "New Game"
PlayAgain.Caption = "New Game"

End Sub




Private Sub Square_Click(Index As Integer)
Dim Flash100 As Integer

'This Procedure checks for a click on a square
'and determines several things:
    '1) Is the move legal?
    '2) Is it the human player's turn?
'If both conditions are true, the player's
'square is selected - if either condition is not true,
'a beep and screen flash effect alert the player
'that they tried to make an illegal move.
If Square(Index).Caption = "" And HumanMovedAlready = False Then
    HumanMovedAlready = True
    Square(Index).Caption = strHuman
    Call CheckForWin
    
    If GameOver = False Then
        Call CheckForCatsGame
        Call ComputerTurn
    End If
    
'Checks for an illegal move
'Prevents player from clicking to move to an
'already occupied square.
Else
    Beep
    For Flash100 = 1 To 100
        Me.BackColor = RGB(Int(256 * Rnd), Int(256 * Rnd), Int(256 * Rnd))
        Me.BackColor = "&H00C0FFFF"
    Next Flash100
End If
End Sub




Private Sub HumanFirst_Click()
'Skips to computer's turn if human player
'opts to play second
If HumanFirst.Value = 1 Then
    HumanMovedAlready = False
Else
    HumanMovedAlready = True
    Cls
    Call ComputerTurn
End If
End Sub


Private Sub HumanX_Click()
If HumanX.Value = 1 Then
    HumanPlayingAsX = True
    strCPU = "O"
    strHuman = "X"
Else
    HumanPlayingAsX = False
    strCPU = "X"
    strHuman = "O"
End If

End Sub


Private Sub Intelligence_Change()
Dim Red As Integer, Green As Integer, Blue As Integer

'Changes the color of the caption which tells which level of intelligence
'the human player is up against. The color goes from blue (easy) to
'red (hard)
Red = (Intelligence.Value / Intelligence.Max) * 255
Green = 0
Blue = (1 / Intelligence.Value) * 255
Level.ForeColor = RGB(Red, Green, Blue)
Call IntelligenceLevelCase
End Sub


Private Sub PlayAgain_Click()
Dim Index As Integer
Dim OldColor As String

'   This section of code rotates the background color
'of the selectable squares for a nice visual effect.
OldColor = Square(0).BackColor
    For Index = 0 To 7
        Square(Index).BackColor = Square(Index + 1).BackColor
    Next Index
Square(8).BackColor = OldColor

'   And now we start a new game!
Form_Load
End Sub


Private Sub Quit_Click()
Unload Me
End Sub


Private Sub XWins()
px = px + 1
Call GameIsOver
MsgBox ("X wins!")
End Sub


Private Sub OWins()
po = po + 1
GameOver = True
Call GameIsOver
MsgBox ("O Wins!")
End Sub


Private Sub CatsGame()
TieGame = True
Call GameIsOver
MsgBox ("Cat's Game!")
End Sub


Private Sub GameIsOver()
GameOver = True
PlayAgain.Caption = "Play Again?"
Beep
If TieGame = False Then Win(CaseCounter).Visible = True
End Sub


Private Sub BoardCheck()
'This procedure runs through the CaseCounter routine
'Which we will use in several different areas to check
'for a win by either side.
'The computer A.I. uses this routine to check for
'which moves would be the most advantageous to make.

'Squares are numbered in our array as follows:

'       0|1|2
'       -----
'       3|4|5
'       -----
'       6|7|8

Select Case CaseCounter
               
'These next 3 cases allow our FOR... NEXT loop to
'check the status of the horizontal rows of the board.
    Case 0
        StartSquare = 0
        EndSquare = 2
        Direction = 1
    Case 1
        StartSquare = 3
        EndSquare = 5
        Direction = 1
    Case 2
        StartSquare = 6
        EndSquare = 8
        Direction = 1
            
'These next 3 cases allow our FOR... NEXT loop to
'check the status of the vertical columns of the board
    Case 3
        StartSquare = 0
        EndSquare = 6
        Direction = 3
    Case 4
        StartSquare = 1
        EndSquare = 7
        Direction = 3
    Case 5
        StartSquare = 2
        EndSquare = 8
        Direction = 3
            
'These last 2 cases allow our FOR... NEXT loop to
'check the status of the diagonals of the board.
    Case 6
        StartSquare = 0
        EndSquare = 8
        Direction = 4
    Case 7
        StartSquare = 2
        EndSquare = 6
        Direction = 2
End Select
End Sub


Private Sub XOSelect(XOSquare)
'This Subroutine allows us to check for varying combinations of squares
'that can win the game or save the game from being lost. The Blank variable
'borrows expressions from our CaseCounter Select Case subroutine. It is used
'to identify the open square in a given sequence so that a point value can
'be assigned to it later.

Select Case XOSquare
    Case 1
        XO(0) = "X"
        XO(1) = "X"
        XO(2) = ""
        Blank = EndSquare
    Case 2
        XO(0) = "X"
        XO(1) = ""
        XO(2) = "X"
        Blank = StartSquare + Direction
    Case 3
        XO(0) = ""
        XO(1) = "X"
        XO(2) = "X"
        Blank = StartSquare
    Case 4
        XO(0) = "O"
        XO(1) = "O"
        XO(2) = ""
        Blank = EndSquare
    Case 5
        XO(0) = "O"
        XO(1) = ""
        XO(2) = "O"
        Blank = StartSquare + Direction
    Case 6
        XO(0) = ""
        XO(1) = "O"
        XO(2) = "O"
        Blank = StartSquare
End Select
End Sub


Private Sub IntelligenceLevelCase()
    Select Case Intelligence.Value
        Case 1
            Level.Caption = "Mindless"
        Case 2
            Level.Caption = "Poor"
        Case 3
            Level.Caption = "Average"
        Case 4
            Level.Caption = "Clever"
        Case 5
            Level.Caption = "Genius"
    End Select
End Sub


Private Sub CheckForCatsGame()
'If all the spaces are filled, the game ends.
Dim Index As Integer
Dim Space As Integer

For Index = 0 To 8
    If Square(Index).Caption = "" Then Space = Space + 1
Next Index

If Space = 0 Then
    Call CatsGame
End If
End Sub


Private Sub SpecialAI()
Dim MiddleSideSquare As Integer
Dim Index As Integer

If (Square(0).Caption = strHuman And Square(8).Caption = strHuman) _
Or (Square(2).Caption = strHuman And Square(6).Caption = strHuman) Then
    
    For Index = 1 To 7 Step 2
        SquareValue(Index) = 10
    Next Index
    
End If
   
If Square(1).Caption = strHuman And Square(5).Caption = strHuman Then
    SquareValue(6) = 0
End If
    
If Square(5).Caption = strHuman And Square(7).Caption = strHuman Then
    SquareValue(0) = 0
End If
    
If Square(3).Caption = strHuman And Square(7).Caption = strHuman Then
    SquareValue(2) = 0
End If
    
If Square(1).Caption = strHuman And Square(3).Caption = strHuman Then
    SquareValue(8) = 0
End If

If Intelligence.Value > 4 Then
    For Index = 1 To 7 Step 2
        If Square(Index).Caption = strHuman Then
            MiddleSideSquare = MiddleSideSquare + 1
        End If
    Next Index
    
    If MiddleSideSquare = 1 Then
        If Square(1).Caption = strHuman Then
            SquareValue(0) = 10
            SquareValue(2) = 10
        End If
        
        If Square(3).Caption = strHuman Then
            SquareValue(0) = 10
            SquareValue(6) = 10
        End If
        
        If Square(5).Caption = strHuman Then
            SquareValue(2) = 10
            SquareValue(8) = 10
        End If
        
        If Square(7).Caption = strHuman Then
            SquareValue(6) = 10
            SquareValue(8) = 10
        End If
    End If
End If

End Sub


Private Sub CheckForWin()
Dim Index As Integer

'Removes the checkboxes that allow human player to
'change sides or change who goes first after the first
'move is made - no changing sides mid-game!  :)
If HumanX.Visible = True Then Call RemoveOptions

'The rest of this procedure checks every possible direction one could
'win and triggers the end of game procedures if anyone gets 3 in a row.
CaseCounter = 0
While CaseCounter < 8
                
    Call BoardCheck
        XsInARow = 0
        OsInARow = 0
    
    For Index = StartSquare To EndSquare Step Direction
        If Square(Index).Caption = "X" Then XsInARow = XsInARow + 1
        If Square(Index).Caption = "O" Then OsInARow = OsInARow + 1
    Next Index
    
If XsInARow = 3 Then Call XWins
If OsInARow = 3 Then Call OWins
      
CaseCounter = CaseCounter + 1
Wend
End Sub

Private Sub RemoveOptions()
    HumanX.Visible = False
    HumanFirst.Visible = False
End Sub

Private Sub CompSquareSelection()
Dim Index As Integer
Dim HighVal As Integer
Dim Counter As Integer
Dim FinalChoice As Integer

Counter = 0
HighVal = 0
For Index = 0 To 8
If SquareValue(Index) = HighVal Then
    RandChoice(Counter) = Index
    Counter = Counter + 1
End If

If SquareValue(Index) > HighVal Then
    HighVal = SquareValue(Index)
    For Counter = 1 To 8
    RandChoice(Counter) = 0
    Next Counter
    RandChoice(0) = Index
    Counter = 1
End If
Next Index

Index = Int(Rnd * Counter)
Square(RandChoice(Index)).Caption = strCPU
HumanMovedAlready = False
End Sub


Private Sub ComputerTurn()

If GameOver = False Then
    Call CompAI
    Call CompSquareSelection
    Call CheckForWin
    Call CheckForCatsGame
End If

End Sub

Private Sub Timer1_Timer()
'Update Score Labels
Label1.Caption = px
Label3.Caption = po
End Sub

Recommended Answers

All 2 Replies

Whom do you expect to go through all that code .

i suggest my opinion, but i attached my codes as well for reference....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.