143 lines
5 KiB
Java
143 lines
5 KiB
Java
package it.arnaldo.unibs.tamagolem;
|
|
|
|
import java.util.*;
|
|
|
|
public class WorldBuilder {
|
|
public static ElementGraph buildWorld() {
|
|
Element[] allElements = Element.values();
|
|
Random rand = new Random();
|
|
// da cambiare il numero a 8
|
|
//int numElements = 3 + rand.nextInt(8);
|
|
|
|
// numero di elementi estratti a caso
|
|
int numElements = 5;
|
|
List<Element> selectedElements = new ArrayList<>(Arrays.asList(allElements));
|
|
|
|
// elementi scelti
|
|
Collections.shuffle(selectedElements);
|
|
selectedElements = selectedElements.subList(0, numElements);
|
|
|
|
ElementGraph world = new ElementGraph();
|
|
|
|
// mappa che contiene una mappa in modo che dato un elemento ti da una mappa con chiave un secondo elemento il cui valore è 1 se l'elemento infligge o -1 se l'elemento subisce
|
|
HashMap<Element, HashMap<Element, Integer>> linkDirections = new HashMap<>();
|
|
for (Element element : selectedElements) {
|
|
HashMap<Element, Integer> links = new HashMap<>();
|
|
Element followingElement = selectedElements.get((selectedElements.indexOf(element) + 1) % selectedElements.size());
|
|
for (Element el : selectedElements) {
|
|
if (el == followingElement) {
|
|
links.put(el, 1);
|
|
} else if (el != element) {
|
|
links.put(el, 0);
|
|
}
|
|
}
|
|
|
|
linkDirections.put(element, links);
|
|
}
|
|
|
|
for (Element element : selectedElements) {
|
|
HashMap<Element, Integer> elementLinks = linkDirections.get(element);
|
|
for (Element el : selectedElements) {
|
|
if (el == element) {
|
|
continue;
|
|
}
|
|
|
|
// controllo anche prima per evitare che magari da un estrazione si vada a togliere
|
|
if (elementLinks.get(el) == 1){
|
|
linkDirections.get(el).put(element, -1);
|
|
}
|
|
|
|
if (elementLinks.get(el) == 0) {
|
|
if (Math.random() < 0.5) {
|
|
linkDirections.get(el).put(element, 1);
|
|
} else {
|
|
linkDirections.get(el).put(element, -1);
|
|
}
|
|
}
|
|
|
|
if (elementLinks.get(el) == 1){
|
|
linkDirections.get(el).put(element, -1);
|
|
} else {
|
|
linkDirections.get(el).put(element, 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
System.out.println(linkDirections);
|
|
|
|
// hashmap che dato una coppia di element (è un set cosi AB = BA) ti torna la posizione all0interno dell'array di valori passato al sistema lineare
|
|
HashMap<Set<Element>, Integer> pairIndex = new HashMap<>();
|
|
|
|
int index = 0;
|
|
for (int i = 0; i < selectedElements.size(); i++) {
|
|
for (int j = i + 1; j < selectedElements.size(); j++) {
|
|
Set<Element> pair = Set.of(selectedElements.get(i), selectedElements.get(j));
|
|
|
|
pairIndex.put(pair, index++);
|
|
}
|
|
}
|
|
|
|
System.out.println(pairIndex);
|
|
|
|
// matrice incognite
|
|
int[][] A = new int[numElements][numElements * (numElements - 1) / 2];
|
|
|
|
// matrice termini noti
|
|
int[] b = new int[numElements];
|
|
|
|
// popolo le 2 matrici
|
|
int i = 0;
|
|
for (Element element : selectedElements) {
|
|
int[] line = new int[numElements * (numElements - 1) / 2];
|
|
HashMap<Element, Integer> elementLinks = linkDirections.get(element);
|
|
for (Element el : selectedElements) {
|
|
if (el == element) {
|
|
continue;
|
|
} else {
|
|
int idx = getIndex(element, el, pairIndex);
|
|
line[idx] = elementLinks.get(el);
|
|
}
|
|
|
|
}
|
|
A[i] = line;
|
|
b[i] = 0;
|
|
i++;
|
|
}
|
|
|
|
|
|
// creo e risolvo il sistema lineare associato
|
|
LinearSystem system = new LinearSystem(numElements, A, b);
|
|
|
|
|
|
int[] solution = system.resolve();
|
|
System.out.println("Soluzione trovata:");
|
|
for (int x : solution) {
|
|
System.out.printf(String.format("%d ", x));
|
|
}
|
|
System.out.println();
|
|
|
|
|
|
// popola il grafo in base ai risultati del sistema lineare
|
|
for (Element element : selectedElements) {
|
|
GraphElement graphElement = new GraphElement(element);
|
|
for (Element el : selectedElements) {
|
|
if (el == element) {
|
|
continue;
|
|
}
|
|
int direction = linkDirections.get(element).get(el);
|
|
graphElement.addLink(new ElementLink(solution[getIndex(element, el, pairIndex)] * direction, el));
|
|
}
|
|
world.addElement(graphElement);
|
|
}
|
|
world.printGraph();
|
|
|
|
return world;
|
|
}
|
|
|
|
private static int getIndex(Element e1, Element e2, HashMap<Set<Element>, Integer> pairIndex) {
|
|
return pairIndex.get(Set.of(e1, e2));
|
|
}
|
|
|
|
}
|
|
|