floridagrrrlll
asked on
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
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
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 ?!
ASKER
Edited text of question.
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).Des tination = 5
Board(1).LegalJumps(1).Jum ped = 2
Board(1).LegalJumps(2).Des tination = 7
Board(1).LegalJumps(2).Jum ped = 3
Board(1).LegalJumps(3).Des tination = 9
Board(1).LegalJumps(3).Jum ped = 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).Destinati on = 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).Jump ed).HoleSt ate = True Then
If Board(Board(Source.Index). LegalJumps (Idx).Dest ination).H oleState = False Then
'change source
Source.Picture = imgHole.Picture
Board(Source.Index).HoleSt ate = False
Source.DragMode = vbManual
'change middle
Idx2 = Board(Source.Index).LegalJ umps(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.
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).Des
Board(1).LegalJumps(1).Jum
Board(1).LegalJumps(2).Des
Board(1).LegalJumps(2).Jum
Board(1).LegalJumps(3).Des
Board(1).LegalJumps(3).Jum
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).Destinati
Next Idx
If Idx > UBound(.LegalJumps) Then
' handle errors here
Exit Sub
End If
End With
If Board(Board(Source.Index).
If Board(Board(Source.Index).
'change source
Source.Picture = imgHole.Picture
Board(Source.Index).HoleSt
Source.DragMode = vbManual
'change middle
Idx2 = Board(Source.Index).LegalJ
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.
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
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
ASKER
Thanks for the comments-I will check it out and let you know how it works! Floridagirl ;)
ASKER
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!!! :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Adjusted points from 50 to 65
ASKER
Thanks! It works great!
ASKER
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!
I thought it was working but not-could you give me an idea of exactly where to put the line of code??? Thanks!
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.
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.