LoserAndFailure
Programmer
Hi, I'm a VB newbie and I keep getting this error saying "If block without
End If". I have pored and pored over this code til I'm blue in the face and
I still see nothing wrong with it. I guess I need more eyes looking at it.
I hope you guys can help me. Your help will be greatly appreciated. The
function that the error appears in is posted below:
Public Function CheckForWin()
Dim liSquaresOccupied As Integer
' First, check for a win by the player
If fbSquareX(0) = True And fbSquareX(1) = True And fbSquareX(2) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(3) = True And fbSquareX(4) = True And fbSquareX(5) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(6) = True And fbSquareX(7) = True And fbSquareX(8) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(0) = True And fbSquareX(3) = True And fbSquareX(6) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(1) = True And fbSquareX(4) = True And fbSquareX(7) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(2) = True And fbSquareX(5) = True And fbSquareX(8) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(0) = True And fbSquareX(4) = True And fbSquareX(8) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(2) = True And fbSquareX(4) = True And fbSquareX(6) = True
Then
fbPlayerWon = True
End If
' Next, check for a win by the puter
If fbSquareO(0) = True And fbSquareO(1) = True And fbSquareO(2) = True
Then
fbComputerWon = True
ElseIf fbSquareO(3) = True And fbSquareO(4) = True And fbSquareO(5) = True
Then
fbComputerWon = True
ElseIf fbSquareO(6) = True And fbSquareO(7) = True And fbSquareO(8) = True
Then
fbComputerWon = True
ElseIf fbSquareO(0) = True And fbSquareO(3) = True And fbSquareO(6) = True
Then
fbComputerWon = True
ElseIf fbSquareO(1) = True And fbSquareO(4) = True And fbSquareO(7) = True
Then
fbComputerWon = True
ElseIf fbSquareO(2) = True And fbSquareO(5) = True And fbSquareO(8) = True
Then
fbComputerWon = True
ElseIf fbSquareO(0) = True And fbSquareO(4) = True And fbSquareO(8) = True
Then
fbComputerWon = True
ElseIf fbSquareO(2) = True And fbSquareO(4) = True And fbSquareO(6) = True
Then
fbComputerWon = True
End If
' And last, check for a tie game
If fbComputerWon = False And fbPlayerWon = False Then
If fbSquareX(0) = True Or fbSquareO(0) = True Then
liSquaresOccupied = liSquaresOccupied + 1
End If
If fbSquareX(1) = True Or fbSquareO(1) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(2) = True Or fbSquareO(2) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(3) = True Or fbSquareO(3) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(4) = True Or fbSquareO(4) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(5) = True Or fbSquareO(5) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(6) = True Or fbSquareO(6) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(7) = True Or fbSquareO(7) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(8) = True Or fbSquareO(8) = True Then
liSquaresOccupied = liSquaresOccupied + 1
End If
' If liSquaresOccupied = 9 (ass aquares are occupied) and neither the
puter nor
' the player has won, then we have a tie game.
If liSquaresOccupied = 9 Then
fbTie = True
End If
End If ' (fbComputerWon = False And fbPlayerWon = False)
' If a win or a tie has occurred, then increment a score.
' Turn off the timer and exit this routine
If fbTie = True Or fbPlayerWon = True Or fbComputerWon = True Then
CheckForWin = True
If fbTie = True Then
fiTieScore = fiTieScore + 1
lblTie.Caption = fiTieScore
lblStatus.Caption = "Tie game. Click start game to play again."
fsWhoseTurnIsIt = "player"
ElseIf fbPlayerWon = True Then
fiPlayerScore = fiPlayerScore + 1
lblPlayer.Caption = fiPlayerScore
lblStatus.Caption = "Congratulations! You won! Click start game to
play again."
fsWhoseTurnIsIt = "player"
ElseIf fbComputerWon = True Then
fiComputerScore = fiComputerScore + 1
lblComputer.Caption = fiComputerScore
lblStatus.Caption = "Sorry, the puter won. Click start game to play
again."
fsWhoseTurnIsIt = "player"
End If
' This part of the code does housekeeping by setting the game
environment to that
' of a new game.
cmdStart.Enabled = True
fbTie = False
fbPlayerWon = False
fbComputerWon = False
For i = 0 To 8
fbSquareX(i) = False
fbSquareO(i) = False
Next i
Timer1.Enabled = False
Exit Function
Else
CheckForWin = False
End If
End Function
End If". I have pored and pored over this code til I'm blue in the face and
I still see nothing wrong with it. I guess I need more eyes looking at it.
I hope you guys can help me. Your help will be greatly appreciated. The
function that the error appears in is posted below:
Public Function CheckForWin()
Dim liSquaresOccupied As Integer
' First, check for a win by the player
If fbSquareX(0) = True And fbSquareX(1) = True And fbSquareX(2) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(3) = True And fbSquareX(4) = True And fbSquareX(5) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(6) = True And fbSquareX(7) = True And fbSquareX(8) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(0) = True And fbSquareX(3) = True And fbSquareX(6) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(1) = True And fbSquareX(4) = True And fbSquareX(7) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(2) = True And fbSquareX(5) = True And fbSquareX(8) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(0) = True And fbSquareX(4) = True And fbSquareX(8) = True
Then
fbPlayerWon = True
ElseIf fbSquareX(2) = True And fbSquareX(4) = True And fbSquareX(6) = True
Then
fbPlayerWon = True
End If
' Next, check for a win by the puter
If fbSquareO(0) = True And fbSquareO(1) = True And fbSquareO(2) = True
Then
fbComputerWon = True
ElseIf fbSquareO(3) = True And fbSquareO(4) = True And fbSquareO(5) = True
Then
fbComputerWon = True
ElseIf fbSquareO(6) = True And fbSquareO(7) = True And fbSquareO(8) = True
Then
fbComputerWon = True
ElseIf fbSquareO(0) = True And fbSquareO(3) = True And fbSquareO(6) = True
Then
fbComputerWon = True
ElseIf fbSquareO(1) = True And fbSquareO(4) = True And fbSquareO(7) = True
Then
fbComputerWon = True
ElseIf fbSquareO(2) = True And fbSquareO(5) = True And fbSquareO(8) = True
Then
fbComputerWon = True
ElseIf fbSquareO(0) = True And fbSquareO(4) = True And fbSquareO(8) = True
Then
fbComputerWon = True
ElseIf fbSquareO(2) = True And fbSquareO(4) = True And fbSquareO(6) = True
Then
fbComputerWon = True
End If
' And last, check for a tie game
If fbComputerWon = False And fbPlayerWon = False Then
If fbSquareX(0) = True Or fbSquareO(0) = True Then
liSquaresOccupied = liSquaresOccupied + 1
End If
If fbSquareX(1) = True Or fbSquareO(1) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(2) = True Or fbSquareO(2) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(3) = True Or fbSquareO(3) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(4) = True Or fbSquareO(4) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(5) = True Or fbSquareO(5) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(6) = True Or fbSquareO(6) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(7) = True Or fbSquareO(7) = True Then
liSquaresOccupied = liSquaresOccupied + 1
If fbSquareX(8) = True Or fbSquareO(8) = True Then
liSquaresOccupied = liSquaresOccupied + 1
End If
' If liSquaresOccupied = 9 (ass aquares are occupied) and neither the
puter nor
' the player has won, then we have a tie game.
If liSquaresOccupied = 9 Then
fbTie = True
End If
End If ' (fbComputerWon = False And fbPlayerWon = False)
' If a win or a tie has occurred, then increment a score.
' Turn off the timer and exit this routine
If fbTie = True Or fbPlayerWon = True Or fbComputerWon = True Then
CheckForWin = True
If fbTie = True Then
fiTieScore = fiTieScore + 1
lblTie.Caption = fiTieScore
lblStatus.Caption = "Tie game. Click start game to play again."
fsWhoseTurnIsIt = "player"
ElseIf fbPlayerWon = True Then
fiPlayerScore = fiPlayerScore + 1
lblPlayer.Caption = fiPlayerScore
lblStatus.Caption = "Congratulations! You won! Click start game to
play again."
fsWhoseTurnIsIt = "player"
ElseIf fbComputerWon = True Then
fiComputerScore = fiComputerScore + 1
lblComputer.Caption = fiComputerScore
lblStatus.Caption = "Sorry, the puter won. Click start game to play
again."
fsWhoseTurnIsIt = "player"
End If
' This part of the code does housekeeping by setting the game
environment to that
' of a new game.
cmdStart.Enabled = True
fbTie = False
fbPlayerWon = False
fbComputerWon = False
For i = 0 To 8
fbSquareX(i) = False
fbSquareO(i) = False
Next i
Timer1.Enabled = False
Exit Function
Else
CheckForWin = False
End If
End Function