Licznik zapętlonego konstruktora

0

Czy da się policzyć w jakiś sposób ile razy wygrał poszczególny warrior ( metoda winsCounter() )?
Pomimo czterech obróceń konstruktora firstWins/secondWins ma wartość 1.
Próbuję zapętlić konstruktor. Domyślam się że za każdym razem tworzę nowy obiekt i każdy ma swoje prywatne instancje. Ale innych pomysłów brak.

public class Battle {

    private ICreature firstWarrior;
    private ICreature secondWarrior;
    private ICreature wonWarrior;
    private double hitPower;
    private double firstWarriorHP;
    private double secondWarriorHP;
    private ICreature winner;
    private int firstWins;
    private int secondWins;
    private int repeats;

    public Battle(ICreature firstWarrior, ICreature secondWarrior, int repeats) {

        this.firstWarrior = firstWarrior;
        this.secondWarrior = secondWarrior;
        this.firstWarriorHP = firstWarrior.getHitPoints();
        this.secondWarriorHP = secondWarrior.getHitPoints();
        this.winner = battleLoop();
        this.repeats = repeats;
        repeats();
    }

    public double getFirstWarriorHP() {
        return firstWarriorHP;
    }

    public double getSecondWarriorHP() {
        return secondWarriorHP;
    }

    public ICreature getWinner() {
        return winner;
    }

    /**
     * Method that returns warrior who can hit now
     *
     * @return who can hit now
     */
    private ICreature whoAttack() {
        Random random = new Random();
        int first = random.nextInt(10);
        int second = random.nextInt(10);

        if(this.firstWarrior.getSpeed() * first > this.secondWarrior.getSpeed() * second) {
            System.out.println("whoAttack() " + firstWarrior.getName() + " --->");
            return this.firstWarrior;
        } else if (this.firstWarrior.getSpeed() * first < this.secondWarrior.getSpeed() * second){
            System.out.println("<--- " + secondWarrior.getName() + " whoAttack()");
            return this.secondWarrior;
        } else {
            System.out.println("||-whoAttack() " + "draw-||");

            return whoAttack();
        }
    }


    /**
     * Method that set how strong Warrior hit
     */
    private void hitPower() {
        Random random = new Random();
        double hitAccuracy = random.nextDouble()*10;

        this.hitPower = wonWarrior.getStrength() * hitAccuracy;
        System.out.printf("%nhitPower() " + "%.2f%n",this.hitPower);
        System.out.println();
    }


    /**
     * Method that set HP of warrior after hit
     */
    private void hpAfterHit() {
        if(this.wonWarrior == this.firstWarrior) {
            this.secondWarriorHP = secondWarriorHP - hitPower;
            System.out.printf("hpAfterHit() " + "%.2f%n",secondWarriorHP);
            System.out.printf("^===-----------------===^%n%n");
        } else {
            this.firstWarriorHP = firstWarriorHP - hitPower;
            System.out.printf("hpAfterHit() " + "%.2f%n",firstWarriorHP);
            System.out.printf("^===-----------------===^%n%n");
        }
    }


    /**
     * Method that returns full battle winner
     *
     * @return full battle winner
     */
    private ICreature battleLoop() {
        while (this.firstWarriorHP > 0) {
            while (this.secondWarriorHP > 0) {

                System.out.printf("==========================%n");
                this.wonWarrior = whoAttack();
                hitPower();

                if (firstWarrior == wonWarrior) {
                    System.out.println("v===-----------------===v");
                    System.out.print(this.secondWarrior.getName() + " - ");
                } else if (secondWarrior == wonWarrior) {
                    System.out.println("v===-----------------===v");
                    System.out.print(this.firstWarrior.getName() + " - ");
                } else {
                    whoAttack();
                }
                hpAfterHit();

                battleLoop();

                break;
            }
            break;
        }
        if (this.firstWarriorHP > this.secondWarriorHP) {
            return this.firstWarrior;
        } else {
            return this.secondWarrior;
        }
    }

    /**
     * Method that counts wins
     */
    private void winsCounter() {
        if (winner == firstWarrior) {
            this.firstWins++;
        } else {
            this.secondWins++;
        }
    }

    /**
     * Method that shows number of wins for each
     */
    public void howManyTimesWon() {
        System.out.println(this.firstWarrior.getName() + " wins: "  + this.firstWins);
        System.out.println(this.secondWarrior.getName() + " wins: "  + this.secondWins);
    }

