|
-
Sep 21st, 2011, 02:27 PM
#1
Thread Starter
Addicted Member
[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.
-
Sep 21st, 2011, 08:52 PM
#2
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:
Public MustInherit Class Weapon Private _isAutomatic As Boolean Private _hasScope As Boolean Private _minimumLevel As Integer Public Property IsAutomatic() As Boolean Get Return _isAutomatic End Get Protected Set(ByVal value As Boolean) _isAutomatic = value End Set End Property Public Property HasScope() As Boolean Get Return _hasScope End Get Protected Set(ByVal value As Boolean) _hasScope = value End Set End Property Public Property MinimumLevel() As Integer Get Return _minimumLevel End Get Protected Set(ByVal value As Integer) _minimumLevel = value End Set End Property 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:
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:
Dim weapon = matchingWeapons(myRandom.Next(matchingWeapons.Length))
-
Sep 24th, 2011, 08:51 AM
#3
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|