PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2008 [RESOLVED] List or Array?-VBForums
Results 1 to 8 of 8

Thread: [RESOLVED] List or Array?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Location
    Netherlands
    Posts
    203

    Resolved [RESOLVED] List or Array?

    Hello,

    I am working on a Card game, and i have a little question. I want to put all the cards in a array or list. The problem is that the list need to be shuffled/randomised. But it should also be able to shrink. Witch of those 2 is better to use? And how can i shuffle them?

    Please tell me,

    ThijsD

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,860

    Re: List or Array?

    the list has better, easier to use, functionality than the array

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,572

    Re: List or Array?

    If you need to change the size at any time, then a list is definitely going to be easier.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Location
    Netherlands
    Posts
    203

    Re: List or Array?

    Ok so list it is

    But how can u shuffle one? (I thought that removing an item was .removeat(), is that true?)

  5. #5
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: List or Array?

    VB.NET Code:
    1. Public Function Shuffle(Of T)(ByVal source As IEnumerable(Of T)) As IEnumerable(Of T)
    2.         Dim rng As New Random
    3.         Dim elements() As T = source.ToArray
    4.         Dim i, SwapIndex As Integer
    5.         Dim tmp As T
    6.         For i = elements.Length - 1 To 1 Step -1
    7.             SwapIndex = rng.Next(i + 1)
    8.             tmp = elements(i)
    9.             elements(i) = elements(SwapIndex)
    10.             elements(SwapIndex) = tmp
    11.         Next
    12.         Return elements
    13. End Function
    14.  
    15. Sub Test()
    16.         Dim ordered() As Integer = {1, 2, 3, 4, 5}
    17.         Dim shuffled = Shuffle(ordered)
    18.         For Each el In shuffled
    19.             Debug.Write(el.ToString + ";")
    20.         Next
    21. End Sub

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,860

    Re: List or Array?

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim cards As New List(Of Integer)
    5.         For x As Integer = 1 To 52
    6.             cards.Add(x)
    7.         Next
    8.         cards = shuffleDeck(cards)
    9.     End Sub
    10.  
    11.     Private Function shuffleDeck(ByVal deck As List(Of Integer)) As List(Of Integer)
    12.         Dim randomNumber As New Random
    13.         Dim used As New List(Of Integer)
    14.         For x As Integer = 0 To deck.Count - 1
    15.             Dim number As Integer = randomNumber.Next(0, deck.Count)
    16.             If Not used.Contains(number) Then
    17.                 used.Add(number)
    18.             Else
    19.                 x -= 1
    20.             End If
    21.         Next
    22.         Dim newList(deck.Count - 1) As Integer
    23.         For x As Integer = 0 To used.Count - 1
    24.             newList(used(x)) = deck(x)
    25.         Next
    26.         deck.Clear()
    27.         deck.AddRange(newList)
    28.         Return deck
    29.     End Function
    30.    
    31. End Class

    with a list you can use .removeat

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Location
    Netherlands
    Posts
    203

    Re: List or Array?

    Yes Both of these codes work! Thank you for helping me out

  8. #8
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] List or Array?

    jmcilhinney has a very nice 'Randomize list' codebank submission which shuffles a list randomly with much less code using LINQ. The query is something simple as this:
    Code:
    From item As Integer In yourList _
    Order By rnd.NextDouble() _
    Select item
    where 'rnd' is a Random object.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width