From 1b6eb4d50ac1769975ed2326668e93e430d081b1 Mon Sep 17 00:00:00 2001 From: iryupnik Date: Wed, 27 May 2020 18:52:56 +0200 Subject: [PATCH] Unit tests --- .../ruscz/game/gameOfProd/Battle/Battle.java | 23 ++- .../gameOfProd/Cells/InteractionCell.java | 2 +- .../Inventory/InventoryManager.java | 24 ++- .../ruscz/game/gameOfProd/Inventory/Item.java | 19 +++ src/main/resources/maps/999/m6.txt | 4 +- src/main/resources/save/savedGame/events.txt | 65 -------- src/main/resources/save/savedGame/person.txt | 6 - src/main/resources/save/savedGame/village.txt | 1 - .../game/gameOfProd/Battle/BattleTest.java | 35 +++++ .../Inventory/InventoryManagerTest.java | 139 ++++++++++++++++++ 10 files changed, 237 insertions(+), 81 deletions(-) create mode 100644 src/test/java/ruscz/game/gameOfProd/Inventory/InventoryManagerTest.java diff --git a/src/main/java/ruscz/game/gameOfProd/Battle/Battle.java b/src/main/java/ruscz/game/gameOfProd/Battle/Battle.java index 5ebc877..72d06bd 100644 --- a/src/main/java/ruscz/game/gameOfProd/Battle/Battle.java +++ b/src/main/java/ruscz/game/gameOfProd/Battle/Battle.java @@ -15,6 +15,8 @@ public class Battle { private Timer timer; private BattlePerson turnBelongTo; + private LinkedList turnOrder; + private int turnCount; private int delay; @@ -67,6 +69,7 @@ public class Battle { windowOfBattle.getMenuOfActions().setVisible(true); windowOfBattle.getEnemyDescription().setVisible(true); windowOfBattle.repaint(); + turnOrder = new LinkedList<>(); timer = new Timer(1000, actionEvent -> { for (Enemy e: enemies) System.out.println(e.health); if (checkHealthOfEnemies()) endBattle(true); @@ -111,6 +114,7 @@ public class Battle { //TODO Animation of Death if (e.health <= 0) enemies.remove(e); + turnOrder.add(turnBelongTo); playerAttack = false; turnBelongTo = null; delay = 2; @@ -123,10 +127,18 @@ public class Battle { LinkedList turns = new LinkedList<>(enemies); turns.addAll(heroes); - //TODO Check turns (maybe List) + + if (checkIfTurnOrderIsFull(turns)) + turnOrder = new LinkedList<>(); + Collections.shuffle(turns); turnBelongTo = turns.getFirst(); + while (turnOrder.contains(turnBelongTo)){ + Collections.shuffle(turns); + turnBelongTo = turns.getFirst(); + } + System.out.println(turnBelongTo.getClass().getName()); } @@ -138,6 +150,8 @@ public class Battle { //TODO Animation of Attack //TODO Animation of Death if (e.health <= 0) heroes.remove(e); + + turnOrder.add(turnBelongTo); delay = 2; turnBelongTo = null; } @@ -161,4 +175,11 @@ public class Battle { public boolean isPlayerAttack() { return playerAttack; } + + private boolean checkIfTurnOrderIsFull(LinkedList allBattlePerson){ + for(BattlePerson battlePerson: allBattlePerson){ + if (!turnOrder.contains(battlePerson)) return false; + } + return true; + } } diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java b/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java index 3d67b6e..2d2e7c5 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java @@ -269,7 +269,7 @@ public class InteractionCell extends Cell { } public void useItem(Item item) { - InventoryManager.removeItem(item); + InventoryManager.removeItem(item, 1); } @Override diff --git a/src/main/java/ruscz/game/gameOfProd/Inventory/InventoryManager.java b/src/main/java/ruscz/game/gameOfProd/Inventory/InventoryManager.java index de432fd..29a8519 100644 --- a/src/main/java/ruscz/game/gameOfProd/Inventory/InventoryManager.java +++ b/src/main/java/ruscz/game/gameOfProd/Inventory/InventoryManager.java @@ -5,24 +5,38 @@ import java.util.LinkedList; public class InventoryManager { private static LinkedList inventory = new LinkedList(); - public static void addItem(Item item) { + for(Item item1: inventory){ + if(item1.equals(item)) { + item1.setMultiplicity(item1.getMultiplicity() + item.getMultiplicity()); + return; + } + } inventory.add(item); } public static void flushInventory() { - inventory = null; + inventory = new LinkedList<>(); } public static boolean isInInventory(Item item) { for (Item inventoryItem : inventory) { - if (inventoryItem == item) return true; + if (inventoryItem.equals(item)) return true; } return false; } - public static void removeItem(Item item) { - inventory.remove(item); + public static boolean removeItem(Item item, int count) { + if (isInInventory(item)) { + Item itemIn = findItemByName(item.getName()); + if (itemIn.getMultiplicity() > count) + itemIn.setMultiplicity(itemIn.getMultiplicity() - count); + else if (itemIn.getMultiplicity() == count) + inventory.remove(item); + else return false; + return true; + } + return false; } public static LinkedList getInventory() { return inventory; } diff --git a/src/main/java/ruscz/game/gameOfProd/Inventory/Item.java b/src/main/java/ruscz/game/gameOfProd/Inventory/Item.java index 9587935..0d27cc8 100644 --- a/src/main/java/ruscz/game/gameOfProd/Inventory/Item.java +++ b/src/main/java/ruscz/game/gameOfProd/Inventory/Item.java @@ -1,5 +1,7 @@ package ruscz.game.gameOfProd.Inventory; +import java.util.Objects; + public class Item { private ItemAction itemAction; private String name; @@ -52,7 +54,24 @@ public class Item { public int getMultiplicity() { return multiplicity; } + public void setMultiplicity(int multiplicity) { this.multiplicity = multiplicity; } + public ItemAction getItemAction() { return itemAction; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Item item = (Item) o; + return image == item.image && + itemAction == item.itemAction && + name.equals(item.name); + } + + @Override + public int hashCode() { + return Objects.hash(itemAction, name, image); + } } diff --git a/src/main/resources/maps/999/m6.txt b/src/main/resources/maps/999/m6.txt index c8fca8e..3e9f63e 100644 --- a/src/main/resources/maps/999/m6.txt +++ b/src/main/resources/maps/999/m6.txt @@ -1,5 +1,5 @@ 36 36 -EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK SPAWN BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY +EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY @@ -23,7 +23,7 @@ EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EM EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK -EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY +EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY SPAWN EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY EMPTY BLOCK BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY BLOCK EMPTY EMPTY BLOCK diff --git a/src/main/resources/save/savedGame/events.txt b/src/main/resources/save/savedGame/events.txt index a18be6b..e69de29 100644 --- a/src/main/resources/save/savedGame/events.txt +++ b/src/main/resources/save/savedGame/events.txt @@ -1,65 +0,0 @@ - - tutorial - - - 11 - 6 - false - - - - - openspace - - - 6 - 4 - true - - - 14 - 16 - false - - - 15 - 7 - true - - - - - office - - - 4 - 2 - false - - - 1 - 9 - true - - - - - boss - - - 4 - 18 - true - - - - - treasure - - - 6 - 1 - true - - - diff --git a/src/main/resources/save/savedGame/person.txt b/src/main/resources/save/savedGame/person.txt index 2c7cd7c..e69de29 100644 --- a/src/main/resources/save/savedGame/person.txt +++ b/src/main/resources/save/savedGame/person.txt @@ -1,6 +0,0 @@ - - office - 8 - 7 - 3 - \ No newline at end of file diff --git a/src/main/resources/save/savedGame/village.txt b/src/main/resources/save/savedGame/village.txt index 1b4bc7b..e69de29 100644 --- a/src/main/resources/save/savedGame/village.txt +++ b/src/main/resources/save/savedGame/village.txt @@ -1 +0,0 @@ -TODO: Village \ No newline at end of file diff --git a/src/test/java/ruscz/game/gameOfProd/Battle/BattleTest.java b/src/test/java/ruscz/game/gameOfProd/Battle/BattleTest.java index f00aa14..d02ee94 100644 --- a/src/test/java/ruscz/game/gameOfProd/Battle/BattleTest.java +++ b/src/test/java/ruscz/game/gameOfProd/Battle/BattleTest.java @@ -55,6 +55,41 @@ public class BattleTest { } + @Test + public void startBattle_battleWithAMightEnemy_winOfEnemy(){ + //Arrange + Battle battle; + Hero h; + Enemy e; + LinkedList llh; + LinkedList lle; + Game game; + + //Act + game = new Game(); + game.setVisible(false); + + h = new Hero(1, 1, "123", 1, 1, 0); + e = new Enemy(2,100,"2", 2, 1); + + llh = new LinkedList<>(Collections.singleton(h)); + lle = new LinkedList<>(Collections.singleton(e)); + + battle = new Battle(llh, lle, new WindowOfBattle(game)); + battle.startBattle(); + + battle.setPlayerAttack(true); + battle.setTimerStopped(false); + battle.getTimer().start(); + + timerOfWaiting(battle); + + //Assertion + Assertions.assertTrue(battle.checkHealthOfHeroes()); + Assertions.assertFalse(battle.checkHealthOfEnemies()); + + } + private void timerOfWaiting(Battle battle){ while (!battle.isTimerStopped()){ sleep(1000); diff --git a/src/test/java/ruscz/game/gameOfProd/Inventory/InventoryManagerTest.java b/src/test/java/ruscz/game/gameOfProd/Inventory/InventoryManagerTest.java new file mode 100644 index 0000000..06ff846 --- /dev/null +++ b/src/test/java/ruscz/game/gameOfProd/Inventory/InventoryManagerTest.java @@ -0,0 +1,139 @@ +package ruscz.game.gameOfProd.Inventory; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class InventoryManagerTest { + + @AfterEach + public void flush(){ + InventoryManager.flushInventory(); + } + + @Test + public void addItem_addItemInInventory_successfulAdd(){ + //Arrange + Item i1, i2, i3; + + //Act + i1 = new Item("Soda", 10, 1, "heal"); + i2 = new Item("sODa", 1, 2, "mana"); + i3 = new Item("Key", 1, 3, "key"); + + InventoryManager.addItem(i1); + InventoryManager.addItem(i2); + InventoryManager.addItem(i3); + + //Assert + Assertions.assertTrue(InventoryManager.getInventory().contains(i1)); + Assertions.assertTrue(InventoryManager.getInventory().contains(i2)); + Assertions.assertTrue(InventoryManager.getInventory().contains(i3)); + } + + @Test + public void removeItem_removeItemFromInventory_successfulRemove(){ + //Arrange + Item i1, i2, i3; + + //Act + i1 = new Item("Soda", 10, 1, "heal"); + i2 = new Item("sODa", 1, 2, "mana"); + i3 = new Item("Key", 1, 3, "key"); + + InventoryManager.addItem(i1); + InventoryManager.addItem(i2); + InventoryManager.addItem(i3); + + InventoryManager.removeItem(i2, 1); + + //Assert + Assertions.assertTrue(InventoryManager.getInventory().contains(i1)); + Assertions.assertFalse(InventoryManager.getInventory().contains(i2)); + Assertions.assertTrue(InventoryManager.getInventory().contains(i3)); + } + + @Test + public void addItem_increaseMultiplicityOfItem_successfulIncreasing(){ + //Arrange + Item i1, i2, i3; + + //Act + i1 = new Item("Soda", 10, 1, "heal"); + i2 = new Item("Soda", 1, 1, "heal"); + i3 = new Item("Soda", 1, 1, "heal"); + + InventoryManager.addItem(i1); + InventoryManager.addItem(i2); + InventoryManager.addItem(i3); + + //Assert + Assertions.assertTrue(InventoryManager.getInventory().contains(i1)); + Assertions.assertEquals(12, InventoryManager.getInventory().getFirst().getMultiplicity()); + } + + @Test + public void removeItem_decreaseMultiplicityOfItem_successfulReduce(){ + //Arrange + Item i1, i2, i3; + + //Act + i1 = new Item("Soda", 10, 1, "heal"); + i2 = new Item("Soda", 1, 1, "heal"); + i3 = new Item("Soda", 1, 1, "heal"); + + InventoryManager.addItem(i1); + InventoryManager.addItem(i2); + InventoryManager.addItem(i3); + + InventoryManager.removeItem(i1, 1); + + //Assert + Assertions.assertTrue(InventoryManager.getInventory().contains(i1)); + Assertions.assertEquals(11, InventoryManager.getInventory().getFirst().getMultiplicity()); + } + + @Test + public void removeItem_tryDecreaseNonExistedItemOrDecreaseTooMuch_unsuccessfulReduce(){ + //Arrange + Item i1, i2; + + //Act + i1 = new Item("Soda", 10, 1, "heal"); + i2 = new Item("SoDa", 1, 1, "mana"); + + InventoryManager.addItem(i1); + + //Assert + Assertions.assertFalse(InventoryManager.removeItem(i2, 10)); + + Assertions.assertFalse(InventoryManager.removeItem(i1, 100)); + Assertions.assertEquals(10, InventoryManager.getInventory().getFirst().getMultiplicity()); + } + + @Test + public void findItemByName_searchingExistedItem_Item(){ + //Arrange + Item i1; + + //Act + i1 = new Item("Soda", 10, 1, "heal"); + + InventoryManager.addItem(i1); + + //Assert + Assertions.assertEquals(i1, InventoryManager.findItemByName(i1.getName())); + } + + @Test + public void findItemByName_trySearchingExistedItem_null(){ + //Arrange + Item i1; + + //Act + i1 = new Item("Soda", 10, 1, "heal"); + + //Assert + Assertions.assertNull(InventoryManager.findItemByName(i1.getName())); + } +}