Link to home
Start Free TrialLog in
Avatar of paulzeb
paulzeb

asked on

Problem with game creation

I am in the process of making a Tic-Tac-Toe based game.  This is the code I am using>

Private Sub Image1_Click(Index As Integer)
If Player = 1 Then
Image1(Index).Picture = O.Picture
Player = 2
Else
Image1(Index).Picture = X.Picture
Player = 1
End If
End Sub

This is the problem:
After a player makes a mark (an X or O) on a box, the other player can make a mark over it, erasing the mark that was already there.  How do I fix this?
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Hi, create an array that save whether the place had been marked by any player.

Something like

if arrayCheck(Index) = True then exit sub ..

'Hope wil help.
Then set arrayCheck(Index) = True after image's clicked
Try this no arrays to create

Private Sub Image1_Click(Index As Integer)

If Image1(Index).Tag = 1 then exit sub
If Player = 1 Then
    Image1(Index).Picture = O.Picture
    Image1(Index).Tag = 1
    Player = 2
  Else
    Image1(Index).Picture = X.Picture
    Image1(Index).Tag = 1
    Player = 1
End If

End Sub

When game is done

For a = 0 to Image1.Ubound
    Image1(Index).Tag = 0
Next a

Avatar of Microsoft
Microsoft

STORE IT IN A MULTI DIM ARRAY REPRESENTING THE GRID

cheers andy
Avatar of paulzeb

ASKER

Sorry guys, but all of the answers above did not work.  It just did the same thing it's doing now.
Hi, The code below ensure players cannot click the same place twice in a single game..

Private Sub Image1_Click(Index As Integer)
if arrayCheck(Index) = True Then Exit Sub
If Player = 1 Then
Image1(Index).Picture = O.Picture
Player = 2
Else
Image1(Index).Picture = X.Picture
Player = 1
End If
arrayCheck(Index) = True
End Sub

Private sub Form_load()
   for i = LBound(arrayCheck) to Ubound(arrayCheck)
       arrayCheck(i) = False
   Next i
End sub

Avatar of paulzeb

ASKER

I copyed and pasted this code.  The code in Form Load doesn't work.
Disable the image control after setting its picture:

Image1(Index).enabled = false


If Player = 1 Then
Image1(Index).Picture = O.Picture
Player = 2
Else
Image1(Index).Picture = X.Picture
Player = 1
End If
' add
Image1(Index).enabled = false
Hi Paul, Of course it it not working, we must define it first:

Option Explicit
Dim arrayCheck(8) as Boolean

..

>>>Sorry guys, but all of the answers above did not work.  It just did the same thing it's doing now

What error did you get using the .Tag property???
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paulzeb

ASKER

This code did exactly what I needed.  Thank you.
Hi Paul, glad can help you.