Untitled
unknown
plain_text
2 years ago
1.4 kB
9
Indexable
import java.util.Random;
class Player {
String name;
int health;
public Player(String name) {
this.name = name;
this.health = 100; // Initial health
}
public void takeDamage(int damage) {
health -= damage;
if (health < 0) {
health = 0;
}
}
public boolean isAlive() {
return health > 0;
}
}
public class BattleRoyaleGame {
public static void main(String[] args) {
Player player1 = new Player("Player 1");
Player player2 = new Player("Player 2");
Random random = new Random();
while (player1.isAlive() && player2.isAlive()) {
int damage = random.nextInt(20) + 1; // Random damage between 1 and 20
player1.takeDamage(damage);
damage = random.nextInt(20) + 1;
player2.takeDamage(damage);
System.out.println("Player 1 health: " + player1.health);
System.out.println("Player 2 health: " + player2.health);
System.out.println();
}
if (player1.isAlive()) {
System.out.println("Player 1 wins!");
} else if (player2.isAlive()) {
System.out.println("Player 2 wins!");
} else {
System.out.println("It's a draw!");
}
}
}
Editor is loading...
Leave a Comment