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();
}
}