-1

I have very minimal experience programming and I'm trying to create a random(ish) class generator for my buddies and I on Warzone for fun. It's a very basic generator but I'm having some trouble figuring out how to get the outputs I want. It turned into a bigger project than I anticipated so I'll take any help I can get!

For anyone who hasn't played the game, there are at most 9 different classes of attachments you can choose from (i.e. suppressors, barrels, optics, stocks, etc) but you can only have a maximum of 5 equipped, meaning some attachment classes won't be used. I haven't added all the attachment categories or special attachments there might be since I'm trying to work out the basics first.

I'd like to have the final output be something like:

Primary Gun: M4A1
Primary Grip: Commando
Primary Laser: None
Primary Stock: No Stock Attachment
Primary Mag: 50
Primary Rear Grip: Stippled
Primary Barrel: Big Barrel 

As of now it just outputs something like

Primary Gun: M4A1 Primary Attachments: Commando None No Stock Attachment 50 Stippled None

This is what I have so far (excuse if the code looks ugly lol)

import java.util.concurrent.ThreadLocalRandom;


public class RandomClass {


    public static void main(String[] args) {



        System.out.println("Primary: " + randomPrimaryGun());
        System.out.println("Attachments: " + primaryAttachments());
        System.out.println("Secondary: " + randomSecondaryGun());
       
    }


    public static String randomPrimaryGun() {

        String[] primaryGuns = {"M4A1", "Kilo", "Grau", "Bruen", "FAL", "M13"};
        int randIdx = ThreadLocalRandom.current().nextInt(primaryGuns.length);

        String randPrimary = primaryGuns[randIdx];
        return randPrimary;
    }


    public static String randomSecondaryGun() {

        String[] secondaryGuns = {"MP5", "P90", "AUG", "Uzi", "MP7"};
        int randIdx = ThreadLocalRandom.current().nextInt(secondaryGuns.length);

        String randSecondary = secondaryGuns[randIdx];
        return randSecondary;
    }


    public static String primaryAttachments() {

        String[] primaryGrip = {"Merc ", "Ranger ", "Commando ", "None "};
        String[] primaryLaser = {"Tac ", "5mw ", "None "};
        String[] primaryStock = {"No Stock Attachment ", "No Stock ", "Fast stock "};
        String[] primaryMag = {"50 ", "60 "};
        String[] primaryRearGrip = {"Stippled ", "None "};
        String[] primaryBarrel = {"Big Barrel", "Short Barrel"};

        int randIdx0 = ThreadLocalRandom.current().nextInt(primaryGrip.length);
        int randIdx1 = ThreadLocalRandom.current().nextInt(primaryLaser.length);
        int randIdx2 = ThreadLocalRandom.current().nextInt(primaryStock.length);
        int randIdx3 = ThreadLocalRandom.current().nextInt(primaryMag.length);
        int randIdx4 = ThreadLocalRandom.current().nextInt(primaryRearGrip.length);
        int randIdx5 = ThreadLocalRandom.current().nextInt(primaryBarrel.length);

        String grip = primaryGrip[randIdx0];
        String laser = primaryLaser[randIdx1];
        String stock = primaryStock[randIdx2];
        String mag = primaryMag[randIdx3];
        String rearGrip = primaryRearGrip[randIdx4];
        String barell = primaryBarrel[randIdx5];

    
    }
}

I know I need some if/else statements and maybe some loops but I have no idea how to implement that with a random selection from a list. If anyone could at least help me get started I'd really appreciate it! Once I know the basic method I can go from there and add whatever else I need to!

I haven't bothered starting with the secondary attachments yet since I can't figure out how to do the primary attachments yet, but it'd essentially be the same code just with a few differences.

Thanks you!

efan
  • 99
  • 1
  • 8

2 Answers2

1

This is how to select a random element in a list:

List<String> myList = new ArrayList<>();
myList.add("M4A1");
myList.add("Kilo");
myList.add("Grau");

int randomIndex = ThreadLocalRandom.current().nextInt(myList.size());

String randomGun = myList.get(randomIndex);

The value of randomGun will be one of the three guns.

Edit:

To choose randomly a list you can create a Map of lists:

Map<Integer, List<String>> myMap = new HashMap<>();
myMap.put(1, Arrays.asList("M4A1","Kilo","Grau"));
myMap.put(2, Arrays.asList("Merc","Ranger","Commando"));
myMap.put(3, Arrays.asList("50","60","70"));

int randomIndex = ThreadLocalRandom.current().nextInt(myMap.size());

List<String> randomList =  myMap.get(randomIndex);

For a simple project is OK, but if you plan to extend this project then I recommend to use classes.

Marc
  • 2,738
  • 1
  • 17
  • 21
  • Thank you so much! How can I make ~9 different lists, then only pick 5 attachments from those lists? – efan Aug 03 '20 at 04:00
  • @efan I cannot post the full code because this is not a training website, and the mods will delete it. On StackOverflow you are supposed to post your attempt and not ask for a complete solution. I updated my question so that you get the idea how to work with multiple lists. Please consider to upvote an pick an answer ;) – Marc Aug 03 '20 at 05:19
  • I understand. Thanks for the help! This put me on the right track! – efan Aug 03 '20 at 14:48
0

This could be achieved by storing your values in a collection that can be accessed by index, as you are doing with your array. Then you generate a random integer between 0 and your highest index. Take the value from the collection at that index. Voila.

  • any idea how I'd go about doing that? Sorry I only have about half a semesters worth of actual experience lol. Thank you. – efan Aug 03 '20 at 00:58