equipments #22

Merged
viud3133 merged 40 commits from equipments into main 2025-10-30 12:09:43 +01:00
28 changed files with 519 additions and 53 deletions
Showing only changes of commit 426eaa0d4e - Show all commits

12
pom.xml
View File

@@ -20,6 +20,18 @@
<version>5.8.1</version> <version>5.8.1</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -1,5 +1,5 @@
package Action; package Action;
public interface Action { public interface Action {
void exectue(Actor player); void execute(Actor player);
} }

View File

@@ -0,0 +1,19 @@
package Action;
import Job.HasJob;
import Job.Wizard;
public class CastAction implements Action {
private Wizard requireWizard(Actor actor) {
if (actor instanceof HasJob hasJob && hasJob.getJob() instanceof Wizard wizard) {
return wizard;
}
throw new IllegalStateException(actor + " cannot perform this action without being a Wizard!");
}
@Override
public void execute(Actor player) {
var wizard = requireWizard(player);
wizard.castSpell(player);
}
}

View File

@@ -5,7 +5,7 @@ import Job.Miner;
public class DigAction implements Action { public class DigAction implements Action {
@Override @Override
public void exectue(Actor actor) { public void execute(Actor actor) {
Miner miner = requireMiner(actor); Miner miner = requireMiner(actor);
miner.dig(actor); miner.dig(actor);
} }

View File

@@ -0,0 +1,19 @@
package Action;
import Job.HasJob;
import Job.Wizard;
public class LearnSpellAction implements Action {
@Override
public void execute(Actor actor) {
Wizard wizard = requireWizard(actor);
wizard.learnSpell(actor);
}
private Wizard requireWizard(Actor actor) {
if (actor instanceof HasJob hasJob && hasJob.getJob() instanceof Wizard wizard) {
return wizard;
}
throw new IllegalStateException(actor + " cannot perform this action without being a Wizard!");
}
}

View File

@@ -0,0 +1,7 @@
package Character;
import java.util.List;
public interface HasSpellBook {
List<String> getSpellBook();
}

View File

@@ -2,5 +2,6 @@ package Combat;
public interface HasHealth { public interface HasHealth {
void setHealth(int health); void setHealth(int health);
int getHealth();
boolean isAlive(); boolean isAlive();
} }

View File

@@ -0,0 +1,6 @@
package Combat;
public interface HasMana {
void setMana(int mana);
int getMana();
}

View File

@@ -0,0 +1,18 @@
package Combat;
import Entity.Entity;
public class OffensiveDamageSpell extends Spell {
public OffensiveDamageSpell(String spellName, int cost, int potency) {
super(spellName, cost, potency);
}
@Override
public void cast(Entity source, Entity target) {
if(target instanceof HasHealth health && source instanceof HasMana mana) {
mana.setMana(mana.getMana() - this.getCost());
health.setHealth(health.getHealth() - this.getPotency());
}
}
}

View File

@@ -0,0 +1,54 @@
package Combat;
import Entity.Entity;
public abstract class Spell {
private final String spellName;
private int cost;
private int potency;
public Spell(String spellName, int cost, int potency) {
this.spellName = spellName;
this.cost = cost;
this.potency = potency;
}
public String getSpellName() {
return spellName;
}
public int getCost() {
return cost;
}
public int getPotency() {
return potency;
}
public abstract void cast(Entity source, Entity target);
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Spell spell = (Spell) o;
if (cost != spell.cost) return false;
if (potency != spell.potency) return false;
return spellName.equals(spell.spellName);
}
@Override
public int hashCode() {
int result = spellName.hashCode();
result = 31 * result + cost;
result = 31 * result + potency;
return result;
}
@Override
public String toString() {
return spellName + " (Cost: " + cost + ", Potency: " + potency + ")";
}
}

View File

@@ -3,18 +3,24 @@ package Entity;
import Action.Action; import Action.Action;
import Action.Actor; import Action.Actor;
import Combat.HasHealth; import Combat.HasHealth;
import Combat.HasMana;
import Combat.Spell;
import Inventory.Inventory;
import Job.Job; import Job.Job;
import Job.HasJob; import Job.HasJob;
import Inventory.HasInventory; import Inventory.HasInventory;
import Inventory.HasSpellBook;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
public class Player extends Entity implements Movable, Actor, HasInventory, HasJob, HasHealth { public class Player extends Entity implements Movable, Actor, HasInventory, HasSpellBook, HasJob, HasHealth, HasMana {
protected int health; protected int health;
protected int mana;
protected Job job; protected Job job;
protected Position position; protected Position position = new Position(0,0);
protected List<String> items = new LinkedList<>(); protected Inventory inventory= new Inventory();
protected List<Spell> spells = new LinkedList<>();
public Player(String name, Job job) { public Player(String name, Job job) {
super(name); super(name);
this.job = job; this.job = job;
@@ -34,7 +40,7 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasJ
@Override @Override
public boolean canMoveTo(Position position) { public boolean canMoveTo(Position position) {
return false; return true;
} }
public Job getJob() { public Job getJob() {
return job; return job;
@@ -45,13 +51,15 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasJ
this.job = job; this.job = job;
} }
public List<String> getInventory() { public Inventory getInventory() {
return items; return inventory;
} }
public List<Spell> getSpellBook() {return spells;}
@Override @Override
public void performAction(Action action) { public void performAction(Action action) {
action.exectue(this); action.execute(this);
} }
@Override @Override
@@ -69,6 +77,15 @@ public class Player extends Entity implements Movable, Actor, HasInventory, HasJ
this.health = health; this.health = health;
} }
@Override
public int getHealth() {return health;}
@Override
public void setMana(int mana) {this.mana = mana;}
@Override
public int getMana() {return mana;}
@Override @Override
public boolean isAlive() { public boolean isAlive() {
return health > 0; return health > 0;

View File

@@ -3,5 +3,5 @@ package Inventory;
import java.util.List; import java.util.List;
public interface HasInventory { public interface HasInventory {
List<String> getInventory(); Inventory getInventory();
} }

View File

@@ -0,0 +1,8 @@
package Inventory;
import java.util.List;
import Combat.Spell;
public interface HasSpellBook {
List<Spell> getSpellBook();
}

View File

@@ -0,0 +1,24 @@
package Inventory;
import java.util.ArrayList;
import java.util.List;
public class Inventory {
List<String> items;
public Inventory() {
items = new ArrayList<>();
}
List<String> getItems() {
return items;
}
public void addItem(String item) {
items.add(item);
}
public boolean containsItem(String item) {
return items.contains(item);
}
}

View File

@@ -1,10 +1,11 @@
package Job; package Job;
import Shared.HasExperience; import Shared.ExperienceTable;
import Shared.Levelable;
import java.util.Objects; import java.util.Objects;
public abstract class Job implements HasExperience { public abstract class Job implements Levelable {
protected int level; protected int level;
protected int experience; protected int experience;
protected String name; protected String name;
@@ -18,7 +19,27 @@ public abstract class Job implements HasExperience {
return level; return level;
} }
@Override
public int remainingXpUntilLevelUp() {
return ExperienceTable.getXpForLevel(level) - experience;
}
@Override
public void gainExperience(int exp) {
experience += exp;
if (experience >= ExperienceTable.getXpForLevel(level)) {
levelUp();
}
}
@Override
public int getExperience() {
return experience;
}
@Override
public void levelUp() { public void levelUp() {
experience = experience - ExperienceTable.getXpForLevel(level);
level++; level++;
} }
@@ -38,4 +59,5 @@ public abstract class Job implements HasExperience {
return Objects.hash(name); return Objects.hash(name);
} }
} }

View File

@@ -11,17 +11,7 @@ public class Miner extends Job {
public void dig(Actor actor) { public void dig(Actor actor) {
if (actor instanceof HasInventory a) { if (actor instanceof HasInventory a) {
a.getInventory().add("Stone"); a.getInventory().addItem("Stone");
} }
} }
@Override
public int getExperience() {
return experience;
}
@Override
public void gainExperience(int exp) {
experience += exp;
}
} }

View File

@@ -0,0 +1,70 @@
package Job;
import Action.Actor;
import Combat.HasMana;
import Combat.OffensiveDamageSpell;
import Entity.Entity;
import Inventory.HasSpellBook;
import Combat.Spell;
import java.util.Scanner;
public class Wizard extends Job {
private final Scanner scanner;
public Wizard(Scanner scanner) {
super("Wizard");
this.scanner = scanner;
}
public Wizard() {
this(new Scanner(System.in));
}
public void learnSpell(Actor actor) {
if(actor instanceof HasSpellBook a ) {
OffensiveDamageSpell defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
a.getSpellBook().add(defaultSpell);
}
}
public void castSpell(Actor actor) {
if(actor instanceof HasSpellBook spellbook && actor instanceof HasMana mana && actor instanceof Entity entity) {
if(spellbook.getSpellBook().isEmpty()){
System.out.println("You haven't learned any spells");
return;
}
System.out.println("Which spell do you want to cast?");
for(int i = 0; i < spellbook.getSpellBook().size(); i++){
System.out.println((i + 1) + ". " + spellbook.getSpellBook().get(i).toString());
}
int spellIndex = scanner.nextInt();
scanner.nextLine();
if(spellIndex < 1 || spellIndex > spellbook.getSpellBook().size()) {
System.out.println("Invalid choice!");
return;
}
Spell spell = spellbook.getSpellBook().get(spellIndex - 1);
if (mana.getMana() >= spell.getCost()){
spell.cast(entity, entity);
}
else{
System.out.println("Not enough mana!");
}
}
}
@Override
public int getExperience() {
return experience;
}
@Override
public void gainExperience(int exp) {
experience += exp;
}
}

View File

@@ -1,3 +1,5 @@
import Entity.Position;
import java.util.*; import java.util.*;
public abstract class Monster extends Character{ public abstract class Monster extends Character{

View File

@@ -1,3 +0,0 @@
public class Shade extends Monster {
}

View File

@@ -0,0 +1,16 @@
package Shared;
import java.util.Map;
public final class ExperienceTable {
private static final Map<Integer, Integer> XP_TABLE = Map.of(
1, 100,
2, 250
);
private ExperienceTable() {}
public static int getXpForLevel(int level) {
return XP_TABLE.getOrDefault(level, Integer.MAX_VALUE);
}
}

View File

@@ -1,6 +1,6 @@
package Shared; package Shared;
public interface HasExperience { public interface Levelable {
int getLevel(); int getLevel();
int getExperience(); int getExperience();
@@ -8,4 +8,6 @@ public interface HasExperience {
void gainExperience(int exp); void gainExperience(int exp);
void levelUp(); void levelUp();
int remainingXpUntilLevelUp();
} }

View File

@@ -1,18 +0,0 @@
public class Spell {
private final String spellName;
private int cost;
private int potency;
public Spell(String spellName, int cost, int potency) {
this.spellName = spellName;
this.cost = cost;
this.potency = potency;
}
public String getSpellName() {return spellName;}
public int getCost() {return cost;}
public int getPotency() {return potency;}
}

View File

@@ -0,0 +1,65 @@
import Combat.OffensiveDamageSpell;
import Entity.Player;
import Job.Wizard;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.util.Scanner;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
public class InterestingTests {
private Wizard wizard;
@BeforeEach
public void setup() {
wizard = new Wizard();
}
@Test
public void testCastSpell_successfulCast() {
Scanner testScanner = new Scanner(new ByteArrayInputStream("1\n".getBytes()));
wizard = new Wizard(testScanner);
Player player = new Player("Gandalf", wizard);
player.setMana(50);
player.setHealth(100);
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 15);
player.getSpellBook().add(fireball);
int initialMana = player.getMana();
int initialHealth = player.getHealth();
wizard.castSpell(player);
assertThat(player.getMana(), is(initialMana - fireball.getCost()));
assertThat(player.getHealth(), is(initialHealth - fireball.getPotency()));
}
@Test
public void testCastSpell_notEnoughMana() {
Scanner testScanner = new Scanner(new ByteArrayInputStream("1\n".getBytes()));
wizard = new Wizard(testScanner);
Player player = new Player("Merlin", wizard);
player.setMana(10);
player.setHealth(100);
OffensiveDamageSpell fireball = new OffensiveDamageSpell("Fireball", 20, 15);
player.getSpellBook().add(fireball);
int initialMana = player.getMana();
int initialHealth = player.getHealth();
wizard.castSpell(player);
assertThat(player.getMana(), is(initialMana));
assertThat(player.getHealth(),is(initialHealth));
}
}

View File

@@ -0,0 +1,61 @@
import Action.CastAction;
import Action.LearnSpellAction;
import Combat.OffensiveDamageSpell;
import Entity.Player;
import Job.Wizard;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class MagicSystemTest {
private Player defaultPlayer() {
return new Player("Alex");
}
@Test void cant_cast_spell_without_being_wizard(){
var p = defaultPlayer();
CastAction action = new CastAction();
IllegalStateException exception = assertThrows(
IllegalStateException.class,
() -> action.execute(p)
);
assertTrue(exception.getMessage().contains("cannot perform this action without being a Wizard"));
}
@Test void cant_learn_spell_without_being_wizard(){
var p = defaultPlayer();
LearnSpellAction action = new LearnSpellAction();
IllegalStateException exception = assertThrows(
IllegalStateException.class,
() -> action.execute(p)
);
assertTrue(exception.getMessage().contains("cannot perform this action without being a Wizard"));
}
@Test void damage_spell_damages_target(){
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
var p = defaultPlayer();
p.setMana(30);
p.setHealth(30);
defaultSpell.cast(p, p);
assertEquals(10, p.getHealth());
}
@Test void damage_spell_drains_mana_from_caster(){
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
var p = defaultPlayer();
p.setMana(30);
p.setHealth(30);
defaultSpell.cast(p, p);
assertEquals(10, p.getMana());
}
@Test void cant_cast_spell_with_empty_spellbook(){
var p = defaultPlayer();
var job = new Wizard();
p.learnJob(job);
p.performAction(new CastAction());
}
}

View File

@@ -6,7 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class MinerTest { public class MinerTest {
@Test @Test
void miner_can_level_up() { void can_level_up() {
var job = new Miner(); var job = new Miner();
assertEquals(1, job.getLevel()); assertEquals(1, job.getLevel());
job.levelUp(); job.levelUp();
@@ -14,12 +14,33 @@ public class MinerTest {
} }
@Test @Test
void miner_can_gain_xp() { void can_gain_xp() {
var job = new Miner(); var job = new Miner();
job.gainExperience(25); job.gainExperience(25);
assertEquals(25, job.getExperience()); assertEquals(25, job.getExperience());
} }
@Test
void level_up_when_experience_cap_is_reached() {
var job = new Miner();
job.gainExperience(job.remainingXpUntilLevelUp());
assertEquals(2, job.getLevel());
}
@Test
void additional_xp_carries_over_on_level_up() {
var job = new Miner();
job.gainExperience(job.remainingXpUntilLevelUp() + 10);
assertEquals(10, job.getExperience());
}
@Test
void dig_on_ice_require_ice_pick() {
}
} }

View File

@@ -1,8 +1,11 @@
import Action.DigAction; import Action.DigAction;
import Action.LearnSpellAction;
import Combat.OffensiveDamageSpell;
import Entity.Position; import Entity.Position;
import Job.Miner; import Job.Miner;
import Entity.Player; import Entity.Player;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import Job.Wizard;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@@ -18,6 +21,18 @@ class PlayerTest {
assertFalse(p.isAlive()); assertFalse(p.isAlive());
} }
@Test void get_health_returns_health() {
var p = defaultPlayer();
p.setHealth(10);
assertEquals(10, p.getHealth());
}
@Test void get_mana_returns_mana() {
var p = defaultPlayer();
p.setMana(10);
assertEquals(10, p.getMana());
}
@Test @Test
public void can_change_position() { public void can_change_position() {
var p = defaultPlayer(); var p = defaultPlayer();
@@ -38,9 +53,19 @@ class PlayerTest {
@Test @Test
void miner_can_dig() { void miner_can_dig() {
var p = new Player("John"); var p = new Player("John");
var job = new Miner(); p.learnJob(new Miner());
p.learnJob(job);
p.performAction(new DigAction()); p.performAction(new DigAction());
assertTrue(p.getInventory().contains("Stone")); assertTrue(p.getInventory().containsItem("Stone"));
}
@Test
void wizard_can_learn_spell() {
var p = new Player("Bob");
var job = new Wizard();
var defaultSpell = new OffensiveDamageSpell("fireball", 20, 20);
p.learnJob(job);
p.performAction(new LearnSpellAction());
System.out.println(p.getSpellBook());
assertTrue(p.getSpellBook().contains(defaultSpell));
} }
} }

View File

@@ -1,3 +1,5 @@
import Combat.OffensiveDamageSpell;
import Combat.Spell;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@@ -5,19 +7,19 @@ import static org.junit.jupiter.api.Assertions.*;
public class SpellTest { public class SpellTest {
private Spell defaultSpell() { private Spell defaultSpell() {
return new Spell("fireball", 20, 40); return new OffensiveDamageSpell("fireball", 20, 40);
} }
@Test @Test
void setSpellNameOnCreation(){ void setSpellNameOnCreation(){
var spell = defaultSpell(); var spell = defaultSpell();
assertEquals("fireball", spell.getSpellName(), "Spell name should be set"); assertEquals("fireball", spell.getSpellName(), "Combat.Spell name should be set");
} }
@Test @Test
void setCostOnCreation() { void setCostOnCreation() {
var spell = defaultSpell(); var spell = defaultSpell();
assertEquals(20, spell.getCost(), "Spell cost should have been set"); assertEquals(20, spell.getCost(), "Combat.Spell cost should have been set");
} }
@Test @Test

View File

@@ -0,0 +1,26 @@
import Job.Wizard;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class WizardTest {
@Test
void wizard_has_level() {
var job = new Wizard();
assertEquals(1, job.getLevel());
}
@Test
void wizard_can_level_up() {
var job = new Wizard();
job.levelUp();
assertEquals(2, job.getLevel());
}
@Test
void wizard_can_gain_xp(){
var job = new Wizard();
job.gainExperience(40);
assertEquals(40, job.getExperience());
}
}