|
#1
|
||||
|
||||
|
Visual Basic 5.0 CCE
Hi, i need help in programming in visual basic 5.0 CCE. The problem i am having is in a game when a computer makes a move. My aim is to make the computer have random moves then if a certain move is made the move is auto countered. The name of the game is Tic-Tac-Toe. So far i have no idea on how to start the code to make a random move. All help is appreciated.
|
|
#2
|
||||
|
||||
|
Pseudo code:
-Pick random number between 1 and 9 -If player picked square 1, make move in square 2 -Else of player picked square 2, make move in square 1 -Etc However I think you'll find that Tic-Tac-Toe AI isn't random or auto-countering at all. There is in fact a standard pattern of moves that should initially be made, followed by a focus on preventing a win by the human player - or, if this isn't necessary, continue following the attack pattern. If you code the AI right, it won't be possible to beat it. See also here and here.
__________________
Oracle's backup tutorial "A lot of people say games are addictive. Well, they're addictive in the sense that anything you like doing you repeat endlessly. But no one would say, 'Mr Kasparov, you have a chess problem,' or 'Tiger Woods, you have a golf addiction.'" - Ian Livingstone, Creative Director, Eidos. "A problem well stated is a problem half solved" - Charles Franklin Kettering |
|
#3
|
||||
|
||||
|
thx for your help, but in ur opinion which is easier:
graphical or text based. Because in my opinion image based is harder compared to text since most of the tutorials are text. Also could you give me examples of the Goto Event Eg. If player picked square 1 then goto CounterMove CounterMove: If square 1 = X then square 2 = O Endif and thats all i get up to, then when i play this error comes up. Also if its not asking too much can i also get an example of the Or Event E.g If square 1 = X then square 2 or 3 or 4 or 5 or 7 or 9 = O Endif then another error occurs when i press play. |
|
#4
|
||||
|
||||
|
Quote:
Quote:
Code:
If x=1 Then Goto ONE Else Goto NOTONE End If ONE: msgbox "One" Goto END NOTONE: msgbox "Not One" END: msgbox "Finished" Quote:
Code:
If x=1 Or x=2 Or x=3 Then msgbox "One, two or three" End If
__________________
Oracle's backup tutorial "A lot of people say games are addictive. Well, they're addictive in the sense that anything you like doing you repeat endlessly. But no one would say, 'Mr Kasparov, you have a chess problem,' or 'Tiger Woods, you have a golf addiction.'" - Ian Livingstone, Creative Director, Eidos. "A problem well stated is a problem half solved" - Charles Franklin Kettering Last edited by oracle128; August 30th, 2007 at 03:28 PM. |
|
#5
|
||||
|
||||
|
Umm not quite the Or statement i was looking for, could you possibly make one that has the properties in with it,
eg imgBox(Index).Picture |
|
#6
|
||||
|
||||
|
Quote:
If <condition1> Or <condition2> Or <condition3> Then <Do this if true> Else <Do this if false> End If
__________________
Oracle's backup tutorial "A lot of people say games are addictive. Well, they're addictive in the sense that anything you like doing you repeat endlessly. But no one would say, 'Mr Kasparov, you have a chess problem,' or 'Tiger Woods, you have a golf addiction.'" - Ian Livingstone, Creative Director, Eidos. "A problem well stated is a problem half solved" - Charles Franklin Kettering |
|
#7
|
||||
|
||||
|
Thanks for the example but now i am having trouble with the program and i'm not sure how to fix it, i've gone through the step into process alot of times and i cannot figure out how to fix the problem. The problem is that when i play the game and then press a square the msgbox which is programmed pops up saying game is a draw when only one box has been picked. I gave programmed the Msgbox to pop up only when all squares are filled up when no X or O match up a line of 3.
If you would like to see the problem pm me and i'll send you the game. All help appreciated. |
|
#8
|
||||
|
||||
|
Just post the code for checking a win on the forum, in [code][/code] tags.
__________________
Oracle's backup tutorial "A lot of people say games are addictive. Well, they're addictive in the sense that anything you like doing you repeat endlessly. But no one would say, 'Mr Kasparov, you have a chess problem,' or 'Tiger Woods, you have a golf addiction.'" - Ian Livingstone, Creative Director, Eidos. "A problem well stated is a problem half solved" - Charles Franklin Kettering |
|
#9
|
||||
|
||||
|
Game Code
Ok i'll post the whole game code, atm i am having trouble, when i click a square it instantly goes X or O wins and im not sure how to fix this.
Option Explicit Dim mStartSquare As Integer, mEndSquare As Integer, mDirection As Integer Dim mXO(2) As String Dim mBlank As Integer Dim mCounter As Integer Dim mXsInARow As Integer, mOsInARow As Integer Dim mHumanPlayingAsX As Boolean Dim mHumanMovedAlready As Boolean, mCompMovedAlready As Boolean Dim mGameOver As Boolean Dim mSquareValue(8) As Integer Dim mRndChoice(8) As Integer Dim mWin(8) As Integer Dim mTie As Boolean Dim mPlayer As String Dim mCPU As String Dim mEmpty As Boolean Private Sub cmdStart_Click() mCPU = imgO.Picture mPlayer = imgX.Picture If mHumanMovedAlready = True Then Cls Call ComputerTurn Else mHumanMovedAlready = False End If End Sub Private Sub imgBox_Click(Index As Integer) Dim mLoop As Integer If imgBox(Index).Picture <> imgX.Picture And imgBox(Index).Picture <> imgO.Picture Then mEmpty = True If mEmpty = True Then imgBox(Index).Picture = imgX.Picture End If Call CheckForWin If mGameOver = False Then Call ComputerTurn For mLoop = 0 To 8 If imgBox(mLoop).Picture <> imgO.Picture And imgBox(mLoop).Picture <> imgX.Picture Then GoTo SkipTieCheck Next mLoop Call CheckForTieGame SkipTieCheck: End If Else MsgBox "Square has already been occupied", vbOKOnly, "Move cannot be made" End If End Sub Private Sub mnuExit_Click() End End Sub Private Sub mnuReset_Click() mnuReset.Enabled = False Form_Load End Sub Private Sub XWins() Call GameOver MsgBox ("X wins!") ' 3 Lines of X ' Horizontal 0,1,2 If imgBox(0).Picture And imgBox(1).Picture And imgBox(2).Picture = imgX.Picture Then imgBox(0).Picture = imgH.Picture imgBox(1).Picture = imgH.Picture imgBox(2).Picture = imgH.Picture End If ' 3,4,5 If imgBox(3).Picture And imgBox(4).Picture And imgBox(5).Picture = imgX.Picture Then imgBox(3).Picture = imgH.Picture imgBox(4).Picture = imgH.Picture imgBox(5).Picture = imgH.Picture End If ' 6,7,8 If imgBox(6).Picture And imgBox(7).Picture And imgBox(8).Picture = imgX.Picture Then imgBox(6).Picture = imgH.Picture imgBox(7).Picture = imgH.Picture imgBox(8).Picture = imgH.Picture End If ' Vertical 0,3,6 If imgBox(0).Picture And imgBox(3).Picture And imgBox(6).Picture = imgX.Picture Then imgBox(0).Picture = imgV.Picture imgBox(3).Picture = imgV.Picture imgBox(6).Picture = imgV.Picture End If ' 1,4,7 If imgBox(1).Picture And imgBox(4).Picture And imgBox(7).Picture = imgX.Picture Then imgBox(1).Picture = imgV.Picture imgBox(4).Picture = imgV.Picture imgBox(7).Picture = imgV.Picture End If ' 3,5,8 If imgBox(3).Picture And imgBox(5).Picture And imgBox(8).Picture = imgX.Picture Then imgBox(3).Picture = imgV.Picture imgBox(5).Picture = imgV.Picture imgBox(8).Picture = imgV.Picture End If ' Diagonal Left 0,4,8 If imgBox(0).Picture And imgBox(4).Picture And imgBox(8).Picture = imgX.Picture Then imgBox(0).Picture = imgDL.Picture imgBox(4).Picture = imgDL.Picture imgBox(8).Picture = imgDL.Picture End If ' Diagonal Right 2,4,6 If imgBox(2).Picture And imgBox(4).Picture And imgBox(6).Picture = imgX.Picture Then imgBox(2).Picture = imgDR.Picture imgBox(4).Picture = imgDR.Picture imgBox(6).Picture = imgDR.Picture End If End Sub Private Sub OWins() Call GameIsOver MsgBox ("O Wins!") ' 3 lines of O ' Horizontal 0,1,2 If imgBox(0).Picture And imgBox(1).Picture And imgBox(2).Picture = imgO.Picture Then imgBox(0).Picture = imgH.Picture imgBox(1).Picture = imgH.Picture imgBox(2).Picture = imgH.Picture End If ' 3,4,5 If imgBox(3).Picture And imgBox(4).Picture And imgBox(5).Picture = imgO.Picture Then imgBox(3).Picture = imgH.Picture imgBox(4).Picture = imgH.Picture imgBox(5).Picture = imgH.Picture End If ' 6,7,8 If imgBox(6).Picture And imgBox(7).Picture And imgBox(8).Picture = imgO.Picture Then imgBox(6).Picture = imgH.Picture imgBox(7).Picture = imgH.Picture imgBox(8).Picture = imgH.Picture End If ' Vertical 0,3,6 If imgBox(0).Picture And imgBox(3).Picture And imgBox(6).Picture = imgO.Picture Then imgBox(0).Picture = imgV.Picture imgBox(3).Picture = imgV.Picture imgBox(6).Picture = imgV.Picture End If ' 1,4,7 If imgBox(1).Picture And imgBox(4).Picture And imgBox(7).Picture = imgO.Picture Then imgBox(1).Picture = imgV.Picture imgBox(4).Picture = imgV.Picture imgBox(7).Picture = imgV.Picture End If ' 3,5,8 If imgBox(3).Picture And imgBox(5).Picture And imgBox(8).Picture = imgO.Picture Then imgBox(3).Picture = imgV.Picture imgBox(5).Picture = imgV.Picture imgBox(8).Picture = imgV.Picture End If ' Diagonal Left 0,4,8 If imgBox(0).Picture And imgBox(4).Picture And imgBox(8).Picture = imgO.Picture Then imgBox(0).Picture = imgDL.Picture imgBox(4).Picture = imgDL.Picture imgBox(8).Picture = imgDL.Picture End If ' Diagonal Right 2,4,6 If imgBox(2).Picture And imgBox(4).Picture And imgBox(6).Picture = imgO.Picture Then imgBox(2).Picture = imgDR.Picture imgBox(4).Picture = imgDR.Picture imgBox(6).Picture = imgDR.Picture End If End Sub Private Sub Draw() mTie = True Call GameOver MsgBox ("Game is a draw!") End Sub Private Sub GameOver() mGameOver = True lblText.Caption = "Play Again, Press Start If So?" Call cmdStart_Click End Sub Private Sub CheckForTieGame() Dim mIndex As Integer Dim mSpace As Integer For mIndex = 0 To 8 If imgBox(mIndex).Picture = imgBlank.Picture Then mSpace = mSpace + 1 Next mIndex If mSpace = 0 Then Call Draw End If End Sub Private Sub CompSquareSelection() Dim mIndex As Integer Dim mHighVal As Integer Dim mCounter As Integer Dim mFinalChoice As Integer mCounter = 0 mHighVal = 0 For mIndex = 0 To 8 If mSquareValue(mIndex) = mHighVal Then mRndChoice(mCounter) = mIndex mCounter = mCounter + 1 End If If mSquareValue(mIndex) > mHighVal Then mHighVal = mSquareValue(mIndex) For mCounter = 1 To 8 mRndChoice(mCounter) = 0 Next mCounter mRndChoice(0) = mIndex mCounter = 1 End If Next mIndex mIndex = Int(Rnd * mCounter) imgBox(mRndChoice(mIndex)).Picture = imgO.Picture mHumanMovedAlready = False End Sub Private Sub CheckForWin() ' 3 lines of X ' Horizontal 0,1,2 If imgBox(0).Picture And imgBox(1).Picture And imgBox(2).Picture = imgX.Picture Then Call XWins End If ' 3,4,5 If imgBox(3).Picture And imgBox(4).Picture And imgBox(5).Picture = imgX.Picture Then Call XWins End If ' 6,7,8 If imgBox(6).Picture And imgBox(7).Picture And imgBox(8).Picture = imgX.Picture Then Call XWins End If ' Vertical 0,3,6 If imgBox(0).Picture And imgBox(3).Picture And imgBox(6).Picture = imgX.Picture Then Call XWins End If ' 1,4,7 If imgBox(1).Picture And imgBox(4).Picture And imgBox(7).Picture = imgX.Picture Then Call XWins End If ' 3,5,8 If imgBox(3).Picture And imgBox(5).Picture And imgBox(8).Picture = imgX.Picture Then Call XWins End If ' Diagonal Left 0,4,8 If imgBox(0).Picture And imgBox(4).Picture And imgBox(8).Picture = imgX.Picture Then Call XWins End If ' Diagonal Right 2,4,6 If imgBox(2).Picture And imgBox(4).Picture And imgBox(6).Picture = imgX.Picture Then Call XWins End If ' 3 lines of O ' Horizontal 0,1,2 If imgBox(0).Picture And imgBox(1).Picture And imgBox(2).Picture = imgO.Picture Then Call OWins End If ' 3,4,5 If imgBox(3).Picture And imgBox(4).Picture And imgBox(5).Picture = imgO.Picture Then Call OWins End If ' 6,7,8 If imgBox(6).Picture And imgBox(7).Picture And imgBox(8).Picture = imgO.Picture Then Call OWins End If ' Vertical 0,3,6 If imgBox(0).Picture And imgBox(3).Picture And imgBox(6).Picture = imgO.Picture Then Call OWins End If ' 1,4,7 If imgBox(1).Picture And imgBox(4).Picture And imgBox(7).Picture = imgO.Picture Then Call OWins End If ' 3,5,8 If imgBox(3).Picture And imgBox(5).Picture And imgBox(8).Picture = imgO.Picture Then Call OWins End If ' Diagonal Left 0,4,8 If imgBox(0).Picture And imgBox(4).Picture And imgBox(8).Picture = imgO.Picture Then Call OWins End If ' Diagonal Right 2,4,6 If imgBox(2).Picture And imgBox(4).Picture And imgBox(6).Picture = imgO.Picture Then Call OWins End If End Sub Private Sub ComputerTurn() Dim mLoop As Integer If mGameOver = False Then Call CompAI Call CompSquareSelection Call CheckForWin For mLoop = 0 To 8 If imgBox(mLoop).Picture <> imgO.Picture And imgBox(mLoop).Picture <> imgX.Picture Then GoTo SkipTieCheck Next mLoop Call CheckForTieGame SkipTieCheck: End If End Sub Private Sub CompAI() Dim mXOSquare As Integer Dim mIndex As Integer Dim mSpace As Integer ' vertical Row 0,1,2 If imgBox(0).Picture = imgX.Picture And imgBox(1).Picture = imgX.Picture Then imgO.Picture = imgBox(2).Picture End If If imgBox(0).Picture = imgX.Picture And imgBox(2).Picture = imgX.Picture Then imgO.Picture = imgBox(1).Picture End If If imgBox(1).Picture = imgX.Picture And imgBox(2).Picture = imgX.Picture Then imgO.Picture = imgBox(0).Picture End If ' Row 3,4,5 If imgBox(3).Picture = imgX.Picture And imgBox(4).Picture = imgX.Picture Then imgO.Picture = imgBox(5).Picture End If If imgBox(4).Picture = imgX.Picture And imgBox(5).Picture = imgX.Picture Then imgO.Picture = imgBox(6).Picture End If If imgBox(3).Picture = imgX.Picture And imgBox(5).Picture = imgX.Picture Then imgO.Picture = imgBox(4).Picture End If ' Row 6,7,8 If imgBox(6).Picture = imgX.Picture And imgBox(7).Picture = imgX.Picture Then imgO.Picture = imgBox(8).Picture End If If imgBox(6).Picture = imgX.Picture And imgBox(8).Picture = imgX.Picture Then imgO.Picture = imgBox(7).Picture End If If imgBox(7).Picture = imgX.Picture And imgBox(8).Picture = imgX.Picture Then imgO.Picture = imgBox(6).Picture End If ' horizontal Row 0,3,6 If imgBox(0).Picture = imgX.Picture And imgBox(3).Picture = imgX.Picture Then imgO.Picture = imgBox(6).Picture End If If imgBox(3).Picture = imgX.Picture And imgBox(6).Picture = imgX.Picture Then imgO.Picture = imgBox(0).Picture End If If imgBox(0).Picture = imgX.Picture And imgBox(6).Picture = imgX.Picture Then imgO.Picture = imgBox(3).Picture End If ' Row 1,4,7 If imgBox(1).Picture = imgX.Picture And imgBox(4).Picture = imgX.Picture Then imgO.Picture = imgBox(7).Picture End If If imgBox(1).Picture = imgX.Picture And imgBox(7).Picture = imgX.Picture Then imgO.Picture = imgBox(4).Picture End If If imgBox(4).Picture = imgX.Picture And imgBox(7).Picture = imgX.Picture Then imgO.Picture = imgBox(1).Picture End If ' Row 2,5,8 If imgBox(2).Picture = imgX.Picture And imgBox(5).Picture = imgX.Picture Then imgO.Picture = imgBox(8).Picture End If If imgBox(2).Picture = imgX.Picture And imgBox(8).Picture = imgX.Picture Then imgO.Picture = imgBox(5).Picture End If If imgBox(5).Picture = imgX.Picture And imgBox(8).Picture = imgX.Picture Then imgO.Picture = imgBox(2).Picture End If ' diagonal left If imgBox(0).Picture = imgX.Picture And imgBox(4).Picture = imgX.Picture Then imgO.Picture = imgBox(8).Picture End If If imgBox(4).Picture = imgX.Picture And imgBox(8).Picture = imgX.Picture Then imgO.Picture = imgBox(0).Picture End If If imgBox(0).Picture = imgX.Picture And imgBox(8).Picture = imgX.Picture Then imgO.Picture = imgBox(4).Picture End If 'diagonal right If imgBox(2).Picture = imgX.Picture And imgBox(4).Picture = imgX.Picture Then imgO.Picture = imgBox(6).Picture End If If imgBox(4).Picture = imgX.Picture And imgBox(6).Picture = imgX.Picture Then imgO.Picture = imgBox(0).Picture End If If imgBox(2).Picture = imgX.Picture And imgBox(6).Picture = imgX.Picture Then imgO.Picture = imgBox(4).Picture End If End If End Sub |
|
#10
|
||||
|
||||
|
Code:
Private Sub CheckForWin() ' 3 lines of X ' Horizontal 0,1,2 If imgBox(0).Picture And imgBox(1).Picture And imgBox(2).Picture = imgX.Picture Then Call XWins End If Code:
If <condition1> Or <condition2> Or <condition3> Then Code:
If imgBox(0).Picture And imgBox(1).Picture And imgBox(2).Picture = imgX.Picture Then Is imgBox(0).Picture true? And, is imgBox(1).Picture true? And, is imgBox(2).Picture equal to imgX.Picture? The first 2 conditions will always be true. imgBox(0).Picture is always true. imgBox(1).Picture is always true. imgBox(2).Picture = imgX.Picture will only be true if there's an X in it. But since you have each possible combination covered, in both X and O, it's always going to result to a win first move.
__________________
Oracle's backup tutorial "A lot of people say games are addictive. Well, they're addictive in the sense that anything you like doing you repeat endlessly. But no one would say, 'Mr Kasparov, you have a chess problem,' or 'Tiger Woods, you have a golf addiction.'" - Ian Livingstone, Creative Director, Eidos. "A problem well stated is a problem half solved" - Charles Franklin Kettering |
|
#11
|
||||
|
||||
|
Thanks alot Oracle i was struggling alot on that, but now the coding is going to get complicated because im not really sure what property to use that has a boolean result.
The only one i know of is enabled, i'll try it out and see what happens. btw thanks again. |
|
#12
|
||||
|
||||
|
Why not try this?
Code:
If imgBox(0).Picture = imgX.Picture And imgBox(1).Picture = imgX.Picture And imgBox(2).Picture = imgX.Picture Then
__________________
Oracle's backup tutorial "A lot of people say games are addictive. Well, they're addictive in the sense that anything you like doing you repeat endlessly. But no one would say, 'Mr Kasparov, you have a chess problem,' or 'Tiger Woods, you have a golf addiction.'" - Ian Livingstone, Creative Director, Eidos. "A problem well stated is a problem half solved" - Charles Franklin Kettering |
|
#13
|
||||
|
||||
|
Could you help me load a help file, i'm not sure how to start the code on the help file.
By the way what format does the help file need to be to load up in Visual Basic 5.0 CCE. |
|
#14
|
||||
|
||||
|
You can make CHM with something like this.
I don't how how VB5 CCE can include a help file, or even if it can since it's meant for ActiveX components. If you have to include a help file you should ask the teacher how to do it.
__________________
Oracle's backup tutorial "A lot of people say games are addictive. Well, they're addictive in the sense that anything you like doing you repeat endlessly. But no one would say, 'Mr Kasparov, you have a chess problem,' or 'Tiger Woods, you have a golf addiction.'" - Ian Livingstone, Creative Director, Eidos. "A problem well stated is a problem half solved" - Charles Franklin Kettering |
|
#15
|
||||
|
||||
|
Oh i've also been struggling with this win bug. When i did a trial on the game it worked perfectly when it was a tie, but when X or O won and there was a spare square left i was still allowed to pick that empty square. Below is evidence of this.
-->![]() ![]() I've tried adding in the code Code:
If X wins then lblSq(index).enabled = false Code:
If X wins then lblSq(0).enabled and lblSq(1).enabled = false and lblSq(2).enabled = false and lblSq(3).enabled = false and lblSq(4).enabled = false and lblSq(5).enabled = false and lblSq(6).enabled = false and lblSq(7).enabled = false and lblSq(8).enabled = false And my code of the whole game is below Code:
Public mChance As Integer
Public mLineNo As Integer
Dim mCompMove As Integer
Private Function Opening_move()
If lblSq(4).Caption <> "X" Then
lblSq(4).Caption = "O"
Else
lblSq(8).Caption = "O"
End If
mCompMove = mCompMove + 1
End Function
Private Function Perform_Check(mCheck As String)
Dim mIndex As Integer
For mIndex = 0 To 6 Step 3
If lblSq(mIndex).Caption = mCheck And lblSq(mIndex + 1).Caption = mCheck And lblSq(mIndex + 2).Caption = "" Then
lblSq(mIndex + 2).Caption = "O"
mCompMove = mCompMove + 1
Exit Function
End If
If lblSq(mIndex).Caption = mCheck And lblSq(mIndex + 1).Caption = "" And lblSq(mIndex + 2).Caption = mCheck Then
lblSq(mIndex + 1).Caption = "O"
mCompMove = mCompMove + 1
Exit Function
End If
If lblSq(mIndex).Caption = "" And lblSq(mIndex + 1).Caption = mCheck And lblSq(mIndex + 2).Caption = mCheck Then
lblSq(mIndex + 0).Caption = "O"
mCompMove = mCompMove + 1
Exit Function
End If
Next mIndex
For mIndex = 0 To 2 'checks vertically 3 times 3 coloumns
If lblSq(mIndex).Caption = mCheck And lblSq(mIndex + 3).Caption = mCheck And lblSq(mIndex + 6).Caption = "" Then
lblSq(mIndex + 6).Caption = "O"
mCompMove = mCompMove + 1
Exit Function
End If
If lblSq(mIndex).Caption = mCheck And lblSq(mIndex + 3).Caption = "" And lblSq(mIndex + 6).Caption = mCheck Then
lblSq(mIndex + 3).Caption = "O"
mCompMove = mCompMove + 1
Exit Function
End If
If lblSq(mIndex).Caption = "" And lblSq(mIndex + 3).Caption = mCheck And lblSq(mIndex + 6).Caption = mCheck Then
lblSq(mIndex + 0).Caption = "O"
mCompMove = mCompMove + 1
Exit Function
End If
Next mIndex
'check diagonal from upper right to lower left
If lblSq(2).Caption = ch And lblSq(4).Caption = mCheck And lblSq(6).Caption = "" Then
lblSq(6).Caption = "O"
mCompMove = mCompMove + 1
Exit Function
End If
'check diagonal from upper right to lower left, 2nd condition
If lblSq(2).Caption = "" And lblSq(4).Caption = mCheck And lblSq(6).Caption = mCheck Then
lblSq(2).Caption = "O"
mCompMove = mCompMove + 1
Exit Function
End If
'check diagonal from upper left to lower right
If lblSq(0).Caption = "" And lblSq(4).Caption = mCheck And lblSq(8).Caption = mCheck Then
lblSq(0).Caption = "O"
mCompMove = mCompMove + 1
Exit Function
End If
'check diagonal from lower right to upper left
If lblSq(8).Caption = "" And lblSq(4).Caption = mCheck And lblSq(0).Caption = mCheckh Then
lblSq(8).Caption = "O"
mCompMove = mCompMove + 1
Exit Function
End If
End Function
Private Function Perform_Win_Check(mCheckWin As String)
Dim mIndex As Integer
For mIndex = 0 To 6 Step 3 'horizontal check
If lblSq(mIndex).Caption = mCheckWin And lblSq(mIndex + 1).Caption = mCheckWin And lblSq(mIndex + 2).Caption = mCheckWin Then
linLine(mIndex / 3).Visible = True
mLineNo = mIndex / 3
If mCheckWin = "O" Then
MsgBox "Computer Wins, You lose"
Else
MsgBox "You win, Computer loses"
End If
End If
Next mIndex
For mIndex = 0 To 2 'vertical check
If lblSq(mIndex).Caption = mCheckWin And lblSq(mIndex + 3).Caption = mCheckWin And lblSq(mIndex + 6).Caption = mCheckWin Then
linLine(mIndex + 3).Visible = True
mLineNo = mIndex + 3
If mCheckWin = "O" Then
MsgBox "Computer Wins, You lose"
Else
MsgBox "You win, Computer loses"
End If
End If
Next mIndex
'two diagonal checks
If lblSq(0).Caption = mCheckWin And lblSq(4).Caption = mCheckWin And lblSq(8).Caption = mCheckWin Then
linLine(6).Visible = True
mLineNo = 6
If mCheckWin = "O" Then
MsgBox "Computer Wins, You lose"
Else
MsgBox "You win, Computer loses"
End If
End If
If lblSq(2).Caption = mCheckWin And lblSq(4).Caption = mCheckWin And lblSq(6).Caption = mCheckWin Then
linLine(7).Visible = True
mLineNo = 7
If mCheckWin = "O" Then
MsgBox "Computer Wins, You lose"
Else
MsgBox "You win, Computer loses"
End If
End If
End Function
Private Function Second_move()
Perform_Check ("X")
If mCompMove = 1 Then
If lblSq(5).Caption = "X" And lblSq(7).Caption = "X" Then
lblSq(8).Caption = "O"
mCompMove = mCompMove + 1
End If
If lblSq(0).Caption = "X" And lblSq(8).Caption = "X" Then
lblSq(1).Caption = "O"
mCompMove = mCompMove + 1
End If
If lblSq(2).Caption = "X" And lblSq(6).Caption = "X" Then
lblSq(1).Caption = "O"
mCompMove = mCompMove + 1
End If
If lblSq(0).Caption = "X" And lblSq(4).Caption = "X" Then
lblSq(6).Caption = "O"
mCompMove = mCompMove + 1
End If
If lblSq(0).Caption = "X" And lblSq(7).Caption = "X" Then
lblSq(6).Caption = "O"
mCompMove = mCompMove + 1
End If
If lblSq(2).Caption = "X" And lblSq(7).Caption = "X" Then
lblSq(8).Caption = "O"
mCompMove = mCompMove + 1
End If
If lblSq(5).Caption = "X" And lblSq(6).Caption = "X" Then
lblSq(8).Caption = "O"
mCompMove = mCompMove + 1
End If
If lblSq(3).Caption = "X" And lblSq(5).Caption = "X" Then
lblSq(0).Caption = "O"
mCompMove = mCompMove + 1
End If
If lblSq(1).Caption = "X" And lblSq(7).Caption = "X" Then
lblSq(0).Caption = "O"
mCompMove = mCompMove + 1
End If
End If
If mCompMove = 1 Then
Seek_Empty_Fill
End If
End Function
Private Function Seek_Empty_Fill()
Dim mIndex As Integer
For mIndex = 0 To 8
If lblSq(mIndex).Caption <> "X" And lblSq(mIndex).Caption <> "O" And flag = 0 Then
lblSq(mIndex).Caption = "O"
mCompMove = mCompMove + 1
Exit For
End If
Next mIndex
End Function
Private Function Third_move()
Perform_Check ("O")
If mCompMove = 2 Then
Perform_Check ("X")
End If
If lblSq(0).Caption = "O" And lblSq(4).Caption = "O" And lblSq(3).Caption = "" And mCompMove = 2 Then
lblSq(3).Caption = "O"
mCompMove = mCompMove + 1
End If
If lblSq(0).Caption = "X" And lblSq(7).Caption = "X" And lblSq(2).Caption = "" And mCompMove = 2 Then
lblSq(6).Caption = "O"
mCompMove = mCompMove + 1
End If
If mCompMove = 2 Then
Seek_Empty_Fill
End If
End Function
Private Function Forth_move()
Perform_Check ("O")
If mCompMove = 3 Then
Perform_Check ("X")
End If
If mCompMove = 3 Then
Seek_Empty_Fill
End If
End Function
Private Sub Form_Load()
frmTicTacToe.Show
frmLoadMenu.Hide
End Sub
Private Sub lblSq_Click(index As Integer)
If lblSq(index).Caption = "" Then
lblSq(index).Caption = "X"
If mCompMove = 4 Then
MsgBox "Looks like we have a draw"
End If
If mCompMove = 3 Then
Forth_move
End If
If mCompMove = 2 Then
Third_move
End If
If mCompMove = 1 Then
Second_move
End If
If mCompMove = 0 Then
Opening_move
End If
End If
Perform_Win_Check ("X")
Perform_Win_Check ("O")
End Sub
Private Sub mnuExit_Click()
End
End Sub
Private Sub mnuHelp_Click()
frmHelp.Show
End Sub
Private Sub mnuNewGame_Click()
Dim mIndex As Integer
mChance = mChance + 1
mCompMove = 0
linLine(mLineNo).Visible = False
lblSq(index).Enabled = True
For mIndex = 0 To 8
lblSq(mIndex).Caption = ""
Next mIndex
If mChance Mod 2 = 0 Then
Opening_move
End If
End Sub
__________________
http://www.speedtest.net/result/120148537.png http://i109.photobucket.com/albums/n.../gundamsig.png Last edited by johnz14; September 7th, 2007 at 03:32 AM. |
![]() |
| Bookmarks |
«
Previous Topic
|
Next Topic
»
| Topic Tools | |
|
|
All times are GMT +1. The time now is 06:26 PM.
[
RSS ]





-->





