Results 1 to 3 of 3

Thread: [RESOLVED] Advice on picking random items

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Location
    Altoona, PA
    Posts
    242

    Resolved [RESOLVED] Advice on picking random items

    I'm making a random class generator for call of duty and I haven't exactly hit a problem, but i need advice on how to go about picking the random weapons.

    When you generate a random class, the user must first enter their current rank (level). This is because you are unable to use certain guns until you reach a specified level. This keeps the program from giving out guns the user can't use. What I am doing is allowing the user to give certain parameters as to what type of gun they want to use (or don't want to use). For example, I have checkboxes for the user to pick if they want assault rifles, SMGs, LMGs, Snipers, Shotguns, RPG, etc.

    I did this in another version of this program I made, but the only option was to pick whether or not you wanted RPGs included. So, it was easy enough to check if the weapon was an RPG and if it was simply generate another random weapon. I could do this, but with the number of options there are, I don't think this would be the best solution.

    Would it be possible to give each weapon some kind of "level" property so I can generate an array with only the weapon types selected and they will still hold on to a property with the level required?

    Any suggestions appreciated.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Advice on picking random items

    I would think that you would have a base Weapon class that would have various properties, e.g.
    vb.net Code:
    1. Public MustInherit Class Weapon
    2.  
    3.     Private _isAutomatic As Boolean
    4.     Private _hasScope As Boolean
    5.     Private _minimumLevel As Integer
    6.  
    7.  
    8.     Public Property IsAutomatic() As Boolean
    9.         Get
    10.             Return _isAutomatic
    11.         End Get
    12.         Protected Set(ByVal value As Boolean)
    13.             _isAutomatic = value
    14.         End Set
    15.     End Property
    16.  
    17.     Public Property HasScope() As Boolean
    18.         Get
    19.             Return _hasScope
    20.         End Get
    21.         Protected Set(ByVal value As Boolean)
    22.             _hasScope = value
    23.         End Set
    24.     End Property
    25.  
    26.     Public Property MinimumLevel() As Integer
    27.         Get
    28.             Return _minimumLevel
    29.         End Get
    30.         Protected Set(ByVal value As Integer)
    31.             _minimumLevel = value
    32.         End Set
    33.     End Property
    34.  
    35. End Class
    Each individual weapon class would set those properties in its constructor. From a full list of weapons, you could then use a LINQ query to get only those that matched the user's criteria:
    vb.net Code:
    1. Dim matchingWeapons = allWeapons.Where(Function(w) w.IsAutomatic = isAutomatic AndAlso w.HasScope = hasScope AndAlso w.MinimumLevel <= minimumLevel).ToArray()
    You can then pick randomly from that array:
    vb.net Code:
    1. Dim weapon = matchingWeapons(myRandom.Next(matchingWeapons.Length))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2008
    Location
    Altoona, PA
    Posts
    242

    Re: Advice on picking random items

    Ahh, thank you for your help. Got it up and running now.

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