+ Reply to Thread
Results 1 to 3 of 3

Thread: Tic tac toe ai help

  1. #1
    Newbie elindel is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    1

    Tic tac toe ai help

    Hey I am new at this whole code thing but I have managed to get a game working thanks to alot of information in a code book. However I have no clue how to get the computer to try and win. Here is the code that I have PLEASE HELP!!!!
    Code:
    Private strPlayer As String = ""    'Used to track whose turn it is
    
        'Declare variable representing game board cells
        Private strpbxA1 As String = "Open"
        Private strpbxA2 As String = "Open"
        Private strpbxA3 As String = "Open"
        Private strpbxB1 As String = "Open"
        Private strpbxB2 As String = "Open"
        Private strpbxB3 As String = "Open"
        Private strpbxC1 As String = "Open"
        Private strpbxC2 As String = "Open"
        Private strpbxC3 As String = "Open"
    
        'This procedure executes procedures required to set up the game
        Private Sub frmMain_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
    
            Set_Game_Defaults() 'Call procedure that sets default assignments
    
            Clear_Board()   'Call procedure that clears the game board
    
    
        End Sub
    
        'This procedure sets default assignments
        Private Sub Set_Game_Defaults()
    
            txt_One.Text = "Click on Play to begin" 'Display opening message
            strPlayer = "Player X"  'Set player X to go first
    
    
        End Sub
    
        'This procedure clears out the game board
        Private Sub Clear_Board()
    
            'Load a blank image into each game board cell
            pbx_A1.Image = iml_One.Images(2)
            pbx_A2.Image = iml_One.Images(2)
            pbx_A3.Image = iml_One.Images(2)
            pbx_B1.Image = iml_One.Images(2)
            pbx_B2.Image = iml_One.Images(2)
            pbx_B3.Image = iml_One.Images(2)
            pbx_C1.Image = iml_One.Images(2)
            pbx_C2.Image = iml_One.Images(2)
            pbx_C3.Image = iml_One.Images(2)
    
    
            'Mark each game board cell as open and available for selection
            strpbxA1 = "Open"
            strpbxA2 = "Open"
            strpbxA3 = "Open"
            strpbxB1 = "Open"
            strpbxB2 = "Open"
            strpbxB3 = "Open"
            strpbxC1 = "Open"
            strpbxC2 = "Open"
            strpbxC3 = "Open"
    
        End Sub
    
        'This procedure executes when the button labeled Play is clicked
        Private Sub btn_One_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles btn_One.Click
    
            Clear_Board()   'Call the procedure that clears out the game board
    
            Play_Game() 'Call the procedure that begins game play
    
    
        End Sub
    
        'This procedure begins game play
        Private Sub Play_Game()
    
            'Post message that identifies whose turn it is
            txt_One.Text = strPlayer & "'s turn."
    
            'Enable all game board cells
            pbx_A1.Enabled = True
            pbx_A2.Enabled = True
            pbx_A3.Enabled = True
            pbx_B1.Enabled = True
            pbx_B2.Enabled = True
            pbx_B3.Enabled = True
            pbx_C1.Enabled = True
            pbx_C2.Enabled = True
            pbx_C3.Enabled = True
            btn_One.Enabled = False 'Disable access to the button labeled Play
    
        End Sub
    
        'This procedure executes when the button labeled exit is clicked
        Private Sub btn_Three_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles btn_Two.Click
    
            Application.Exit()
    
        End Sub
    
      'This proceudre executes when a player clicks on the first cell in the first row
        Private Sub pbx_A1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles pbx_A1.Click
    
            Dim strGameOver As String = ""  'Used to track game status
    
            'Notify the player if the cell has already been taken.
            If strpbxA1 <> "Open" Then
                txt_One.Text = "The square has already been taken." & _
                  ControlChars.CrLf & strPlayer & "'s turn."
                Return  ''Leave the Sub procedure
            End If
    
            If strPlayer = "Player X" Then
                pbx_A1.Image = iml_One.Images(0)
                strpbxA1 = "Player X"
            Else
                pbx_A1.Image = iml_One.Images(1)
                strpbxA1 = "Player O"
            End If
    
            'Call the procedure that checks to see if the game has been won
            strGameOver = Check_For_Winner()
    
            'Call the procedure that switched player turns or displays a message declaring a winner
            Determine_Game_Status(strGameOver)
    
        End Sub
    
        'This proceudre executes when a player clicks on the second cell in the first row
        Private Sub pbx_A2_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles pbx_A2.Click
    
            Dim strGameOver As String = ""  'Used to track game status
    
    
            'Notify the player if the cell has already been taken.
            If strpbxA2 <> "Open" Then
                txt_One.Text = "The square has already been taken." & _
                  ControlChars.CrLf & strPlayer & "'s turn."
                Return 'Leave the Sub procedure
            End If
    
            If strPlayer = "Player X" Then
                pbx_A2.Image = iml_One.Images(0)
                strpbxA2 = "Player X"
            Else
                pbx_A2.Image = iml_One.Images(1)
                strpbxA2 = "Player O"
            End If
    
            'Call the procedure that checks to see if the game has been won
            strGameOver = Check_For_Winner()
    
            'Call the procedure that switched player turns or displays a message declaring a winner
            Determine_Game_Status(strGameOver)
    
        End Sub
    
        'This proceudre executes when a player clicks on the third cell in the first row
        Private Sub pbx_A3_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles pbx_A3.Click
    
            Dim strGameOver As String = ""  'Used to track game status
    
    
            'Notify the player if the cell has already been taken.
            If strpbxA3 <> "Open" Then
                txt_One.Text = "The square has already been taken." & _
                  ControlChars.CrLf & strPlayer & "'s turn."
                Return  'Leave the Sub procedure
            End If
    
            If strPlayer = "Player X" Then
                pbx_A3.Image = iml_One.Images(0)
                strpbxA3 = "Player X"
            Else
                pbx_A3.Image = iml_One.Images(1)
                strpbxA3 = "Player O"
            End If
    
            'Call the procedure that checks to see if the game has been won
            strGameOver = Check_For_Winner()
    
            'Call the procedure that switched player turns or displays a message declaring a winner
            Determine_Game_Status(strGameOver)
    
    
        End Sub
    
        'This proceudre executes when a player clicks on the first cell in the second row
        Private Sub pbx_B1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles pbx_B1.Click
    
            Dim strGameOver As String = ""  'Used to track game status
    
    
            'Notify the player if the cell has already been taken.
            If strpbxB1 <> "Open" Then
                txt_One.Text = "The square has already been taken." & _
                  ControlChars.CrLf & strPlayer & "'s turn."
                Return 'Leave the Sub procedure
            End If
    
            If strPlayer = "Player X" Then
                pbx_B1.Image = iml_One.Images(0)
                strpbxB1 = "Player X"
            Else
                pbx_B1.Image = iml_One.Images(1)
                strpbxB1 = "Player O"
            End If
    
            'Call the procedure that checks to see if the game has been won
            strGameOver = Check_For_Winner()
    
            'Call the procedure that switched player turns or displays a message declaring a winner
            Determine_Game_Status(strGameOver)
    
        End Sub
    
        'This proceudre executes when a player clicks on the second cell in the second row
        Private Sub pbx_B2_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles pbx_B2.Click
    
            Dim strGameOver As String = ""  'Used to track game status
    
    
            'Notify the player if the cell has already been taken.
            If strpbxB2 <> "Open" Then
                txt_One.Text = "The square has already been taken." & _
                  ControlChars.CrLf & strPlayer & "'s turn."
                Return 'Leave the Sub procedure
            End If
    
            If strPlayer = "Player X" Then
                pbx_B2.Image = iml_One.Images(0)
                strpbxB2 = "Player X"
            Else
                pbx_B2.Image = iml_One.Images(1)
                strpbxB2 = "Player O"
            End If
    
            'Call the procedure that checks to see if the game has been won
            strGameOver = Check_For_Winner()
    
            'Call the procedure that switched player turns or displays a message declaring a winner
            Determine_Game_Status(strGameOver)
    
        End Sub
    
        'This proceudre executes when a player clicks on the third cell in the second row
        Private Sub pbx_B3_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles pbx_B3.Click
    
            Dim strGameOver As String = ""  'Used to track game status
    
    
            'Notify the player if the cell has already been taken.
            If strpbxB3 <> "Open" Then
                txt_One.Text = "The square has already been taken." & _
                  ControlChars.CrLf & strPlayer & "'s turn."
                Return  'Leave the Sub procedure
            End If
    
            If strPlayer = "Player X" Then
                pbx_B3.Image = iml_One.Images(0)
                strpbxB3 = "Player X"
            Else
                pbx_B3.Image = iml_One.Images(1)
                strpbxB3 = "Player O"
            End If
    
            'Call the procedure that checks to see if the game has been won
            strGameOver = Check_For_Winner()
    
            'Call the procedure that switched player turns or displays a message declaring a winner
            Determine_Game_Status(strGameOver)
    
        End Sub
    
        'This proceudre executes when a player clicks on the first cell in the third row
        Private Sub pbx_C1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles pbx_C1.Click
    
            Dim strGameOver As String = ""  'Used to track game status
    
    
            'Notify the player if the cell has already been taken.
            If strpbxC1 <> "Open" Then
                txt_One.Text = "The square has already been taken." & _
                  ControlChars.CrLf & strPlayer & "'s turn."
                Return  'Leave the Sub procedure
            End If
    
            If strPlayer = "Player X" Then
                pbx_C1.Image = iml_One.Images(0)
                strpbxC1 = "Player X"
            Else
                pbx_C1.Image = iml_One.Images(1)
                strpbxC1 = "Player O"
            End If
    
            'Call the procedure that checks to see if the game has been won
            strGameOver = Check_For_Winner()
    
            'Call the procedure that switched player turns or displays a message declaring a winner
            Determine_Game_Status(strGameOver)
    
        End Sub
        'This proceudre executes when a player clicks on the second cell in the third row
    
        Private Sub pbx_C2_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles pbx_C2.Click
    
            Dim strGameOver As String = ""  'Used to track game status
    
    
            'Notify the player if the cell has already been taken.
            If strpbxC2 <> "Open" Then
                txt_One.Text = "The square has already been taken." & _
                  vbCrLf & strPlayer & "'s turn."
                Return  'Leave the Sub procedure
            End If
    
            If strPlayer = "Player X" Then
                pbx_C2.Image = iml_One.Images(0)
                strpbxC2 = "Player X"
            Else
                pbx_C2.Image = iml_One.Images(1)
                strpbxC2 = "Player O"
            End If
    
            'Call the procedure that checks to see if the game has been won
            strGameOver = Check_For_Winner()
    
            'Call the procedure that switched player turns or displays a message declaring a winner
            Determine_Game_Status(strGameOver)
    
        End Sub
    
        'This proceudre executes when a player clicks on the third cell in the third row
        Private Sub pbx_C3_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles pbx_C3.Click
    
            Dim strGameOver As String = ""  'Used to track game status
    
    
            'Notify the player if the cell has already been taken.
            If strpbxC3 <> "Open" Then
                txt_One.Text = "The square has already been taken." & _
                  ControlChars.CrLf & strPlayer & "'s turn."
                Return  'Leave the Sub procedure
            End If
    
            If strPlayer = "Player X" Then
                pbx_C3.Image = iml_One.Images(0)
                strpbxC3 = "Player X"
            Else
                pbx_C3.Image = iml_One.Images(1)
                strpbxC3 = "Player O"
            End If
    
            'Call the procedure that checks to see if the game has been won
            strGameOver = Check_For_Winner()
    
            'Call the procedure that switched player turns or displays a message declaring a winner
            Determine_Game_Status(strGameOver)
    
    
        End Sub
    
        'This procedure determines whether the game has been won and by whom
        Function Check_For_Winner() As String
    
            'Check the first row for a winner
            If strpbxA1 = strPlayer Then
                If strpbxA2 = strPlayer Then
                    If strpbxA3 = strPlayer Then
                        Return strPlayer
                    End If
                End If
            End If
    
            'Check the second row for a winner
            If strpbxB1 = strPlayer Then
                If strpbxB2 = strPlayer Then
                    If strpbxB3 = strPlayer Then
                        Return strPlayer
                    End If
                End If
            End If
    
            'Check the third row for a winner
            If strpbxC1 = strPlayer Then
                If strpbxC2 = strPlayer Then
                    If strpbxC3 = strPlayer Then
                        Return strPlayer
                    End If
                End If
            End If
    
            'Check the first colum for a winner
            If strpbxA1 = strPlayer Then
                If strpbxB1 = strPlayer Then
                    If strpbxC1 = strPlayer Then
                        Return strPlayer
                    End If
                End If
            End If
    
            'Check the second colum for a winner
            If strpbxA2 = strPlayer Then
                If strpbxB2 = strPlayer Then
                    If strpbxC2 = strPlayer Then
                        Return strPlayer
                    End If
                End If
            End If
    
            'Check the third colum for a winner
            If strpbxA3 = strPlayer Then
                If strpbxB3 = strPlayer Then
                    If strpbxC3 = strPlayer Then
                        Return strPlayer
                    End If
                End If
            End If
    
            'Check diagonally from top=left to bottom_right for a winner
            If strpbxA1 = strPlayer Then
                If strpbxB2 = strPlayer Then
                    If strpbxC3 = strPlayer Then
                        Return strPlayer
                    End If
                End If
            End If
    
    
            'Check diagonally from top-right to bottom-left for a winner
            If strpbxA3 = strPlayer Then
                If strpbxB2 = strPlayer Then
                    If strpbxC1 = strPlayer Then
                        Return strPlayer
                    End If
                End If
            End If
    
    
    
            'Check to see if the game has resulted in a tie
    
            If strpbxA1 = "Player X" Or _
                       strpbxA1 = "Player O" Then
                If strpbxA2 = "Player X" Or _
                   strpbxA2 = "Player O" Then
                    If strpbxA3 = "Player X" Or _
                       strpbxA3 = "Player O" Then
                        If strpbxB1 = "Player X" Or _
                           strpbxB1 = "Player O" Then
                            If strpbxB2 = "Player X" Or _
                               strpbxB2 = "Player O" Then
                                If strpbxB3 = "Player X" Or _
                                    strpbxB3 = "Player O" Then
                                    If strpbxC1 = "Player X" Or _
                                        strpbxC1 = "Player O" Then
                                        If strpbxC2 = "Player X" Or _
                                            strpbxC2 = "Player O" Then
                                            If strpbxC3 = "Player X" Or _
                                                strpbxC3 = "Player O" Then
                                                Return "Tie"
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
    
    
    
            Return ""
    
        End Function
    
        'This procedure determines whether or not the game is over
        Private Sub Determine_Game_Status(ByVal strGameOver As String)
    
            If strGameOver = "" Then  'The game is not over yet
                Switch_Players()  'Call procedure that switches player turns
                'Post message stating that it is time for players to switch turns
                txt_One.Text = strPlayer & "'s turn."
            Else
                If strGameOver <> "Tie" Then    'There is a winner
                    btn_One.Enabled = True  ''Enable the button labeled Play
                    'Call procedure that disables game board cells
                    Disable_Squares()
                    'Display game over message
                    txt_One.Text = "Game over. " & strGameOver & " has won."
                Else    'The game has resulted in a tie
                    btn_One.Enabled = True  'Enable the button Play
                    'Call procedure that disables game board cells
                    Disable_Squares()
                    'Display game over message
                    txt_One.Text = "Game over. There was no winner."
                End If
            End If
    
        End Sub
    
        'This procedure is responsible for toggling between players turns
        Private Sub Switch_Players()
    
            If strPlayer = "Player X" Then
                strPlayer = "Player O"
            Else
                strPlayer = "Player X"
            End If
    
        End Sub
    
        'This procedure disables all game board cells
        Private Sub Disable_Squares()
    
            pbx_A1.Enabled = False
            pbx_A2.Enabled = False
            pbx_A3.Enabled = False
            pbx_B1.Enabled = False
            pbx_B2.Enabled = False
            pbx_B3.Enabled = False
            pbx_C1.Enabled = False
            pbx_C2.Enabled = False
            pbx_C3.Enabled = False
    
        End Sub
    
    
    End Class
    Last edited by WingedPanther; 04-22-2009 at 03:36 PM. Reason: add code tags (the # button)

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,689
    Blog Entries
    57

    Re: Tic tac toe ai help

    Start by playing a few games and thinking about what YOU do when you want to win. Can you explain the strategy in words?
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    16
    Posts
    8,793
    Blog Entries
    5

    Re: Tic tac toe ai help

    I created a Tic tad toe game with ai. I made so if it could win it did that. If it couldn't but the player could win the next turn, It had, i think, 70% chance to block and if nothing of this happened it took an empty square by random.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts