Helpful Information
 
 
Category: Visual Basic Programming
Tic Tac Toe

Automated Tic Tac Toe

I am having a bit of a problem. I am trying to develop an entry form for my Tic-Tac-Toe game. I have a board of 9 picture boxes and i want the computer to play against itself. Once X or O wins, i also want the boxes to clear and a new game to begin. Here is what i came up with so far, it ain't much! Any help yould be appreciated


Dim onePlayer As Boolean
Dim xTurn As Boolean
Dim gameOver As Boolean
Dim intBoard(0 To 2, 0 To 2) As Integer
Dim taken(0 To 8) As Boolean


Private Sub CmdExit_Click()
MsgBox "Bye Bye"
End
End Sub

Private Sub CmdHelp_Click()
MsgBox "1 - Choose the number of players"
MsgBox "2 - Select a time limit"
MsgBox "3 - Click New Game"
MsgBox "4 - Play!"

End Sub

Private Sub CmdPlay_Click()

Enter.Hide
Game.Show
End Sub




Private Sub Timer1_Timer()

xTurn = True
gameOver = False

Dim i As Integer
For i = 0 To 8
taken(i) = False
Picture1(i).Tag = 0
Picture1(i).Cls
Picture1(i).Refresh
Next i
End Sub



Private Sub Form_Load()



Randomize

xTurn = True

onePlayer = True

Dim i As Integer
For i = 0 To 8
taken(i) = False
Next i
xTurn = True

Call XPlay

End Sub


Private Function allFull() As Boolean
Dim i As Integer
For i = 0 To 8
If Picture1(i).Tag = 0 Then
allFull = False
Exit Function
End If
Next i
allFull = True
End Function
Private Sub compPlay(ByVal intPlayMode As Integer)
Select Case strLine
Case "XX-", "-XX", "X-X"
' Yipee! I'm going to win!
Case "OO-", "O-O", "-OO"
' Arghh I'm going to lose
Case '...

End Sub
Private Sub OPlay()
'first see if computer can win; if not, try to block human win
'If i can't win, you can't either'
'Delay computer move so it seems less robotic'
Dim t As Double
Dim Index As Integer
Dim Delay As Double
Dim i As Integer

t = Timer
Delay = Int(Rnd * 2#) + 1 'Random time delay to simulate thinking time.'
Do
DoEvents
Loop Until Timer - t# > Delay

Index = compPlayPosition
Select Case Index
Case -1 'This case is for when neither human nor computer can win

Dim choice As Integer
If Not allFull Then
Do
choice = Int(Rnd * 9)
Loop Until Not taken(choice)
End If
Picture1_Click (choice)
Case Else
Call Picture1_Click(Index)
End Select

xTurn = True

End Sub

Private Sub XPlay()
'first see if computer can win; if not, try to block human win
'If i can't win, you can't either'
'Delay computer move so it seems less robotic'
Dim t As Double
Dim Index As Integer
Dim Delay As Double
Dim i As Integer

t = Timer
Delay = Int(Rnd * 2#) + 1 'Random time delay to simulate thinking time.'
Do
DoEvents
Loop Until Timer - t# > Delay

Index = compPlayPosition
Select Case Index
Case -1 'This case is for when neither human nor computer can win

Dim choice As Integer
If Not allFull Then
Do
choice = Int(Rnd * 9)
Loop Until Not taken(choice)
End If

End Select

xTurn = True

End Sub



Private Function compPlayPosition() As Integer

'this function returns the potential win position for the computer, O
Dim BestPos As Integer
BestPos = WinPositionForPlayer(1) ' try to win
If BestPos > -1 Then
compPlayPosition = BestPos
Else
compPlayPosition = WinPositionForPlayer(2)
End If
End Function

Private Function WinPositionForPlayer(i_Player As Integer) As Integer
Dim j As Integer
j = i_Player
'this function returns the potential win position for the player i_Player

If Picture1(0).Tag = 0 And (Picture1(1).Tag = j And Picture1(2).Tag = j _
Or Picture1(4).Tag = j And Picture1(8).Tag = j) Or Picture1(3).Tag = j And Picture1(6).Tag = j Then
WinPositionForPlayer = 0
ElseIf Picture1(1).Tag = 0 And (Picture1(0).Tag = j And Picture1(2).Tag = j _
Or Picture1(4).Tag = j And Picture1(7).Tag = j) Then
WinPositionForPlayer = 1
ElseIf Picture1(2).Tag = 0 And (Picture1(0).Tag = j And Picture1(1).Tag = j _
Or Picture1(6).Tag = j And Picture1(4).Tag = j) Or Picture1(5).Tag = j And Picture1(8).Tag = j Then
WinPositionForPlayer = 2
ElseIf Picture1(3).Tag = 0 And (Picture1(0).Tag = j And Picture1(6).Tag = j _
Or Picture1(4).Tag = j And Picture1(5).Tag = j) Then
WinPositionForPlayer = 3
ElseIf Picture1(4).Tag = 0 And (Picture1(0).Tag = j And Picture1(8).Tag = j _
Or Picture1(6).Tag = j And Picture1(2).Tag = j Or Picture1(1).Tag = j And Picture1(7).Tag = j _
Or Picture1(3).Tag = j And Picture1(5).Tag = j) Then
WinPositionForPlayer = 4
ElseIf Picture1(5).Tag = 0 And (Picture1(2).Tag = j And Picture1(8).Tag = j _
Or Picture1(3).Tag = j And Picture1(4).Tag = j) Then
WinPositionForPlayer = 5
ElseIf Picture1(6).Tag = 0 And (Picture1(0).Tag = j And Picture1(3).Tag = j _
Or Picture1(4).Tag = j And Picture1(2).Tag = j Or Picture1(7).Tag = j And Picture1(8).Tag = j) Then
WinPositionForPlayer = 6
ElseIf Picture1(7).Tag = 0 And (Picture1(1).Tag = j And Picture1(4).Tag = j _
Or Picture1(6).Tag = j And Picture1(8).Tag = j) Then
WinPositionForPlayer = 7
ElseIf Picture1(8).Tag = 0 And (Picture1(0).Tag = j And Picture1(4).Tag = j _
Or Picture1(2).Tag = j And Picture1(5).Tag = j Or Picture1(6).Tag = j And Picture1(7).Tag = j) Then
WinPositionForPlayer = 8
Else
'if no found win position for X
WinPositionForPlayer = -1
End If
End Function

'By Derrick Boyd'










privacy (GDPR)