Skip to content

SiriWastaken/Battle-Bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Battle Bot - How It Works (OOP Overview)

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.


Class Overview

Main

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.


Robot

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.


How Each Class Works

Main Class

Game Setup

When the program starts, I:

  • Create a Scanner for input
  • Create a Random object for probability-based events
  • Instantiate a Robot object with starting stats

This gives me a single robot instance that the entire game revolves around.


Game Loop

I use a while loop to keep the game running. Every loop represents one round.

Inside each round, I:

  1. Ask the player for a move
  2. Validate the input
  3. Execute the chosen action
  4. Check for win or loss

Player Actions

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.


Win Conditions

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.


Robot Class

Fields (State)

I store everything that defines the robot:

  • health for survivability
  • energy for limiting attacks
  • isDefending to track defence mode
  • isAttackable to control damage timing
  • damageAmount for attack power
  • enemyHealth to track the opponent

Constructor

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.


attack()

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.


defend()

When I call defend(), the robot enters a defensive state and becomes temporarily unattackable. This is how I model blocking without needing complex mechanics.


takeDamage(int damageAmount)

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.


endDefend()

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.


damageAmount()

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.


Why I Used OOP Here

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.

About

A simple Java console game where I control a battle robot using attack, defend, or neutral moves. Built to practice OOP, the game uses a main loop for turn logic and a Robot class to manage health, energy, and combat behavior. It’s unfinished, a bit rough, but designed to be expandable and easy to understand.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages