Refactored configuration package
This commit is contained in:
parent
1f0a14bb23
commit
bc48da8109
@ -1,165 +1,75 @@
|
|||||||
package configuration;
|
package configuration;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.io.*;
|
||||||
|
import org.json.simple.*;
|
||||||
public class Config {
|
import org.json.simple.parser.*;
|
||||||
|
|
||||||
// tester a la main (okay normalement mais ajouter des test)
|
public class JsonToJava {
|
||||||
public static void main(String[] args) {
|
|
||||||
int[] tempfortest = { 0, 2, 1, 1 }; // {arene/nb_joueur/perso1/perso2} 0=alatoire
|
public static void main(String args[]) {
|
||||||
config(tempfortest);
|
|
||||||
}
|
JsonRecover();
|
||||||
|
|
||||||
// les variable a configurer
|
}
|
||||||
// sel1 pour savoir si on a deja selectionner le joueur 1
|
|
||||||
|
private static void JsonRecover() {
|
||||||
public static String arene, perso1, perso2;
|
// initialize the parser
|
||||||
public static int nb_joueur;
|
JSONParser jsonP = new JSONParser();
|
||||||
private static boolean sel1 = false;
|
try {
|
||||||
|
// read the json document
|
||||||
/*
|
JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader("src/configuration/config.json"));
|
||||||
* fonction config qui prend en entre un tableau d'entier chaque case
|
|
||||||
* correspondant a une variable, et ne retourne rien mais modifie la valeur des
|
// to print all values
|
||||||
* variables par des fonctions annexes
|
// System.out.println(jsonO.values());
|
||||||
*/
|
|
||||||
|
// isolate the "test" part and print it
|
||||||
private static void config(int[] tab) {
|
// String test = (String) jsonO.get("test");
|
||||||
int i = 0;
|
// System.out.println("ceci est un test :" + test);
|
||||||
while (i < tab.length) {
|
|
||||||
|
// arena selection
|
||||||
switch (i) {
|
// select an element on the list
|
||||||
case 0:
|
JSONArray arena = (JSONArray) jsonO.get("arena");
|
||||||
SelArene(tab[i]);
|
// print a case of this element
|
||||||
i++;
|
System.out.println("arena : " + arena.get(1));
|
||||||
break;
|
|
||||||
|
// nb players selection
|
||||||
case 1:
|
JSONArray nb_players = (JSONArray) jsonO.get("nb_players");
|
||||||
NbJoueur(tab[i]);
|
System.out.println("nb_player : " + nb_players.get(1));
|
||||||
i++;
|
|
||||||
break;
|
// character selection
|
||||||
|
JSONArray character1 = (JSONArray) jsonO.get("character1");
|
||||||
case 2:
|
System.out.println("players 1 : " + character1.get(1));
|
||||||
SelPerso(tab[i]);
|
|
||||||
i++;
|
JSONArray character2 = (JSONArray) jsonO.get("character2");
|
||||||
break;
|
System.out.println("players 2 : " + character2.get(1));
|
||||||
|
|
||||||
case 3:
|
// resolution
|
||||||
SelPerso(tab[i]);
|
JSONArray resolution = (JSONArray) jsonO.get("resolution");
|
||||||
i++;
|
// resolution string " width x heigth"
|
||||||
break;
|
JSONObject reso = (JSONObject) resolution.get(1);
|
||||||
/*
|
|
||||||
* case 4: ?
|
String heightStr = (String) reso.get("height");
|
||||||
*/
|
int height = Integer.parseInt(heightStr); // String to int
|
||||||
|
|
||||||
default:
|
String widthStr = (String) reso.get("width");
|
||||||
System.out.println("ERROR OUT OF BOUNDS CONFIG ARRAY");
|
int width = Integer.parseInt(widthStr);
|
||||||
i = tab.length;
|
|
||||||
break;
|
System.out.println("heigth : " + height + " width : " + width);
|
||||||
}
|
|
||||||
}
|
// button selection
|
||||||
System.out.println(arene + " " + nb_joueur + " " + perso1 + " " + perso2 + " " + sel1);
|
JSONArray allButton = (JSONArray) jsonO.get("button");
|
||||||
}
|
System.out.println(allButton);
|
||||||
|
|
||||||
/*
|
String up = (String) allButton.get(0);
|
||||||
* fonction SelArene prend un entier en parametre et permet de choisir l'arene
|
System.out.println("button for up is : " + up);
|
||||||
* du jeu
|
|
||||||
*/
|
} catch (FileNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
private static void SelArene(int s) {
|
} catch (IOException e) {
|
||||||
switch (s) {
|
e.printStackTrace();
|
||||||
case 0:
|
} catch (ParseException e) {
|
||||||
SelArene(random(1, 2));
|
e.printStackTrace();
|
||||||
break;
|
|
||||||
case 1:
|
}
|
||||||
arene = "arene1.png";
|
}
|
||||||
break;
|
}
|
||||||
|
|
||||||
case 2:
|
|
||||||
arene = "arene2.png";
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
System.out.println("ERROR ARENE INEXISTANTE");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* fonction NbJoueur prend un entier en parametre et permet de determiner si un
|
|
||||||
* bot sera necessaire
|
|
||||||
*/
|
|
||||||
|
|
||||||
private static void NbJoueur(int s) {
|
|
||||||
switch (s) {
|
|
||||||
case 1:
|
|
||||||
nb_joueur = 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
nb_joueur = 2;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
System.out.println("ERROR NUMBER OF PLAYER");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* fonction SelArene prend un entier en parametre et permet de choisir le
|
|
||||||
* personnage en fonction du joueur
|
|
||||||
*/
|
|
||||||
|
|
||||||
private static void SelPerso(int s) {
|
|
||||||
if (sel1 == false) {
|
|
||||||
switch (s) {
|
|
||||||
case 0:
|
|
||||||
SelPerso(random(1, 2));
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
perso1 = "perso1.png";
|
|
||||||
sel1 = true;
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
perso1 = "perso2.png";
|
|
||||||
sel1 = true;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
System.out.println("ERROR PERSO INEXISTANT");
|
|
||||||
}
|
|
||||||
} else if (sel1 == true) {
|
|
||||||
|
|
||||||
switch (s) {
|
|
||||||
case 0:
|
|
||||||
SelPerso(random(1, 2));
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
perso2 = "perso1.png";
|
|
||||||
sel1 = false;
|
|
||||||
if (perso1 == perso2) {
|
|
||||||
perso2 = "perso1_swapcolor.png";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
perso2 = "perso2.png";
|
|
||||||
sel1 = false;
|
|
||||||
if (perso1 == perso2) {
|
|
||||||
perso2 = "perso2_swapcolor.png";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
System.out.println("ERROR PERSO INEXISTANT");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
System.out.println("ERROR SELECTION PLAYER");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// fonction nombre aleatoire entre deux borne
|
|
||||||
private static int random(int min, int max) {
|
|
||||||
Random random = new Random();
|
|
||||||
int value = random.nextInt(max - 1 + min) + min;
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -1,75 +0,0 @@
|
|||||||
package configuration;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import org.json.simple.*;
|
|
||||||
import org.json.simple.parser.*;
|
|
||||||
|
|
||||||
public class JsonToJava {
|
|
||||||
|
|
||||||
public static void main(String args[]) {
|
|
||||||
|
|
||||||
JsonRecover();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void JsonRecover() {
|
|
||||||
// initialize the parser
|
|
||||||
JSONParser jsonP = new JSONParser();
|
|
||||||
try {
|
|
||||||
// read the json document
|
|
||||||
JSONObject jsonO = (JSONObject) jsonP.parse(new FileReader("src/configuration/config.json"));
|
|
||||||
|
|
||||||
// to print all values
|
|
||||||
// System.out.println(jsonO.values());
|
|
||||||
|
|
||||||
// isolate the "test" part and print it
|
|
||||||
// String test = (String) jsonO.get("test");
|
|
||||||
// System.out.println("ceci est un test :" + test);
|
|
||||||
|
|
||||||
// arena selection
|
|
||||||
// select an element on the list
|
|
||||||
JSONArray arena = (JSONArray) jsonO.get("arena");
|
|
||||||
// print a case of this element
|
|
||||||
System.out.println("arena : " + arena.get(1));
|
|
||||||
|
|
||||||
// nb players selection
|
|
||||||
JSONArray nb_players = (JSONArray) jsonO.get("nb_players");
|
|
||||||
System.out.println("nb_player : " + nb_players.get(1));
|
|
||||||
|
|
||||||
// character selection
|
|
||||||
JSONArray character1 = (JSONArray) jsonO.get("character1");
|
|
||||||
System.out.println("players 1 : " + character1.get(1));
|
|
||||||
|
|
||||||
JSONArray character2 = (JSONArray) jsonO.get("character2");
|
|
||||||
System.out.println("players 2 : " + character2.get(1));
|
|
||||||
|
|
||||||
// resolution
|
|
||||||
JSONArray resolution = (JSONArray) jsonO.get("resolution");
|
|
||||||
// resolution string " width x heigth"
|
|
||||||
JSONObject reso = (JSONObject) resolution.get(1);
|
|
||||||
|
|
||||||
String heightStr = (String) reso.get("height");
|
|
||||||
int height = Integer.parseInt(heightStr); // String to int
|
|
||||||
|
|
||||||
String widthStr = (String) reso.get("width");
|
|
||||||
int width = Integer.parseInt(widthStr);
|
|
||||||
|
|
||||||
System.out.println("heigth : " + height + " width : " + width);
|
|
||||||
|
|
||||||
// button selection
|
|
||||||
JSONArray allButton = (JSONArray) jsonO.get("button");
|
|
||||||
System.out.println(allButton);
|
|
||||||
|
|
||||||
String up = (String) allButton.get(0);
|
|
||||||
System.out.println("button for up is : " + up);
|
|
||||||
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (ParseException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,55 +0,0 @@
|
|||||||
{
|
|
||||||
"arena": [
|
|
||||||
"random",
|
|
||||||
"arena1.png",
|
|
||||||
"arena2.png"
|
|
||||||
],
|
|
||||||
"nb_players": [
|
|
||||||
"1",
|
|
||||||
"2"
|
|
||||||
],
|
|
||||||
"character1": [
|
|
||||||
"random",
|
|
||||||
"character1.png",
|
|
||||||
"character2.png"
|
|
||||||
],
|
|
||||||
"character2": [
|
|
||||||
"random",
|
|
||||||
"character1.png",
|
|
||||||
"character1_swapcolor.png",
|
|
||||||
"character2.png",
|
|
||||||
"character2_swapcolor.png"
|
|
||||||
],
|
|
||||||
"resolution": [
|
|
||||||
{
|
|
||||||
"width": "800",
|
|
||||||
"height": "600"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"width": "1280",
|
|
||||||
"height": "1024"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"width": "1680",
|
|
||||||
"height": "1050"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"width": "1920",
|
|
||||||
"height": "1080"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"persoWidth": "800",
|
|
||||||
"persoHeight": "600"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"button": [
|
|
||||||
"UP",
|
|
||||||
"DOWN",
|
|
||||||
"RIGTH",
|
|
||||||
"LEFT",
|
|
||||||
"A",
|
|
||||||
"B",
|
|
||||||
"X",
|
|
||||||
"Y"
|
|
||||||
]
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user