【问题标题】:Trying to ask if the user want to do another round. In java code试图询问用户是否想再做一轮。在java代码中
【发布时间】: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


【解决方案1】:

我相信在game over之后很难问你是否要重启游戏的原因是你可能没有意识到它正在使用recursion重启游戏。

你打电话

   fieldOfHonor.battle();

然后你去

    endGame();

去哪

     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(); // <-- look at that line of recursion right there
     }

这会“重启”游戏。实际上,您可以将其替换为布尔返回值和 do-while 循环。

public void battle() {
    boolean restart = false;
    do {
        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'");
            }
        }
        restart = endGame();
    } while (restart);
}

像这样

 public boolean endGame() {
     boolean restart = false;
     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(); // nope
        restart = true;
      }
    } else if(!this.currentOpponent.isAlive()){
        System.out.println("You died and left the game");
    }

    if(!restart) { // changed here slightly
        System.out.println("Game Over.");
    }
    return restart;
}

这样,你就可以改变了

    FieldOfHonor fieldOfHonor = new FieldOfHonor(player1, opponents);
    String input = "no";
    do {
        fieldOfHonor.battle();
        System.out.println("Would you like to play another game?");
        input = reader.readInput();
    } while("yes".equals(input));

【讨论】:

  • 那行得通,现在我什至了解到,如果我想从其他方法重新启动,我可以添加一个布尔值!谢谢!
  • 不客气! :) 如果它从头到尾都有效,请考虑勾选答案左上角的复选标记以“接受”它。
  • Okey :) 这是我在这里的第一篇文章,所以我不知道,呵呵。谢谢你的帮助,真的很有帮助:)
猜你喜欢
  • 2012-02-19
  • 2020-02-15
  • 2016-09-22
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
相关资源
最近更新 更多