如何将 ArrayList 的随机索引相互配对

How to Pair Up Random Indices of an ArrayList with each other

我目前正在做一个项目,我在其中读取了一个 CSV 文件,其中包含口袋妖怪列表及其特征。我正在尝试 运行 一个战斗模拟器,它随机将这些口袋妖怪彼此配对并比较它们的战斗分数,这是使用它们的速度、攻击、防御等特征进行简单计算的结果。我读了CSV 文件中的所有 Pokemon 都转换为 Pokemon 类型的 ArrayList。现在,我想将它们随机配对并比较它们的战斗分数;得分较高的人进入下一轮,失败者被放入另一个被击败的神奇宝贝的 ArrayList 中。但是,我不知道如何随机配对神奇宝贝。到目前为止,这是我的主要 class 代码:

import java.io.*;
import java.util.ArrayList;
import java.util.Random;

public class assign1 {

public static void main(String[] args) throws IOException {

    String csvFile = args[0];   //path to CSV file
    String writeFile = args[1]; //name of output file that contains list of Pokemon and their traits
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";

    ArrayList<Pokemon> population = new ArrayList<Pokemon>();

    FileWriter fileWriter = new FileWriter(writeFile);

    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

    try {

        br = new BufferedReader(new FileReader(csvFile));
        String headerLine = br.readLine(); // used to read first line of CSV file that contains headers
        while ((line = br.readLine()) != null) {

            Pokemon creature = new Pokemon();
            // use comma as separator
            String[] pokemon = line.split(cvsSplitBy);

            creature.setId(pokemon[0]);
            creature.setName(pokemon[1]);
            creature.setType1(pokemon[2]);
            creature.setType2(pokemon[3]);
            creature.setTotal(pokemon[4]);
            creature.setHp(Integer.parseInt(pokemon[5]));
            creature.setAttack(Integer.parseInt(pokemon[6]));
            creature.setDefense(Integer.parseInt(pokemon[7]));
            creature.setSpAtk(Integer.parseInt(pokemon[8]));
            creature.setSpDef(Integer.parseInt(pokemon[9]));
            creature.setSpeed(Integer.parseInt(pokemon[10]));
            creature.setGeneration(Integer.parseInt(pokemon[11]));
            creature.setLegendary(Boolean.parseBoolean(pokemon[12]));
            creature.getCombatScore();

            // Adds individual Pokemon to the population ArrayList
            population.add(creature);

            // Writes to pokemon.txt the list of creatures
            bufferedWriter.write(creature.getId() + ". "
                    + "Name: " + creature.getName() + ": "
                    + "Type 1: " + creature.getType1() + ", "
                    + "Type 2: " + creature.getType2() + ", "
                    + "Total: " + creature.getTotal() + ", "
                    + "HP: " + creature.getHp() + ", "
                    + "Attack: " + creature.getAttack() + ", "
                    + "Defense: " + creature.getDefense() + ", "
                    + "Special Attack: " + creature.getSpAtk() + ", "
                    + "Special Defense: " + creature.getSpDef() + ", "
                    + "Speed: " + creature.getSpeed() + ", "
                    + "Generation: " + creature.getGeneration() + ", "
                    + "Legendary? " + creature.isLegendary() + ", "
                    + "Score: " + creature.getCombatScore());
            bufferedWriter.newLine();

        }

    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        if (br != null) {
            try {
                br.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    bufferedWriter.close();

}
}

这是我的口袋妖怪的代码 class:

public class Pokemon {
String id;
String name;
String type1;
String type2;
String total;
int hp;
int attack;
int defense;
int spAtk;
int spDef;
int speed;
int generation;
boolean legendary;


public Pokemon() {}

public String getId () {
        return id;
    }

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getType1() {
    return type1;
}

public void setType1(String type1) {
    this.type1 = type1;
}

public String getType2() {
    return type2;
}

public void setType2(String type2) {
    this.type2 = type2;
}

public String getTotal() {
    return total;
}

public void setTotal(String total) {
    this.total = total;
}

public int getHp() {
    return hp;
}

public void setHp(int hp) {
    this.hp = hp;
}

public int getAttack() {
    return attack;
}

public void setAttack(int attack) {
    this.attack = attack;
}

public int getDefense() {
    return defense;
}

public void setDefense(int defense) {
    this.defense = defense;
}

public int getSpAtk() {
    return spAtk;
}

public void setSpAtk(int spAtk) {
    this.spAtk = spAtk;
}

public int getSpDef() {
    return spDef;
}

public void setSpDef(int spDef) {
    this.spDef = spDef;
}

public int getSpeed() {
    return speed;
}

public void setSpeed(int speed) {
    this.speed = speed;
}

public int getGeneration() {
    return generation;
}

public void setGeneration(int generation) {
    this.generation = generation;
}

public boolean isLegendary() {
    return legendary;
}

public void setLegendary(boolean legendary) {
    this.legendary = legendary;
}

public int getCombatScore() {
    return (speed/2) * (attack + (spAtk/2)) + (defense + (spDef/2));
}

@Override
public String toString() {
    return "Name: " + this.getName()
            + ", Type 1: " + this.getType1()
            + ", Type 2: " + this.getType2()
            + ", Total: " + this.getTotal()
            + ", HP: " + this.getHp()
            + ", Attack: " + this.getAttack()
            + ", Defense: " + this.getDefense()
            + ", Sp. Attack: " + this.getSpAtk()
            + ", Sp. Defense: " + this.getSpDef()
            + ", Generation: " + this.getGeneration()
            + ", Legnedary: " + this.isLegendary()
            + ", Score: " + this.getCombatScore();
}
}

我只想比较他们的战斗分数值。任何 help/suggestions 将不胜感激。

由于 ArrayList 中的每个元素都有一个索引,您可以通过调用

从中随机获取一个元素
Pokemon pokemon1;
Pokemon pokemon2;
pokemon1 = arrayList.get(Math.random()*arrayList.size());
do {
    pokemon2 = arrayList.get(Math.random()*arrayList.size());
} while(pokemon1.getId() == pokemon2.getId());

然后将您从 List1 中获得的神奇宝贝与您从 List2 中获得的神奇宝贝进行比较。

如果您愿意,您当然可以从列表中删除神奇宝贝。

希望对您有所帮助!

我想到的是这个。您从数组列表中选择一个随机项目(口袋妖怪)。从数组列表中删除它。然后你再次选择一个随机项目并将其删除。现在你有一对物品。对数组列表中的剩余项目重复上述步骤,直到没有更多项目可用。

或者您可以先打乱整个数组列表,然后选择第 i 项和第 i+1 项作为对 i=0,2,4,6,...

Collections.shuffle(pokemonsArrayList);
for (int i=0; i< pokemonsArrayList.size(); i+=2) {
     pokemon1 = pokemonsArrayList.get(i);
     pokemon2 = pokemonsArrayList.get(i+1);
}

只需确保 ArrayList 中的元素数量是偶数即可。否则上面的代码将抛出异常 index out of bound