Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie with If/End If problem needs help

Status
Not open for further replies.

LoserAndFailure

Programmer
Jan 27, 2005
4
US
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

 
It's an error in this section:
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

You need to either place the if statements on one line, or close all the if's 1 at a time:

eg:If fbSquareX(8) = True Or fbSquareO(8) = True Then liSquaresOccupied = liSquaresOccupied + 1

or
If fbSquareX(8) = True Or fbSquareO(8) = True Then
liSquaresOccupied = liSquaresOccupied + 1
end if

BB
 
Look at the following code fragment in your subroutine.
___
[tt]
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[/tt]
___

Change it as follows.
___
[tt]
If fbSquareX(1) = True Or fbSquareO(1) = True Then
liSquaresOccupied = liSquaresOccupied + 1
ElseIf fbSquareX(2) = True Or fbSquareO(2) = True Then
liSquaresOccupied = liSquaresOccupied + 1
ElseIf fbSquareX(3) = True Or fbSquareO(3) = True Then
liSquaresOccupied = liSquaresOccupied + 1
ElseIf fbSquareX(4) = True Or fbSquareO(4) = True Then
liSquaresOccupied = liSquaresOccupied + 1
ElseIf fbSquareX(5) = True Or fbSquareO(5) = True Then
liSquaresOccupied = liSquaresOccupied + 1
ElseIf fbSquareX(6) = True Or fbSquareO(6) = True Then
liSquaresOccupied = liSquaresOccupied + 1
ElseIf fbSquareX(7) = True Or fbSquareO(7) = True Then
liSquaresOccupied = liSquaresOccupied + 1
ElseIf fbSquareX(8) = True Or fbSquareO(8) = True Then
liSquaresOccupied = liSquaresOccupied + 1
End If[/tt]
___

Hope it should work now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top