Deciding the winner in Tic-Tac-Toe

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michelle Monty
    New Member
    • Oct 2007
    • 24

    Deciding the winner in Tic-Tac-Toe

    I'm creating a two-player Tic-Tac-Toe game. I have the game programmed up until the point that someone wins. I'm not sure how to code this. My board is set as an array - board(2,2). My code is below. Can anyone give me an idea of how to proceed? I'm sure I probably need to set a loop in the code somehow but unsure of where and how to do it. Thanks!!!
    [code=vbnet]
    Public Class Form1
    Public player1 As Boolean

    'start button starts game and sets first player as X's

    Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles startbutton.Cli ck
    Dim firstplayer As Char
    Dim secondplayer As Char
    Dim x As Char
    Dim O As Char
    firstplayer = x
    secondplayer = O
    Dim board(2, 2) As Integer
    MessageBox.Show ("First player, please type an X in a square", "Tic-Tac-Toe", MessageBoxButto ns.OK)
    End Sub

    Private Sub TextBox1_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TextBox1.Click
    Dim y As String
    y = Checkwin()
    player1 = Not player1
    If player1 = True Then
    TextBox1.Text = "X"
    Else
    TextBox1.Text = "O"
    End If
    End Sub


    Private Sub TextBox2_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TextBox2.Click
    Dim y As String
    y = Checkwin()
    player1 = Not player1
    If player1 = True Then
    TextBox2.Text = "X"
    Else
    TextBox2.Text = "O"
    End If
    End Sub

    Private Sub TextBox3_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TextBox3.Click
    Dim y As String
    y = Checkwin()
    player1 = Not player1
    If player1 = True Then
    TextBox3.Text = "X"
    Else
    TextBox3.Text = "O"
    End If
    End Sub

    Private Sub TextBox4_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TextBox4.Click
    Dim y As String
    y = Checkwin()
    player1 = Not player1
    If player1 = True Then
    TextBox4.Text = "X"
    Else
    TextBox4.Text = "O"
    End If
    End Sub

    Private Sub TextBox5_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TextBox5.Click
    Dim y As String
    y = Checkwin()
    player1 = Not player1
    If player1 = True Then
    TextBox5.Text = "X"
    Else
    TextBox5.Text = "O"
    End If
    End Sub

    Private Sub TextBox6_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TextBox6.Click
    Dim y As String
    y = Checkwin()
    player1 = Not player1
    If player1 = True Then
    TextBox6.Text = "X"
    Else
    TextBox6.Text = "O"
    End If
    End Sub

    Private Sub TextBox7_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TextBox7.Click
    Dim y As String
    y = Checkwin()
    player1 = Not player1
    If player1 = True Then
    TextBox7.Text = "X"
    Else
    TextBox7.Text = "O"
    End If
    End Sub

    Private Sub TextBox8_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TextBox8.Click
    Dim y As String
    y = Checkwin()
    player1 = Not player1
    If player1 = True Then
    TextBox8.Text = "X"
    Else
    TextBox8.Text = "O"
    End If
    End Sub

    Private Sub TextBox9_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TextBox9.Click
    Dim y As String
    y = Checkwin()
    player1 = Not player1
    If player1 = True Then
    TextBox9.Text = "X"
    Else
    TextBox9.Text = "O"
    End If
    End Sub

    End Class[/code]
    Last edited by debasisdas; Nov 15 '07, 04:42 AM. Reason: Formatted using code tags.
  • jaz215
    New Member
    • Nov 2007
    • 55

    #2
    im not sure on this one.. i came up with one solution but its going to be tedious..
    assuming that you placed the textbox in this order

    1 2 3
    4 5 6
    7 8 9

    Code:
    if textbox1.text = x then
        if textbox2.text = x and textbox3.text = x then
              player 1 win
        end if
        if textbox5.text = x and textbox9.text = x then
               player 1 win
        end if
    end if
    then do the same for the other textboxes and redo again for the player 2 win
    its a bit too long.. someone here might be able to came up with other possible solutions

    Hope i put you on the right direction .

    Comment

    • Michelle Monty
      New Member
      • Oct 2007
      • 24

      #3
      Thanks. I was hoping to avoid typing it out like that but that's the only way I can think of to do it too. Thanks again!


      Originally posted by jaz215
      im not sure on this one.. i came up with one solution but its going to be tedious..
      assuming that you placed the textbox in this order

      1 2 3
      4 5 6
      7 8 9

      Code:
      if textbox1.text = x then
          if textbox2.text = x and textbox3.text = x then
                player 1 win
          end if
          if textbox5.text = x and textbox9.text = x then
                 player 1 win
          end if
      end if
      then do the same for the other textboxes and redo again for the player 2 win
      its a bit too long.. someone here might be able to came up with other possible solutions

      Hope i put you on the right direction .

      Comment

      Working...