Results 1 to 3 of 3

Thread: keeping turns with an array...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2003
    Posts
    98

    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!

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, you could simply use an integer which holds the value of whose turn it is:
    VB Code:
    1. 'suppose 4 players
    2. Dim NextPlayersTurn As Integer = 1
    3.  
    4. 'on next turn
    5. If Not NextPlayersTurn = 4 Then
    6. NextPlayersTurn += 1
    7. Else
    8. NextPlayersTurn = 1
    9. 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...

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. 'make players
    2.     Dim players() As String = {"Ed", "Cookie", "Sky"}
    3.     Dim turnq As New Queue
    4.  
    5.     Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         'load players into q
    7.         For Each plyr As Object In players
    8.             turnq.Enqueue(plyr)
    9.         Next
    10.     End Sub
    11.  
    12.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    13.         'take a turn and move 1st player to the end
    14.         Dim current As String = turnq.Dequeue
    15.         turnq.Enqueue(current)
    16.         'see whos next
    17.         MsgBox(turnq.Peek)
    18.     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
  •  



Click Here to Expand Forum to Full Width