Я новичок в java, поэтому, пожалуйста, будьте осторожны.
Итак, у меня есть класс mainPlayer и badPlayer , который является суперклассом для других подклассы.
mainPlayer
имеет экземпляр int health
, установленный на 100
badPlayer
, имеет здоровье и меняется в зависимости от подклассов. У обоих классов есть атаки, которые рассчитываются случайными числами
Я создал другой класс Battle
, который содержит while
l oop в методе, но я пытаюсь выяснить, как сделать его l oop, пока здоровье одного из игроков не станет равным 0? Целью метода является атака обоих игроков: если одна атака больше другой, игрок теряет здоровье. Цель состоит в том, чтобы когда один из игроков получил здоровье <= 0, метод вывести «Вы выиграли» или «Игра окончена». Любая помощь по этому поводу? </p>
// The Battle class where the while loop is
public static String startFight(BadPlayer b, MainPlayer m) {
int damage1 = m.maxAttackMain();
int damage2 = b.maxAttack();
int mainPlayerHealth = m.getHealth();
int enemyPlayerHealth = b.getHealth();
String outcome = " ";
while(m.health<=0 || b.health<=0) {
int decreaseHealth = 20;
if (damage1 > damage2) {
mainPlayerHealth =- decreaseHealth;
mainPlayerHealth--;
outcome += "game over";
}
if (damage1 < damage2) {
enemyPlayerHealth=-decreaseHealth;
enemyPlayerHealth--;
outcome += "You win";
}
}
return outcome;
}
// The enemy class
public class BadPlayer {
String description;
int health;
protected int award;
int max_attack;
int max_block;
public BadPlayer(String description, int health, int attack, int block) {
this.description=description;
this.health = health;
this.award=50;
this.max_attack = attack;
this.max_block=block;
}
Random rnd = new Random();
public int maxAttack() {
int rand_int1 = 1 + rnd.nextInt(max_attack);
return rand_int1;
}
public int maxBlock() {
int rand_int2 = 1 + rnd.nextInt(max_block);
return rand_int2;
}
public int awardCarried(){
return this.award;
}
public String getDesc(){
return description;
}
public int getHealth(){
return health;
}
}
// Class Mainplayer
public class MainPlayer {
public int health;
int max_attack;
int max_block;
public MainPlayer() {
this.health=100;
this.max_attack = 100;
this.max_block = 150;
}
public int getMaxAttack() {
return max_attack;
}
public int getHealth() {
return health;
}
public int getMaxBlock() {
return max_block;
}
Random random = new Random();
public int maxAttackMain() {
int rand_int1 = 50 + random.nextInt(max_attack);
return rand_int1;
}
public int maxBlockMain() {
int rand_int2 = 1 + random.nextInt(max_attack);
return rand_int2;
}
}
Большое спасибо за любую помощь!