LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 June 10 2013

roytarabay
Member

Hey guys , i have the tic tac toe in visual basic

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 .

Offline

#2 June 10 2013

DNA
Member

Re: Hey guys , i have the tic tac toe in visual basic

First of all as a good practice don't reset your variables or labels or whatever with a space in vb i use =nothing
your code logic is wrong and there are a lot of things missing not to mention that your expecting things to work without actually coding it. her are some of the things i noticed while reading the code first time:

1-To what does placeplayermark() function return the characters? this is not how a function is used.
2-the human vs computer logic does not exist is this code final?
3-the player can't choose in which label does the X or O goes.
4-the placeplayermark function and setplayerturn procedure logic are absolutely wrong.
5-your iswon() procedure conditions are wrong for example "If lblbox1.Text = lblbox2.Text And lblbox2.Text = lblbox3.Text And lblbox1.Text <> lblbox2.Text" this condition will never be true since if a=b=c how is it possible for a to be different from b at the same time?  -_-.
6- the code execution sequence is bad your procedures will work once that's it, you need to have a loop or buttons for each label with an "X" or "O" and then after the two player finish you would have to run the iswon() procedure to check for a winner.

dude the only thing that should work is the resetvalues() procedure. so Google some more information and re do the code in a logical way.

Offline

#3 June 11 2013

amkahal
Member

Re: Hey guys , i have the tic tac toe in visual basic

my advice to you dear, don't put your whole code, determine your problems well, and put lines that point to the problem, so we can assist you at best.

Offline

Board footer