tic tac toe

Keltus
12-20-2000, 07:15 PM
hello, I made this 1 player tic-tac-toe program and it doesn't seem to work quite right: After it says you win, the computer keeps going for an extra move

is there anyway to fix it? the code is below

Dim Turn As Integer
Sub NewGame()
For Index = 0 To 8
imgSquare(Index).Picture = imgBlank.Picture
Next
Turn = 0
End Sub
Sub Win()
MsgBox "You win!"
NewGame
End Sub
Sub Lose()
MsgBox "You lose!"
NewGame
End Sub
Private Sub imgSquare_Click(Index As Integer)
Randomize
If imgSquare(Index).Picture = None Then
imgSquare(Index).Picture = imgX.Picture
Turn = Turn + 1
If Turn < 5 Then
FindSquare:
Square = Int(9 * Rnd)
If imgSquare(Square).Picture = None Then
imgSquare(Square).Picture = imgO.Picture
Else
GoTo FindSquare
End If
End If
End If
If imgSquare(0).Picture = imgX.Picture And imgSquare(1).Picture = imgX.Picture And imgSquare(2).Picture = imgX.Picture Then Win
If imgSquare(3).Picture = imgX.Picture And imgSquare(4).Picture = imgX.Picture And imgSquare(5).Picture = imgX.Picture Then Win
If imgSquare(6).Picture = imgX.Picture And imgSquare(7).Picture = imgX.Picture And imgSquare(8).Picture = imgX.Picture Then Win
If imgSquare(0).Picture = imgX.Picture And imgSquare(4).Picture = imgX.Picture And imgSquare(8).Picture = imgX.Picture Then Win
If imgSquare(2).Picture = imgX.Picture And imgSquare(4).Picture = imgX.Picture And imgSquare(6).Picture = imgX.Picture Then Win
If imgSquare(0).Picture = imgX.Picture And imgSquare(3).Picture = imgX.Picture And imgSquare(6).Picture = imgX.Picture Then Win
If imgSquare(1).Picture = imgX.Picture And imgSquare(4).Picture = imgX.Picture And imgSquare(7).Picture = imgX.Picture Then Win
If imgSquare(2).Picture = imgX.Picture And imgSquare(5).Picture = imgX.Picture And imgSquare(8).Picture = imgX.Picture Then Win
If imgSquare(0).Picture = imgO.Picture And imgSquare(1).Picture = imgO.Picture And imgSquare(2).Picture = imgO.Picture Then Lose
If imgSquare(3).Picture = imgO.Picture And imgSquare(4).Picture = imgO.Picture And imgSquare(5).Picture = imgO.Picture Then Lose
If imgSquare(6).Picture = imgO.Picture And imgSquare(7).Picture = imgO.Picture And imgSquare(8).Picture = imgO.Picture Then Lose
If imgSquare(0).Picture = imgO.Picture And imgSquare(4).Picture = imgO.Picture And imgSquare(8).Picture = imgO.Picture Then Lose
If imgSquare(2).Picture = imgO.Picture And imgSquare(4).Picture = imgO.Picture And imgSquare(6).Picture = imgO.Picture Then Lose
If imgSquare(0).Picture = imgO.Picture And imgSquare(3).Picture = imgO.Picture And imgSquare(6).Picture = imgO.Picture Then Lose
If imgSquare(1).Picture = imgO.Picture And imgSquare(4).Picture = imgO.Picture And imgSquare(7).Picture = imgO.Picture Then Lose
If imgSquare(2).Picture = imgO.Picture And imgSquare(5).Picture = imgO.Picture And imgSquare(8).Picture = imgO.Picture Then Lose
If Turn = 5 Then
MsgBox "Game is drawn"
NewGame
End If
End Sub

Private Sub mnuNewGame_Click()
NewGame
End Sub

Private Sub mnuQuit_Click()
End
End Sub


thanks

MichaelParks
12-20-2000, 09:27 PM
You have the code right. Just move the IF statements to before the computer takes it's turn.


