Solved

Need help with validation of moves in a game

Posted on 2000-03-23
11
136 Views
Last Modified: 2012-05-04
I have a simple game here - kinda like tic-tac-toe.  I need to write the code to validate the moves-You can only jump over another peg, one space at a time and you have to jump into an empty hole, not on top of another peg.  Any help would be appreciated!!!  Here is the code-

BTW-the grid is 10*10-Thx!

Option Explicit

Private Sub Image1_Click()

End Sub

Private Sub Form_Load()
    Dim intx
    Dim inthole As Integer
    For intx = 0 To 99
        imgGame(intx).Picture = imgPeg.Picture
        imgGame(intx).DragIcon = imgIcon.Picture
    Next intx
    'random hole
    Randomize
    inthole = CInt(Rnd * 99)
    imgGame(inthole).Picture = imgHole.Picture
    imgGame(inthole).Tag = "H"
    imgGame(inthole).DragMode = vbManual
End Sub

Private Sub imgGame_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
    Dim intmid As Integer
    intmid = (Source.Index + Index) / 2
    'change source
    Source.Picture = imgHole.Picture
    Source.Tag = "H"
    Source.DragMode = vbManual
    'change middle
    imgGame(intmid).Picture = imgHole.Picture
    imgGame(intmid).Tag = "H"
    imgGame(intmid).DragMode = vbManual
    'change dest
    imgGame(Index).Picture = imgPeg.Picture
    imgGame(Index).Tag = "P"
    imgGame(Index).DragMode = vbAutomatic
End Sub
0
Comment
Question by:floridagrrrlll
  • 6
  • 4
11 Comments
 
LVL 54

Expert Comment

by:nico5038
ID: 2651078
I assume you have a square playfield where the game is played. Something like a chessboard. However, I can't find out how, as normally I would expect to find a double indexed field (having a X- and Y-axis) and that's missing in your code ?!
0
 

Author Comment

by:floridagrrrlll
ID: 2651132
Edited text of question.
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2651241
You will need to have some sort of a data structure to identify which holes are adjacent to which holes.  For example, let's use a board a little more complex than a simple square:

   o
  ooo
 ooooo
ooooooo
 ooooo
  ooo
   o

Using your apparent structure (from your code), we could number the top hole 1, the next three 2,3,4, the next 5 5-9, etc, up to hole 25.

Now, build a data structure containing all of the holes.  Each hole entry contains the following data:
   Hole index
   Hole state (pegged or not pegged)
   Legal Jumps - another data structure containing legal destination holes, and the holes to jump, by index

When you drag/drop, you check four things:
   Is Hole(Hole Index).Hole State = pegged?  (Was there a peg there?)
   Is Destination Index a legal target?
   Is Hole(Destination Index).Hole State = not pegged?
   Is Hole(Hole(Hole Index).(Destination Index).(Jumped Hole)).Hole State = pegged?

Let's build a VB Type structure (there are other ways) that would work for this.

Type JumpInfo
    Destination As Integer
    Jumped      As Integer
End Type

Type HoleInfo
   HoleState    As Boolean   ' True = Peg, False = No Peg
   LegalJumps() As JumpInfo
End Type

Then, we create our data structure:

Dim Board(1 to 25) As HoleInfo

And load the info:

LoadBoardData   ' You can build this subroutine :)

Looking at our sample board, let's consider jumps from the top hole (1).

Legal Destinations are 5 (jumping 2), 7 (Jumping 3), and 9 (jumping 4), so your data in Board(1) would be:
  Board(1).HoleState = True ' has a peg
  Board(1).LegalJumps(1).Destination = 5
  Board(1).LegalJumps(1).Jumped = 2
  Board(1).LegalJumps(2).Destination = 7
  Board(1).LegalJumps(2).Jumped = 3
  Board(1).LegalJumps(3).Destination = 9
  Board(1).LegalJumps(3).Jumped = 4

Possible code to determine if the jump was legal (using your code as a base):

