complete code

This commit is contained in:
2026-05-14 15:41:00 +03:30
parent 9fb8bdb5a3
commit a4bf8a7a3d
44 changed files with 415 additions and 110 deletions
@@ -1,157 +1,274 @@
package org.project.game;
package org.project;
import org.project.entity.players.Player;
import org.project.entity.players.Knight;
import org.project.entity.players.Wizard;
import org.project.entity.players.Assassin;
import org.project.entity.enemies.Enemy;
import org.project.entity.enemies.Goblin;
import org.project.entity.players.Knight;
import org.project.entity.players.Player;
import org.project.entity.players.Warrior;
import org.project.entity.enemies.Skeleton;
import org.project.entity.enemies.Vampire;
import org.project.entity.enemies.Dragon;
import org.project.item.Item;
import org.project.location.Location;
import java.util.Scanner;
import java.util.*;
public class setupGame {
public class GameEngine {
private Player player;
private Location currentLocation;
private Location castle;
private Scanner scanner;
public setupGame() {
private Map<Location, Enemy[]> possibleEnemiesMap = new HashMap<>();
private Enemy currentEnemy;
public GameEngine() {
scanner = new Scanner(System.in);
setupGame();
}
private void setupGame() {
System.out.println("Choose your class:");
System.out.println("1. Knight");
System.out.println("2. Mage");
System.out.println("3. Assassin");
player = new Knight("Hero");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
player = new Knight("Hero");
break;
case 2:
player = new Wizard("Hero");
break;
case 3:
player = new Assassin("Hero");
break;
default:
System.out.println("Invalid choice, defaulting to Knight.");
player = new Knight("Hero");
}
// Create Locations
Location village = new Location("Village");
Location forest = new Location("Forest");
Location dungeon = new Location("Dungeon");
Location graveyard = new Location("Graveyard");
castle = new Location("Castle");
// Connect Locations
village.connectLocation(forest);
forest.connectLocation(village);
forest.connectLocation(dungeon);
dungeon.connectLocation(forest);
// Add Enemies
forest.addEnemy(new Goblin());
forest.connectLocation(graveyard);
graveyard.connectLocation(forest);
village.connectLocation(castle);
castle.connectLocation(village);
// Starting Location
currentLocation = village;
possibleEnemiesMap.put(forest, new Enemy[] { new Goblin() });
possibleEnemiesMap.put(dungeon, new Enemy[] { new Skeleton(), new Vampire() });
possibleEnemiesMap.put(graveyard, new Enemy[] { new Skeleton(), new Vampire() });
possibleEnemiesMap.put(castle, new Enemy[] { new Dragon() });
spawnEnemyForLocation(currentLocation);
}
private void spawnEnemyForLocation(Location loc) {
Enemy[] possibilities = possibleEnemiesMap.get(loc);
if (possibilities == null || possibilities.length == 0) {
currentEnemy = null;
return;
}
Random rand = new Random();
Enemy template = possibilities[rand.nextInt(possibilities.length)];
currentEnemy = createEnemyInstance(template);
}
private Enemy createEnemyInstance(Enemy e) {
if (e instanceof Goblin) return new Goblin();
if (e instanceof Skeleton) return new Skeleton();
if (e instanceof Vampire) return new Vampire();
if (e instanceof Dragon) return new Dragon();
return null;
}
public void startGame() {
boolean running = true;
System.out.println("=== RPG Game Started ===");
boolean running = true;
while (running) {
System.out.println("\nCurrent Location: " + currentLocation.getName());
System.out.println("\n===== MENU =====");
System.out.println("1. Move");
System.out.println("Location: " + currentLocation.getName());
System.out.println("HP: " + player.getHp());
System.out.println("Mana: " + player.getMana());
System.out.println("Level: " + player.getLevel());
System.out.println("XP: " + player.getXp());
System.out.println("Keys: " + player.getKeyCount());
System.out.println("\n1. Move");
System.out.println("2. Fight");
System.out.println("3. Show Inventory");
System.out.println("4. Exit");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
move();
break;
case 2:
fight();
break;
case 3:
player.getInventory().showInventory();
break;
case 4:
running = false;
System.out.println("Game Over.");
break;
default:
System.out.println("Invalid choice.");
}
if (!player.isAlive()) {
System.out.println("\nYou died. Game Over.");
running = false;
}
}
}
private void move() {
if (currentLocation.getConnectedLocations().isEmpty()) {
System.out.println("No connected locations.");
return;
}
System.out.println("Where do you want to go?");
System.out.println("\nWhere do you want to go?");
for (int i = 0; i < currentLocation.getConnectedLocations().size(); i++) {
System.out.println(
(i + 1) + ". " +
currentLocation.getConnectedLocations().get(i).getName()
);
System.out.println((i + 1) + ". " + currentLocation.getConnectedLocations().get(i).getName());
}
int choice = scanner.nextInt();
if (choice < 1 || choice > currentLocation.getConnectedLocations().size()) {
System.out.println("Invalid location.");
System.out.println("Invalid choice.");
return;
}
currentLocation =
currentLocation.getConnectedLocations().get(choice - 1);
Location nextLocation = currentLocation.getConnectedLocations().get(choice - 1);
System.out.println("You moved to " + currentLocation.getName());
if (nextLocation == castle && player.getKeyCount() < 3) {
System.out.println("\nThe castle gate is locked!");
System.out.println("You need 3 keys.");
return;
}
currentLocation = nextLocation;
spawnEnemyForLocation(currentLocation);
System.out.println("\nYou moved to " + currentLocation.getName());
if (currentLocation == castle) {
System.out.println("\nThe final battle awaits...");
}
}
private void fight() {
if (currentLocation.getEnemies().isEmpty()) {
if (currentEnemy == null || !currentEnemy.isAlive()) {
System.out.println("No enemies here.");
return;
}
Enemy enemy = currentLocation.getEnemies().get(0);
Enemy enemy = currentEnemy;
System.out.println("A wild " + enemy.getName() + " appeared!");
System.out.println("\nA wild " + enemy.getName() + " appeared!");
while (player.isAlive() && enemy.isAlive()) {
player.lightAttack(enemy);
System.out.println("\n===== COMBAT =====");
System.out.println(player.getName() + " HP: " + player.getHp());
System.out.println(enemy.getName() + " HP: " + enemy.getHp());
System.out.println("\nChoose action:");
System.out.println("1. Light Attack");
System.out.println("2. Heavy Attack");
System.out.println("3. Defend");
System.out.println("4. Heal");
System.out.println("5. Special Ability");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
player.lightAttack(enemy);
break;
case 2:
player.heavyAttack(enemy);
break;
case 3:
player.defend();
break;
case 4:
player.heal();
break;
case 5:
player.specialAbility(enemy);
break;
default:
System.out.println("Invalid choice.");
continue;
}
if (enemy.isAlive()) {
System.out.println("\n" + enemy.getName() + "'s turn!");
enemy.attack(player);
}
}
if (!enemy.isAlive()) {
System.out.println("\n" + enemy.getName() + " was defeated!");
System.out.println(enemy.getName() + " defeated!");
player.gainXP(enemy.getXpReward());
currentLocation.removeEnemy(enemy);
}
Item loot = enemy.dropLoot();
if (!player.isAlive()) {
if (loot != null) {
player.getInventory().addItem(loot);
System.out.println(enemy.getName() + " dropped: " + loot.getName());
}
System.out.println("You died.");
currentEnemy = null;
if (enemy instanceof Dragon) {
System.out.println("\n====================");
System.out.println("YOU DEFEATED THE DRAGON!");
System.out.println("====== YOU WIN ======");
System.exit(0);
}
}
}
}
@@ -1,4 +1,46 @@
package org.project;
package org.project.inventory;
import org.project.item.Item;
import java.util.ArrayList;
public class Inventory {
private ArrayList<Item> items;
public Inventory() {
items = new ArrayList<>();
}
public void addItem(Item item) {
items.add(item);
System.out.println(item.getName() + " added to inventory.");
}
public void removeItem(Item item) {
items.remove(item);
}
public ArrayList<Item> getItems() {
return items;
}
public void showInventory() {
if(items.isEmpty()) {
System.out.println("Inventory is empty.");
return;
}
System.out.println("=== Inventory ===");
for(int i = 0; i < items.size(); i++) {
System.out.println(
(i + 1) + ". " + items.get(i).getName()
);
}
}
}
@@ -1,15 +1,10 @@
package org.project;
import org.project.location.Location;
import java.util.ArrayList;
import java.util.List;
import org.project.GameEngine;
public class Main {
public static void main(String[] args) {
// TODO: ADD LOCATIONS TO YOUR GAME
List<Location> locations = new ArrayList<>();
// TODO: IMPLEMENT GAMEPLAY
GameEngine game = new GameEngine();
game.startGame();
}
}
}
@@ -1,4 +1,16 @@
package org.project.entity.players;
public class CombatActions {
import org.project.entity.Entity;
public interface CombatActions {
void lightAttack(Entity target);
void heavyAttack(Entity target);
void defend();
void heal();
void specialAbility(Entity target);
}
@@ -3,9 +3,8 @@ package org.project.item;
import org.project.entity.Entity;
public interface Item {
void use(Entity target);
/*
TODO: ADD OTHER REQUIRED AND BONUS METHODS
*/
String getName();
void use(Entity target);
}
@@ -1,4 +1,24 @@
package org.project.item;
public class Key {
import org.project.entity.Entity;
import org.project.item.Item;
public class Key implements Item {
private String name;
public Key(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public void use(Entity target) {
System.out.println("This key is used automatically to unlock the castle.");
}
}
@@ -1,7 +1,7 @@
package org.project.item.armors;
// TODO: UPDATE IMPLEMENTATION
public abstract class Armor {
private int defense;
private int maxDefense;
private int durability;
@@ -14,16 +14,23 @@ public abstract class Armor {
this.durability = durability;
this.maxDefense = defense;
this.maxDurability = durability;
this.isBroke = false;
}
public void checkBreak() {
if (durability <= 0) {
durability = 0;
isBroke = true;
defense = 0;
}
}
// TODO: (BONUS) UPDATE THE REPAIR METHOD
public void takeDamage(int amount) {
durability -= amount;
checkBreak();
}
public void repair() {
isBroke = false;
defense = maxDefense;
@@ -31,6 +38,9 @@ public abstract class Armor {
}
public int getDefense() {
if (isBroke) {
return 0;
}
return defense;
}
@@ -1,4 +1,7 @@
package org.project.item.armors;
public class AssassinArmor {
public class AssassinArmor extends Armor {
public AssassinArmor(){
super(5,25);
}
}
@@ -1,6 +1,9 @@
package org.project.item.armors;
// TODO: UPDATE IMPLEMENTATION
public class KnightArmor {
// TODO: DESIGN ARMOR'S ATTRIBUTES IMPLEMENT THE CONSTRUCTOR
}
public class KnightArmor extends Armor {
public KnightArmor() {
super(8, 40);
}
}
@@ -1,4 +1,7 @@
package org.project.item.armors;
public class MageArmor {
public class MageArmor extends Armor{
public MageArmor(){
super(2,15);
}
}
@@ -1,8 +1,17 @@
package org.project.item.consumables;
// TODO: UPDATE IMPLEMENTATION
public abstract class Consumable {
/*
TODO: ADD OTHER REQUIRED AND BONUS METHODS
*/
import org.project.item.Item;
public abstract class Consumable implements Item {
private String name;
public Consumable(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
}
@@ -2,15 +2,33 @@ package org.project.item.consumables;
import org.project.entity.Entity;
// TODO: UPDATE IMPLEMENTATION
public class Flask {
/*
THIS IS AN EXAMPLE OF A CONSUMABLE DESIGN.
*/
public class Flask extends Consumable {
private int healAmount;
public Flask() {
super("Flask");
this.healAmount = 30;
}
// TODO: UPDATE USE METHOD
@Override
public void use(Entity target) {
target.heal(target.getMaxHP() / 10);
target.heal(healAmount);
System.out.println(
target.getName() +
" drinks a flask and restores " +
healAmount + " HP."
);
}
public int getHealAmount() {
return healAmount;
}
public void setHealAmount(int healAmount) {
this.healAmount = healAmount;
}
}
@@ -1,4 +1,28 @@
package org.project.item.weapons;
public class Dragger {
import org.project.entity.Entity;
public class Dagger extends Weapon {
public Dagger() {
super(10, 0);
}
@Override
public String getName() {
return "Dagger";
}
@Override
public void use(Entity target) {
int damage = getDamage();
if (target.isDefending()) {
damage /= 2;
}
target.setHp(target.getHp() - damage);
System.out.println("Dagger deals " + damage + " damage to " + target.getName());
}
}
@@ -1,4 +1,28 @@
package org.project.item.weapons;
public class Staff {
import org.project.entity.Entity;
public class Staff extends Weapon {
public Staff() {
super(12, 5);
}
@Override
public String getName() {
return " Staff";
}
@Override
public void use(Entity target) {
int damage = getDamage();
if (target.isDefending()) {
damage /= 2;
}
target.setHp(target.getHp() - damage);
System.out.println("Staff deals " + damage + " damage to " + target.getName());
}
}
@@ -2,25 +2,27 @@ package org.project.item.weapons;
import org.project.entity.Entity;
import java.util.ArrayList;
// TODO: UPDATE IMPLEMENTATION
public class Sword {
/*
THIS IS AN EXAMPLE OF A WEAPON DESIGN.
*/
int abilityCharge;
public class Sword extends Weapon {
public Sword() {
// TODO: DESIGN SWORD'S ATTRIBUTES IMPLEMENT THE CONSTRUCTOR
super(15, 0);
}
// TODO: (BONUS) UPDATE THE UNIQUE ABILITY
public void uniqueAbility(ArrayList<Entity> targets) {
abilityCharge += 2;
for (Entity target : targets) {
target.takeDamage(getDamage());
@Override
public String getName() {
return "Sword";
}
@Override
public void use(Entity target) {
int damage = getDamage();
if (target.isDefending()) {
damage /= 2;
}
target.setHp(target.getHp() - damage);
System.out.println("Sword deals " + damage + " damage to " + target.getName());
}
}
@@ -1,19 +1,18 @@
package org.project.item.weapons;
import org.project.entity.Entity;
import org.project.item.Item;
public abstract class Weapon {
protected int damage;
protected int manaCost;
public abstract class Weapon implements Item {
private int damage;
private int manaCost;
public Weapon(int damage, int manaCost) {
this.damage = damage;
this.manaCost = manaCost;
}
public abstract void use(Entity target);
public int getDamage() {
return damage;
}
@@ -22,5 +21,14 @@ public abstract class Weapon {
return manaCost;
}
public void setDamage(int damage) {
this.damage = damage;
}
public void setManaCost(int manaCost) {
this.manaCost = manaCost;
}
public abstract void use(Entity target);
}
@@ -5,24 +5,40 @@ import org.project.entity.enemies.Enemy;
import java.util.ArrayList;
public class Location {
private String name;
private ArrayList<Location> connectedLocations;
private ArrayList<Enemy> enemies;
public Location(ArrayList<Location> locations, ArrayList<Enemy> enemies) {
this.locations = locations;
this.enemies = enemies;
public Location(String name) {
this.name = name;
this.connectedLocations = new ArrayList<>();
this.enemies = new ArrayList<>();
}
public String getName() {
return name;
}
public ArrayList<Location> getLocations() {
return locations;
public ArrayList<Location> getConnectedLocations() {
return connectedLocations;
}
public ArrayList<Enemy> getEnemies() {
return enemies;
}
public void connectLocation(Location location) {
connectedLocations.add(location);
}
public void addEnemy(Enemy enemy) {
enemies.add(enemy);
}
public void removeEnemy(Enemy enemy) {
enemies.remove(enemy);
}
}
Binary file not shown.