diff --git a/Lib/kibofplib-1.0.jar b/Lib/kibofplib-1.0.jar new file mode 100644 index 0000000..8929bd5 Binary files /dev/null and b/Lib/kibofplib-1.0.jar differ diff --git a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/Element.class b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/Element.class index 3ef3bf3..9f2bd76 100644 Binary files a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/Element.class and b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/Element.class differ diff --git a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/ElementGraph.class b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/ElementGraph.class index 6cedb9a..7e9db6f 100644 Binary files a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/ElementGraph.class and b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/ElementGraph.class differ diff --git a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/ElementLink.class b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/ElementLink.class index cbc81aa..8daf6c4 100644 Binary files a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/ElementLink.class and b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/ElementLink.class differ diff --git a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/Game.class b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/Game.class index c01d743..50c6d8b 100644 Binary files a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/Game.class and b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/Game.class differ diff --git a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/GraphElement.class b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/GraphElement.class index 4cac44a..e1a7f72 100644 Binary files a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/GraphElement.class and b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/GraphElement.class differ diff --git a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/Player.class b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/Player.class index 6df87d6..d7be542 100644 Binary files a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/Player.class and b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/Player.class differ diff --git a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/TamaGolem.class b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/TamaGolem.class index 7dcef0b..acce035 100644 Binary files a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/TamaGolem.class and b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/TamaGolem.class differ diff --git a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/TamaGolemMain.class b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/TamaGolemMain.class index 8bfb5fb..9f03158 100644 Binary files a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/TamaGolemMain.class and b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/TamaGolemMain.class differ diff --git a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/WorldBuilder.class b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/WorldBuilder.class index 0c5c0c5..5634304 100644 Binary files a/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/WorldBuilder.class and b/out/production/TamaGolem/it/arnaldo/unibs/tamagolem/WorldBuilder.class differ diff --git a/src/ElementGraph.java b/src/ElementGraph.java index 1680423..9e47145 100644 --- a/src/ElementGraph.java +++ b/src/ElementGraph.java @@ -50,5 +50,13 @@ public class ElementGraph { return totalStrength; } + public List getElementsNames(){ + List elements = new ArrayList<>(); + for (GraphElement el : this.elements ){ + elements.add(el.getElement()); + } + return elements; + } + } diff --git a/src/Game.java b/src/Game.java index ccb7058..4b00357 100644 --- a/src/Game.java +++ b/src/Game.java @@ -1,5 +1,9 @@ package it.arnaldo.unibs.tamagolem; +import it.kibo.fp.lib.Menu; + +import java.util.List; + public class Game { private final Player player1; private final Player player2; @@ -34,35 +38,32 @@ public class Game { public void start() { // N - int numberOfElements = worldBalance.getElements().size(); + numberOfElements = worldBalance.getElements().size(); // P - int numberOfStones = (int) Math.ceil((numberOfElements + 1) / 3.0) + 1; + numberOfStones = (int) Math.ceil((numberOfElements + 1) / 3.0) + 1; // G - int numberOfTamaGolems = (int) Math.ceil(((numberOfElements - 1) * (numberOfElements - 2)) / (2.0 * numberOfStones)); + numberOfTamaGolems = (int) Math.ceil(((numberOfElements - 1) * (numberOfElements - 2)) / (2.0 * numberOfStones)); // S - int totalNumberOfStones = (int) Math.ceil((2.0 * numberOfTamaGolems * numberOfStones)); + totalNumberOfStones = (int) Math.ceil((2.0 * numberOfTamaGolems * numberOfStones)); // Stones for each element - int numberOfStonesPerElement = totalNumberOfStones / numberOfElements; + numberOfStonesPerElement = totalNumberOfStones / numberOfElements; // deve essere uguale alla somma della potenza di tutti gli elementi - int totalPower = worldBalance.getTotalStrength(); - - System.out.println("Game started between " + player1.getName() + " and " + player2.getName()); - System.out.println("Number of stones: " + numberOfStones); - System.out.println("Number of elements: " + numberOfElements); - System.out.println("Number of TamaGolems for every player: " + numberOfTamaGolems); - System.out.println("Total number of stones: " + totalNumberOfStones); - System.out.println("Total number of stones for each element: " + numberOfStonesPerElement); - System.out.println("Total power: " + totalPower); - System.out.println("Life points: " + totalPower); - System.out.println("Press R to create a new game"); + totalPower = worldBalance.getTotalStrength(); + lifePoints = totalPower; } + public void start() { + setup(); + loop(); + worldBalance.printGraph(); + } + } diff --git a/src/GameLoop.java b/src/GameLoop.java new file mode 100644 index 0000000..0619953 --- /dev/null +++ b/src/GameLoop.java @@ -0,0 +1,46 @@ +package it.arnaldo.unibs.tamagolem; + +import it.kibo.fp.lib.InputData; +import it.kibo.fp.lib.Menu; + +public class GameLoop { + private Game game; + + public void start() { + String[] firstMenuEntries = {"New game"}; + Menu firstMenu = new Menu("TamaGolem", firstMenuEntries, true, true, false); + int firstmenuChoise; + + do { + createNewGame(); + game.start(); + firstmenuChoise = firstMenu.choose(); + } while (firstmenuChoise == 1); + } + + public void createNewGame() { + String[] diffucultyMenuEntries = {"Easy", "Medium", "Hard"}; + Menu difficultyMenu = new Menu("Select Mode", diffucultyMenuEntries, true, true, false); + + Player p1 = new Player(InputData.readNonEmptyString(""" + \s + Player 1: + \tChoose your name:\s""", true)); + + Player p2 = new Player(InputData.readNonEmptyString(""" + \s + Player 1: + \tChoose your name:\s""", true)); + + int difficultyChoise = difficultyMenu.choose(); + Modes difficulty = null; + + switch (difficultyChoise){ + case 1 -> difficulty = Modes.EASY; + case 2 -> difficulty = Modes.MEDIUM; + case 3 -> difficulty = Modes.HARD; + } + + game = new Game(p1, p2, WorldBuilder.buildWorld(difficulty)); + } +} diff --git a/src/LinearSystem.java b/src/LinearSystem.java index 750e34a..5f317d5 100644 --- a/src/LinearSystem.java +++ b/src/LinearSystem.java @@ -1,5 +1,7 @@ package it.arnaldo.unibs.tamagolem; +import it.kibo.fp.lib.RandomDraws; + import java.util.*; public class LinearSystem { @@ -30,12 +32,11 @@ public class LinearSystem { } int[] solutions; - Random rand = new Random(); int tries = 1000000; while (tries-- > 0) { int[] lambdaValues = new int[indipendentVariables]; for (int i = 0; i < indipendentVariables; i++) { - lambdaValues[i] = (rand.nextInt(m) + 1); // valori tra 1 e m + lambdaValues[i] = RandomDraws.drawInteger(1, m); } solutions = constructSolution(extendedMatrix, lambdaValues, rank); diff --git a/src/Modes.java b/src/Modes.java new file mode 100644 index 0000000..a3d9abd --- /dev/null +++ b/src/Modes.java @@ -0,0 +1,7 @@ +package it.arnaldo.unibs.tamagolem; + +public enum Modes { + EASY, + MEDIUM, + HARD +} diff --git a/src/TamaGolemMain.java b/src/TamaGolemMain.java index ee6e4d0..c7ed769 100644 --- a/src/TamaGolemMain.java +++ b/src/TamaGolemMain.java @@ -1,36 +1,8 @@ package it.arnaldo.unibs.tamagolem; -import java.util.*; - public class TamaGolemMain { public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - - Game game; - - do { - System.out.println("TamaGolem"); - System.out.println(""" - - Player 1: - Choose your name - - """); - - Player p1 = new Player(sc.nextLine()); - - System.out.println(""" - - Player 2: - Choose your name - - """); - - Player p2 = new Player(sc.nextLine()); - - game = new Game(p1, p2, WorldBuilder.buildWorld()); - - game.start(); - } while (game.isRestart()); + GameLoop gameLoop = new GameLoop(); + gameLoop.start(); } } \ No newline at end of file diff --git a/src/WorldBuilder.java b/src/WorldBuilder.java index abb58e0..19a62d8 100644 --- a/src/WorldBuilder.java +++ b/src/WorldBuilder.java @@ -1,13 +1,21 @@ package it.arnaldo.unibs.tamagolem; +import it.kibo.fp.lib.RandomDraws; + import java.util.*; public class WorldBuilder { - public static ElementGraph buildWorld() { + public static ElementGraph buildWorld(Modes difficulty) { // genero un numero casuale che mi dice quanti elementi prendo Element[] allElements = Element.values(); - int numElements = (int)(Math.random() * 8) + 3; + int numElements = 0; + + switch (difficulty) { + case EASY -> numElements = RandomDraws.drawInteger(3, 5); + case MEDIUM -> numElements = RandomDraws.drawInteger(6, 8); + case HARD -> numElements = RandomDraws.drawInteger(9, 10); + } List selectedElements = new ArrayList<>(Arrays.asList(allElements)); Collections.shuffle(selectedElements); @@ -20,7 +28,6 @@ public class WorldBuilder { Map> linkDirections = generateValidDirections(selectedElements); - // Mappa indice per ogni coppia (non ordinata) di elementi Map, Integer> pairIndex = new HashMap<>(); int index = 0; @@ -48,7 +55,6 @@ public class WorldBuilder { } // sistema lineare per trovare le soluzioni che verificano l'equilibrio - try { LinearSystem system = new LinearSystem(numElements, A, b); int[] solution = system.resolve(); @@ -66,21 +72,15 @@ public class WorldBuilder { world.addElement(graphElement); } - world.printGraph(); return world; - }catch (Exception e){ - buildWorld(); + } catch (Exception e){ + return buildWorld(difficulty); } - - - return null; - } private static Map> generateValidDirections(List elements) { int n = elements.size(); - Random rand = new Random(); Map> linkDirections = new HashMap<>(); for (Element e : elements) { @@ -114,7 +114,7 @@ public class WorldBuilder { for (Pair pair : pairs) { Element e1 = pair.getFirst(); Element e2 = pair.getSecond(); - boolean dir = rand.nextBoolean(); + boolean dir = RandomDraws.estraiBoolean(); if (dir) { linkDirections.get(e1).put(e2, 1); linkDirections.get(e2).put(e1, -1);