As i posted last night, i was making a game of tic-tac-toe.
Im pretty much done, i just dont know how to add a win check. Once i get that ill make it winsock for online fun ; )..Heres the code:
I'm guessing that your label array is arranged like this:
012
345
678
There is only 5 ways someone can win. If the labels 0, 1, and 2 have the same caption. If label 3, 4, and 5 have the same caption. If label 6, 7, and 8 have the same caption. If labels 0, 4, and 8 have the same caption. If labels 2, 4, and 6 have the same caption. I guess you just have to check that. Of course the same caption doesn't mean an empty string in all positions
have a little code, it's untested...
Private Function checkforwin() As Boolean
If xolbl(0).Caption = xolbl(1).Caption And xolbl(1).Caption = xolbl(2).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
If xolbl(3).Caption = xolbl(4).Caption And xolbl(4).Caption = xolbl(5).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
If xolbl(6).Caption = xolbl(7).Caption And xolbl(7).Caption = xolbl(8).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
If xolbl(0).Caption = xolbl(3).Caption And xolbl(3).Caption = xolbl(6).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
If xolbl(1).Caption = xolbl(4).Caption And xolbl(4).Caption = xolbl(7).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
If xolbl(2).Caption = xolbl(5).Caption And xolbl(5).Caption = xolbl(8).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
If xolbl(2).Caption = xolbl(4).Caption And xolbl(4).Caption = xolbl(6).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
If xolbl(0).Caption = xolbl(4).Caption And xolbl(4).Caption = xolbl(8).Caption And Len(xolbl(1).Caption) <> 0 Then checkforwin = True
End Function
Called like so (pseudocode)
Xplays
if Checkforwin then
msgbox "X wins!"
restartgame
end if
Oplays
if Checkforwin then
msgbox "O wins!"
restartgame
end if
Last edited by Turiya; Mar 25th, 2005 at 07:40 PM.
Reason: ...on And Len(xolbl(1).Caption) <> 0 Th...checks for 0 length strings and fixes the problem
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
Turiya's code is not checking for empty values, either (Mine was not checking it because it was not checking play-by-play but at the end). After the first play, that code would find one line or column (and possibly a diagonal too) which has all the position equalls to "". For example, I play my "X" in the center box... the line If xolbl(0).Caption = xolbl(1).Caption And xolbl(1).Caption = xolbl(2).Caption Then checkforwin = True will fit the condition and your code would yield I won. You should bear that in mind.
Emiliano F. Martín
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
Have a look at this full-working code. Now you can play.
Emiliano F. Martín
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
Yes it works, but it seems you over complicated the game a little too much...
You asked for a neater way. I think that's as neat as it could be done. I could add some comments lines... but you don't seem willing to learn from it, so it would be a waste of time.
Emiliano F. Martín
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
In other words... a neater code would be a code that would let you (in the future) upgrade it so that you can let the user decide how many rows and columns he wants without modifying the whole code. My code would need to use a variable instead of all of those "2" (or "3"), and a little modification on the diagonal checking (because there would be more than 2 diagonals if there are more than 9 squares). That's neat!!
The other way (proposed coding) you should limit the number of lines and/or columns the user wants and write a load of IF lines as above according to the number of lines and/or columns the user wanted.
Emiliano F. Martín
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
Don't worry. You did not offend me. I was just trying to make my point. If you want a neater way... you should build up your knowledge to understand and code harder things. For example, here's the first hint:
VB Code:
Select Case teamname
Case "x"
xolbl(Index).Caption = "x"
Case "o"
xolbl(Index).Caption = "o"
End Select
This code makes no sense at all. A neater way would be
VB Code:
xolbl(Index).Caption = teamname
Emiliano F. Martín
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
If the guy doesn't want to change his code like that, he doesn't have to. I used to have messy code back in grade 7 and 8, I have neater code now, but I think that if the code works, you have a good program, if you have code that does what you want, you're amazing.
Hey! I'm not forcing anyone here! I don't want him to change all his program. He asked for a neater way and I posted an example. If he does not want to implement something like that... he won't do it.
Emiliano F. Martín
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
yup it works good, except i hate all the if statements
No Ifs, but Fors... and a much simpler way.
Emiliano F. Martín
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
The CheckForWinners calls the functions CheckForLines, CheckForColumns and CheckForDiagonals.
VB Code:
Private Function CheckForLines() As Boolean
Dim sValue As String
For iLine = 0 To 2
'We'll start with the first line... that's Line=0, then Line=1 and finally Line=2
iColumn = 0 'Set the column variable to the first one.
sValue = arr(Index) 'Load the variable sValue with the content of the
'first element on this line. This is the one that's
'on Column=0
For iColumn = 1 To 2
'We'll check the other two values (Column=1 and Column=2)
If sValue <> arr(Index) Or sValue = "" Then
'The value in this square does not match with the value in
'the first column (sValue)...
'or the value in the first square was empty.
Exit For
End If
Next iColumn
If iColumn > 2 Then
'If we get here... the three columns have the same value and did not
'did not "EXIT FOR".
CheckForLines = True
End If
Next iLine
End Function
The other functions (CheckForColumns and CheckForDiagonals) are very similar.
BTW, it seems you did not download the latest code (Tic-Tac-Toe.zip (5.1 KB, 0 views)). It's much simpler than the one I posted first. As you can see in this post, the functions returns a boolean instead of a collection.
Last edited by Mc Brain; Mar 25th, 2005 at 08:08 PM.
Emiliano F. Martín
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
cool, can you tell me why sometimes, this doesnt clear one label?
VB Code:
Private Sub Restart1()
Dim i As Integer
Randomize
x = Int((2 * Rnd) + 1)
If x = 2 Then
teamname = "x"
Else
teamname = "o"
End If
For i = 0 To xolbl.UBound
xolbl(i).Caption = vbNullString
End If
Next i
End Sub
xolbl(i).Caption = vbNullString, that should clear alll in the array, but it leaves the last selected box sometimes..do you know why?
First tell me what's this END IF is for....
VB Code:
Private Sub Restart1()
Dim i As Integer
Randomize
x = Int((2 * Rnd) + 1)
If x = 2 Then
teamname = "x"
Else
teamname = "o"
End If
For i = 0 To xolbl.UBound
xolbl(i).Caption = vbNullString
End If '<---- What is this for??
Next i
End Sub
Emiliano F. Martín
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
And which one is the one that's not being cleared?
Emiliano F. Martín
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
Then... I'm guessing the problem is that it's caption is being re-written after you've deleted ots contents. Place a breakpoint at the end of the routine and check if it everything is Ok. If it does... you have the answer.
Emiliano F. Martín
If a post has helped you then pleaseRate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.