projet-genie-logiciel-systeme/runtime-workspace/fr.n7.game.examples/src-gen/Jeu.java

111 lines
3.7 KiB
Java
Raw Normal View History

2021-12-09 17:54:08 +00:00
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
2022-01-18 22:29:53 +00:00
import java.util.Map;
2021-12-09 17:54:08 +00:00
public class Jeu {
public static Explorateur explorateur;
Territoire territoire;
2022-01-18 22:29:53 +00:00
Map<String, Objet> objets;
Map<String, Connaissance> connaissances;
Map<String, Personne> personnes;
Map<String, Transformation> transformations;
2021-12-09 17:54:08 +00:00
2022-01-18 22:29:53 +00:00
static Lieu lieu = null;
static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("Lieu actuel: " + lieu + "\n");
System.out.println("Explorateur:\n" + Jeu.explorateur + "\n");
System.out.println("-".repeat(50));
}
2022-01-16 17:14:57 +00:00
2021-12-09 17:54:08 +00:00
public Jeu(
Territoire territoire,
2022-01-18 22:29:53 +00:00
Map<String, Objet> objets,
Map<String, Connaissance> connaissances,
Map<String, Personne> personnes,
Map<String, Transformation> transformations) {
2021-12-09 17:54:08 +00:00
this.territoire = territoire;
this.objets = objets;
this.connaissances = connaissances;
this.personnes = personnes;
this.transformations = transformations;
}
void jouer() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
2022-01-16 17:14:57 +00:00
// on cherche le (premier) lieu de départ
2022-01-18 22:29:53 +00:00
for (Lieu l : territoire.lieux.values()) {
2021-12-09 17:54:08 +00:00
if (l.depart.evaluer()) {
lieu = l;
break;
}
}
2022-01-16 17:14:57 +00:00
// tant qu'on est pas sur un lieu de fin
mainloop: while (!lieu.fin.evaluer()) {
2021-12-09 17:54:08 +00:00
2022-01-18 18:49:37 +00:00
int selection = 0;
List<Object> choix = new ArrayList<>();
clearScreen();
2022-01-18 18:49:37 +00:00
2022-01-18 22:29:53 +00:00
if (lieu.deposable.evaluer()) {
System.out.println("[" + choix.size() + "] déposer/ramasser");
choix.add(lieu);
}
2021-12-09 17:54:08 +00:00
for (Personne p : lieu.personnes) {
2022-01-18 22:29:53 +00:00
if (p.visible.evaluer() && p.obligatoire.evaluer()) {
2022-01-18 18:49:37 +00:00
System.out.println(p + ":");
2022-01-18 22:29:53 +00:00
p.interragir(reader);
2022-01-16 17:14:57 +00:00
continue mainloop;
2022-01-18 22:29:53 +00:00
} else if (lieu.personnes.contains(p) && p.visible.evaluer()) {
System.out.println("[" + choix.size() + "] " + p);
choix.add(p);
2021-12-09 17:54:08 +00:00
}
}
2022-01-18 22:29:53 +00:00
for (Chemin c : territoire.chemins.values()) {
2021-12-09 17:54:08 +00:00
if (c.lieuIn == lieu) {
if (c.visible.evaluer() && c.obligatoire.evaluer() && c.ouvert.evaluer()) {
lieu = c.lieuOut;
2022-01-16 17:14:57 +00:00
continue mainloop;
2022-01-18 22:29:53 +00:00
} else if (c.visible.evaluer()) {
2022-01-18 18:49:37 +00:00
if (c.ouvert.evaluer()) {
System.out.println("[" + choix.size() + "] " + c);
choix.add(c);
} else {
System.out.println("[X] " + c);
}
2021-12-09 17:54:08 +00:00
}
}
}
try {
System.out.print("\nChoix : ");
2022-01-18 18:49:37 +00:00
selection = Integer.parseInt(reader.readLine());
Object choix_selection = choix.get(selection);
2022-01-16 17:14:57 +00:00
2022-01-18 22:29:53 +00:00
if (choix_selection instanceof Lieu) {
((Lieu) choix_selection).interragir(reader);
} else if (choix_selection instanceof Chemin) {
2022-01-18 18:49:37 +00:00
lieu = ((Chemin) choix_selection).emprunter();
} else if (choix_selection instanceof Personne) {
2022-01-18 22:29:53 +00:00
((Personne) choix_selection).interragir(reader);
2021-12-09 17:54:08 +00:00
} else {
2022-01-18 18:49:37 +00:00
throw new UnsupportedOperationException();
2021-12-09 17:54:08 +00:00
}
2022-01-18 18:49:37 +00:00
2022-01-16 17:14:57 +00:00
} catch (Exception e) {
2021-12-09 17:54:08 +00:00
continue;
}
}
System.out.println("FIN : " + lieu.nom);
}
}