vendredi 4 mai 2018

Java - Trying to avoid duplicates when using random to get lines in a text file [duplicate]

This question already has an answer here:

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!




Aucun commentaire:

Enregistrer un commentaire