VB help needed please

Soldato
Joined
25 Jul 2006
Posts
3,529
Location
Taunton
Right im creating a simple game of snakes and ladders and im confused by the last steps of the game. (Ie. where you have to roll the exact number to finish the game, or if you roll over that number you move back the remaining spaces)

so this is the code i have so far but cant for the life of me figure out how to get it to go up to 99 (effectively the 100th square but due to array's ...) and then backwards, i know my code doesnt exactly help what i want to do as its just adding the dice roll every time, but as i said i cant figure it out :confused:

Code:
    EndPosition = MyPosition + Text1        'myposition + dice roll
    For i = MyPosition To EndPosition
        Call SetPosition(i)
        MyPosition = i                      'basically just setting it so i can use it if needs be
        Delay 0.5
        Text5 = MyPosition                  'just to display position
    Next i

Code:
Private Sub SetPosition(ByVal N As Long)

r = N \ 10

If IsOdd(r) Then
c = 9 - (N Mod 10)
Else
c = N Mod 10
End If

Shape1.Left = (c * 41) + 10
Shape1.Top = 410 - 30 - (2 * 41)
End Sub
 
You could try sticking something like this at the beginning of SetPosition:

Code:
If N > 99 Then
    N = 99 - ((N + 1) Mod 100)
End If
 
Last edited:
I'm not sure exactly what you're trying to do here so I'm going to give a couple of comments on the code in general.

Firstly, your variable names.
What are r, N and c in the SetPosition procedure? Where are r and c defined?

Also, use the .Text property of the text boxes - your text box isn't a string, the text property is. I know VB6 uses the default property if you don't specify one but get into that habit.

Some descriptive names for the textboxes and shape would be better too.

I know this is probably just a fun thing for you to do, but I would urge you to get into good habits. VB6 is great for quickly knocking up applications, but it can be easily abused.

I assume the code posted is to move the counter after a dice roll, if you can be a little more specific about what the issue is we'll be in a better position to help.
 
I understand what he's asking for perfectly :confused:

Granted, the code could be clearer and include variable declarations, more meaningful names, etc., but it's a fairly straightforward question: how would he go about getting the player to 'bounce' back from the end of the board with the code he's supplied?

Btw, you don't need to use a Long for this, as there's very little chance the user will roll more than 6 on a die, let alone several million ;) You should generally use the Integer for integers unless you have a specific reason not to.

Also, what Haircut said about coding style: use meaninful variable/control names, always declare your variables and don't rely on implicit .Text assignment for controls.
 
Last edited:
Use a while loop instead of a for loop, and have a directional control variable you add on at each stage.
 
I understand what he's asking for perfectly :confused:

Granted, the code could be clearer and include variable declarations, more meaningful names, etc., but it's a fairly straightforward question: how would he go about getting the player to 'bounce' back from the end of the board with the code he's supplied?

Btw, you don't need to use a Long for this, as there's very little chance the user will roll more than 6 on a die, let alone several million ;) You should generally use the Integer for integers unless you have a specific reason not to.

Also, what Haircut said about coding style: use meaninful variable/control names, always declare your variables and don't rely on implicit .Text assignment for controls.

Ah, I see what he wants now!
In which case your code should do the job.

Also, I was under the impression that using a Long was actually quicker than using an Integer for VB6.
Though I'm sure the performance difference wouldn't make much of a difference in this case :p
 
Also, I was under the impression that using a Long was actually quicker than using an Integer for VB6.
Though I'm sure the performance difference wouldn't make much of a difference in this case :p

I tend to steer well clear of VB6 so I wouldn't know these things :p
 
cheers for the help guys, got it sorted finally, sorry bout all the variables not being explained, was just using this to get my head round the coding side, wasnt too bothered about the actual final product, got couple of months for that :D

but just to clear things up,
N is the position of the counter used to get the column(C) and row (R), thus the lines
Code:
For i = MyPosition To EndPosition
        Call SetPosition(i)
most of the variables are defined in the general declerations as i wanted some of them to be global
 
Back
Top Bottom