Solved

Problem with game creation

Posted on 2001-06-18
13
176 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 50

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 50

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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 50

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
 

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 50

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 50

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 50

Expert Comment

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

776 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