Refactored configuration package

This commit is contained in:
François Autin 2021-06-10 13:30:07 +02:00
parent 1f0a14bb23
commit bc48da8109
No known key found for this signature in database
GPG Key ID: 24025429AC559B7C
3 changed files with 75 additions and 295 deletions

View File

@ -1,165 +1,75 @@
package configuration;
import java.util.Random;
import java.io.*;
import org.json.simple.*;
import org.json.simple.parser.*;
public class Config {
public class JsonToJava {
public static void main(String args[]) {
JsonRecover();
// tester a la main (okay normalement mais ajouter des test)
public static void main(String[] args) {
int[] tempfortest = { 0, 2, 1, 1 }; // {arene/nb_joueur/perso1/perso2} 0=alatoire
config(tempfortest);
}
// les variable a configurer
// sel1 pour savoir si on a deja selectionner le joueur 1
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"));
public static String arene, perso1, perso2;
public static int nb_joueur;
private static boolean sel1 = false;
// to print all values
// System.out.println(jsonO.values());
/*
* 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
* variables par des fonctions annexes
*/
// isolate the "test" part and print it
// String test = (String) jsonO.get("test");
// System.out.println("ceci est un test :" + test);
private static void config(int[] tab) {
int i = 0;
while (i < tab.length) {
// 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));
switch (i) {
case 0:
SelArene(tab[i]);
i++;
break;
// nb players selection
JSONArray nb_players = (JSONArray) jsonO.get("nb_players");
System.out.println("nb_player : " + nb_players.get(1));
case 1:
NbJoueur(tab[i]);
i++;
break;
// character selection
JSONArray character1 = (JSONArray) jsonO.get("character1");
System.out.println("players 1 : " + character1.get(1));
case 2:
SelPerso(tab[i]);
i++;
break;
JSONArray character2 = (JSONArray) jsonO.get("character2");
System.out.println("players 2 : " + character2.get(1));
case 3:
SelPerso(tab[i]);
i++;
break;
/*
* case 4: ?
*/
// resolution
JSONArray resolution = (JSONArray) jsonO.get("resolution");
// resolution string " width x heigth"
JSONObject reso = (JSONObject) resolution.get(1);
default:
System.out.println("ERROR OUT OF BOUNDS CONFIG ARRAY");
i = tab.length;
break;
}
}
System.out.println(arene + " " + nb_joueur + " " + perso1 + " " + perso2 + " " + sel1);
}
String heightStr = (String) reso.get("height");
int height = Integer.parseInt(heightStr); // String to int
/*
* fonction SelArene prend un entier en parametre et permet de choisir l'arene
* du jeu
*/
String widthStr = (String) reso.get("width");
int width = Integer.parseInt(widthStr);
private static void SelArene(int s) {
switch (s) {
case 0:
SelArene(random(1, 2));
break;
case 1:
arene = "arene1.png";
break;
System.out.println("heigth : " + height + " width : " + width);
case 2:
arene = "arene2.png";
break;
// 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();
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;
}
}

View File

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

View File

@ -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"
]
}