Private Sub imgGame_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)
    Dim Idx as Integer
    Dim Idx2 as Integer

    With Board(Source.Index)
        For Idx=1 to UBound(.LegalJumps)
            If .LegalJumps(Idx).Destination = Index Then Exit For
        Next Idx
        If Idx > UBound(.LegalJumps) Then
            ' handle errors here
            Exit Sub
        End If
    End With
   
    If Board(Board(Source.Index).LegalJumps(Idx).Jumped).HoleState = True Then
        If Board(Board(Source.Index).LegalJumps(Idx).Destination).HoleState = False Then
            'change source
            Source.Picture = imgHole.Picture
            Board(Source.Index).HoleState = False
            Source.DragMode = vbManual

           'change middle
           Idx2 = Board(Source.Index).LegalJumps(Idx).Jumped
           imgGame(Idx2).Picture = imgHole.Picture
           Board(Idx2).HoleState = False
           imgGame(Idx2).DragMode = vbManual

           'change dest
           imgGame(Index).Picture = imgPeg.Picture
           Board(Index).HoleState = True
           imgGame(Index).DragMode = vbAutomatic
           Exit Sub
       Else
           ' Illegal Destination Error
           Exit Sub
       End If
    Else
       ' Illegal Midpoint Error
       Exit Sub
    End If
End Sub

Hope this is the sort of thing you were looking for.
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2651272
Oops - took to long typing, and didn't see your edited text.

With a square grid, we can simplify a lot.

Private Sub imgGame_DragDrop(Index As Integer, Source As Control, X As Single, Y As Single)

    Dim intmid As Integer
    Dim JumpDist as Integer

'   Legal jump?
    JumpDist = Index - Source.Index
    If abs(JumpDist) <> 2 and Abs(JumpDist) <> 20 Then
        ' Illegal destination
        Exit Sub
    End If

'    intmid = (Source.Index + Index) / 2
'   This is wrong - should be:

    intmid = JumpDist/2 + Source.Index
   
    If ImgGame(intMid).Tag="P" Then
        If imgGame(Index).Tag = "H" Then
            'change source
            Source.Picture = imgHole.Picture
            Source.Tag = "H"
            Source.DragMode = vbManual
            'change middle
            imgGame(intmid).Picture = imgHole.Picture
            imgGame(intmid).Tag = "H"
            imgGame(intmid).DragMode = vbManual
            'change dest
            imgGame(Index).Picture = imgPeg.Picture
            imgGame(Index).Tag = "P"
            imgGame(Index).DragMode = vbAutomatic
        Else
            ' Illegal Destination
            Exit Sub
        End If
    Else
        ' Illegal Jump over
        Exit Sub
    End If
End Sub


0
 

Author Comment

by:floridagrrrlll
ID: 2651562
Thanks for the comments-I will check it out and let you know how it works! Floridagirl ;)
0
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!

 

Author Comment

by:floridagrrrlll
ID: 2656517
Hey!  Thanks for the help-it works great!  Except...I forgot to add that you can't jump over an empty hole!  HELLPPP!!!  I am pretty new to this and really appreciate the feedback-Maybe someday I will qualify as an expert!!!  :)
0
 
LVL 32

Accepted Solution

by:
bhess1 earned 65 total points
ID: 2661226
The test

    If ImgGame(intMid).Tag="P" Then

Checks to see if there is a peg in the middle hole.  It's in the code.  If it's not working, check to ensure that the tag is being set to "P" when you load up the game, and that it's being set to 'H' when you remove a peg.
0
 

Author Comment

by:floridagrrrlll
ID: 2661697
Adjusted points from 50 to 65
0
 

Author Comment

by:floridagrrrlll
ID: 2661698
Thanks!  It works great!
0
 

Author Comment

by:floridagrrrlll
ID: 2669015
For bhess1-
I thought it was working but not-could you give me an idea of exactly where to put the line of code???  Thanks!
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2670024
Whoops - which line of code?  The

    If ImgGame(intMid).Tag="P" Then

line?

Look up at my sample code.  First, just in case you hadn't noticed, note that I changed your intmid value - the original formula would not have worked.

Just below that, I have a pair of nested IF statements.  The first checks for a "P" tag on the 'intmid' image, and the second checks for an "H" tag on the destination hole.  It only works if both cases are true.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

746 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

10 Experts available now in Live!

Get 1:1 Help Now