You don't need a manual to run this. Download it, open it in your IDE, hit run, and start throwing hands with a robot.
Controls:
- A → Attack
- D → Defend
- N → Neutral (50% chance the enemy smacks you for 30 HP)
This is unfinished. It is not optimized. And that’s totally fine. What matters is that it actually uses OOP, not just classes slapped together.
Below is a deep, line-by-line breakdown of how this thing works and how object-oriented thinking shows up throughout.
This class runs the game loop. I use it to:
- Handle user input
- Decide what happens each turn
- Call methods on the robot
- Check win and loss conditions
Main does not store game logic directly in variables. Instead, it tells the robot what to do and reacts to the robot’s state.
This class represents a robot as an object with:
- State (health, energy, defence)
- Behaviour (attack, defend, take damage)
All combat-related logic lives here so that it can be reused or expanded later.
When the program starts, I:
- Create a
Scannerfor input - Create a
Randomobject for probability-based events - Instantiate a
Robotobject with starting stats
This gives me a single robot instance that the entire game revolves around.
I use a while loop to keep the game running. Every loop represents one round.
Inside each round, I:
- Ask the player for a move
- Validate the input
- Execute the chosen action
- Check for win or loss
Attack (A)
When you choose to attack, I call the robot’s attack() method, reduce energy, and apply damage to the enemy. The robot decides whether to attack based on its energy and state.
Defend (D)
When you choose to defend, I call robot.defend(), which puts the robot into a defensive state, temporarily preventing it from being attacked.
Neutral (N)
When you choose neutral, I do nothing that round. Instead, the enemy has a 50% chance to attack me using a random number. If it attacks, you lose 30 health.
This adds risk without player input.
At the end of each round, I check:
- If my robot’s health is zero or less, I lose
- If the robot’s energy reaches 0, I win
These checks are based entirely on the robot’s state.
I store everything that defines the robot:
healthfor survivabilityenergyfor limiting attacksisDefendingto track defence modeisAttackableto control damage timingdamageAmountfor attack powerenemyHealthto track the opponent
When I create a robot, the constructor initializes its starting values. This allows me to easily create robots with different stats later if I want to expand the game.
When I wrote attack(), I wanted the robot to control its own combat logic. The method:
- Checks if there is enough energy
- Prevents attacking if the enemy is defending
- Reduces enemy health if the attack is successful
This keeps attack logic within the robot rather than in Main.
When I call defend(), the robot enters a defensive state and becomes temporarily unattackable. This is how I model blocking without needing complex mechanics.
This method reduces the robot’s health by a given amount. I included it to centralize damage handling so future features like armor or shields can be added cleanly.
I added this method to handle exiting defence mode after a short delay. It uses a timer to simulate recovery time before the robot becomes attackable again.
This method sets and returns the amount of damage the robot deals per attack. I used a method instead of hardcoding damage everywhere so I can easily change or scale it later.
I structured the project this way:
- Game flow stays in
Main - Combat logic stays in
Robot - State and behavior live together
- The code is easy to expand later
MIT License - Feel free to fork and learn! More info can be found in the license.md file.