Results 1 to 5 of 5

Thread: Algo/Code help for tictactoe ai

  1. #1
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,323

    Algo/Code help for tictactoe ai

    Dunno if I should post this in the vb.net section or not, but I'm having a tough time wrapping my head around the ai code for my tictactoe game. I know very little about the min/max algorithm, but I do kinda understand the basics of it. Here is what I'm using right now for my ai:
    Code:
    If player1Turn = False Then
                'Block wins
                If Button1.Text = "X" And Button2.Text = "X" And Button3.Text = String.Empty Then
                    Button3.Text = "O"
                ElseIf Button1.Text = "X" And Button4.Text = "X" And Button7.Text = String.Empty Then
                    Button7.Text = "O"
                ElseIf Button1.Text = "X" And Button5.Text = "X" And Button9.Text = String.Empty Then
                    Button9.Text = "O"
                ElseIf Button4.Text = "X" And Button5.Text = "X" And Button6.Text = String.Empty Then
                    Button6.Text = "O"
                ElseIf Button2.Text = "X" And Button5.Text = "X" And Button8.Text = String.Empty Then
                    Button8.Text = "O"
                ElseIf Button3.Text = "X" And Button5.Text = "X" And Button7.Text = String.Empty Then
                    Button7.Text = "O"
                ElseIf Button7.Text = "X" And Button8.Text = "X" And Button9.Text = String.Empty Then
                    Button9.Text = "O"
                ElseIf Button3.Text = "X" And Button6.Text = "X" And Button9.Text = String.Empty Then
                    Button9.Text = "O"
    
                    'If cpu can win
                ElseIf Button1.Text = "O" And Button2.Text = "O" And Button3.Text = String.Empty Then
                    Button3.Text = "O"
                ElseIf Button1.Text = "O" And Button4.Text = "O" And Button7.Text = String.Empty Then
                    Button7.Text = "O"
                ElseIf Button1.Text = "O" And Button5.Text = "O" And Button9.Text = String.Empty Then
                    Button9.Text = "O"
                ElseIf Button4.Text = "O" And Button5.Text = "O" And Button6.Text = String.Empty Then
                    Button6.Text = "O"
                ElseIf Button2.Text = "O" And Button5.Text = "O" And Button8.Text = String.Empty Then
                    Button8.Text = "O"
                ElseIf Button3.Text = "O" And Button5.Text = "O" And Button7.Text = String.Empty Then
                    Button7.Text = "O"
                ElseIf Button7.Text = "O" And Button8.Text = "O" And Button9.Text = String.Empty Then
                    Button9.Text = "O"
                ElseIf Button3.Text = "O" And Button6.Text = "O" And Button9.Text = String.Empty Then
                    Button9.Text = "O"
                Else
                    'If there are no possibilities to win or block user's  win
    
    
                End If
            End If
    basically the board looks like this:
    button1 | button2 | button3
    button4 | button5 | button6
    button7 | button8 | button9

    as you can tell, I hardcoded the possible wins and possible blocks, now what I'm stuck on is if it's the computer's turn and there are no possibilities to win or block the user's win. Any help or suggestions.

    Edit - This isn't much of an edit to the code, but I changed the button.text to button.performclick. It works better that way.
    Last edited by dday9; Jul 11th, 2012 at 01:38 PM.
    Vb.Net Contributions:-Here-
    Game Contributions:-Here- New - 2d map creator
    XNA in Vb.Net Tutorials:-Here-

    Links:
    LegalShield | AUP

  2. #2
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,323

    Re: Algo/Code help for tictactoe ai

    I got something, although I really don't like how it works:

    vb Code:
    1. If pvp = False Then
    2.             If player1Turn = False Then
    3.  
    4.                 'If cpu can win
    5.                 If Button1.Text = "O" And Button2.Text = "O" And Button3.Text = String.Empty Then
    6.                     Button3.PerformClick()
    7.                 ElseIf Button1.Text = "O" And Button4.Text = "O" And Button7.Text = String.Empty Then
    8.                     Button7.PerformClick()
    9.                 ElseIf Button1.Text = "O" And Button5.Text = "O" And Button9.Text = String.Empty Then
    10.                     Button9.PerformClick()
    11.                 ElseIf Button4.Text = "O" And Button5.Text = "O" And Button6.Text = String.Empty Then
    12.                     Button6.PerformClick()
    13.                 ElseIf Button2.Text = "O" And Button5.Text = "O" And Button8.Text = String.Empty Then
    14.                     Button8.PerformClick()
    15.                 ElseIf Button3.Text = "O" And Button5.Text = "O" And Button7.Text = String.Empty Then
    16.                     Button7.PerformClick()
    17.                 ElseIf Button7.Text = "O" And Button8.Text = "O" And Button9.Text = String.Empty Then
    18.                     Button9.PerformClick()
    19.                 ElseIf Button3.Text = "O" And Button6.Text = "O" And Button9.Text = String.Empty Then
    20.                     Button9.PerformClick()
    21.                     'Block wins
    22.                 ElseIf Button1.Text = "X" And Button2.Text = "X" And Button3.Text = String.Empty Then
    23.                     Button3.PerformClick()
    24.                 ElseIf Button1.Text = "X" And Button4.Text = "X" And Button7.Text = String.Empty Then
    25.                     Button7.PerformClick()
    26.                 ElseIf Button1.Text = "X" And Button5.Text = "X" And Button9.Text = String.Empty Then
    27.                     Button9.PerformClick()
    28.                 ElseIf Button4.Text = "X" And Button5.Text = "X" And Button6.Text = String.Empty Then
    29.                     Button6.PerformClick()
    30.                 ElseIf Button2.Text = "X" And Button5.Text = "X" And Button8.Text = String.Empty Then
    31.                     Button8.PerformClick()
    32.                 ElseIf Button3.Text = "X" And Button5.Text = "X" And Button7.Text = String.Empty Then
    33.                     Button7.PerformClick()
    34.                 ElseIf Button7.Text = "X" And Button8.Text = "X" And Button9.Text = String.Empty Then
    35.                     Button9.PerformClick()
    36.                 ElseIf Button3.Text = "X" And Button6.Text = "X" And Button9.Text = String.Empty Then
    37.                     Button9.PerformClick()
    38.                 Else
    39.  
    40.                     'If there are no possibilities to win or block user's  win
    41.                     If Button1.Enabled = True Then
    42.                         Button1.PerformClick()
    43.                     ElseIf Button9.Enabled = True Then
    44.                         Button9.PerformClick()
    45.                     ElseIf Button2.Enabled = True Then
    46.                         Button2.PerformClick()
    47.                     ElseIf Button8.Enabled = True Then
    48.                         Button8.PerformClick()
    49.                     ElseIf Button3.Enabled = True Then
    50.                         Button3.PerformClick()
    51.                     ElseIf Button7.Enabled = True Then
    52.                         Button7.PerformClick()
    53.                     ElseIf Button4.Enabled = True Then
    54.                         Button4.PerformClick()
    55.                     ElseIf Button6.Enabled = True Then
    56.                         Button6.PerformClick()
    57.                     ElseIf Button5.Enabled = True Then
    58.                         Button5.PerformClick()
    59.                     End If
    60.  
    61.                 End If
    62.             End If
    63.         End If

    If y'all could help refine it, I'm up for suggestions.
    Vb.Net Contributions:-Here-
    Game Contributions:-Here- New - 2d map creator
    XNA in Vb.Net Tutorials:-Here-

    Links:
    LegalShield | AUP

  3. #3
    Frenzied Member boops boops's Avatar
    Join Date
    Nov 08
    Location
    Holland/France
    Posts
    1,999

    Re: Algo/Code help for tictactoe ai

    Does the logic now work as intended? If so, what don't you like?

    If I were coding it myself, I think I would use a 9-member array of integers instead of keeping the game data in the button texts; the integer values could be 0=empty, 1=X, 2=O. Then I would code a single event sub to handle all the button clicks. This sub would update the array, change the clicked button's text and disable it, and call the computer-move sub. This sub would check the array values using similar logic to your code, although I would prefer a Select-Case statement to all those ElseIfs. For its move, it would change the button text and enabled state as well as updating the array. But then I just don't like PerformClick.

    By the way, what is the min/max algorithm?

    BB

  4. #4
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,323

    Re: Algo/Code help for tictactoe ai

    Does the logic now work as intended? If so, what don't you like?
    If you play corners, or the same thing over and over again, then it takes the same path.
    the integer values could be 0=empty, 1=X, 2=O
    I didn't think about that, I'm going to change to storing values as an int array.
    Select-Case statement to all those ElseIfs
    I don't usually do select/case's, simply because I rarely find a use for them in my usual(non game programming) code. But I do see where it'd be easier.
    By the way, what is the min/max algorithm?
    You can find it on wiki here, but simplified; It's an algorithm for 2 player games where player2's the best move/play is based on the play of player1. Pretty much think of it as an if/then statement. If I click on button1 then is button2,3,4 ect. my best move? And if I do choose, say button2, then what are the possibilities after that? Or another good example is to think of it as a tree, as shown here:
    Last edited by dday9; Jul 18th, 2012 at 09:58 PM. Reason: Getting that darn image to show correctly, geez!
    Vb.Net Contributions:-Here-
    Game Contributions:-Here- New - 2d map creator
    XNA in Vb.Net Tutorials:-Here-

    Links:
    LegalShield | AUP

  5. #5
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,323

    Re: Algo/Code help for tictactoe ai

    Here is something much more random that I started using:

    Code:
    Private Function randomBtn() As Button
            Dim list As New List(Of Button)
            For Each btn As Button In Panel1.Controls.OfType(Of Button)()
                If btn.Enabled = True Then
                    list.Add(btn)
                End If
            Next
            Dim r As New Random
            Dim i As Integer
            i = r.Next(0, list.Count - 1)
            randomBtn = list.Item(i)
    
        End Function
    I like that much more :]
    Vb.Net Contributions:-Here-
    Game Contributions:-Here- New - 2d map creator
    XNA in Vb.Net Tutorials:-Here-

    Links:
    LegalShield | AUP

Posting Permissions

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