diff --git a/conf.xml b/conf.xml deleted file mode 100644 index f963780..0000000 --- a/conf.xml +++ /dev/null @@ -1,63 +0,0 @@ - - - - - 22 - 22 - - - - - 1 - 2 - - - 3 - 4 - - - - - - - - - - - - - 2 - 2 - - true - - - - 19 - 19 - - true - - - - - - - 2 - 2 - - false - - - - - - - 2 - 2 - - res/save/ - false - - - \ No newline at end of file diff --git a/src/Configuration.java b/src/Configuration.java deleted file mode 100644 index 4c49591..0000000 --- a/src/Configuration.java +++ /dev/null @@ -1,7 +0,0 @@ -import javax.xml.*; - -public class Configuration { - public Configuration() { - - } -} diff --git a/src/Main.java b/src/Main.java index ccf7660..a14bf8a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,8 @@ import java.io.File; +import java.io.ObjectInputFilter.Config; +import java.util.ArrayList; +import configuration.ConfigXml; import game.Terminal; import game.environnement.*; import personnage.*; @@ -7,8 +10,6 @@ import personnage.IAQLearning.QTable; import tests.IATest; public class Main { - private static Map map = new Map(12, 22); - /** * Dans ce jeu, il y a 3 types de personnage disponible : * - Les Joueurs (Player) @@ -33,12 +34,12 @@ public class Main { * QTable qtable = new QTable(); * */ - private static Personnage[] personnages = new Personnage[] { - new Player(new int[] {2, 2}, "Philippe Etchebest"), - new Player(new int[] {map.getGrid()[0].length - 3, map.getGrid().length - 3}, "Luke Skywalker") - }; public static void main(String[] args) { + ConfigXml configXml = new ConfigXml(null); + Personnage[] personnages = configXml.getCharacters(); + Map map = configXml.getMap(); + Personnage.n = 4; if (args.length < 1) { new Terminal(map, personnages).run(); } // lancer en local diff --git a/src/conf.xml b/src/conf.xml new file mode 100644 index 0000000..61d3e7a --- /dev/null +++ b/src/conf.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/configuration/ConfigXml.java b/src/configuration/ConfigXml.java new file mode 100644 index 0000000..0879a60 --- /dev/null +++ b/src/configuration/ConfigXml.java @@ -0,0 +1,86 @@ +package configuration; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import javax.xml.parsers.ParserConfigurationException; + +import org.xml.sax.SAXException; + +import game.environnement.Map; +import personnage.IA; +import personnage.Personnage; +import personnage.Player; +import personnage.Robot; +import personnage.IAQLearning.QTable; + +public class ConfigXml { + private HashMap>> informationConfig; + + public ConfigXml(String path) { + try { + FileReaderXml fileReader = new FileReaderXml(null); + informationConfig = fileReader.getElements(); + System.out.println(informationConfig); + + } catch (IOException | SAXException | ParserConfigurationException e) { + e.printStackTrace(); + } + } + + public Personnage[] getCharacters() throws Error { + String[] playableCharacters = new String[] {"Player", "Robot", "IA"}; + ArrayList personnages = new ArrayList<>(); + + if (informationConfig.get("Configuration.Personnage") == null) { + throw new Error("Pas de personnage !"); + } + + for (String character : playableCharacters) { + ArrayList> hashMapChar = informationConfig.get("Personnage." + character); + + if(hashMapChar != null) { + for(int i = 0; i 2) { + throw new Error("Trop de personnage dans la partie !"); + } + + int[] coordinate = new int[] {Integer.parseInt(hashMapChar.get(i).get("x")), Integer.parseInt(hashMapChar.get(i).get("y"))}; + + if (character.toLowerCase().equals("player")) personnages.add(new Player(coordinate, hashMapChar.get(i).get("name"))); + if (character.toLowerCase().equals("robot")) personnages.add(new Robot(hashMapChar.get(i).get("name"), coordinate)); + + if (character.toLowerCase().equals("ia")){ + QTable qTable; + String path; + + if((path = hashMapChar.get(i).get("QTable")).equals("")) { + qTable = new QTable("res/save/"); + } else { + qTable = new QTable(path); + } + personnages.add(new IA(coordinate, qTable, hashMapChar.get(i).get("name"))); + } + } + } + } + + return new Personnage[] {personnages.get(0), personnages.get(1)}; + } + + public Map getMap() throws Error { + if (informationConfig.get("Configuration.Map") == null) { + throw new Error("Pas de Map !"); + } + + ArrayList> map = informationConfig.get("Map.Coordinate"); + + if (map.size() != 1) { + throw new Error("problème de coordonnée"); + } + + return new Map(Integer.parseInt(map.get(0).get("x")), Integer.parseInt(map.get(0).get("y"))); + } +} \ No newline at end of file diff --git a/src/configuration/FileReaderXml.java b/src/configuration/FileReaderXml.java new file mode 100644 index 0000000..02eea41 --- /dev/null +++ b/src/configuration/FileReaderXml.java @@ -0,0 +1,96 @@ +package configuration; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; + +public class FileReaderXml { + private String pathname = "src" + File.separator + "conf.xml"; + private Document document; + private File path; + + protected FileReaderXml(String pathname) throws IOException, SAXException, ParserConfigurationException { + initXML(pathname); + } + + protected void initXML(String pathname) throws SAXException, IOException, ParserConfigurationException { + if(!(pathname == null || pathname.equals(""))) this.pathname = pathname; + path = new File(this.pathname); + if (!path.exists()) throw new IOException(path.getAbsolutePath() + " : le pathname n'est pas valide."); + + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + document = builder.parse(path); + document.getDocumentElement().normalize(); + } + + protected HashMap>> getElements() { + HashMap>> elementsMap = new HashMap<>(); + NodeList nodeList = document.getDocumentElement().getChildNodes(); + + readElements(elementsMap, nodeList); + + return elementsMap; + } + + private void readElements(HashMap>> elementsMap, NodeList nodeList) { + for (int i = 0; i < nodeList.getLength(); i++) { + Node childNodeList = nodeList.item(i); + + if (childNodeList.getNodeType() == Node.ELEMENT_NODE) { + HashMap 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()); + } + + 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()); + } + } + } + } + + protected void printHashMap() { + HashMap>> file = getElements(); + + for (Object objectName : file.keySet()) { + System.out.println("name=" + objectName + " value="+file.get(objectName)); + } + } + + public static void printHashMap(String path) { + try { + FileReaderXml fileReader = new FileReaderXml(null); + fileReader.printHashMap(); + + } catch (IOException | SAXException | ParserConfigurationException e) { + e.printStackTrace(); + System.exit(0); + } + } +} + diff --git a/src/personnage/IAQLearning/QTable.java b/src/personnage/IAQLearning/QTable.java index 9678d76..d024a4f 100644 --- a/src/personnage/IAQLearning/QTable.java +++ b/src/personnage/IAQLearning/QTable.java @@ -27,6 +27,15 @@ public class QTable { public QTable() { qValues = new HashMap<>(); } + + /** + * Constructeur de la classe QTable cree le HashMap qValues et mets dans la liste + * les informations du fichier dans le path. + */ + public QTable(String pathFile) { + qValues = new HashMap<>(); + getValues(pathFile); + } /** * cette fonction renvoie soit la valeur associé à l'action de l'etat diff --git a/src/tests/XmlReaderTest.java b/src/tests/XmlReaderTest.java new file mode 100644 index 0000000..8c16d17 --- /dev/null +++ b/src/tests/XmlReaderTest.java @@ -0,0 +1,11 @@ +package tests; + +import configuration.ConfigXml; +import configuration.FileReaderXml; + +public class XmlReaderTest { + public static void main(String[] args) { + ConfigXml configXml = new ConfigXml(null); + System.out.println(configXml.getCharacters()); + } +}