新手试图在java中创建

时间:2016-07-19 21:33:22

标签: java

如果我在一个用java编码的游戏中有2个玩家,我如何添加代码以跟踪游戏中的攻击总数?每位玩家赢得多少次攻击并追踪每位玩家每次攻击所造成的平均伤害?

这是我到目前为止所做的:

public abstract class Player {

    private int health = 1000;
    private String playerName;
    private String playerWeapon;
    private String battleLocation;
    private String specialGift;
    private boolean isPain;

    // Week2Day2 NVA.P8 HOMEWORK ACTIVITY ADDITON-  next 4 lines only. including cb's.
    public String trackAttacks(int totalAttacks, int attacksWon, int damageGiven) {
        public String totalAttacks;                                 // added for P.8  *****REMOVE ME IF WRONG
        private String totalattacksWon;                           // added for P.8  *****REMOVE ME IF WRONG
        private String averageDamageGiven;{                       // added for P.8  *****REMOVE ME IF WRONG

            public void printStats ();                            // added for P.8  *****REMOVE ME IF WRONG
            {                                                     // added for P.8  *****REMOVE ME IF WRONG
                System.out.println("Attack    " + this.attack);   // added for P.8  *****REMOVE ME IF WRONG
                // other stats                                    // added for P.8  *****REMOVE ME IF WRONG
            }                                                     // added for P.8  *****REMOVE ME IF WRONG
        }                                                         // added for P.8  *****REMOVE ME IF WRONG
    }                                                             // added for P.8  *****REMOVE ME IF WRONG


    public int getHealth() {
        return health;
    }

    public void removeHealth(int hit) {
        health -= hit;
    }

    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public String getPlayerWeapon() {
        return playerWeapon;
    }

    public void setPlayerWeapon(String playerWeapon) {
        this.playerWeapon = playerWeapon;
    }

    public String getBattleLocation() {
        return battleLocation;
    }

    public void setBattleLocation(String battleLocation) {
        this.battleLocation = battleLocation;
    }

    public String getSpecialGift() {
        return specialGift;

    }

    public void setSpecialGift(String specialGift) {
        this.specialGift = specialGift;

    }

    public boolean getisPain() {
        return isPain;

    }

    public void setisSPain(boolean isPain) {
        this.isPain = isPain;
    }

    public String gettotalAttacks() { // added for P.8  *****REMOVE ME IF WRONG
        return totalAttacks;
    }

    public void settotalAttacks(String totalAttacks) { // added for P.8  *****REMOVE ME IF WRONG
        this.totalAttacks = totalAttacks;
    }

    public String gettotalattacksWon() {                 // added for P.8 *****REMOVE ME IF WRONG
        return playerWeapon;
    }

    public void settotalattacksWon(String totalattacksWon) {// added for P.8*****REMOVE ME IF WRONG
        this.totalattacksWon = totalattacksWon;
    }

    public String getaverageDamageGiven() {           // added for P.8*****REMOVE ME IF WRONG
        return playerWeapon;
    }

    public void setaverageDamageGiven(String averageDamageGiven) {   // added for P.8*****REMOVE ME IF WRONG
        this.averageDamageGiven = averageDamageGiven;


    }

    private int getSpecialDamage(int Damage) {
        double extDamage = 1;
        int randNum = RandInt.randomInt(0, 100);


        if (getSpecialGift().equalsIgnoreCase("speed") && randNum < 75) {
            extDamage = 1.2;

        } else if (getSpecialGift().equalsIgnoreCase("power") && randNum < 50) {
            extDamage = 1.5;
        } else if (getisPain() || getSpecialGift().equalsIgnoreCase("pain") && randNum < 10) {
            setisSPain(true);
            Damage -= 3;
            if (Damage < 1) ;
            {
                Damage = 1;
            }
        }
        extDamage = extDamage * Damage;
        Double d = new Double(extDamage);
        return d.intValue();
    }

    public int getSharkAttack() {
        int biteDamage = 0;
        int randNum = RandInt.randomInt(0, 100);

        if (getBattleLocation().equalsIgnoreCase("ocean") && randNum < 25) {
            biteDamage = 100;
            System.out.println("shark attack");
        }

        if (randNum <= 5) {
            biteDamage = 100;
            System.out.println("bitten");
        }
        return biteDamage;
    }

1 个答案:

答案 0 :(得分:1)

如果您想跟踪每个玩家之间游戏中的攻击总数,请尝试使用静态实例变量。静态实例变量由具有相同名称的所有对象共享,因此无论您制作多少个玩家,总是可以跟踪它们之间的所有攻击。 Java Static vs Instance

要跟踪每位玩家获胜的攻击次数并跟踪每位玩家每次攻击所造成的平均伤害,请再次使用对象实例变量。

public abstract class Player {

   private static int totalAttacks;
   private int attacksWon;
   private int damageGiven;
   private int health = 1000;
   private String playerName;
   private String playerWeapon;
   private String battleLocation;
   private String specialGift;
   private boolean isPain;

然后你可以摆脱trackAttacks方法。然后使用正确的逻辑将其替换为正确的getter和setter方法。