ajustement et correction

This commit is contained in:
2024-08-14 22:31:54 +02:00
parent cb2173d717
commit a8f0e7cdca
7 changed files with 110 additions and 56 deletions

View File

@@ -1,13 +1,7 @@
import java.io.File;
import java.io.ObjectInputFilter.Config;
import java.util.ArrayList;
import configuration.ConfigGame;
import game.Terminal;
import game.environnement.*;
import personnage.*;
import personnage.IAQLearning.QTable;
import tests.IATest;
public class Main {
/**

View File

@@ -1,22 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Configuration globale -->
<Configuration>
<Size n="2"/>
<Configuration n="2">
<!-- Configuration de la map -->
<Map>
<Coordinate x="22" y="22"/>
<Wall x="1" y="2"/>
<Wall x="3" y="4"/>
<Fraise x="5" y="6"/>
</Map>
<Map x="22" y="22"></Map>
<!-- Configuration des personnages -->
<Personnage>
<Player name="Philippe_Etchebest" x="2" y="2"/>
<!-- <Player name="Luke Skywalker" x="19" y="19"/> -->
<!-- <IA name="" x="19" y="19" QTable=""/> -->
<Robot name="R2D2" x="19" y="19"/>
<Player name="Luke Skywalker" x="19" y="19"/>
</Personnage>
</Configuration>

View File

@@ -23,21 +23,27 @@ public class ConfigGame {
try {
FileReaderXml fileReaderXml = new FileReaderXml(path);
this.data = fileReaderXml.getElements();
if (this.data.get("Configuration") == null || this.data == null) {
System.err.println("Erreur: le fichier de configuration est introuvable.");
System.exit(-1);
}
} catch (IOException | SAXException | ParserConfigurationException e) {
e.printStackTrace();
}
}
public Map getMap() {
ArrayList<HashMap<String,String>> mapList = data.get("Map.Coordinate");
ArrayList<HashMap<String,String>> mapList = data.get("Configuration.Map");
if (data.get("Configuration.Map") == null) {
if (mapList == null) {
System.err.println("Erreur: La balise Map est introuvable.");
System.exit(-1);
}
if (mapList.size() != 1) {
System.err.println("Erreur: Plusieurs Coordonnées trouvées.");
System.err.println("Erreur: Plusieurs ou Aucune Coordonnées trouvées.");
System.exit(-1);
}
@@ -52,13 +58,13 @@ public class ConfigGame {
}
public int getN() {
ArrayList<HashMap<String, String>> n;
HashMap<String, String> n;
if ((n = data.get("Configuration.Size")) == null) {
if ((n = data.get("Configuration").get(0)).isEmpty()) {
return 4;
}
return Integer.parseInt(n.get(0).get("n"));
return Integer.parseInt(n.get("n"));
}
public Personnage[] getPersonnages() {

View File

@@ -36,36 +36,39 @@ public class FileReaderXml {
protected HashMap<String, ArrayList<HashMap<String, String>>> getElements() {
HashMap<String, ArrayList<HashMap<String, String>>> elementsMap = new HashMap<>();
NodeList nodeList = document.getDocumentElement().getChildNodes();
Node nodeList = document.getDocumentElement();
readElements(elementsMap, nodeList);
return elementsMap;
}
private void readElements(HashMap<String, ArrayList<HashMap<String, String>>> elementsMap, NodeList nodeList) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node childNodeList = nodeList.item(i);
private void readElements(HashMap<String, ArrayList<HashMap<String, String>>> elementsMap, Node currentNode) {
if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
HashMap<String, String> attributeMap = new HashMap<>();
NamedNodeMap attributes = currentNode.getAttributes();
for (int j = 0; j < attributes.getLength(); j++) {
Node attribute = attributes.item(j);
attributeMap.put(attribute.getNodeName(), attribute.getNodeValue());
}
String parentNodeName, fullNodeName;
if ((parentNodeName = currentNode.getParentNode().getNodeName()).charAt(0) == '#') {
fullNodeName = currentNode.getNodeName();
} else {
fullNodeName = parentNodeName + "." + currentNode.getNodeName();
}
if (childNodeList.getNodeType() == Node.ELEMENT_NODE) {
HashMap<String, String> attributeMap = new HashMap<>();
NamedNodeMap attributes = childNodeList.getAttributes();
for (int j = 0; j < attributes.getLength(); j++) {
Node attribute = attributes.item(j);
attributeMap.put(attribute.getNodeName(), attribute.getNodeValue());
}
if (!elementsMap.containsKey(fullNodeName)) {
elementsMap.put(fullNodeName, new ArrayList<>());
}
String fullNodeName = childNodeList.getParentNode().getNodeName() + "." + childNodeList.getNodeName();
if (!elementsMap.containsKey(fullNodeName)) {
elementsMap.put(fullNodeName, new ArrayList<>());
}
elementsMap.get(fullNodeName).add(attributeMap);
if (childNodeList.hasChildNodes()) {
readElements(elementsMap, childNodeList.getChildNodes());
}
elementsMap.get(fullNodeName).add(attributeMap);
NodeList childNodes = currentNode.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
readElements(elementsMap, childNodes.item(i));
}
}
}

View File

@@ -3,8 +3,6 @@ package tests;
import java.io.File;
import java.util.Arrays;
import display.Display;
import game.environnement.Grid;
import game.environnement.Map;
import personnage.IA;
import personnage.Personnage;

View File

@@ -5,8 +5,6 @@ import configuration.ConfigGame;
public class XmlReaderTest {
public static void main(String[] args) {
ConfigGame configXml = new ConfigGame(null);
System.out.println(configXml.getPersonnages());
System.out.println(configXml.getN());
System.out.println(configXml.getMap());
}
}