It is a turn-based battle between two players in an arena. Each player has attributes: health, strength, and attack. The game continues in turns until one player's health reaches zero.
- Dynamic input of player attributes (health, strength, attack).
- Turn-based combat simulation between two players.
- Randomized dice rolling for attack and defense calculations.
- Determines and prints the winner based on the players' health.
- Users provide input for Player A and Player B's attributes:
health
,strength
, andattack
values. These inputs define the initial conditions and capabilities of each player in the simulated duel. - Based on the user inputs, two
Player
objects are instantiated(Player playerA = new Player(healthA, strengthA, attackA);
andPlayer playerB = new Player(healthB, strengthB, attackB);)
.
EachPlayer
object encapsulates attributes such ashealth
,strength
, andattack
, which determine their combat effectiveness. - A
Battle
object is created to manage the combat interaction between Player A and Player B(Battle battle = new Battle(playerA, playerB);)
.
TheBattle
object is initialized with references toPlayer A
andPlayer B
, preparing for the duel to commence. - The
Battle
class determines which player starts as the attacker based on their initial health. This decision is typically made to ensure fairness, often favoring the player with lowerhealth
at the start. - The
Battle
proceeds in turns, where eachPlayer
alternates between attacking and defending. During each turn, the current attacker rolls a dice (simulated by the Dice class) to determine the effectiveness of their attack(attackRoll = dice.rollDie();)
.
Simultaneously, the defending player rolls the dice to gauge the effectiveness of their defense(defendRoll = dice.rollDie();)
. Attacker
Damage: Calculate the damage inflicted by the attacker using theirattack
value and the dice roll(attackDamage = attackRoll * attacker.getAttack();)
.Defender
Defense: Determine the defender's ability to mitigate damage based on theirstrength
and the defense roll(defendDamage = defendRoll * defender.getStrength();)
.- Net Damage: Compute the net damage inflicted on the defender after considering both attack and defense (
netDamage = Math.max(0, attackDamage - defendDamage);
) - Apply the computed net damage to reduce the defender's
health
using thereduceHealth()
method of thePlayer
class.
Ensure that the defender'shealth
does not drop to (or below) zero, safeguarding against negative health values. - After each turn, roles switch between attacker and defender (
battle.switchRoles();
).
It continues in turns until the health of eitherPlayer A
orPlayer B
(or both) drops to zero or below. - Winning Condition: If one
Player
has zero or negativehealth
, declare the otherPlayer
as the winner of the duel.
User Inputs Player A and Player B Attributes
↓
Initialize Players and Match
↓
Start Battle
↓
Players Take Turns Attacking
↓
Determine Winner
↓
Print Outcome
Player A
Enter the health of Player A:
70
Enter the strength of Player A:
6
Enter the attack of Player A:
9
Player B
Enter the health of Player B:
70
Enter the strength of Player B:
6
Enter the attack of Player B:
9
Winner of magical arena: Player A wins the match!
- Download/Clone this repository.
- Install
maven
(You can skip this part if already installed) usingsudo apt-get install maven
. - Clean the maven packages for a clean build using
mvn clean
. - Install the dependencies using
mvn install
. - Build the jar file using maven
mvn compile
- Run the binary using
mvn exec:java -Dexec.mainClass=com.mycompany.App