mirror of
https://github.com/Cpt-Adok/SNAKE.git
synced 2026-01-25 10:34:06 +00:00
ajustement sur les fichiers de configurations
This commit is contained in:
@@ -2,7 +2,7 @@ import java.io.File;
|
|||||||
import java.io.ObjectInputFilter.Config;
|
import java.io.ObjectInputFilter.Config;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import configuration.ConfigXml;
|
import configuration.ConfigGame;
|
||||||
import game.Terminal;
|
import game.Terminal;
|
||||||
import game.environnement.*;
|
import game.environnement.*;
|
||||||
import personnage.*;
|
import personnage.*;
|
||||||
@@ -36,11 +36,11 @@ public class Main {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ConfigXml configXml = new ConfigXml(null);
|
ConfigGame config = new ConfigGame(null);
|
||||||
Personnage[] personnages = configXml.getCharacters();
|
|
||||||
Map map = configXml.getMap();
|
|
||||||
|
|
||||||
Personnage.n = 4;
|
Personnage[] personnages = config.getPersonnages();
|
||||||
|
Map map = config.getMap();
|
||||||
|
Personnage.n = config.getN();
|
||||||
|
|
||||||
if (args.length < 1) { new Terminal(map, personnages).run(); } // lancer en local
|
if (args.length < 1) { new Terminal(map, personnages).run(); } // lancer en local
|
||||||
else if (args.length == 2) { new Terminal(map, personnages).run(args[0], args[1]); } // lancer en ligne
|
else if (args.length == 2) { new Terminal(map, personnages).run(args[0], args[1]); } // lancer en ligne
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
<!-- Configuration globale -->
|
<!-- Configuration globale -->
|
||||||
<Configuration>
|
<Configuration>
|
||||||
<Size n="4"/>
|
<Size n="2"/>
|
||||||
<!-- Configuration de la map -->
|
<!-- Configuration de la map -->
|
||||||
<Map>
|
<Map>
|
||||||
<Coordinate x="22" y="22"/>
|
<Coordinate x="22" y="22"/>
|
||||||
@@ -15,8 +15,8 @@
|
|||||||
<!-- Configuration des personnages -->
|
<!-- Configuration des personnages -->
|
||||||
<Personnage>
|
<Personnage>
|
||||||
<Player name="Philippe_Etchebest" x="2" y="2"/>
|
<Player name="Philippe_Etchebest" x="2" y="2"/>
|
||||||
<Player name="Luke Skywalker" x="19" y="19"/>
|
<!-- <Player name="Luke Skywalker" x="19" y="19"/> -->
|
||||||
<!-- <IA name="" x="19" y="19" QTable=""/> -->
|
<!-- <IA name="" x="19" y="19" QTable=""/> -->
|
||||||
<!-- <Robot name="R2D2" x="19" y="19"/> -->
|
<Robot name="R2D2" x="19" y="19"/>
|
||||||
</Personnage>
|
</Personnage>
|
||||||
</Configuration>
|
</Configuration>
|
||||||
143
src/configuration/ConfigGame.java
Normal file
143
src/configuration/ConfigGame.java
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
package configuration;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
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;
|
||||||
|
import personnage.types.Item;
|
||||||
|
|
||||||
|
public class ConfigGame {
|
||||||
|
private HashMap<String, ArrayList<HashMap<String, String>>> data;
|
||||||
|
|
||||||
|
public ConfigGame(String path) {
|
||||||
|
try {
|
||||||
|
FileReaderXml fileReaderXml = new FileReaderXml(path);
|
||||||
|
this.data = fileReaderXml.getElements();
|
||||||
|
} catch (IOException | SAXException | ParserConfigurationException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map getMap() {
|
||||||
|
ArrayList<HashMap<String,String>> mapList = data.get("Map.Coordinate");
|
||||||
|
|
||||||
|
if (data.get("Configuration.Map") == 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.exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map map = new Map(
|
||||||
|
Integer.parseInt(mapList.get(0).get("x")),
|
||||||
|
Integer.parseInt(mapList.get(0).get("y"))
|
||||||
|
);
|
||||||
|
|
||||||
|
addItems(map);
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getN() {
|
||||||
|
ArrayList<HashMap<String, String>> n;
|
||||||
|
|
||||||
|
if ((n = data.get("Configuration.Size")) == null) {
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Integer.parseInt(n.get(0).get("n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Personnage[] getPersonnages() {
|
||||||
|
String[] personnagesList = new String[] {"Player", "Robot", "IA"};
|
||||||
|
Personnage[] personnages = new Personnage[2];
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
if (data.get("Configuration.Personnage") == null) {
|
||||||
|
System.err.println("Erreur: La balise Personnage est introuvable.");
|
||||||
|
System.exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String personnage : personnagesList) {
|
||||||
|
ArrayList<HashMap<String, String>> informations = data.get("Personnage."+personnage);
|
||||||
|
if (informations != null) {
|
||||||
|
for(HashMap<String, String> information : informations) {
|
||||||
|
if (index > 2) {
|
||||||
|
System.err.println("Erreur: Il doit y avoir au ]0; 3[ personnages");
|
||||||
|
System.exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
personnages[index++] = choosePersonnage(personnage, information);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return personnages;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Personnage choosePersonnage(String name, HashMap<String, String> information) throws Error {
|
||||||
|
int[] coordinate = new int[] {
|
||||||
|
Integer.parseInt(information.get("x")),
|
||||||
|
Integer.parseInt(information.get("y")),
|
||||||
|
};
|
||||||
|
|
||||||
|
switch (name.toLowerCase()) {
|
||||||
|
case "player": return new Player(coordinate, information.get("name"));
|
||||||
|
case "robot": return new Robot(information.get("name"), coordinate);
|
||||||
|
case "ia": {
|
||||||
|
String path = information.get("QTable");
|
||||||
|
return new IA(coordinate, (path.equals("")) ? new QTable("res/save/") : new QTable(path), name);
|
||||||
|
}
|
||||||
|
|
||||||
|
default: {
|
||||||
|
System.err.println("Erreur: Il n'existe aucun personnage jouable dans la balise Personnage");
|
||||||
|
System.exit(-1);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addItems(Map map) {
|
||||||
|
String[] itemList = new String[]{"Wall", "Fraise"};
|
||||||
|
|
||||||
|
for(String item : itemList) {
|
||||||
|
ArrayList<HashMap<String, String>> informations = data.get("Map."+item);
|
||||||
|
|
||||||
|
if(informations != null) {
|
||||||
|
for(HashMap<String, String> information : informations) {
|
||||||
|
map.addObjects(
|
||||||
|
chooseItem(item, information),
|
||||||
|
Integer.parseInt(information.get("x")),
|
||||||
|
Integer.parseInt(information.get("y"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Item chooseItem(String name, HashMap<String, String> information) {
|
||||||
|
switch (name.toLowerCase()) {
|
||||||
|
case "wall": return Item.WALL;
|
||||||
|
case "fraise": return Item.FRAISE;
|
||||||
|
|
||||||
|
default: {
|
||||||
|
System.err.println("Erreur: Il n'existe aucun item valide dans la balise Map");
|
||||||
|
System.exit(-1);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
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")));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package configuration;
|
package configuration;
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
|
||||||
import org.w3c.dom.NamedNodeMap;
|
import org.w3c.dom.NamedNodeMap;
|
||||||
import org.w3c.dom.Node;
|
import org.w3c.dom.Node;
|
||||||
import org.w3c.dom.NodeList;
|
import org.w3c.dom.NodeList;
|
||||||
@@ -13,10 +12,7 @@ import javax.xml.parsers.ParserConfigurationException;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class FileReaderXml {
|
public class FileReaderXml {
|
||||||
private String pathname = "src" + File.separator + "conf.xml";
|
private String pathname = "src" + File.separator + "conf.xml";
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public class IA extends Personnage {
|
|||||||
this.qLearning = new QLearning(qTable, alpha, gamma, epsilon);
|
this.qLearning = new QLearning(qTable, alpha, gamma, epsilon);
|
||||||
|
|
||||||
// Attribution d'un nom unique à l'IA.
|
// Attribution d'un nom unique à l'IA.
|
||||||
this.name = (name == null) ? "IA : " + UUID.randomUUID() : name;
|
super.name = (name == null) ? "IA : " + UUID.randomUUID() : name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -24,8 +24,7 @@ public class Player extends Personnage {
|
|||||||
*/
|
*/
|
||||||
public Player(int[] coordinate, String name) {
|
public Player(int[] coordinate, String name) {
|
||||||
super(coordinate);
|
super(coordinate);
|
||||||
|
super.name = name;
|
||||||
this.name = name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean moveCoordinate(int keys) {
|
public boolean moveCoordinate(int keys) {
|
||||||
|
|||||||
@@ -8,14 +8,12 @@ import game.environnement.*;
|
|||||||
import personnage.types.*;
|
import personnage.types.*;
|
||||||
|
|
||||||
public class Robot extends Personnage {
|
public class Robot extends Personnage {
|
||||||
|
|
||||||
Map m;
|
Map m;
|
||||||
Mouvement move;
|
Mouvement move;
|
||||||
String name;
|
|
||||||
|
|
||||||
public Robot(String name, int[] coordinate) {
|
public Robot(String name, int[] coordinate) {
|
||||||
super(coordinate);
|
super(coordinate);
|
||||||
this.name = name;
|
super.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**Fonction commune aux sous-classes de Personnage
|
/**Fonction commune aux sous-classes de Personnage
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
package tests;
|
package tests;
|
||||||
|
|
||||||
import configuration.ConfigXml;
|
import configuration.ConfigGame;
|
||||||
import configuration.FileReaderXml;
|
|
||||||
|
|
||||||
public class XmlReaderTest {
|
public class XmlReaderTest {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ConfigXml configXml = new ConfigXml(null);
|
ConfigGame configXml = new ConfigGame(null);
|
||||||
System.out.println(configXml.getCharacters());
|
System.out.println(configXml.getPersonnages());
|
||||||
|
System.out.println(configXml.getN());
|
||||||
|
System.out.println(configXml.getMap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user