我将如何存储健康值并再次运行,直到玩家健康== 0或敌人健康== 0

时间:2014-02-22 19:09:06

标签: java eclipse

对我来说,编码两个角色之间的实际攻击/防守的最佳方式是什么?如何存储健康值以便可以存储重新攻击,直到玩家健康或敌人的生命值达到0,然后宣告胜利者。这是我从各种渠道进行自我教学后第一次尝试任何类型的编程,请给我反馈我可以做的任何改进,我相信会有很多。

提前谢谢。

: - )

package test;

public class BattleClass {

    public static void main(String[] args) {
        PlayerStats ps = new PlayerStats();
        EnemyStats es = new EnemyStats();
        int eh = es.getEnemyHealth();
        int ph = ps.getPlayerHealth();

        ps.PlayerAttackDefend();
        es.AttackDefend();

        System.out.println("You chose to " + ps.getpInput() + " and rolled "
                + ps.getPlayerRoll());

        System.out.println("The enemy chose to " + es.getEaod()
                + " and rolled " + es.getEnemyRoll() + ".");

        if (ps.getpInput().equals("Attack")) {

            if (es.getEaod().equals("Attack")) {

                System.out
                        .println("YOUR SWORDS BOUNCE OFF EACHOUTHERS... TRY AGAIN!");
                System.exit(0);

            }

            if (es.getEaod().equals("Defend")) {

                if (ps.getPlayerRoll() > es.getEnemyRoll())

                    eh -= ps.getPlayerRoll() - es.getEnemyRoll();

                System.out.println("Enemy Health is " + eh);
            }
        }

        if (ps.getpInput().equals("Defend")) {

            if (es.getEaod().equals("Defend")) {

                System.out
                        .println("YOUR SHIELDS BOUNCE OFF EACHOTHERS... TRY AGAIN!");
                System.exit(0);
            }
        }

        if (es.getEaod().equals("Attack")) {

            if (es.getEnemyRoll() > ps.getPlayerRoll())

                ph -= es.getEnemyRoll() - ps.getPlayerRoll();

            System.out.println("Your Health is " + ph);

        }
    }
}

package test;

import java.util.Random;
import java.util.Scanner;

public class PlayerStats {

    static Scanner paod = new Scanner(System.in);

    //Players initial health value.

    private int playerHealth = 10;

    //RNG for attack value / defence value using dice as object.

    private int playerRoll = new Random().nextInt(6) + 1;
    private String pInput;

    //Method for selecting Attack or Defence.

    public void PlayerAttackDefend() {

        System.out.println("Do you want to Attack or Defend?");
        System.out.println("a = Attack  / d = Defend");

        //Player selects attack or defend.

        String userInput = paod.nextLine();
        if (userInput.equals("a")) {
            pInput = "Attack";
        }

        if (userInput.equals("d")) {
            pInput = "Defend";

        }
    }

    public static Scanner getPaod() {
        return paod;
    }


    public int getPlayerHealth() {
        return playerHealth;
    }


    public int getPlayerRoll() {
        return playerRoll;
    }


    public String getpInput() {
        return pInput;
    }


    public static void setPaod(Scanner paod) {
        PlayerStats.paod = paod;
    }


    public void setPlayerHealth(int playerHealth) {
        this.playerHealth = playerHealth;
    }


    public void setPlayerRoll(int playerRoll) {
        this.playerRoll = playerRoll;
    }


    public void setpInput(String pInput) {
        this.pInput = pInput;
    }
}

package test;

import java.util.Random;

public class EnemyStats {

    //Enemy initial health value.

    private int enemyHealth = 10;

    //RNG for attack value / defence value using dice as object.

    private static int enemyRoll = new Random().nextInt(6) + 1;

    //RNG for enemy decision to Attack or Defend.

    private static int eAttackDefend = new Random().nextInt(2) + 1;

    //Used for returning attack or defend string.

    private static String eaod;

    //Attack or Defend method.

    public void AttackDefend() {

        if (eAttackDefend == 1) {
            eaod = "Attack";

        } else {

            eaod = "Defend";
        }
    }

    public int getEnemyHealth() {
        return enemyHealth;
    }

    public int getEnemyRoll() {
        return enemyRoll;
    }

    public int geteAttackDefend() {
        return eAttackDefend;
    }

    public String getEaod() {
        return eaod;
    }

    public void setEnemyHealth(int enemyHealth) {
        this.enemyHealth = enemyHealth;
    }

    public void setEnemyRoll(int enemyRoll) {
        EnemyStats.enemyRoll = enemyRoll;
    }

    public void seteAttackDefend(int eAttackDefend) {
        EnemyStats.eAttackDefend = eAttackDefend;
    }

    public void setEaod(String eaod) {
        EnemyStats.eaod = eaod;
    }

}

2 个答案:

答案 0 :(得分:0)

如果你想要“治愈”,一个简单的方法就是设置maxHp和actualHp值。 如果你只是减少直到一个人死了,你可以减少你已经拥有的实际健康变量。

您可能想要查看一般的继承,因为您有很多重复的代码。

一般情况下,只需制作一个循环

while(ps.getHealth() > 0 && es.getHealth() > 0) {
  // your battle code
}

您可能希望删除System.exit(0)来电,因为他们会终止该计划。

向玩家/敌人添加dealDamage(int damage)方法,以确实能够降低他们的健康状况 健康值应该在对象中,您不需要将它们存储在BattleClass中。

答案 1 :(得分:0)

我可以给你一个简短的答案,但我想你会从详细的解释中得到更多: - )

你想要运行你的代码"直到玩家健康或敌人的健康状况达到0"所以你需要一个循环。

在java中你有3种循环:

for循环

for(int i=1;i<=3;i++) System.out.println("Hello Musketeer Nr. "+i);

最复杂的循环,for循环由三部分组成,初始化,条件和事后补充。虽然for循环可以使用不同的方式,但它主要以这里显示的方式使用,也就是说,你有一个计数器变量,你需要它以某种方式。

如果您不需要计数器变量值,则可以使用带有集合和数组的简短形式:

for(Person p: persons) System.out.println("Hello, "+person.getName()+"!");

while循环 第二个最常用(至少是我)循环,它有一个初始条件并迭代,只要它是真的。

while(ph>0&&eh>0)
{
 ...
}

如您所见,它非常适合您的问题。为了完整起见,我将描述第三个循环,即

do-while循环

do
{
 ...
}
while(ph>0&&eh>0)

你像while循环一样使用这个循环但是如果你想要至少有一次运行。

其他备注

为什么在战斗系统中有两个类PlayerStats和EnemyStats(它们似乎都具有相同的动作和值)?你可能只有:

Stats playerStats=new Stats();
Stats enemyStats=new Stats();