0

I'm in a bit of a bind mainly because I'm a newbie at programming. I'm part of the Mass Effect 2 speedrunning community and we tried to make a randomizer for the game. The basic idea is to get random powers for the characters using the console. This is, in essence, really easy to do and after some research I managed to do a, maybe not optimized, but working program you can find below.

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    import java.util.ArrayList;
    import java.util.Random;


  public class Main {
     public static void main(String [ ] args) throws IOException {
        FileInputStream fs = null;
          try {
          fs = new FileInputStream("powers.txt");
        } catch (FileNotFoundException e1) {
          e1.printStackTrace();
      }
        PrintStream out = new PrintStream("powers2.txt");
        System.setOut(out);
        BufferedReader br = new BufferedReader(new InputStreamReader(fs));
        ArrayList<String> array = new ArrayList<>();
        int i=1;
        System.out.println("GiveTalentPoints 50");
        System.out.println("RemovePower self SFXPower_Incinerate_Engineer");
        System.out.println("RemovePower self SFXPower_IncendiaryAmmo_Engineer");
        System.out.println("RemovePower self SFXPower_CombatDrone_Engineer");
        System.out.println("RemovePower self SFXPower_AIHacking_Engineer");
        System.out.println("RemovePower self SFXPower_CryoFreeze_Engineer");
        System.out.println("RemovePower self SFXPower_Overload_Engineer");
        System.out.println("RemovePower self SFXPower_AntiOrganicAmmo_Player");
        String line;{
            do {
            try {
                while((line = br.readLine()) != null)
                    array.add(line);
            } catch (IOException e) {
                e.printStackTrace();
        }
        // variable so that it is not re-seeded every call.
            Random rand = new Random();

        // nextInt is exclusive. Should be good with output for array.
            int randomIndex = rand.nextInt(array.size());

        // Print the powers
            System.out.print("GivePower self ");
            System.out.println(array.get(randomIndex));
            System.out.print("SetRank self ");
            System.out.print(array.get(randomIndex));
            System.out.println(" 3");
            i=i+1;
    }   
    while (i<=5);       
}
out.flush();
out.close();
fs.close();
}
}

So that works. The idea is to just create a text file that we then execute in the console of the game. Now my main problem is the fact that this can give me duplicates. If the random value falls on the same line I'll have twice the same power which is impossible in the game. So I need a way to either make "exclusive" random values or to do a check after the file is printed for duplicates, and if done, restart the program till I don't have any duplicates. I hope I make sense and I'm sure there's an easy solution to this, but coding isn't at all my main terrain and I'm really bad at learning it.

Thanks in advance. Also I know my indentation is probably not good, I was just trying to make it read in code for the site. Sorry!

Strife
  • 1
  • It didn't prevent me from doing it but I actually didn't understand the answer because I'm really bad. Sorry for that. – Strife May 04 '18 at 21:55
  • Read all the lines into a `List`. *Shuffle* the list. Now just read from the list starting at the beginning. They are in random order because of the shuffle, and the same line will not occur twice. If needed, when reaching the end, re-shuffle, and start from beginning again. – Andreas May 05 '18 at 00:34

0 Answers0