Ajout du fichier de sauvegarde

This commit is contained in:
2024-08-13 22:30:48 +02:00
parent 6c8482e4d2
commit 9ef8ef2bc4
8 changed files with 231 additions and 76 deletions

View File

@@ -1,7 +0,0 @@
import javax.xml.*;
public class Configuration {
public Configuration() {
}
}

View File

@@ -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

22
src/conf.xml Normal file
View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Configuration globale -->
<Configuration>
<Size n="4"/>
<!-- 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>
<!-- 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"/> -->
</Personnage>
</Configuration>

View File

@@ -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<String, ArrayList<HashMap<String, String>>> 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<Personnage> personnages = new ArrayList<>();
if (informationConfig.get("Configuration.Personnage") == null) {
throw new Error("Pas de personnage !");
}
for (String character : playableCharacters) {
ArrayList<HashMap<String, String>> hashMapChar = informationConfig.get("Personnage." + character);
if(hashMapChar != null) {
for(int i = 0; i<hashMapChar.size(); i++) {
if (personnages.size() > 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<HashMap<String,String>> 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")));
}
}

View File

@@ -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<String, ArrayList<HashMap<String, String>>> getElements() {
HashMap<String, ArrayList<HashMap<String, String>>> elementsMap = new HashMap<>();
NodeList nodeList = document.getDocumentElement().getChildNodes();
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);
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());
}
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<String, ArrayList<HashMap<String, String>>> 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);
}
}
}

View File

@@ -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

View File

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