Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old February 7th, 2003, 04:00 AM
Black Death's Avatar
Black Death Black Death is offline
Bringer Of Death
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 Black Death User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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'
Comments on this post
Gran Roguismo agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Tic Tac Toe


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway