Hey guys , well i have the Tic Tac Toe project , i wrote some codes, but i don't know why isn't working . Hopefully you can help me
my project contain 9 labels , 2 texboxs ( first player , second player ) , button ( new game ), 2 radio buttons ( human vs human ) and ( human vs computer)
my code that i wrote is like that :
Public Class Form1

    Dim isFirstPlayerTurn As Boolean = True


Dim isSecondPlayerTurn As Boolean = False
Dim isPlayerWon As Boolean = False

Function placeplayermark() As Char
    If isFirstPlayerTurn = True Then
        Return "X"
    ElseIf isSecondPlayerTurn = True Then
        Return "O"
    End If
End Function

Sub setplayerturn()
    If isFirstPlayerTurn = True Then
        isFirstPlayerTurn = False
        isSecondPlayerTurn = False
        txtfirstplayer.BackColor = Color.LightGreen
        txtsecondplayer.BackColor = Color.White
    ElseIf isSecondPlayerTurn = True Then
        isSecondPlayerTurn = False
        isFirstPlayerTurn = False
        txtsecondplayer.BackColor = Color.LightGreen
        txtfirstplayer.BackColor = Color.White
    End If
End Sub

Sub resetvalues()
    lblbox1.Text = " "

    lblbox1.ForeColor = Color.Black
    lblbox1.BackColor = Color.Transparent
    txtfirstplayer.BackColor = Color.White

    lblbox2.Text = " "
    lblbox2.ForeColor = Color.Black
    lblbox2.BackColor = Color.Transparent
    txtfirstplayer.BackColor = Color.White

    lblbox3.Text = " "
    lblbox3.ForeColor = Color.Black
    lblbox3.BackColor = Color.Transparent
    txtfirstplayer.BackColor = Color.White

    lblbox4.Text = " "
    lblbox4.ForeColor = Color.Black
    lblbox4.BackColor = Color.Transparent
    txtfirstplayer.BackColor = Color.White

    lblbox5.Text = " "
    lblbox5.ForeColor = Color.Black
    lblbox5.BackColor = Color.Transparent
    txtfirstplayer.BackColor = Color.White

    lblbox6.Text = " "
    lblbox6.ForeColor = Color.Black
    lblbox6.BackColor = Color.Transparent
    txtfirstplayer.BackColor = Color.White

    lblbox7.Text = " "
    lblbox7.ForeColor = Color.Black
    lblbox7.BackColor = Color.Transparent
    txtfirstplayer.BackColor = Color.White

    lblbox8.Text = " "
    lblbox8.ForeColor = Color.Black
    lblbox8.BackColor = Color.Transparent
    txtfirstplayer.BackColor = Color.White

    lblbox9.Text = " "
    lblbox9.ForeColor = Color.Black
    lblbox9.BackColor = Color.Transparent
    txtfirstplayer.BackColor = Color.White

