【发布时间】:2015-04-18 14:08:50
【问题描述】:
如果玩家还活着并希望与另一个对手战斗,这是我想从一个新对手重新开始的 while 循环。当玩家击败对手并且 while 循环结束时,它会转到 endGame() 方法并结束游戏,而不是等待另一个用户输入。所以基本上,我怎样才能让 java 终端等待另一个用户输入,然后再次从包含 while 循环的 Battle() 方法开始。
import java.util.ArrayList;
import java.util.Random;
public class FieldOfHonor {
private Player player;
private ArrayList<Opponent> opponents;
private Opponent currentOpponent;
private Random random;
private InputReader reader;
private String stance;
/**
* Initializes a FieldOfHonor with a player, a list of opponents,
* a Random-object and a random opponent.
*
* @param player - the player who will shoot 'em up
* @param opponents - target practice
*/
public FieldOfHonor(Player player, ArrayList<Opponent> opponents) {
this.player = player;
this.opponents = opponents;
this.random = new Random();
this.currentOpponent = selectRandomOpponent();
this.reader = new InputReader();
}
/**
* Starts the battle and handles input from the player. Controls which methods are called
* during battle.
*/
public void battle() {
printWelcome();
while(this.player.isAlive() && this.currentOpponent.isAlive()) {
String input = reader.getInput();
if(input.contains("attack")) {
newRound();
} else if (input.contains("run")) {
run();
break;
} else if (input.contains("joke")) {
System.out.println("What did the homeless man get for christmas? Nothing.");
} else if (input.contains("use")){
String itemName = input.replace("use ", "");
player.useItem(itemName);
} else if (input.contains("stance "+"")){
this.player.setStance(input.split(" ", 2) [1]);
}
else {
System.err.println("What? Choose 'attack', 'joke' 'use <item>' or 'run'");
}
}
endGame();
}
/**
* Handles one round of attacking. The player attacks the opponent and the opponent
* attacks back if it is still alive after the player attack
*/
public void newRound() {
int dmgToOpponent = this.player.attack(this.currentOpponent);
System.out.println(this.player.getName() + " attacks " + this.currentOpponent.getName());
System.out.println(this.currentOpponent.getName() + " looses " + dmgToOpponent + " hp");
System.out.println(this.currentOpponent.getName() + " now has " + this.currentOpponent.getHealth() + " hp");
System.out.println();
if(this.currentOpponent.isAlive()) {
int dmgToPlayer = this.currentOpponent.attack(this.player);
System.out.println(this.currentOpponent.getName() + " attacks " + this.player.getName());
System.out.println(this.player.getName() + " looses " + dmgToPlayer + " hp");
System.out.println(this.player.getName() + " now has " + this.player.getHealth() + " hp");
}
}
/**
* Handles what happens if the player runs away. Takes 25 reputation from the player.
*/
public void run() {
int reputationLoss = 25;
this.player.setReputation(this.player.getReputation() - reputationLoss);
System.out.println(this.player.getName() + " runs from the duel and looses " + reputationLoss + " reputation.");
}
public void endGame() {
String input = reader.getInput();
int pointsWon = this.random.nextInt(91) + 10;
this.player.setReputation(this.player.getReputation() + pointsWon);
System.out.println(this.player.getName() + " wins the game and an extra " + pointsWon + " reputation");
System.out.println("");
if(this.player.isAlive() && !this.currentOpponent.isAlive() && opponents.size() != 0 ) {
System.out.println("Do you want to fight another opponent? yes or run?");
if(input.contains("yes")){
this.selectRandomOpponent();
this.battle();
}
} else if(!this.currentOpponent.isAlive()){
System.out.println("You died and left the game");
}
System.out.println("Game Over.");
}
/**
* Prints a welcome message at the start of the battle
*/
public void printWelcome() {
System.out.println("Welcome to the Field of Honor!");
System.out.println("Your opponent is " + this.currentOpponent.getName() + "!");
}
/**
* Selects a random opponent from the list of opponents
* and removes it from the list.
*
* @return a random opponent
*/
public Opponent selectRandomOpponent() {
return this.opponents.remove(this.random.nextInt(this.opponents.size()));
}
}
// 启动游戏的主要方法
public static void main(String[] args) {
Weapon weapon1 = new Weapon("Bare hands", "Skullcrusher and facebreaker", 0, 0, "punch", 1000);
Weapon weapon2 = new Weapon("Winchester Carbine", "Fires .44-40 cartridge shots", 40, 8, "shoots", 30);
Weapon weapon3 = new Weapon("Knife", "A small but sharp blade", 10, 1, "cuts", 10);
Weapon weapon4 = new Weapon("Colt .45", "A fine six-shot revolver", 50, 3, "shoots", 20);
Potion potion1 = new Potion("Fullrestore", "A magical elixir that heals some health", 15, 4, "Heals some health", 100);
Potion potion2 = new Potion("Apple", "Tasty tree fruit. One of five each day", 10, 1, "eats", 20);
Potion potion3 = new Potion("Health Potion", "A red liquid that heals wounds when you drink it.. weird, I know", 10, 1, "drinks", 50);
Player player1 = new Player("Billy", "Thief", 50, 20, 50, weapon1);
player1.addItem(weapon1);
player1.addItem(weapon2);
player1.addItem(weapon3);
player1.addItem(weapon4);
player1.addItem(potion1);
player1.addItem(potion2);
player1.addItem(potion3);
System.out.println(player1);
ArrayList<Opponent> opponents = new ArrayList<Opponent>();
opponents.add(new Opponent("Billy the Kid", "Gang Leader", 30));
opponents.add(new Opponent("Joe Dalton", "Robber", 35));
opponents.add(new Opponent("Everett Murdoch", "Nemesis", 50));
opponents.add(new Opponent("Marty McFly", "FutureMan", 22));
opponents.add(new Opponent("Rattata", "Dog", 21));
FieldOfHonor fieldOfHonor = new FieldOfHonor(player1, opponents);
fieldOfHonor.battle();
}
}
【问题讨论】:
-
粘贴你看到的控制台输出?
-
我看不到屏幕截图吗?
标签: java