Dim Turn As Integer
Sub NewGame()
For Index = 0 To 8
ImgSquare(Index).Picture = ImgBlank.Picture
Next
Turn = 0
End Sub
Sub Win()
MsgBox "You win!"
NewGame
End Sub
Sub Lose()
MsgBox "You lose!"
NewGame
End Sub
Private Sub imgSquare_Click(Index As Integer)
Randomize

If ImgSquare(Index).Picture = None Then
ImgSquare(Index).Picture = ImgX.Picture
Turn = Turn + 1
If Turn < 5 Then
FindSquare:
If ImgSquare(0).Picture = ImgX.Picture And ImgSquare(1).Picture = ImgX.Picture And ImgSquare(2).Picture = ImgX.Picture Then Win: Exit Sub
If ImgSquare(3).Picture = ImgX.Picture And ImgSquare(4).Picture = ImgX.Picture And ImgSquare(5).Picture = ImgX.Picture Then Win: Exit Sub
If ImgSquare(6).Picture = ImgX.Picture And ImgSquare(7).Picture = ImgX.Picture And ImgSquare(8).Picture = ImgX.Picture Then Win: Exit Sub
If ImgSquare(0).Picture = ImgX.Picture And ImgSquare(4).Picture = ImgX.Picture And ImgSquare(8).Picture = ImgX.Picture Then Win: Exit Sub
If ImgSquare(2).Picture = ImgX.Picture And ImgSquare(4).Picture = ImgX.Picture And ImgSquare(6).Picture = ImgX.Picture Then Win: Exit Sub
If ImgSquare(0).Picture = ImgX.Picture And ImgSquare(3).Picture = ImgX.Picture And ImgSquare(6).Picture = ImgX.Picture Then Win: Exit Sub
If ImgSquare(1).Picture = ImgX.Picture And ImgSquare(4).Picture = ImgX.Picture And ImgSquare(7).Picture = ImgX.Picture Then Win: Exit Sub
If ImgSquare(2).Picture = ImgX.Picture And ImgSquare(5).Picture = ImgX.Picture And ImgSquare(8).Picture = ImgX.Picture Then Win: Exit Sub
If ImgSquare(0).Picture = ImgO.Picture And ImgSquare(1).Picture = ImgO.Picture And ImgSquare(2).Picture = ImgO.Picture Then Lose: Exit Sub
If ImgSquare(3).Picture = ImgO.Picture And ImgSquare(4).Picture = ImgO.Picture And ImgSquare(5).Picture = ImgO.Picture Then Lose: Exit Sub
If ImgSquare(6).Picture = ImgO.Picture And ImgSquare(7).Picture = ImgO.Picture And ImgSquare(8).Picture = ImgO.Picture Then Lose: Exit Sub
If ImgSquare(0).Picture = ImgO.Picture And ImgSquare(4).Picture = ImgO.Picture And ImgSquare(8).Picture = ImgO.Picture Then Lose: Exit Sub
If ImgSquare(2).Picture = ImgO.Picture And ImgSquare(4).Picture = ImgO.Picture And ImgSquare(6).Picture = ImgO.Picture Then Lose: Exit Sub
If ImgSquare(0).Picture = ImgO.Picture And ImgSquare(3).Picture = ImgO.Picture And ImgSquare(6).Picture = ImgO.Picture Then Lose: Exit Sub
If ImgSquare(1).Picture = ImgO.Picture And ImgSquare(4).Picture = ImgO.Picture And ImgSquare(7).Picture = ImgO.Picture Then Lose: Exit Sub
If ImgSquare(2).Picture = ImgO.Picture And ImgSquare(5).Picture = ImgO.Picture And ImgSquare(8).Picture = ImgO.Picture Then Lose: Exit Sub
If Turn = 5 Then
MsgBox "Game is drawn"
NewGame
End If
Square = Int(9 * Rnd)
If ImgSquare(Square).Picture = None Then
ImgSquare(Square).Picture = ImgO.Picture
Else
GoTo FindSquare
End If
End If
End If
End Sub

