-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPartie.java
More file actions
410 lines (318 loc) · 10.4 KB
/
Partie.java
File metadata and controls
410 lines (318 loc) · 10.4 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import java.util.Scanner;
import java.io.*;
import java.nio.file.*;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
public class Partie implements Serializable{
private Echiquier jeu;
private boolean joueur;
private String nomPartie;
public Partie(){
this.jeu = new Echiquier();
this.joueur = true;
}
public String help(){
return "rien";
}
public void initPartie(String newNomPartie){
this.jeu.initCaseEtPiece();
this.joueur = true;
this.nomPartie = newNomPartie;
try{
File sauvegarde = new File("Saves\\"+nomPartie+"_history.txt");
sauvegarde.createNewFile();
}
catch(IOException e){
System.err.println(e.getStackTrace());
}
System.out.println("Joueur blanc, c'est à vous !");
}
public void setNomPartie(String name){
this.nomPartie=name;
}
public void setJoueur(boolean couleur){
this.joueur=couleur;
}
public void saveCoup(String coup){
File historique = new File("Saves\\"+this.nomPartie+"_history.txt");
try{
FileWriter fw = new FileWriter(historique, true);
fw.append(coup);
fw.close();
}
catch(IOException e){
System.err.println(e.getStackTrace());
}
}
public void loadPartie(String name){
String path="Saves\\"+name+".txt";
try{
//System.out.println("1");
FileInputStream fichier = new FileInputStream(path);
//System.out.println("2");
ObjectInputStream load = new ObjectInputStream(fichier);
//System.out.println("3");
this.jeu = (Echiquier) load.readObject();
//System.out.println("4");
load.close();
//System.out.println("5");
BufferedReader br = Files.newBufferedReader(Paths.get("Saves\\"+name+"_history.txt"));
String ligne;
int derJoueur = 9999;
ligne = br.readLine();
while (ligne != null) {
derJoueur = Integer.valueOf(ligne);
ligne = br.readLine();
}
if(derJoueur<8889){
this.joueur=false;
System.out.println("Joueu noir, c'est à vous !");
}
else{
this.joueur=true;
System.out.println("Joueu blanc, c'est à vous !");
}
}
catch (IOException e){
System.err.println(e.getStackTrace());
}
catch (ClassNotFoundException ex){
System.err.println(ex.getStackTrace());
}
this.nomPartie= name;
}
public boolean coup(int caseD, int caseA, boolean couleurJoueur){
this.jeu.calculDeplacementPiece(caseD);
if(this.jeu.coupValide(caseD, caseA, couleurJoueur)==true){
this.jeu.deplacerPiece(caseD, caseA);
this.jeu.calculDeplacementPiece(caseA);
return true;
}
return false;
}
public void savePartie(String name){
String path="Saves\\"+name+".txt";
Path chemin= Paths.get(path);
try{
Files.deleteIfExists(chemin);
FileOutputStream fichier = new FileOutputStream(path);
ObjectOutputStream save = new ObjectOutputStream(fichier);
save.writeObject(this.jeu);
save.flush();
save.close();
}
catch (IOException e){
System.err.println(e.getStackTrace());
}
}
public static void main(String args[]) throws UnsupportedEncodingException{
Partie p = new Partie();
String coup = "";
String commande="";
int deplacement = 0;
String commandeCharger = "";
String nomCharge = "";
String commandeStop = "";
//*********CODAGE DU COMMENCEMENT D'UNE PARTIE************//
System.out.println("Bienvenue au jeu d'echecs de la mort !!!! (sans interface).\nPour commencer, voulez vous charger une partie ou en commencer une nouvelle ? charger/commencer:");
boolean cBon=false;
while (cBon == false){
Scanner debut = new Scanner(System.in);
String confirm = debut.nextLine();
if(confirm.equals("charger")){
System.out.println("Entrez le nom de la partie a charger :\n");
Scanner verif = new Scanner (System.in);
//System.out.println("0");
String ication = verif.nextLine();
//System.out.println("1");
File test = new File("Saves\\"+ication+".txt");
//System.out.println("2");
if (test.exists()){
// System.out.println("3");
p.loadPartie(ication);
// System.out.println("4");
cBon = true;
}
else{
System.out.println("Le fichier n'existe pas.");
}
}
else if(confirm.equals("commencer")){
System.out.println("Donnez un nom a votre partie\n");
Scanner name = new Scanner (System.in);
if(name.hasNextLine()){
String nom = name.nextLine();
p.initPartie(nom);
PrintStream out = new PrintStream (System.out, true , "UTF8" );
out.print(p.jeu.toString());
System.out.println(nom+" initialisee avec succes.");
cBon = true;
}
}
else {
System.out.println("Je n'ai pas compris. Rentrez à nouveau :\n");
}
}
//********************CODAGE D'UN TOUR********************//
boolean stop = false;
while (stop == false ){
// tour du joueur blanc
if(p.joueur == true){
System.out.println("\nVeuillez rentrer votre commande :\n");
Scanner commandeB = new Scanner(System.in);
if (commandeB.hasNextInt()){
System.out.println("\n");
deplacement = commandeB.nextInt();
if (deplacement>=1111 && deplacement<=8888){
int value = deplacement;
int caseD = (int) value/100;
int caseA = value - caseD*100;
if (p.coup(caseD, caseA, p.joueur)==true){
PrintStream out = new PrintStream (System.out, true , "UTF8" );
out.print(p.jeu.toStringReverse());
System.out.println("\nJoueur Noir, c'est a vous !\n");
coup+=Integer.toString(value);
p.saveCoup(coup);
p.joueur = false;
}
else{
System.out.println("Erreur : Ce coup n'est pas possible.\n");
}
}
else{
System.out.println("Erreur : l'une de vos cases est hors de l'echiquier.");
}
}
else{
System.out.println("\n");
commande = commandeB.nextLine();
if(commande.equals("sauvegarder")){
p.savePartie(p.nomPartie);
System.out.println("'"+p.nomPartie+"'a ete sauvegardee dans 'Saves\\"+p.nomPartie+".txt' avec succes.");
}
else if (commande.equals("charger")){
System.out.println("Voulez-vous charger une partie (pensez a sauvegarder) ? oui/non:\n");
Scanner verifCharger = new Scanner(System.in);
commandeCharger = verifCharger.nextLine();
if(commandeCharger.equals("oui")){
System.out.println("Saisissez le nom de votre partie ('stop' pour sortir) :\n");
boolean stopCharger = false;
while(stopCharger == false){
Scanner nomC = new Scanner(System.in);
nomCharge = nomC.nextLine();
if (nomCharge.equals("stop")){
stopCharger=true;
}
else{
Path cheminCharger = Paths.get("Saves\\"+nomCharge+".txt");
if(Files.exists(cheminCharger)==true){
p.loadPartie(nomCharge);
stopCharger=true;
System.out.println("'"+p.nomPartie+"'a ete chargee depuis '"+cheminCharger.toString()+"' avec succes.");
}
else{
System.out.println("Erreur : La partie n'existe pas.\n");
}
}
}
}
}
else if(commande.equals("aide")){
System.out.println(p.help()); //************Ã faire
}
else if(commande.equals("stop")){
System.out.println("En êtes-vous sûr(e) ? oui/non:\n");
Scanner verifStop = new Scanner(System.in);
if (verifStop.hasNextLine()){
commandeStop = verifStop.nextLine();
if (commandeStop.equals("oui")){
stop=true;
System.out.println("La partie s'arrête.");
}
}
}
else{
System.out.println("Commande erronée");
}
}
}
// tour du joueur noir
else{
System.out.println("\nVeuillez rentrer votre commande :\n");
Scanner commandeN = new Scanner(System.in);
if (commandeN.hasNextInt()){
System.out.println("\n");
deplacement = commandeN.nextInt();
if (deplacement>=1111 && deplacement<=8888){
int value = deplacement;
int caseD = (int) value/100;
int caseA = value - caseD*100;
if (p.coup(caseD, caseA, p.joueur)==true){
PrintStream out = new PrintStream (System.out, true , "UTF8" );
out.print(p.jeu.toString());
System.out.println("\nJoueur Blanc, c'est a vous !\n");
coup+=Integer.toString(value);
p.saveCoup(coup+"\n");
p.joueur = true;
}
else{
System.out.println("Erreur : Ce coup n'est pas possible.\n");
}
}
else{
System.out.println("Erreur : l'une de vos cases est hors de l'echiquier.");
}
}
else{
System.out.println("\n");
commande = commandeN.nextLine();
if(commande.equals("sauvegarder")){
p.savePartie(p.nomPartie);
System.out.println("'"+p.nomPartie+"'a ete sauvegardee dans 'Saves\\"+p.nomPartie+".txt' avec succes.");
}
else if (commande.equals("charger")){
System.out.println("Voulez-vous charger une partie (pensez a sauvegarder) ? oui/non:\n");
Scanner verifCharger = new Scanner(System.in);
commandeCharger = verifCharger.nextLine();
if(commandeCharger.equals("oui")){
System.out.println("Saisissez le nom de votre partie ('stop' pour sortir) :\n");
boolean stopCharger = false;
while(stopCharger == false){
Scanner nomC = new Scanner(System.in);
nomCharge = nomC.nextLine();
if (nomCharge.equals("stop")){
stopCharger=true;
}
else{
Path cheminCharger = Paths.get("Saves\\"+nomCharge+".txt");
if(Files.exists(cheminCharger)==true){
p.loadPartie(nomCharge);
stopCharger=true;
System.out.println("'"+p.nomPartie+"'a ete chargee depuis '"+cheminCharger.toString()+"' avec succes.");
}
else{
System.out.println("Erreur : La partie n'existe pas.\n");
}
}
}
}
}
else if(commande.equals("aide")){
System.out.println(p.help());
}
else if(commande.equals("stop")){
System.out.println("En êtes-vous sûr(e) ? oui/non:\n");
Scanner verifStop = new Scanner(System.in);
if (verifStop.hasNextLine()){
commandeStop = verifStop.nextLine();
if (commandeStop.equals("oui")){
stop=true;
System.out.println("La partie s'arrête.");
}
}
}
}
}
}
}
}