-4

If i have 2 players in a game that is coded in java, how do i add code in order to Track the total number of attacks in the game? How many attacks each player won and track the average damage each player gave per attack?

Here is what I've done so far:

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;
    }
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97

1 Answers1

1

If you want to track the total number of attacks in a game between each player try using a static instance variable. Static instance variable are shared by all objects with the same name, so no matter how many players you make it is always possible to keep track of all attacks between them all. Java Static vs Instance?

To keep track of how many attacks each player won and track the average damage each player gave per attack, just use object instance variables again.

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;

You can then get rid of the trackAttacks method. Then replace it with the correct getters and setters methods with the correct logic.

Community
  • 1
  • 1
CJMobileApps
  • 177
  • 1
  • 12