#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Tic tac toe (draw code)


    i have done the whole code for tic tac toe game but i have a problem of doing the draw code !
    here is my code until now

    Public Class Form1
    Dim isFirstPlayerTurn As Boolean = True
    Dim isSecondPlayerTurn As Boolean = False
    Dim isPlayerWon As Boolean=False
    Sub PlacePlayerMark()
    isFirstPlayerTurn = True
    isSecondPlayerTurn = False
    SetPlayerTurn()
    End Sub
    Sub SetPlayerTurn()
    If TextBox1.Text = "Player 1" Then
    TextBox1.Text = "Player 2"
    Else : TextBox1.Text = "Player 1"
    End If
    End Sub
    Sub IsWon()
    If Label1.Text = "X" And Label2.Text = "X" And Label3.Text = "X" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label1.Text = "0" And Label2.Text = "0" And Label3.Text = "0" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label4.Text = "X" And Label5.Text = "X" And Label6.Text = "X" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label4.Text = "0" And Label5.Text = "0" And Label6.Text = "0" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label7.Text = "X" And Label8.Text = "X" And Label9.Text = "X" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label7.Text = "0" And Label8.Text = "0" And Label9.Text = "0" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label1.Text = "X" And Label4.Text = "X" And Label7.Text = "X" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label1.Text = "0" And Label4.Text = "0" And Label7.Text = "0" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label2.Text = "X" And Label5.Text = "X" And Label8.Text = "X" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label2.Text = "0" And Label5.Text = "0" And Label8.Text = "0" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label3.Text = "X" And Label6.Text = "X" And Label9.Text = "X" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label3.Text = "0" And Label6.Text = "0" And Label9.Text = "0" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label1.Text = "X" And Label5.Text = "X" And Label9.Text = "X" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label1.Text = "0" And Label5.Text = "0" And Label9.Text = "0" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label3.Text = "X" And Label5.Text = "X" And Label7.Text = "X" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    ElseIf Label3.Text = "0" And Label5.Text = "0" And Label7.Text = "0" Then
    colors()
    MessageBox.Show(TextBox1.Text & "win", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Else : MessageBox.Show("draw", "result", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If

    End Sub
    Sub colors()
    Label1.BackColor = Color.Yellow
    Label2.BackColor = Color.Yellow
    Label3.BackColor = Color.Yellow
    Label4.BackColor = Color.Yellow
    Label5.BackColor = Color.Yellow
    Label6.BackColor = Color.Yellow
    Label7.BackColor = Color.Yellow
    Label8.BackColor = Color.Yellow
    Label9.BackColor = Color.Yellow
    Label1.ForeColor = Color.Red
    Label2.ForeColor = Color.Red
    Label3.ForeColor = Color.Red
    Label4.ForeColor = Color.Red
    Label5.ForeColor = Color.Red
    Label6.ForeColor = Color.Red
    Label7.ForeColor = Color.Red
    Label8.ForeColor = Color.Red
    Label9.ForeColor = Color.Red

    End Sub


    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
    If TextBox1.Text = "Player 1" Then
    isFirstPlayerTurn = True
    Label2.Text = "X"
    Else : Label2.Text = "0"
    End If
    IsWon()
    PlacePlayerMark()

    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
    If TextBox1.Text = "Player 1" Then
    isFirstPlayerTurn = True
    Label1.Text = "X"
    Else : Label1.Text = "0"

    End If
    IsWon()
    PlacePlayerMark()

    End Sub

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
    If TextBox1.Text = "Player 1" Then
    isFirstPlayerTurn = True
    Label3.Text = "X"
    Else : Label3.Text = "0"
    End If
    IsWon()
    PlacePlayerMark()


    End Sub

    Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click
    If TextBox1.Text = "Player 1" Then
    isFirstPlayerTurn = True
    Label4.Text = "X"
    Else : Label4.Text = "0"
    End If
    IsWon()
    PlacePlayerMark()

    End Sub

    Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click
    If TextBox1.Text = "Player 1" Then
    isFirstPlayerTurn = True
    Label5.Text = "X"
    Else : Label5.Text = "0"
    End If
    IsWon()
    PlacePlayerMark()

    End Sub

    Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click
    If TextBox1.Text = "Player 1" Then
    isFirstPlayerTurn = True
    Label6.Text = "X"
    Else : Label6.Text = "0"
    End If
    IsWon()
    PlacePlayerMark()

    End Sub

    Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click
    If TextBox1.Text = "Player 1" Then
    isFirstPlayerTurn = True
    Label7.Text = "X"
    Else : Label7.Text = "0"
    End If
    IsWon()
    PlacePlayerMark()

    End Sub

    Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click
    If TextBox1.Text = "Player 1" Then
    isFirstPlayerTurn = True
    Label8.Text = "X"
    Else : Label8.Text = "0"
    End If
    IsWon()
    PlacePlayerMark()

    End Sub

    Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click
    If TextBox1.Text = "Player 1" Then
    isFirstPlayerTurn = True
    Label9.Text = "X"
    Else : Label9.Text = "0"
    End If
    IsWon()
    PlacePlayerMark()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If MessageBox.Show("Play a new game?", "new Game", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
    TextBox1.Text = "Player 1"
    Label1.Text = ""
    Label2.Text = ""
    Label3.Text = ""
    Label4.Text = ""
    Label5.Text = ""
    Label6.Text = ""
    Label7.Text = ""
    Label8.Text = ""
    Label9.Text = ""
    Label1.BackColor = Color.Transparent
    Label2.BackColor = Color.Transparent
    Label3.BackColor = Color.Transparent
    Label4.BackColor = Color.Transparent
    Label5.BackColor = Color.Transparent
    Label6.BackColor = Color.Transparent
    Label7.BackColor = Color.Transparent
    Label8.BackColor = Color.Transparent
    Label9.BackColor = Color.Transparent
    Label1.ForeColor = Color.Black
    Label2.ForeColor = Color.Black
    Label3.ForeColor = Color.Black
    Label4.ForeColor = Color.Black
    Label5.ForeColor = Color.Black
    Label6.ForeColor = Color.Black
    Label7.ForeColor = Color.Black
    Label8.ForeColor = Color.Black
    Label9.ForeColor = Color.Black
    isFirstPlayerTurn = True
    isSecondPlayerTurn = False
    isPlayerWon = False

    End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    End Class
  2. #2
  3. Type Cast Exception
    Devshed Supreme Being (6500+ posts)

    Join Date
    Apr 2004
    Location
    OAKLAND CA | Adam's Point (Fairyland)
    Posts
    14,954
    Rep Power
    8617
    So you posted a bunch of code and simply describe your trouble as "i have a problem" ...

    Good luck.
    medialint.com

    “Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by medialint
    So you posted a bunch of code and simply describe your trouble as "i have a problem" ...

    Good luck.
    I said there is a problem how to write the draw code ! I wrote the win case but in case of draw i did not knew how to do it ! Anw tnx !
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0
    Just a suggestion, I recomend you convert this over to an array. It will make life alot easier.

IMN logo majestic logo threadwatch logo seochat tools logo