|
-
May 29th, 2003, 05:58 PM
#1
Thread Starter
Lively Member
keeping turns with an array...
Okay, i need to be able to keep track of whose turn it is, players 1 though X. I realize i can use an array. I can make the array, but cannot figure out how to cycle through the turns, any help would be great (ive read everything on arrays, but it doesnt seem to be much help) thanks!
-
May 30th, 2003, 01:01 PM
#2
I wonder how many charact
Well, you could simply use an integer which holds the value of whose turn it is:
VB Code:
'suppose 4 players
Dim NextPlayersTurn As Integer = 1
'on next turn
If Not NextPlayersTurn = 4 Then
NextPlayersTurn += 1
Else
NextPlayersTurn = 1
End If
Now I don't know what you are planning on storing in the array... but if it something like a Player class ... where each instance
describes a certain player....
You would probably want to make a collection of Players... and simply iterate through the collection on each, and reset to the first element after the last player's turn...
-
May 30th, 2003, 01:29 PM
#3
I'd use a Queue, which uses a first in first out method, then it doesn't matter how many players there are it will still keep the order. This lets you store the actual player object in the queue instead of an index or something, but nemaroller's way is easier.
VB Code:
'make players
Dim players() As String = {"Ed", "Cookie", "Sky"}
Dim turnq As New Queue
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'load players into q
For Each plyr As Object In players
turnq.Enqueue(plyr)
Next
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'take a turn and move 1st player to the end
Dim current As String = turnq.Dequeue
turnq.Enqueue(current)
'see whos next
MsgBox(turnq.Peek)
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|