'F' → Fullscreen
Create the GameCharacter class by following the example shown above.
class GameCharacter{ private String name; private int maxHealth; private int currentHealth; private int attackPower;
public GameCharacter(String name, int maxHealth, int attackPower) { this.name = name; this.maxHealth = maxHealth; this.currentHealth = maxHealth; // start full health this.attackPower = attackPower; }
public String getName() { return name; }
public int getCurrentHealth() { return currentHealth; }
public int getAttackPower() { return attackPower; }
// Reduce health but never below 0 public void takeDamage(int amount) { if (amount > 0) { currentHealth -= amount;
if (currentHealth < 0) { currentHealth = 0; } } }
// Heal but never above maxHealth public void heal(int amount) { if (amount > 0) { currentHealth += amount; if (currentHealth > maxHealth) { currentHealth = maxHealth; } } }
// Deal attack damage equal to attackPower public void attack(GameCharacter target) { target.takeDamage(attackPower); }
public boolean isDefeated() { return currentHealth == 0; }}Write the main game loop where the player chooses actions and the enemy responds automatically.
import java.util.Scanner;
public class Main{ public static void main(String[] args) { Scanner input = new Scanner(System.in);
GameCharacter player = new GameCharacter("Hero", 100, 15); GameCharacter enemy = new GameCharacter("Goblin", 75, 10);
System.out.println("-- Turn-Based Battle --");
while (!player.isDefeated() && !enemy.isDefeated()) { System.out.println("\n" + player.getName() + " HP: " + player.getCurrentHealth()); System.out.println(enemy.getName() + " HP: " + enemy.getCurrentHealth());
System.out.println("\nChoose an action:"); System.out.println("1) Attack"); System.out.println("2) Heal"); System.out.print("> "); String choice = input.nextLine();
if (choice.equals("1")) { player.attack(enemy); System.out.println("\nYou dealt " + player.getAttackPower() + " damage!"); } else if (choice.equals("2")) { player.heal(10); System.out.println("\nYou healed 10 HP."); } else { System.out.println("Invalid choice. You lose your turn!"); }
if (enemy.isDefeated()) { break; }
enemy.attack(player); System.out.println("The " + enemy.getName() + " attacks for " + enemy.getAttackPower() + " damage!"); }
if (player.isDefeated()) { System.out.println("\nYou were defeated..."); } else { System.out.println("\nYou defeated the enemy!"); } }}-- Turn-Based Battle --Hero HP: 100Goblin HP: 75
Choose an action:1) Attack2) Heal> 1
You dealt 15 damage!The Goblin attacks for 10 damage!
Hero HP: 90Goblin HP: 60...
You defeated the enemy!Submit your project and reflection answers to the appropriate dropbox.