GameLoop setup

This commit is contained in:
L3o15 2025-05-14 18:38:51 +02:00
parent c00feab2c3
commit 9a24645cab
17 changed files with 96 additions and 61 deletions

BIN
Lib/kibofplib-1.0.jar Normal file

Binary file not shown.

View file

@ -50,5 +50,13 @@ public class ElementGraph {
return totalStrength; return totalStrength;
} }
public List<Element> getElementsNames(){
List<Element> elements = new ArrayList<>();
for (GraphElement el : this.elements ){
elements.add(el.getElement());
}
return elements;
}
} }

View file

@ -1,5 +1,9 @@
package it.arnaldo.unibs.tamagolem; package it.arnaldo.unibs.tamagolem;
import it.kibo.fp.lib.Menu;
import java.util.List;
public class Game { public class Game {
private final Player player1; private final Player player1;
private final Player player2; private final Player player2;
@ -34,35 +38,32 @@ public class Game {
public void start() { public void start() {
// N // N
int numberOfElements = worldBalance.getElements().size(); numberOfElements = worldBalance.getElements().size();
// P // P
int numberOfStones = (int) Math.ceil((numberOfElements + 1) / 3.0) + 1; numberOfStones = (int) Math.ceil((numberOfElements + 1) / 3.0) + 1;
// G // 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 // S
int totalNumberOfStones = (int) Math.ceil((2.0 * numberOfTamaGolems * numberOfStones)); totalNumberOfStones = (int) Math.ceil((2.0 * numberOfTamaGolems * numberOfStones));
// Stones for each element // Stones for each element
int numberOfStonesPerElement = totalNumberOfStones / numberOfElements; numberOfStonesPerElement = totalNumberOfStones / numberOfElements;
// deve essere uguale alla somma della potenza di tutti gli elementi // deve essere uguale alla somma della potenza di tutti gli elementi
int totalPower = worldBalance.getTotalStrength(); 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");
lifePoints = totalPower;
} }
public void start() {
setup();
loop();
worldBalance.printGraph();
}
} }

46
src/GameLoop.java Normal file
View file

@ -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));
}
}

View file

@ -1,5 +1,7 @@
package it.arnaldo.unibs.tamagolem; package it.arnaldo.unibs.tamagolem;
import it.kibo.fp.lib.RandomDraws;
import java.util.*; import java.util.*;
public class LinearSystem { public class LinearSystem {
@ -30,12 +32,11 @@ public class LinearSystem {
} }
int[] solutions; int[] solutions;
Random rand = new Random();
int tries = 1000000; int tries = 1000000;
while (tries-- > 0) { while (tries-- > 0) {
int[] lambdaValues = new int[indipendentVariables]; int[] lambdaValues = new int[indipendentVariables];
for (int i = 0; i < indipendentVariables; i++) { 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); solutions = constructSolution(extendedMatrix, lambdaValues, rank);

7
src/Modes.java Normal file
View file

@ -0,0 +1,7 @@
package it.arnaldo.unibs.tamagolem;
public enum Modes {
EASY,
MEDIUM,
HARD
}

View file

@ -1,36 +1,8 @@
package it.arnaldo.unibs.tamagolem; package it.arnaldo.unibs.tamagolem;
import java.util.*;
public class TamaGolemMain { public class TamaGolemMain {
public static void main(String[] args) { public static void main(String[] args) {
Scanner sc = new Scanner(System.in); GameLoop gameLoop = new GameLoop();
gameLoop.start();
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());
} }
} }

View file

@ -1,13 +1,21 @@
package it.arnaldo.unibs.tamagolem; package it.arnaldo.unibs.tamagolem;
import it.kibo.fp.lib.RandomDraws;
import java.util.*; import java.util.*;
public class WorldBuilder { public class WorldBuilder {
public static ElementGraph buildWorld() { public static ElementGraph buildWorld(Modes difficulty) {
// genero un numero casuale che mi dice quanti elementi prendo // genero un numero casuale che mi dice quanti elementi prendo
Element[] allElements = Element.values(); 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<Element> selectedElements = new ArrayList<>(Arrays.asList(allElements)); List<Element> selectedElements = new ArrayList<>(Arrays.asList(allElements));
Collections.shuffle(selectedElements); Collections.shuffle(selectedElements);
@ -20,7 +28,6 @@ public class WorldBuilder {
Map<Element, Map<Element, Integer>> linkDirections = generateValidDirections(selectedElements); Map<Element, Map<Element, Integer>> linkDirections = generateValidDirections(selectedElements);
// Mappa indice per ogni coppia (non ordinata) di elementi // Mappa indice per ogni coppia (non ordinata) di elementi
Map<Set<Element>, Integer> pairIndex = new HashMap<>(); Map<Set<Element>, Integer> pairIndex = new HashMap<>();
int index = 0; int index = 0;
@ -48,7 +55,6 @@ public class WorldBuilder {
} }
// sistema lineare per trovare le soluzioni che verificano l'equilibrio // sistema lineare per trovare le soluzioni che verificano l'equilibrio
try { try {
LinearSystem system = new LinearSystem(numElements, A, b); LinearSystem system = new LinearSystem(numElements, A, b);
int[] solution = system.resolve(); int[] solution = system.resolve();
@ -66,21 +72,15 @@ public class WorldBuilder {
world.addElement(graphElement); world.addElement(graphElement);
} }
world.printGraph();
return world; return world;
}catch (Exception e){ } catch (Exception e){
buildWorld(); return buildWorld(difficulty);
} }
return null;
} }
private static Map<Element, Map<Element, Integer>> generateValidDirections(List<Element> elements) { private static Map<Element, Map<Element, Integer>> generateValidDirections(List<Element> elements) {
int n = elements.size(); int n = elements.size();
Random rand = new Random();
Map<Element, Map<Element, Integer>> linkDirections = new HashMap<>(); Map<Element, Map<Element, Integer>> linkDirections = new HashMap<>();
for (Element e : elements) { for (Element e : elements) {
@ -114,7 +114,7 @@ public class WorldBuilder {
for (Pair pair : pairs) { for (Pair pair : pairs) {
Element e1 = pair.getFirst(); Element e1 = pair.getFirst();
Element e2 = pair.getSecond(); Element e2 = pair.getSecond();
boolean dir = rand.nextBoolean(); boolean dir = RandomDraws.estraiBoolean();
if (dir) { if (dir) {
linkDirections.get(e1).put(e2, 1); linkDirections.get(e1).put(e2, 1);
linkDirections.get(e2).put(e1, -1); linkDirections.get(e2).put(e1, -1);