Hello Everybody,

I just started learning programming about 2 months ago. I'm trying to do a Tic-Tac-Toe game using labels but I have a little difficulty. How do I get the computer to check if 3 labels are of the same value.

I've tried this:

If lblLeft.Text And lblCenter.Text And lblRight.Text = "X" Then
     MessageBox.Show("You Win", "Well Done")
End If

But when I tried debugging, Visual Basic tells me that "Conversion from string "O" to type 'Long' is not valid". What should I do?

my idea is , to declare 9 dims

ex.

dim UL 'upperleft
dim T 'top
dim UR 'upper right
and so on,


after doing it, put 9 txtbox
declare their name as

ex.

txtUL 'for dim UL
txtT 'for dim T
txtUR 'for dim UR
and so on,

this will let u knw where that textbox is located


after this do the if then statements
for all possible answers

ex.

UL = txtUL' make this also for all

if  UL = X and T = X and UR = x then
msgbox "X won"
else
if UL = O and T = O and UR = o then
msgbox "O won"
end if
end if

'''
after all if then statement, put codes in all textbox_keydown event of all textbox

Forgive me if your version of VB allows the syntax you have shown.
In VB 4 it would be

if label1="X" and label2="X" and label3="X" then

I would do it this way

'set up flags for all possible lines, do this bit
'in the form load event

For I = 1 To 3
Select Case I
Case 1
For J = 1 To 3
tttline(I, J) = J
Next J
Case 2
For J = 4 To 6
tttline(I, J) = J
Next J
Case 3
For J = 7 To 9
tttline(I, J) = J
Next J
End Select
Next I

tttline(4, 1) = 1
tttline(4, 2) = 5
tttline(4, 3) = 9

tttline(5, 1) = 3
tttline(5, 2) = 5
tttline(5, 3) = 7

'create an array of nine textboxes on your form
'change the indexes downward from 9 to 1 instead
'of 8 to 0
'set the maximum length of each to 1
'in the change event, exit if not O or X

' in the change event check all lines everytime
' a valid key is pressed.

'the following code should be put in the change
'event. assume you have called the squares 'square(1), to square(9)'

'so now check all lines like this
whoisthewinner = ""
For I = 1 To 5
If square(tttline(I, 1)) = "X" And square(tttline(I, 2)) = "X" And square(tttline(I, 1)) = "X" Then
whoisthewinner = "X wins"
Exit For
End If
If square(tttline(I, 1)) = "O" And square(tttline(I, 2)) = "O" And square(tttline(I, 1)) = "O" Then
whoisthewinner = "O Wins"
Exit For
End If
Next I
If whoisthewinner = "" Then Exit Sub

'at this point set up your bells and whistles to declare the winner

Hello Everybody,

I just started learning programming about 2 months ago. I'm trying to do a Tic-Tac-Toe game using labels but I have a little difficulty. How do I get the computer to check if 3 labels are of the same value.

I've tried this:

If lblLeft.Text And lblCenter.Text And lblRight.Text = "X" Then
     MessageBox.Show("You Win", "Well Done")
End If

But when I tried debugging, Visual Basic tells me that "Conversion from string "O" to type 'Long' is not valid". What should I do?

should be :

If lblLeft.Text="X" And lblCenter.Text="X" And lblRight.Text = "X" Then
     MessageBox.Show("You Win", "Well Done")
End If

sorry the last tttlline should be I,3 not I,1
i.e square(tttline(I,3)) fot X's and for O's

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.