Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

Need help with validation of moves in a game

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
floridagrrrlll
Asked:
floridagrrrlll
  • 6
  • 4
1 Solution
 
nico5038Commented:
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
 
floridagrrrlllAuthor Commented:
Edited text of question.
0
 
Brendt HessSenior DBACommented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Brendt HessSenior DBACommented:
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
 
floridagrrrlllAuthor Commented:
Thanks for the comments-I will check it out and let you know how it works! Floridagirl ;)
0
 
floridagrrrlllAuthor Commented:
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
 
Brendt HessSenior DBACommented:
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
 
floridagrrrlllAuthor Commented:
Adjusted points from 50 to 65
0
 
floridagrrrlllAuthor Commented:
Thanks!  It works great!
0
 
floridagrrrlllAuthor Commented:
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
 
Brendt HessSenior DBACommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now