    /**
     * Method that repeats this constructor
     */
    public void repeats() {
        winsCounter();
        if(repeats > 0) {
            new Battle(firstWarrior, secondWarrior, repeats-1);
        } else {
            howManyTimesWon();
        }
    }

}

main:

public class Killers {
    public static void main (String[] args) {
        ICreature cow = new Cow("COW");
        ICreature cat = new Cat("CAT");

        Battle battle = new Battle(cow, cat, 3);

        System.out.println("===============================================");
        System.out.printf("Left HP - " + cow.getName() + " " + "%.2f%n",battle.getFirstWarriorHP());
        System.out.printf("Left HP - " + cat.getName() + " " + "%.2f%n",battle.getSecondWarriorHP());
        System.out.println("===============================================");
        System.out.println("WINNER: " + battle.getWinner().getName());
        battle.howManyTimesWon();

    }
}
 
1

i tak cały kod jest do przepisania.... więc teraz po prostu zmień deklarację zmiennych:

    private static int firstWins = 0;
    private static int secondWins = 0;
1

@Spine, nie.

@Dawid Sosnowicz, dużo błędów, które utrudniają ci pracę. Po pierwsze w konstruktorze ustaw tylko pola, ale nie wywołuj żadnej metody "biznesowej". Następnie mając obiekt typu Battle dodaj do niego metodę fight, która rozpocznie walkę. Walka składa się z kilku rund (klasa Round), gdzie każda runda zawiera informację o zwycięzcy.

Metoda fight zwróci ci listę rund, którą można już odpowiednio upchnąć w pętlę, by otrzymać podsumowanie.

0

@Koziołek: próbuję zrobić jak mówisz od paru godzin i już zgłupiałem.
Przede wszystkim nie wiem za bardzo jak miałaby wyglądać klasa Round:

public class Battle {

    private ICreature firstWarrior;
    private ICreature secondWarrior;
    private ICreature wonWarrior;
    private double hitPower;
    private double firstWarriorHP;
    private double secondWarriorHP;
    private ICreature winner;
    private int firstWins;
    private int secondWins;
    private int repeats;
    Round rounds;

    public Battle(ICreature firstWarrior, ICreature secondWarrior, int repeats) {

        this.firstWarrior = firstWarrior;
        this.secondWarrior = secondWarrior;
        this.firstWarriorHP = firstWarrior.getHitPoints();
        this.secondWarriorHP = secondWarrior.getHitPoints();
        this.repeats = repeats;
    }

    public Battle() {}


    public double getFirstWarriorHP() {
        return firstWarriorHP;
    }

    public double getSecondWarriorHP() {
        return secondWarriorHP;
    }

    public ICreature getWinner() {
        return winner;
    }

    public List<ICreature> getRounds() {
        return rounds.getWinners();
    }

    /**
     * Method that returns warrior who can hit now
     *
     * @return who can hit now
     */
    private ICreature whoAttack() {
        Random random = new Random();
        int first = random.nextInt(10);
        int second = random.nextInt(10);

        if(this.firstWarrior.getSpeed() * first > this.secondWarrior.getSpeed() * second) {
            System.out.println("whoAttack() " + firstWarrior.getName() + " --->");
            return this.firstWarrior;
        } else if (this.firstWarrior.getSpeed() * first < this.secondWarrior.getSpeed() * second){
            System.out.println("<--- " + secondWarrior.getName() + " whoAttack()");
            return this.secondWarrior;
        } else {
            System.out.println("||-whoAttack() " + "draw-||");

            return whoAttack();
        }
    }


    /**
     * Method that set how strong Warrior hit
     */
    private void hitPower() {
        Random random = new Random();
        double hitAccuracy = random.nextDouble()*10;

        this.hitPower = wonWarrior.getStrength() * hitAccuracy;
        System.out.printf("%nhitPower() " + "%.2f%n",this.hitPower);
        System.out.println();
    }


    /**
     * Method that set HP of warrior after hit
     */
    private void hpAfterHit() {
        if(this.wonWarrior == this.firstWarrior) {
            this.secondWarriorHP = secondWarriorHP - hitPower;
            System.out.printf("hpAfterHit() " + "%.2f%n",secondWarriorHP);
            System.out.printf("^===-----------------===^%n%n");
        } else {
            this.firstWarriorHP = firstWarriorHP - hitPower;
            System.out.printf("hpAfterHit() " + "%.2f%n",firstWarriorHP);
            System.out.printf("^===-----------------===^%n%n");
        }
    }


