Solved

Problem with game creation

Posted on 2001-06-18
13
174 Views
Last Modified: 2010-05-02
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?
0
Comment
Question by:paulzeb
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6204998
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.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6205003
Then set arrayCheck(Index) = True after image's clicked
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6205013
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

0
 
LVL 2

Expert Comment

by:Microsoft
ID: 6205245
STORE IT IN A MULTI DIM ARRAY REPRESENTING THE GRID

cheers andy
0
 

Author Comment

by:paulzeb
ID: 6206595
Sorry guys, but all of the answers above did not work.  It just did the same thing it's doing now.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6206625
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

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:paulzeb
ID: 6206813
I copyed and pasted this code.  The code in Form Load doesn't work.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6206982
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
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6208815
Hi Paul, Of course it it not working, we must define it first:

Option Explicit
Dim arrayCheck(8) as Boolean

..
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6208822

>>>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???
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 50 total points
ID: 6208917
Hi guys, please allow me to post my complete code, thanks:

Dim arrayCheck(8) As Boolean
Dim i As Integer, Player As Byte

Private Sub Form_Load()
    For i = LBound(arrayCheck) To UBound(arrayCheck)
        arrayCheck(i) = False
    Next i
    Player = 1
End Sub

Private Sub Image1_Click(Index As Integer)
    If arrayCheck(Index) 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
0
 

Author Comment

by:paulzeb
ID: 6210915
This code did exactly what I needed.  Thank you.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6212656
Hi Paul, glad can help you.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now