我正在尝试创建一个口袋妖怪之间的大型战斗的模拟。为了提供一个例子,我将使用两个Pokemon,名为pokemon1和pokemon2。我希望程序像以下一样工作:
在没有越来越少的胜利人口的情况下重复这些轮次,直到只剩下一名胜利者为止。
我创建了一个do-while循环,它将从群体中随机检索两个Pokemon,并使用if-else分支根据许多不同的条件对它们进行比较:
反之亦然。我遇到的问题是:大多数时候,程序运行干净,第1轮完成。所有的战斗都写在一个pokemon.txt文件中,明确说明谁赢了。但是,有时程序将开始运行并保持运行状态。当我去检查.txt文件时,它将停止在同一位置(第275行)周围写句子。
现在,我只想进行第一轮比赛。以下是运行此模拟器的代码:
do {
Pokemon pokemon1;
Pokemon pokemon2;
pokemon1 = population.get((int) (Math.random() * population.size()));
do {
pokemon2 = population.get((int) (Math.random() * population.size()));
} while (pokemon1.getId() == pokemon2.getId());
if (pokemon2.getCombatScore() > pokemon1.getCombatScore()
&& pokemon2.isLegendary() && pokemon1.isLegendary()) {
bufferedWriter.write("The legendary " + pokemon2.getName() + " defeats the legendary " +
pokemon1.getName() + " with a combat score of " + pokemon2.getCombatScore() + "!");
bufferedWriter.newLine();
/* pokemon2 gains xp from winning */
pokemon2.incAttack(pokemon1.getCombatScore());
pokemon2.incDefense(pokemon1.getCombatScore());
pokemon2.addVict();
victorious.add(pokemon2);
population.remove(pokemon2);
pokemon1.addLoss();
if (pokemon1.numLoss == 3) {
defeated.add(pokemon1);
population.remove(pokemon1);
}
}
else if (pokemon2.getCombatScore() > pokemon1.getCombatScore()
&& pokemon1.isLegendary()) {
bufferedWriter.write(pokemon2.getName() + " defeats the legendary " +
pokemon1.getName() + " with a combat score of " + pokemon2.getCombatScore() + "!");
bufferedWriter.newLine();
/* pokemon2 gains xp from winning */
pokemon2.incAttack(pokemon1.getCombatScore());
pokemon2.incDefense(pokemon1.getCombatScore());
pokemon2.addVict();
if (pokemon2.numVict == 3) {
bufferedWriter.write(pokemon2.getName() + " has become legendary!");
}
victorious.add(pokemon2);
population.remove(pokemon2);
pokemon1.addLoss();
if (pokemon1.numLoss == 3) {
defeated.add(pokemon1);
population.remove(pokemon1);
}
}
else if (pokemon2.getCombatScore() > pokemon1.getCombatScore()
&& pokemon2.isLegendary()) {
bufferedWriter.write("The legendary " + pokemon2.getName() + " defeats " +
pokemon1.getName() + " with a combat score of " + pokemon2.getCombatScore() + "!");
bufferedWriter.newLine();
/* pokemon2 gains xp from winning */
pokemon2.incAttack(pokemon1.getCombatScore());
pokemon2.incDefense(pokemon1.getCombatScore());
pokemon2.addVict();
victorious.add(pokemon2);
population.remove(pokemon2);
pokemon1.addLoss();
defeated.add(pokemon1);
population.remove(pokemon1);
}
else if (pokemon2.getCombatScore() > pokemon1.getCombatScore()) {
bufferedWriter.write(pokemon2.getName() + " defeats " +
pokemon1.getName() + " with a combat score of " + pokemon2.getCombatScore() + "!");
bufferedWriter.newLine();
/* pokemon2 gains xp from winning */
pokemon2.incAttack(pokemon1.getCombatScore());
pokemon2.incDefense(pokemon1.getCombatScore());
pokemon2.addVict();
if (pokemon2.numVict == 3) {
bufferedWriter.write(pokemon2.getName() + " has become legendary!");
}
victorious.add(pokemon2);
population.remove(pokemon2);
pokemon1.addLoss();
defeated.add(pokemon1);
population.remove(pokemon1);
}
else if (pokemon1.getCombatScore() > pokemon2.getCombatScore()
&& pokemon2.isLegendary() && pokemon1.isLegendary()) {
bufferedWriter.write("The legendary " + pokemon1.getName() + " defeats the legendary " +
pokemon2.getName() + " with a combat score of " + pokemon1.getCombatScore() + "!");
bufferedWriter.newLine();
/* pokemon2 gains xp from winning */
pokemon1.incAttack(pokemon2.getCombatScore());
pokemon1.incDefense(pokemon2.getCombatScore());
pokemon1.addVict();
victorious.add(pokemon1);
population.remove(pokemon1);
pokemon2.addLoss();
if (pokemon2.numLoss == 3) {
defeated.add(pokemon2);
population.remove(pokemon2);
}
}
else if (pokemon1.getCombatScore() > pokemon2.getCombatScore()
&& pokemon2.isLegendary()) {
bufferedWriter.write(pokemon1.getName() + " defeats the legendary " +
pokemon2.getName() + " with a combat score of " + pokemon1.getCombatScore() + "!");
bufferedWriter.newLine();
/* pokemon2 gains xp from winning */
pokemon1.incAttack(pokemon2.getCombatScore());
pokemon1.incDefense(pokemon2.getCombatScore());
pokemon1.addVict();
if (pokemon1.numVict == 3) {
bufferedWriter.write(pokemon1.getName() + " has become legendary!");
}
victorious.add(pokemon1);
population.remove(pokemon1);
pokemon2.addLoss();
if (pokemon2.numLoss == 3) {
defeated.add(pokemon2);
population.remove(pokemon2);
}
}
else if (pokemon1.getCombatScore() > pokemon2.getCombatScore()
&& pokemon1.isLegendary()) {
bufferedWriter.write("The legendary " + pokemon1.getName() + " defeats " +
pokemon2.getName() + " with a combat score of " + pokemon1.getCombatScore() + "!");
bufferedWriter.newLine();
/* pokemon2 gains xp from winning */
pokemon1.incAttack(pokemon2.getCombatScore());
pokemon1.incDefense(pokemon2.getCombatScore());
pokemon1.addVict();
victorious.add(pokemon1);
population.remove(pokemon1);
pokemon2.addLoss();
defeated.add(pokemon2);
population.remove(pokemon2);
}
else {
bufferedWriter.write(pokemon1.getName() + " defeats " +
pokemon2.getName() + " with a combat score of " + pokemon1.getCombatScore() + "!");
bufferedWriter.newLine();
/* pokemon2 gains xp from winning */
pokemon1.incAttack(pokemon2.getCombatScore());
pokemon1.incDefense(pokemon2.getCombatScore());
pokemon1.addVict();
if (pokemon1.numVict == 3) {
bufferedWriter.write(pokemon1.getName() + " has become legendary!");
}
victorious.add(pokemon1);
population.remove(pokemon1);
pokemon2.addLoss();
defeated.add(pokemon2);
population.remove(pokemon2);
}
}while (population.size() != 0);
以下是.txt文件“永远”运行时的示例屏幕截图:
272 Stoutland defeats Loudred with a combat score of 5415!
273 Croconaw defeats Electrike with a combat score of 3272!
274 Rapidash defeats Meditite with a combat score of 7390!
275 Jynx defeat
我已经尝试调试并逐步完成每个条件以确保它们正常工作。我很有信心他们都这样做。问题出在其他地方吗?当人口中存在奇怪数量的口袋妖怪时,它是否会遇到问题?任何建议将不胜感激。
答案 0 :(得分:1)
问题在于,当为战斗选择两个竞争者时,你进入一个永不退出的循环:
pokemon1 = population.get((int) (Math.random() * population.size()));
do {
pokemon2 = population.get((int) (Math.random() * population.size()));
} while (pokemon1.getId() == pokemon2.getId());
这看起来很好,但是如果人口只有一个口袋妖怪会怎么样?它永远不会选择第二个并且永远不会退出循环。
至于为什么它停止写中句,你正在使用缓冲的编写器,它不一定立即将它的内容刷新到文件中。当你陷入循环时,编写器正在等待更多信息写入,并将其余部分保留在缓冲区中。对flush()的调用可以帮助你解决这个问题,但确保有足够的口袋妖怪进行战斗是一个更大的问题。