Private Sub mnuNewGame_Click()
NewGame
End Sub

Private Sub mnuQuit_Click()
End
End Sub

Keltus
12-20-2000, 09:47 PM
I see,

but now the draw game thing doesn't load up<P ID="edit"><FONT SIZE=-1><EM>Edited by Keltus on 12/20/00 10:53 PM (server time).</EM></FONT></P>

MichaelParks
12-20-2000, 10:26 PM
It was working for me, make sure you have the codes where they are supposed to be.

Keltus
12-21-2000, 09:48 AM
I got it to work :) thanks

Dim Turn As Integer
Sub NewGame()
For Index = 0 To 8
imgSquare(Index).Picture = imgBlank.Picture
Next
Turn = 0
End Sub
Sub Win()
MsgBox "You win!"
NewGame
End Sub
Sub Lose()
MsgBox "You lose!"
NewGame
End Sub
Private Sub imgSquare_Click(Index As Integer)
Randomize

If imgSquare(Index).Picture = None Then
imgSquare(Index).Picture = imgX.Picture
Turn = Turn + 1
If imgSquare(0).Picture = imgX.Picture And imgSquare(1).Picture = imgX.Picture And imgSquare(2).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(3).Picture = imgX.Picture And imgSquare(4).Picture = imgX.Picture And imgSquare(5).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(6).Picture = imgX.Picture And imgSquare(7).Picture = imgX.Picture And imgSquare(8).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(0).Picture = imgX.Picture And imgSquare(4).Picture = imgX.Picture And imgSquare(8).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(2).Picture = imgX.Picture And imgSquare(4).Picture = imgX.Picture And imgSquare(6).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(0).Picture = imgX.Picture And imgSquare(3).Picture = imgX.Picture And imgSquare(6).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(1).Picture = imgX.Picture And imgSquare(4).Picture = imgX.Picture And imgSquare(7).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(2).Picture = imgX.Picture And imgSquare(5).Picture = imgX.Picture And imgSquare(8).Picture = imgX.Picture Then Win: Exit Sub
If Turn < 5 Then
FindSquare:
Square = Int(9 * Rnd)
If imgSquare(Square).Picture = None Then
imgSquare(Square).Picture = imgO.Picture
Else
GoTo FindSquare
End If
If imgSquare(0).Picture = imgO.Picture And imgSquare(1).Picture = imgO.Picture And imgSquare(2).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(3).Picture = imgO.Picture And imgSquare(4).Picture = imgO.Picture And imgSquare(5).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(6).Picture = imgO.Picture And imgSquare(7).Picture = imgO.Picture And imgSquare(8).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(0).Picture = imgO.Picture And imgSquare(4).Picture = imgO.Picture And imgSquare(8).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(2).Picture = imgO.Picture And imgSquare(4).Picture = imgO.Picture And imgSquare(6).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(0).Picture = imgO.Picture And imgSquare(3).Picture = imgO.Picture And imgSquare(6).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(1).Picture = imgO.Picture And imgSquare(4).Picture = imgO.Picture And imgSquare(7).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(2).Picture = imgO.Picture And imgSquare(5).Picture = imgO.Picture And imgSquare(8).Picture = imgO.Picture Then Lose: Exit Sub
End If
End If
If Turn = 5 Then
MsgBox "Game is drawn"
NewGame
End If
End Sub

Private Sub mnuNewGame_Click()
NewGame
End Sub

Private Sub mnuQuit_Click()
Unload frmTicTacToe
End Sub

vanyaka
02-04-2001, 12:24 PM
But then.....
the odds that computer will win are almost zero!
there is no AI :(

Keltus
02-04-2001, 12:49 PM
if you make em too smart, it's no fun cause it's always a draw\

Windows Error 015: Unable to exit Windows. Try the door.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum