2017-02-06 13 views
0

我目前正在研究一个项目,在该项目中读取包含Pokemon列表以及其特征的CSV文件。我试图运行一个战斗模拟器,随机将这些宠物小精灵相互配对,并比较他们的战斗分数,这是使用他们的特性如速度,攻击力,防御等简单计算的结果。我读了所有的口袋妖怪从CSV文件转换为Pokemon类型的ArrayList。现在,我想随机配对并比较他们的战斗评分;谁拥有更高的分数移动到下一轮,失败者被放入另一个失败的宠物小精灵ArrayList。但是,我不知道如何随机配对宠物小精灵。这里是我的主类的代码至今:如何将ArrayList的随机指数相互配对

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(); 

} 
} 

,这里是我的宠物小精灵类的代码:

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(); 
} 
} 

我只希望他们combatScore值相互比较。任何帮助/建议将不胜感激。

回答

1

因为在一个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()); 

然后比较你和你从列表2得到了一个走出列表1的神奇宝贝从它那里得到一个随机元素。

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

希望能帮助你!

+0

这让我走上了正轨。感谢您的建议! –

+0

非常欢迎您! – matejetz

0

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

或者你可以先洗牌全数组列表,然后从中挑选项目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中的元素是偶数。否则上面的代码会抛出异常索引出界