-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJeu.java
365 lines (331 loc) · 12.2 KB
/
Jeu.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import java.util.*;
import java.io.*;
public class Jeu{
/**
* Attributs
*/
/**
* liste de joueurs
*/
public static ArrayList<Joueur> listeJoueur=new ArrayList<Joueur>();
/**
* Joueur courant
*/
public static Joueur joueurCharge;
/**
* Partie courante
*/
public static Partie game;
/**
* méthode qui permet de scanner un entier
*/
public static int nextInt() {
boolean type = false;
int n = 0;
while (!type) {
try {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
type = true;
} catch (InputMismatchException e) {
System.out.println("Donnez un entier !");
}
}
return n;
}
/**
* méthode qui permet de charger la liste de joueurs sauvegardée
*/
public static void chargerListeJoueur(){
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("donnes.dat"));
//on est obligé de faire cela car sinon le programme ne veut pas compiler alors qu'il est juste
@SuppressWarnings("unchecked")
ArrayList<Joueur> inter=(ArrayList<Joueur>) ois.readObject();
Jeu.listeJoueur = new ArrayList<Joueur>(inter);
ois.close();
for(Joueur j : Jeu.listeJoueur){
j.calculerScoreMoyen();
}
}catch (IOException ioe){
System.out.println("ALERTE : Une erreur dans le chargement (erreur fichier)");
}catch (ClassNotFoundException cnf){
System.out.println("ALERTE : Une erreur dans le chargement (erreur classe)");
}
}
/**
* méthode qui permet de sauvegarder le joueur courant dans la liste des joueurs
*/
public static void sauvegarder(){
Jeu.joueurCharge.calculerScore();
Jeu.joueurCharge.ajouterPartie(Jeu.game);
Joueur joueurASupprimer = new Debutant("jarod");
for(Joueur j : Jeu.listeJoueur){
if(Jeu.joueurCharge.getNom().equals(j.getNom())){
joueurASupprimer=j;
}
}
Jeu.listeJoueur.remove(joueurASupprimer);
Jeu.listeJoueur.add(Jeu.joueurCharge);
try{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("donnes.dat"));
oos.writeObject(Jeu.listeJoueur);
oos.close();
}catch (IOException ioe){
System.out.println("ALERTE : Une erreur dans la sauvegarde (erreur fichier)");
}
}
/**
* Comparateur par nom du joueur
*/
public static Comparator<Joueur> compareByName = new Comparator<Joueur>() {
@Override
public int compare(Joueur j1, Joueur j2) {
return j1.getNom().compareTo(j2.getNom());
}
};
/**
* Comparateur par score du joueur
*/
public static Comparator<Joueur> compareByScore = new Comparator<Joueur>() {
@Override
public int compare(Joueur j1, Joueur j2) {
if (j1.getScore() < j2.getScore()) {
return -1;
}
else if(j1.getScore() == j2.getScore()) {
return j1.getNom().compareTo(j2.getNom());
}
else {
return 1;
}
}
};
/**
* méthode qui permet d'afficher la liste de joueurs par ordre alphabétique
*/
public static void afficherListeAlpha() {
ArrayList<Joueur> listeAlpha = Jeu.listeJoueur;
Collections.sort(listeAlpha, compareByName);
for (int i = 0; i < listeAlpha.size(); i++) {
System.out.printf("%d : %12s %7.2f\n" , i, listeAlpha.get(i).getNom(), listeAlpha.get(i).getScore());
}
}
/**
* méthode qui permet d'afficher la liste de joueurs triée par score
*/
public static void afficherListeScore() {
ArrayList<Joueur> listeScore = Jeu.listeJoueur;
Collections.sort(listeScore, compareByScore);
for (int i = 0; i < listeScore.size(); i++) {
System.out.printf("%d : %12s %7.2f\n" , i, listeScore.get(i).getNom(), listeScore.get(i).getScore());
}
}
/**
* méthode qui permet d'afficher le menu principal du jeu
*/
public static int affichageMenu() {
System.out.println("0 : quitter le jeu");
System.out.println("1 : creer un nouveau joueur");
System.out.println("2 : choisir un joueur");
System.out.print("Que voulez vous faire ? ");
int choix = Jeu.nextInt();
return choix;
}
/**
* méthode qui permet de choisir un joueur parmi la liste existante
*/
public static Joueur choisirJoueur() {
// cas où la liste de joueurs est vide
if(Jeu.listeJoueur.size()==0){
System.out.println("Pas de joueur! Redirection vers la creation de joueur\n");
Jeu.creerJoueur();
}
System.out.println("Quel joueur desirez vous jouer ?");
System.out.println("Affichage alphabetique");
Jeu.afficherListeAlpha();
System.out.println("Affichage au score");
Jeu.afficherListeScore();
// réception du résultat
System.out.print("Votre choix : ");
int choix = Jeu.nextInt();
while (choix < 0 || choix > Jeu.listeJoueur.size()) {
System.out.print("Votre choix : ");
choix = Jeu.nextInt();
}
System.out.println();
return Jeu.listeJoueur.get(choix);
}
/**
* méthode qui permet de créer un nouveau joueur
*/
public static void creerJoueur(){
Scanner sc = new Scanner(System.in);
int difficulte;
String nom,validation;
boolean existeDeja=false;
boolean termine=false;
System.out.println("Saisissez le nom du nouveau joueur : ");
while(!termine){
nom = sc.nextLine();
for(Joueur j : Jeu.listeJoueur){
if(nom.equals(j.getNom())){
System.out.println("Ce nom de joueur est deja pris!");
existeDeja=true;
}
}
while(existeDeja){
System.out.println("Entrez un nom non existant : ");
nom = sc.nextLine();
existeDeja=false;
for(Joueur j : Jeu.listeJoueur){
if(nom.equals(j.getNom())){
System.out.println("Ce nom de joueur est deja pris!");
existeDeja=true;
}
}
}
System.out.println("etes vous sur de bien creer un joueur nomme \""+nom+"\"? (oui ou non)");
validation = sc.nextLine();
if(validation.equals("oui")){
System.out.println("Choisissez une difficulte (1 : Debutant, 2 : Intermediaire, 3 : Avance) :");
difficulte = Jeu.nextInt();
while(difficulte<1 && difficulte>3){
System.out.println("Mauvais chiffre! (tapez un nombre entre 1 et 3) :");
difficulte = Jeu.nextInt();
}
switch(difficulte){
case 1:
Jeu.listeJoueur.add(new Debutant(nom));
break;
case 2:
Jeu.listeJoueur.add(new Intermediaire(nom));
break;
case 3:
Jeu.listeJoueur.add(new Avance(nom));
break;
default:
Jeu.listeJoueur.add(new Debutant(nom));
break;
}
System.out.println("Joueur cree avec succes!");
termine=true;
}else{
System.out.println("Saisissez a nouveau un nom : ");
}
}
}
/**
* méthode qui permet de choisir une partie dans la liste du joueur chargé
*/
public static void choisirPartie() {
if(Jeu.joueurCharge.getListe().size()==0){
System.out.println("Pas de partie! Redirection vers la creation de partie");
Jeu.creerPartie();
}
Jeu.joueurCharge.afficherParties();
int choix = Jeu.nextInt();
while (choix < 0 || choix > Jeu.joueurCharge.getListe().size()) {
choix = Jeu.nextInt();
}
Jeu.game = (Partie)Jeu.joueurCharge.getListe().get(choix);
Jeu.joueurCharge.getListe().remove(choix);
}
/**
* méthode qui permet de créer une nouvelle partie
*/
public static void creerPartie() {
Jeu.game = new Partie();
}
/**
* méthode qui permet de jouer une partie
*/
public static void startGame() {
if (Jeu.joueurCharge != null && Jeu.game != null) {
boolean sortie = false;
while (!sortie) {
Jeu.game.afficherGrille();
System.out.println();
Jeu.game.afficherListePiece();
System.out.println("Quelle piece voulez vous jouer ? Tapez -1 pour quitter la partie ou -2 pour enlever la derniere piece.");
System.out.print("Votre choix : ");
int choix = Jeu.nextInt();
System.out.println();
Piece p = null;
// On sort de la partie si l'utilisateur tape -1
// On enlève la dernière pièce si il tape -2
while (choix < -2 || choix > Jeu.game.getPiaPosees().size()) {
System.out.println("Tapez un numero existant ! -1 pour quitter. -2 pour enlever la derniere piece posee.");
}
if (choix == -1) {
sortie = true;
Jeu.sauvegarder();
}
else if (choix == -2) {
Jeu.game.supprimerDernierePieceAPoser();
Jeu.game.retirerDernierePiece();
}
else {
p = Jeu.game.getPiaPosees().get(choix);
Jeu.joueurCharge.poserPiece(p);
}
}
}
}
/**
* méthode qui permet l'affichage du menu des parties
* @return un entier correspondant au choix du joueur
*/
public static int menuParties() {
System.out.println("0 : retourner au menu");
System.out.println("1 : creer partie");
System.out.println("2 : choisir partie");
System.out.print("Votre choix : ");
int choix = Jeu.nextInt();
while (choix < 0 || choix > 2) {
System.out.print("Votre choix : ");
choix = Jeu.nextInt();
}
System.out.println();
return choix;
}
/**
* méthode principale du jeu, permet les différents affichages, et le chargement des fichiers de sauvegarde
* @param args
*/
public static void main(String[] args){
boolean session = true;
chargerListeJoueur();
System.out.println();
System.out.println(" ____ __ _ \n / __ \\___ ____ / /_____ _____ ___ (_)___ ____ \n / /_/ / _ \\/ __ \\/ __/ __ `/ __ `__ \\/ / __ \\/ __ \\\n / ____/ __/ / / / /_/ /_/ / / / / / / / / / / /_/ /\n/_/ \\___/_/ /_/\\__/\\__,_/_/ /_/ /_/_/_/ /_/\\____/ ");
System.out.println();
while (session) {
// affiche l'accueil
int choix = Jeu.affichageMenu();
// cas de la création d'un joueur
if (choix == 1) {
Jeu.creerJoueur();
}
// cas de la sélection d'un joueur
else if (choix == 2) {
// Initialise le joueur dans le joueur courant
Jeu.joueurCharge = Jeu.choisirJoueur();
// Initialise la partie dans la partie courante
int dec = Jeu.menuParties();
if (dec == 0)
break;
else if (dec == 1)
Jeu.creerPartie();
else if (dec == 2)
Jeu.choisirPartie();
Jeu.startGame();
Jeu.chargerListeJoueur();
}
// cas de sortie du jeu
else if (choix == 0) {
session = false;
}
}
System.exit(0);
}
}