diff --git a/src/main/java/Combat/Spell.java b/src/main/java/Combat/Spell.java index c675c26..48d77e8 100644 --- a/src/main/java/Combat/Spell.java +++ b/src/main/java/Combat/Spell.java @@ -37,13 +37,6 @@ public abstract class Spell { return spellName.equals(spell.spellName); } - @Override - public int hashCode() { - int result = spellName.hashCode(); - result = 31 * result + cost; - result = 31 * result + range; - return result; - } @Override public String toString() { diff --git a/src/main/java/Entity/Chest.java b/src/main/java/Entity/Chest.java new file mode 100644 index 0000000..add8311 --- /dev/null +++ b/src/main/java/Entity/Chest.java @@ -0,0 +1,20 @@ +package Entity; + +public class Chest extends Entity { + + private Position position; + + public Chest(String name, Position position) { + super(name); + this.position = position; + } + + public Position getPosition() { + return position; + } + + @Override + public void setPosition(Position position) { + this.position = position; + } +} diff --git a/src/main/java/Entity/Entity.java b/src/main/java/Entity/Entity.java index 9e2898f..dd3e8dd 100644 --- a/src/main/java/Entity/Entity.java +++ b/src/main/java/Entity/Entity.java @@ -21,7 +21,6 @@ public abstract class Entity implements Positionable { @Override public boolean equals(Object obj) { if (this == obj) return true; - if (obj == null || getClass() != obj.getClass()) return false; Entity entity = (Entity) obj; diff --git a/src/main/java/Item/BodyArmour.java b/src/main/java/Item/BodyArmour.java index 743039d..dd2ad26 100644 --- a/src/main/java/Item/BodyArmour.java +++ b/src/main/java/Item/BodyArmour.java @@ -2,17 +2,13 @@ package Item; public class BodyArmour extends EquipmentType{ - private final String name; private final AttributeModifier modifiers; public BodyArmour(String name, AttributeModifier modifiers) { - this.name = name; + super(name); this.modifiers = new AttributeModifier(modifiers.getMaxHpMod(), modifiers.getMaxMpMod(), modifiers.getStrMod(), modifiers.getMagicStrMod(), modifiers.getDefMod()*2, modifiers.getMagicDefMod()*2); } - public String getName() { - return name; - } public AttributeModifier getModifiers() { return modifiers; @@ -20,6 +16,6 @@ public class BodyArmour extends EquipmentType{ @Override public String toString() { - return name + modifiers; + return getName() + modifiers; } } \ No newline at end of file diff --git a/src/main/java/Item/EquipmentType.java b/src/main/java/Item/EquipmentType.java index 2854c7e..501053d 100644 --- a/src/main/java/Item/EquipmentType.java +++ b/src/main/java/Item/EquipmentType.java @@ -1,10 +1,15 @@ package Item; public abstract class EquipmentType { - public String name; - public AttributeModifier attributeModifier; + private String name; - public abstract String getName(); + public EquipmentType(String name) { + this.name = name; + } + + public String getName() { + return name; + }; public abstract String toString(); public abstract AttributeModifier getModifiers(); diff --git a/src/main/java/Item/MageHat.java b/src/main/java/Item/MageHat.java index f9a2685..c35578f 100644 --- a/src/main/java/Item/MageHat.java +++ b/src/main/java/Item/MageHat.java @@ -2,17 +2,13 @@ package Item; public class MageHat extends EquipmentType{ - private final String name; private final AttributeModifier modifiers; public MageHat(String name, AttributeModifier modifiers) { - this.name = name; + super(name); this.modifiers = new AttributeModifier(modifiers.getMaxHpMod(), modifiers.getMaxMpMod(), modifiers.getStrMod(), modifiers.getMagicStrMod()*2, modifiers.getDefMod(), modifiers.getMagicDefMod()*2); } - public String getName() { - return name; - } public AttributeModifier getModifiers() { return modifiers; @@ -20,6 +16,6 @@ public class MageHat extends EquipmentType{ @Override public String toString() { - return name + modifiers; + return getName() + modifiers; } } \ No newline at end of file diff --git a/src/main/java/Monster/Shade.java b/src/main/java/Monster/Shade.java index 51c376c..ee98e5b 100644 --- a/src/main/java/Monster/Shade.java +++ b/src/main/java/Monster/Shade.java @@ -31,9 +31,6 @@ public class Shade extends Monster implements CanMove, CanAttack { if (isDead()) { return false; } - if (health == MIN_HEALTH) { - return false; - } if (health == MAX_HEALTH) { return false; } diff --git a/src/main/java/Monster/Troll.java b/src/main/java/Monster/Troll.java index 4489103..aae0b6c 100644 --- a/src/main/java/Monster/Troll.java +++ b/src/main/java/Monster/Troll.java @@ -36,9 +36,7 @@ public class Troll extends Monster implements CanMove, CanAttack { if (isDead()) { return false; } - if (health == MIN_HEALTH) { - return false; - } + if (health == MAX_HEALTH) { return false; } diff --git a/src/main/java/World/World.java b/src/main/java/World/World.java index 33b741a..9619737 100644 --- a/src/main/java/World/World.java +++ b/src/main/java/World/World.java @@ -12,7 +12,7 @@ public class World { private int height; public World(int x, int y) { - this.map = new String[(x * 2) + 1][y + 2]; + this.map = new String[x][y]; addWorldToMap(); width = x; height = y; diff --git a/src/test/java/ChestTest.java b/src/test/java/ChestTest.java new file mode 100644 index 0000000..f7c7d3c --- /dev/null +++ b/src/test/java/ChestTest.java @@ -0,0 +1,24 @@ +import Entity.Chest; +import Entity.Position; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +public class ChestTest { + private Chest defaultChest() { + return new Chest("Lost chest", new Position(0,0)); + } + @Test + void chest_has_a_position() { + Chest chest = defaultChest(); + assertThat(chest.getPosition(), equalTo(new Position(0,0))); + } + + @Test + void chest_position_can_update() { + Chest chest = defaultChest(); + chest.setPosition(new Position(2,2)); + assertThat(chest.getPosition(), equalTo(new Position(2,2))); + } +} diff --git a/src/test/java/EntityTest.java b/src/test/java/EntityTest.java new file mode 100644 index 0000000..b98a91e --- /dev/null +++ b/src/test/java/EntityTest.java @@ -0,0 +1,14 @@ +import Entity.Entity; +import Entity.Player; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +public class EntityTest { + private final String DEFAULT_NAME = "John"; + @Test + void entity_does_not_equal_other_objects() { + Entity p = new Player(DEFAULT_NAME); + assertNotEquals("", p); + } +} diff --git a/src/test/java/EquipmentTest.java b/src/test/java/EquipmentTest.java index 7fcd92f..b3fc5a5 100644 --- a/src/test/java/EquipmentTest.java +++ b/src/test/java/EquipmentTest.java @@ -1,13 +1,12 @@ import Entity.Player; -import Item.MageHat; +import Item.*; import Job.Wizard; import Job.Knight; import org.junit.jupiter.api.Test; -import Item.Equipment; -import Item.BodyArmour; -import Item.AttributeModifier; - +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; import static org.junit.jupiter.api.Assertions.*; public class EquipmentTest { @@ -16,8 +15,14 @@ public class EquipmentTest { private Player defaultWizard(){ return new Player("name", wizard); } + private AttributeModifier defaultAttributeModifier(){ + return new AttributeModifier(4,8,7,1,2,8); + } + private BodyArmour defaultArmorType(){ + return new BodyArmour("Body Armour", defaultAttributeModifier()); + } private Equipment defaultBodyArmour() { - AttributeModifier a = new AttributeModifier(4,8,7,1,2,8); + AttributeModifier a = defaultAttributeModifier(); BodyArmour b = new BodyArmour("Body Armour", a); return new Equipment("5", "Thornmail", b, knight, 2); } @@ -129,6 +134,14 @@ public class EquipmentTest { assertTrue(p.getEquipments().get(defaultMageHat().getSlot()).equals(defaultMageHat()), "Wizard can equip wizard hat"); } + @Test + void isNotEqualIfNameIsDifferent() { + var e1 = new Equipment("e1", "equipment 1", defaultArmorType(), knight, 1); + var e2 = new Equipment("e2", "equipment 2", defaultArmorType(), knight, 1); + assertFalse(e1.equals(e2)); + } + + @Test void canNotEquipWithWrongJob() { var p = defaultWizard(); diff --git a/src/test/java/ItemStackTest.java b/src/test/java/ItemStackTest.java index 00ced2a..395cf4c 100644 --- a/src/test/java/ItemStackTest.java +++ b/src/test/java/ItemStackTest.java @@ -4,26 +4,37 @@ import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; public class ItemStackTest { - @Test - void instantiated_with_no_quantity_throws_exception() { - assertThrows(IllegalArgumentException.class, () -> new ItemStack( - new BasicItem("id", "name", 5), 0 - )); + private ItemStack defaultStack() { + return new ItemStack(new BasicItem("id", "name", 5), 5); + } + + private ItemStack stackNoQty() { + return new ItemStack(new BasicItem("id", "name", 5), 0); } @Test - void instantiated_with_negative_quantity_throws_exception() { - assertThrows(IllegalArgumentException.class, () -> new ItemStack( - new BasicItem("id", "name", 5), -4 - )); + void instantiated_with_no_quantity_throws_exception() { + assertThrows(IllegalArgumentException.class, this::stackNoQty); } @Test void can_be_created_with_positive_quantity() { - var i = new ItemStack(new BasicItem("id", "name", 5), 5); - assertThat(i, instanceOf(ItemStack.class)); + assertThat(defaultStack(), instanceOf(ItemStack.class)); + } + + @Test + void has_default_record_method_for_item() { + var i = defaultStack(); + assertThat(i.item(), instanceOf(BasicItem.class)); + } + + @Test + void has_default_record_method_for_quantity() { + var i = defaultStack(); + assertEquals(5, i.quantity()); } } diff --git a/src/test/java/LootTableTest.java b/src/test/java/LootTableTest.java index 25ab0d5..24ff438 100644 --- a/src/test/java/LootTableTest.java +++ b/src/test/java/LootTableTest.java @@ -42,4 +42,16 @@ public class LootTableTest { assertThat(result, equalTo("Iron")); } + @Test + void returns_null_if_broken_roll() { + LootTable loot = new LootTable<>(); + var mockRandomProvider = mock(RandomProvider.class); + when(mockRandomProvider.nextInt(anyInt())).thenReturn(Integer.MAX_VALUE); + loot.addEntry("Stone", 90); + loot.addEntry("Iron", 10); + loot.setRandomProvider(mockRandomProvider); + String result = loot.roll(); + assertThat(result, is(nullValue())); + } + } diff --git a/src/test/java/MonsterTest.java b/src/test/java/MonsterTest.java index 6acf0bb..3c7ae25 100644 --- a/src/test/java/MonsterTest.java +++ b/src/test/java/MonsterTest.java @@ -27,13 +27,13 @@ public class MonsterTest { } @Test - void shade_cannot_move_after_death() { + void shade_cannot_moveTo_after_death() { defaultShade.kill(); assertThat(false, equalTo(defaultShade.moveTo(defaultDestination, mockWorld))); } @Test - void troll_cannot_move_after_death() { + void troll_cannot_moveTo_after_death() { defaultTroll.kill(); assertThat(false, equalTo(defaultTroll.moveTo(defaultDestination, mockWorld))); } @@ -130,13 +130,13 @@ public class MonsterTest { @Test void shade_cannot_heal_when_out_of_energy() { - Shade shade = new Shade(Shade.MAX_HEALTH, Shade.MIN_ENERGY, Shade.DEFAULT_POSITION); + Shade shade = new Shade(Shade.MAX_HEALTH - 1, Shade.MIN_ENERGY, Shade.DEFAULT_POSITION); assertThat(shade.heal(), equalTo(false)); } @Test void troll_cannot_heal_when_out_of_energy() { - Troll troll = new Troll(Troll.MAX_HEALTH, Troll.MIN_ENERGY, Troll.DEFAULT_POSITION); + Troll troll = new Troll(Troll.MAX_HEALTH - 1, Troll.MIN_ENERGY, Troll.DEFAULT_POSITION); assertThat(troll.heal(), equalTo(false)); } @@ -257,8 +257,136 @@ public class MonsterTest { assertThat(defaultTroll.heal(), equalTo(false)); } - // HÄR BÖRJAR BESLUTSTABELLSTESTERNA + @Test + void shade_wont_heal_at_max_health() { + assertThat(defaultShade.heal(), equalTo(false)); + } + @Test + void troll_wont_heal_at_max_health() { + assertThat(defaultTroll.heal(), equalTo(false)); + } + + @Test + void shade_taking_damage_greater_than_health_dies() { + defaultShade.takeDamage(Shade.MAX_HEALTH+1); + assertThat(defaultShade.isDead(), equalTo(true)); + } + + @Test + void troll_taking_damage_greater_than_health_dies() { + defaultTroll.takeDamage(Troll.MAX_HEALTH + 1); + assertThat(defaultTroll.isDead(), equalTo(true)); + } + + @Test + void shade_wont_move_at_zero_energy() { + defaultShade = new Shade(Shade.MAX_HEALTH, Shade.MIN_ENERGY, Shade.DEFAULT_POSITION); + assertThat(defaultShade.move(mockWorld), equalTo(false)); + } + + @Test + void troll_wont_move_at_zero_energy() { + defaultTroll = new Troll(Troll.MAX_HEALTH, Troll.MIN_ENERGY, Troll.DEFAULT_POSITION); + assertThat(defaultTroll.move(mockWorld), equalTo(false)); + } + + @Test + void cant_create_shade_with_negative_energy() { + defaultShade = new Shade(Shade.MAX_HEALTH, Shade.MIN_ENERGY - 1, Shade.DEFAULT_POSITION); + assertThat(defaultShade.getEnergy(), equalTo(Shade.MIN_ENERGY)); + } + + @Test + void cant_create_troll_with_negative_energy() { + defaultTroll = new Troll(Troll.MAX_HEALTH, Troll.MIN_ENERGY - 1, Troll.DEFAULT_POSITION); + assertThat(defaultTroll.getEnergy(), equalTo(Troll.MIN_ENERGY)); + } + + @Test + void cant_create_shade_with_no_health() { + defaultShade = new Shade(Shade.MIN_HEALTH, Shade.MAX_ENERGY, Shade.DEFAULT_POSITION); + assertThat(defaultShade.getHealth(), greaterThan(Shade.MIN_HEALTH)); + } + + @Test + void cant_create_troll_with_no_health() { + defaultTroll = new Troll(Troll.MIN_HEALTH, Troll.MAX_ENERGY, Troll.DEFAULT_POSITION); + assertThat(defaultTroll.getHealth(), greaterThan(Troll.MIN_HEALTH)); + } + + @Test + void shade_cant_take_negative_damage() { + assertThat(defaultShade.takeDamage(-9), equalTo(false)); + } + + @Test + void troll_cant_take_negative_damage() { + assertThat(defaultTroll.takeDamage(-9), equalTo(false)); + } + + @Test + void shade_wont_move_when_dead() { + defaultShade.kill(); + assertThat(defaultShade.move(mockWorld), equalTo(false)); + } + + @Test + void troll_wont_move_when_dead() { + defaultTroll.kill(); + assertThat(defaultTroll.move(mockWorld), equalTo(false)); + } + + @Test + void shade_wont_moveTo_at_zero_energy() { + defaultShade = new Shade(Shade.MAX_HEALTH, Shade.MIN_ENERGY, Shade.DEFAULT_POSITION); + assertThat(defaultShade.moveTo(defaultDestination, mockWorld), equalTo(false)); + } + + @Test + void troll_wont_moveTo_at_zero_energy() { + defaultTroll = new Troll(Troll.MAX_HEALTH, Troll.MIN_ENERGY, Troll.DEFAULT_POSITION); + assertThat(defaultTroll.moveTo(defaultDestination, mockWorld), equalTo(false)); + } + + @Test + void cant_create_shade_with_more_than_max_health() { + defaultShade = new Shade(Shade.MAX_HEALTH + 1, Shade.MAX_ENERGY, Shade.DEFAULT_POSITION); + assertThat(defaultShade.getHealth(), lessThanOrEqualTo(Shade.MAX_HEALTH)); + } + + @Test + void cant_create_troll_with_more_than_max_health() { + defaultTroll = new Troll(Troll.MAX_HEALTH + 1, Troll.MAX_ENERGY, Troll.DEFAULT_POSITION); + assertThat(defaultTroll.getHealth(), lessThanOrEqualTo(Troll.MAX_HEALTH)); + } + + @Test + void cant_create_shade_with_more_than_max_energy() { + defaultShade = new Shade(Shade.MAX_HEALTH, Shade.MAX_ENERGY + 1, Shade.DEFAULT_POSITION); + assertThat(defaultShade.getEnergy(), lessThanOrEqualTo(Shade.MAX_ENERGY)); + } + + @Test + void cant_create_troll_with_more_than_max_energy() { + defaultTroll = new Troll(Troll.MAX_HEALTH, Troll.MAX_ENERGY + 1, Troll.DEFAULT_POSITION); + assertThat(defaultTroll.getEnergy(), lessThanOrEqualTo(Troll.MAX_ENERGY)); + } + + @Test + void shade_can_move_to_tile_with_non_player_entity() { + Chest mockChest = mock(Chest.class); + when(mockChest.getPosition()).thenReturn(defaultDestination); + Map> entitiesAtDestination = new HashMap<>(); + entitiesAtDestination.put(defaultDestination, List.of(mockChest)); + when(mockWorld.getPositionEntityMap()).thenReturn(entitiesAtDestination); + + assertThat(defaultShade.moveTo(defaultDestination, mockWorld), equalTo(true)); + } + + // + // HÄR BÖRJAR BESLUTSTABELLSTESTERNA + // @Test void decision_table_t1() { defaultTroll.kill(); diff --git a/src/test/java/WorldTest.java b/src/test/java/WorldTest.java index 9ca607b..b520555 100644 --- a/src/test/java/WorldTest.java +++ b/src/test/java/WorldTest.java @@ -23,14 +23,7 @@ class WorldTest { @Test void worldCreationTest() { World test = new World(5, 5); - String[][] mapTest = test.getMap(); - int counter = 0; - for (int i = 0; i < mapTest.length; i++) { - for (int j = 0; j < mapTest[i].length; j++) { - counter += 1; - } - } - assertEquals(counter, 5 * 5); + assertEquals(25, test.getMap().length * test.getMap()[0].length); } @Test void mapCreationTest() {