End Sub
Sub iswon()
    If lblbox1.Text = lblbox2.Text And lblbox2.Text = lblbox3.Text And lblbox1.Text <> lblbox2.Text Then
        lblbox1.ForeColor = Color.Red
        lblbox2.ForeColor = Color.Red
        lblbox3.ForeColor = Color.Red

        lblbox1.BackColor = Color.Yellow
        lblbox2.BackColor = Color.Yellow
        lblbox3.BackColor = Color.Yellow

    ElseIf lblbox4.Text = lblbox5.Text And lblbox5.Text = lblbox6.Text And lblbox4.Text <> lblbox5.Text Then
        lblbox4.ForeColor = Color.Red
        lblbox5.ForeColor = Color.Red
        lblbox6.ForeColor = Color.Red

        lblbox4.BackColor = Color.Yellow
        lblbox5.BackColor = Color.Yellow
        lblbox6.BackColor = Color.Yellow

    ElseIf lblbox7.Text = lblbox8.Text And lblbox8.Text = lblbox9.Text And lblbox7.Text <> lblbox8.Text Then
        lblbox7.ForeColor = Color.Red
        lblbox8.ForeColor = Color.Red
        lblbox9.ForeColor = Color.Red

        lblbox7.BackColor = Color.Yellow
        lblbox8.BackColor = Color.Yellow
        lblbox9.BackColor = Color.Yellow

    ElseIf lblbox1.Text = lblbox4.Text And lblbox4.Text = lblbox7.Text And lblbox1.Text <> lblbox3.Text Then
        lblbox1.ForeColor = Color.Red
        lblbox4.ForeColor = Color.Red
        lblbox7.ForeColor = Color.Red

        lblbox1.BackColor = Color.Yellow
        lblbox4.BackColor = Color.Yellow
        lblbox7.BackColor = Color.Yellow

    ElseIf lblbox2.Text = lblbox5.Text And lblbox5.Text = lblbox8.Text And lblbox2.Text <> lblbox5.Text Then
        lblbox2.ForeColor = Color.Red
        lblbox5.ForeColor = Color.Red
        lblbox8.ForeColor = Color.Red

        lblbox2.BackColor = Color.Yellow
        lblbox5.BackColor = Color.Yellow
        lblbox8.BackColor = Color.Yellow

    ElseIf lblbox3.Text = lblbox6.Text And lblbox6.Text = lblbox9.Text And lblbox3.Text <> lblbox6.Text Then
        lblbox3.ForeColor = Color.Red
        lblbox6.ForeColor = Color.Red
        lblbox9.ForeColor = Color.Red

        lblbox3.BackColor = Color.Yellow
        lblbox6.BackColor = Color.Yellow
        lblbox9.BackColor = Color.Yellow

    ElseIf lblbox1.Text = lblbox5.Text And lblbox5.Text = lblbox9.Text And lblbox1.Text <> lblbox5.Text Then
        lblbox1.ForeColor = Color.Red
        lblbox5.ForeColor = Color.Red
        lblbox9.ForeColor = Color.Red

        lblbox1.BackColor = Color.Yellow
        lblbox5.BackColor = Color.Yellow
        lblbox9.BackColor = Color.Yellow

    ElseIf lblbox3.Text = lblbox5.Text And lblbox5.Text = lblbox7.Text And lblbox3.Text <> lblbox5.Text Then
        lblbox3.ForeColor = Color.Red
        lblbox5.ForeColor = Color.Red
        lblbox7.ForeColor = Color.Red

        lblbox3.BackColor = Color.Yellow
        lblbox5.BackColor = Color.Yellow
        lblbox7.BackColor = Color.Yellow

        MessageBox.Show("player", "Player Won", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf lblbox1.Text = lblbox2.Text Or lblbox2.Text = lblbox3.Text Or lblbox3.Text = lblbox4.Text Or lblbox4.Text = lblbox5.Text Or lblbox5.Text = lblbox6.Text Or lblbox6.Text = lblbox7.Text Or lblbox7.Text = lblbox8.Text Or lblbox8.Text = lblbox9.Text Then
        MessageBox.Show("it is a tie ", "tie", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End If
End Sub

Private Sub btnnewgame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnewgame.Click
    MessageBox.Show("Play a new game?", "New Game", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
    resetvalues()
End Sub

Private Sub Radhumanvshuman_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Radhumanvshuman.CheckedChanged
    placeplayermark()

    setplayerturn()
    iswon()
End Sub

End Class

but there is something missing and i don't know why isn't working , so hopefully you can help me , thank you .

Recommended Answers

All 5 Replies

What exactly is not working in your program? I was looking through your code and in the sub iswon you have impossible if statements.
EX. If lblbox1.Text = lblbox2.Text And lblbox2.Text = lblbox3.Text And lblbox1.Text <> lblbox2.Text Then
For this statement to work lblbox1.text has to equal lblbox2.text and not equal it at the same time. This is an impossible condition. Why is the lblbox1.text <> lblbox2.text needed? I may be incorrect but I do not think that you need this.

Your first mistake is not commenting anything. If we don't know what the code is supposed to do then we can't tell if it's not doing it. A simple comment such as "check for three X or O in a row" speaks volumes. Your second mistake is in hard coding everything. A better approach is to either create your grid at runtime and save references to each control in a 3x3 array, or to create the grid at design time and populate a 3x3 array with references to the controls. That way you can use loops to check for three in a row across, down or diagonally. If you have an array declared as follows

Private grid(2,2) As Button    'or whatever type of control you want

You can populate the grid at form load by

grid(0, 0) = Button1
grid(0, 1) = Button2
grid(0, 2) = Button3
grid(1, 0) = Button4
grid(1, 1) = Button5
grid(1, 2) = Button6
grid(2, 0) = Button7
grid(2, 1) = Button8
grid(2, 2) = Button9

and use the same handler for all grid buttons as

For r As Integer = 0 To 2
    For c As Integer = 0 To 2
        AddHandler grid(r, c).Click, AddressOf ClickTile
    Next
Next

Private Sub ClickTile(sender As System.Object, e As System.EventArgs)

    Dim btn As Button = sender
    .
    .
    .

End Sub

then resetting the entire grid becomes

For r As Integer = 0 To 2
    For c As Integer = 0 To 2
        grid(r, c).Text = " "
        grid(r, c).BackColor = Color.LightGray
        .
        .
        .
    Next
Next

Look at your code on setplayerturn:

Sub setplayerturn()
    If isFirstPlayerTurn = True Then
        isFirstPlayerTurn = False
        isSecondPlayerTurn = False
        txtfirstplayer.BackColor = Color.LightGreen
        txtsecondplayer.BackColor = Color.White
    ElseIf isSecondPlayerTurn = True Then
        isSecondPlayerTurn = False
        isFirstPlayerTurn = False
        txtsecondplayer.BackColor = Color.LightGreen
        txtfirstplayer.BackColor = Color.White
    End If
End Sub

If IsFirstPlayerTurn is True then you set isFirstPlayerTurn to False AND isSecondPlayerTurn to False If isFirstPlayerTurn is False you set isSecondPlayerTurn to False AND isFirstPlayerTurn to False so either way it will never be any players turn.

Instead of having two variables for isFirstPlayerTurn and isSecondPlayerTurn, why not just have one variable that contains either "X" or "O"? Then you only need one test as in

If Player = "X" Then

and you can toggle by

Player = IIF(Player = "X", "O", "X")

Also, if you use a common click event for all tiles you can just do

Dim btn As Button = sender
btn.Text = Player

There is a lot of unnecessary code in your original post.

commented: IIF to the rescue! +0

If you use a For-Each then the code becomes simpler as

For Each btn As Button In grid
    btn.Text = " "
    btn.BackColor = Color.LightGray
    . 
    .
    .
Next
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.