|
-
Jul 11th, 2012, 11:18 AM
#1
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.
-
Jul 12th, 2012, 02:00 PM
#2
Re: Algo/Code help for tictactoe ai
I got something, although I really don't like how it works:
vb Code:
If pvp = False Then
If player1Turn = False Then
'If cpu can win
If Button1.Text = "O" And Button2.Text = "O" And Button3.Text = String.Empty Then
Button3.PerformClick()
ElseIf Button1.Text = "O" And Button4.Text = "O" And Button7.Text = String.Empty Then
Button7.PerformClick()
ElseIf Button1.Text = "O" And Button5.Text = "O" And Button9.Text = String.Empty Then
Button9.PerformClick()
ElseIf Button4.Text = "O" And Button5.Text = "O" And Button6.Text = String.Empty Then
Button6.PerformClick()
ElseIf Button2.Text = "O" And Button5.Text = "O" And Button8.Text = String.Empty Then
Button8.PerformClick()
ElseIf Button3.Text = "O" And Button5.Text = "O" And Button7.Text = String.Empty Then
Button7.PerformClick()
ElseIf Button7.Text = "O" And Button8.Text = "O" And Button9.Text = String.Empty Then
Button9.PerformClick()
ElseIf Button3.Text = "O" And Button6.Text = "O" And Button9.Text = String.Empty Then
Button9.PerformClick()
'Block wins
ElseIf Button1.Text = "X" And Button2.Text = "X" And Button3.Text = String.Empty Then
Button3.PerformClick()
ElseIf Button1.Text = "X" And Button4.Text = "X" And Button7.Text = String.Empty Then
Button7.PerformClick()
ElseIf Button1.Text = "X" And Button5.Text = "X" And Button9.Text = String.Empty Then
Button9.PerformClick()
ElseIf Button4.Text = "X" And Button5.Text = "X" And Button6.Text = String.Empty Then
Button6.PerformClick()
ElseIf Button2.Text = "X" And Button5.Text = "X" And Button8.Text = String.Empty Then
Button8.PerformClick()
ElseIf Button3.Text = "X" And Button5.Text = "X" And Button7.Text = String.Empty Then
Button7.PerformClick()
ElseIf Button7.Text = "X" And Button8.Text = "X" And Button9.Text = String.Empty Then
Button9.PerformClick()
ElseIf Button3.Text = "X" And Button6.Text = "X" And Button9.Text = String.Empty Then
Button9.PerformClick()
Else
'If there are no possibilities to win or block user's win
If Button1.Enabled = True Then
Button1.PerformClick()
ElseIf Button9.Enabled = True Then
Button9.PerformClick()
ElseIf Button2.Enabled = True Then
Button2.PerformClick()
ElseIf Button8.Enabled = True Then
Button8.PerformClick()
ElseIf Button3.Enabled = True Then
Button3.PerformClick()
ElseIf Button7.Enabled = True Then
Button7.PerformClick()
ElseIf Button4.Enabled = True Then
Button4.PerformClick()
ElseIf Button6.Enabled = True Then
Button6.PerformClick()
ElseIf Button5.Enabled = True Then
Button5.PerformClick()
End If
End If
End If
End If
If y'all could help refine it, I'm up for suggestions.
-
Jul 18th, 2012, 02:14 AM
#3
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
-
Jul 18th, 2012, 09:55 PM
#4
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!
-
Jul 27th, 2012, 11:41 PM
#5
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 :]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|