    /**
     * Method that returns full battle winner
     *
     * @return full battle winner
     */
    private ICreature battleLoop() {
        while (this.firstWarriorHP > 0) {
            while (this.secondWarriorHP > 0) {

                System.out.printf("==========================%n");
                this.wonWarrior = whoAttack();
                hitPower();

                if (firstWarrior == wonWarrior) {
                    System.out.println("v===-----------------===v");
                    System.out.print(this.secondWarrior.getName() + " - ");
                } else if (secondWarrior == wonWarrior) {
                    System.out.println("v===-----------------===v");
                    System.out.print(this.firstWarrior.getName() + " - ");
                } else {
                    whoAttack();
                }
                hpAfterHit();


                battleLoop();

                break;
            }
            break;
        }
        if (this.firstWarriorHP > this.secondWarriorHP) {
            return this.firstWarrior;
        } else {
            return this.secondWarrior;
        }
    }

    /**
     * Method that counts wins
     */
    private void winsCounter() {
        if (winner == firstWarrior) {
            firstWins++;
        } else {
            secondWins++;
        }
    }

    /**
     * Method that shows number of wins for each
     */
    public void howManyTimesWon() {
        System.out.println(this.firstWarrior.getName() + " wins: "  + firstWins);
        System.out.println(this.secondWarrior.getName() + " wins: "  + secondWins);
    }

    /**
     * Method that repeats this constructor
     */
//    public void repeats() {
//        winsCounter();
//        if(repeats > 0) {
//            new Battle(firstWarrior, secondWarrior, repeats-1);
//        } else {
//            howManyTimesWon();
//        }
//    }

    /**
     * method that starts fight
     * @return Round object with winner of fight as argument
     */
    public Round fight() {
        winner = battleLoop();

        return new Round(winner);
    }
}
public class Round {
//    int rounds;
    private List<ICreature> winners = new ArrayList<>();

    public Round(ICreature iCreature/*, int rounds*/) {
        this.winners.add(iCreature);
//        this.rounds = rounds;
    }

//    public int getRounds() {
//        return rounds;
//    }

    public List<ICreature> getWinners() {
        return winners;
    }

//    public void Rounds() {
//        while (rounds > 0) {
//            Battle battle = new Battle();
//            battle.fight();
//            rounds--;
//        }
//    }

}

main:

public class Killers {
    public static void main (String[] args) {
        ICreature cow = new Cow("COW");
        ICreature cat = new Cat("CAT");

        Battle battle = new Battle(cow, cat, 3);
        battle.fight();
        System.out.println("===============================================");
        System.out.printf("Left HP - " + cow.getName() + " " + "%.2f%n",battle.getFirstWarriorHP());
        System.out.printf("Left HP - " + cat.getName() + " " + "%.2f%n",battle.getSecondWarriorHP());
        System.out.println("===============================================");
        System.out.println("WINNER: " + battle.getWinner().getName());
        System.out.println("WINNER of all fights: " + battle.getRounds());
        battle.howManyTimesWon();

    }
}
1

W battle zrób drugi konstruktor:

public Battle(ICreature firstWarrior, ICreature secondWarrior, int repeats, int firstWins, int secondWins) {

        this.firstWins=firstWins;
        this.secondWins=secondWins;
        this.firstWarrior = firstWarrior;
        this.secondWarrior = secondWarrior;
        this.firstWarriorHP = firstWarrior.getHitPoints();
        this.secondWarriorHP = secondWarrior.getHitPoints();
        this.winner = battleLoop();
        this.repeats = repeats;
        repeats();
    }

powinno zadziałać

i zmien metode:

public void repeats() {
        winsCounter();
        if(repeats > 0) {
            new Battle(firstWarrior, secondWarrior, repeats-1,firstWins,secondWins);
        } else {
            howManyTimesWon();
        }
    }
0

Nie tak… (uwaga pisane na szybko bez sprawdzania składni na 100%):


class Battle{
    public Battle(ICreature czerwony, ICreature niebieski, int maxRounds){
       // ustawiasz sobie pola
   }


  public List<Round> fight(){
        List<Rounds> rounds  = new ArrayList<>(maxRounds);
    
       for(int i =0; i < maxRounds; i++){
           Round r = new Round(czerwony, niebieski);
           r.fight();
           rounds.add(r);
         }
     return rounds;
   }  
}


class Round{

       private ICreature winner = null;

      public Round(ICreature czerwony, ICreature niebieski){
              // ustawiasz pola
       }

      public Round fight(){
           // walka
          this.winner = andRhwWinnerIs;
         return this;
      }
}

TO tak mniej więcej powinno wyglądać.

1 użytkowników online, w tym zalogowanych: 0, gości: 1