Reputation: 1
I'm a newbie programmer, and right now I'm creating a backgammon game in VB (a simple version right now which will be built up overtime).
I am trying to get the game to record how many checkers a player has, and where these checkers are on the board. The white checkers are represented as negative integers and black checkers as positive integers. Once this is done the player would be able to select which checker to move and where depending on their dice roll.
So my question is, how would I be able to record the element position in an array, for example, when the array is checked for values where the value is > 0 for the white checker player. I'll add my code for this section - sorry if my coding is poorly executed.
If possible dumb down the advice haha
Console.WriteLine("Make your first move: ")
For Each x In Gameboard
If x > 0 Then
Console.WriteLine("You have {0} pieces in position ", x)
End If
Next
Upvotes: 0
Views: 163
Reputation: 43743
You could, create a counter variable, above your loop, and increment it in each iteration, but that's a bit ugly. The preferable way to do it is to use a plain For
loop, with a counter, rather than a For Each
loop. For instance:
Console.WriteLine("Make your first move: ")
For i As Integer = 0 To Gameboard.Length
If Gameboard(i) > 0 Then
Console.WriteLine("You have {0} pieces in position {1}", Gameboard(i), i)
End If
Next
General rule of thumb:
For
loopFor Each
loopUpvotes: 1