-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJeuEchec.cpp
489 lines (413 loc) · 16.2 KB
/
JeuEchec.cpp
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*****************************************************************************************
En-tete du programme
=====================
Programme : JeuEchec.cpp
Auteur : Alexandre Beaulieu et Vincent Philippon
Date de creation : 27 avril 2010
But du programme : Un jeu d'�chec jouable en r�seau.
******************************************************************************************/
//Directives au pr�processeur
#include <iostream>
#include <pthread.h>
#include "piece.h"
#include "tour.h"
#include "cavalier.h"
#include "fou.h"
#include "reine.h"
#include "roi.h"
#include "pion.h"
#include "passif.h"
#include "actif.h"
#include "sockets.h"
#include "affichage.h"
#include <SDL/SDL_image.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
using namespace std;
int main(int argc, char *argv[])
{
//D�claration des variables
plateau sTabPlateau[8][8] =
{NULL}; //Tableau du plateau
piece *temp = NULL; //Pi�ce temporaire pour �change
int iMenu = 1, //Choix du joueur dans le menu de d�part
iJoueur[2] =
{0}, //Si le joueur joue les blancs ou les noirs
iTour, //Joueur en cours
iXPiece, //Coordon�e de la pi�ce dans les abscisses
iYPiece, //Coordon�e de la pi�ce dans les ordonn�es
iXDestination, //Coordon�e de la destination dans les abscisses
iYDestination, //Coordon�e de la destination dans les ordonn�es
iXRoiBlanc, //X du roi blanc
iYRoiBlanc, //Y du roi blanc
iXRoiNoir, //X du roi noir
iYRoiNoir, //Y du roi noir
iAction, //Fait-on l'action (donn�e transmise)
iPromotion, //Piece choisi pour la promotion
iNbPiece, //Nb de pieces sur le jeu
iXPassant = -1, //X de la case "prenable en passant"
iYPassant = -1; //Y de la case "prenable en passant"
bool bPiecePrete = 1, //Si les pi�ces sont pretes
bZoneSaisie = 0, //Si c'est le moment de la saisie
bAction, //Fait-on l'action
bEchec, //Le joueur est-il echec
bBouger, //Le joueur peut-il faire un mouvement
bFinPartie = 0; //Si c'est la fin de la partie
char strJoueur[9]; //Joueur actuel
SDL_Event event; //�v�nement capte par SDL_WaitEvent
SDL_Surface *ecran = NULL; //pointeur de l'�cran
SDL_Rect positionTexte; //O� est affich� le joueur actuel
SDL_Color couleur =
{136, 0, 21}; //Couleur pour le texte en jeu
Uint32 couleurFond = NULL; //Couleur de fond pour le texte
pthread_t threadAccept, //Thread pour accepter la connexion
threadReception; //Thread pour la r�ception de donn�e
socketClientRecue client; //Structure pour le thread d'acceptation
receptionCoords coords; //Structure pour le thread de r�ception de coordonn�e
receptionProm prom; //Structure pour le thread de r�ception de promotion
//Contexte d'adressage
SOCKADDR_IN sin, //du serveur
cSin; //du client
//Num�ro de la socket
SOCKET sock, //du serveur
cSock, //du client
sockTransmition; //Socket pour les transmitions de donn�es
socklen_t recsize = sizeof(sin); /*Grandeur du contexte d'adressage
du serveur */
//Chargement
if (!initialiserLibSocket())
{
cout << "Erreur lors de l'initialisation des sockets";
fflush(stdin);
cin.get();
exit(EXIT_FAILURE);
}
if (configurationServeur(sin, sock, recsize) == -1)
{
cout << "Erreur lors de la configuration des sockets serveur";
fflush(stdin);
cin.get();
exit(EXIT_FAILURE);
}
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1 || TTF_Init() == -1) //evenement d'�chec de sdl
{
cout << "Erreur lors de l'initialisation de l'une des librairies SDL", SDL_GetError();
fflush(stdin);
cin.get();
exit(EXIT_FAILURE);
}
//Positions des cadres
positionTexte.x = 600;
positionTexte.y = 200;
positionTexte.h = 80;
positionTexte.w = 200;
preparerPieces(sTabPlateau, iXRoiBlanc, iYRoiBlanc, iXRoiNoir, iYRoiNoir);
client.sock = &sock;
client.cRecsize = sizeof(client.cSin);
//D�finition de l'icone
SDL_WM_SetIcon(IMG_Load("roi_blanc.png"), NULL);
//Param�tre du mode vid�o
ecran = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
couleurFond = SDL_MapRGB(ecran->format, 192, 192, 192);
//Changer le nom de la fenetre
SDL_WM_SetCaption("Jeu d'Echec", NULL);
afficherImage(ecran, "Menu.PNG");
//Affectation des adresse dans les variables des structures des threads
client.iJoueur[0] = &iJoueur[0];
client.iJoueur[1] = &iJoueur[1];
client.sockTransmition = &sockTransmition;
client.bZoneSaisie = &bZoneSaisie;
client.ecran = ecran;
client.cSock = &cSock;
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
coords.sTabPlateau[i][j] = &sTabPlateau[i][j];
coords.ecran = ecran;
coords.bAction = &bAction;
coords.iAction = &iAction;
coords.iXDestination = &iXDestination;
coords.iXPiece = &iXPiece;
coords.iYDestination = &iYDestination;
coords.iYPiece = &iYPiece;
coords.sockTransmition = prom.sockTransmition = &sockTransmition;
prom.iPromotion = &iPromotion;
//Boucle du menu du jeu
do
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT: //Sur le x de la fenetre, on arr�te le programme
iMenu = 0;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
iMenu = 0;
break;
case SDL_MOUSEBUTTONUP: //Lorsque l'utilisateur clique
//Si c'est le bouton gauche
if (event.button.button == SDL_BUTTON_LEFT)
{
//Si c'est Jouer
if (event.button.x > 300 && event.button.x < 480
&& event.button.y > 180 && event.button.y < 240)
iMenu = 2;
//Si c'est Instruction
else if (event.button.x > 270 && event.button.x < 520
&& event.button.y > 270 && event.button.y < 320)
iMenu = 3;
//Si c'est Quitter
else if (event.button.x > 310 && event.button.x < 490
&& event.button.y > 400 && event.button.y < 450)
iMenu = 0;
}
}
if (iMenu == 2) //Si l'option jouer est choisie
{
bZoneSaisie = 1;
pthread_create(&threadAccept, NULL, accepterConnexion, &client);
cSock = socket(AF_INET, SOCK_STREAM, 0); //Cr�e la socket cliente
//Connexion, quitte si le joueur a fait quitter
if (connecterAuServeur(ecran, cSin, cSock, sockTransmition, iJoueur,
threadAccept)
== 0)
break;
iMenu = 1;
iTour = 1;
bFinPartie = 0;
SDL_FillRect(ecran, NULL, couleurFond);
SDL_FillRect(ecran, NULL, couleurFond);
creerDamier(ecran, sTabPlateau, iXRoiBlanc, iYRoiBlanc, iXRoiNoir,
iYRoiNoir, bPiecePrete);
do
{
//Assigner le bon nom
if (iTour == 1)
strcpy(strJoueur, "Blanc");
else
strcpy(strJoueur, "Noir");
afficherTexte(ecran, strJoueur, "papyrus.ttf", 40, couleur,
positionTexte, couleurFond);
//V�rification si le joueur est en �chec et/ou ne peut plus faire de mouvement(echec, mat et pat)
//Blanc
if (iTour > 0)
bEchec = verifierEchec(sTabPlateau, iTour, iXRoiBlanc, iYRoiBlanc);
//Noir
else
bEchec = verifierEchec(sTabPlateau, iTour, iXRoiNoir, iYRoiNoir);
bBouger = verifierBouger(sTabPlateau, iTour, iXRoiBlanc, iYRoiBlanc,
iXRoiNoir, iYRoiNoir, iXPassant, iYPassant);
iNbPiece = compterPiece(sTabPlateau);
//S'il est echec et mat
if (bEchec && !bBouger)
{
bFinPartie = 1;
afficherEtat(ecran, "Echec et mat", false);
}
//Si il est juste �chec
else if (bEchec)
afficherEtat(ecran, "Echec", false);
//Si c'est nul
else if (!bBouger || iNbPiece == 2)
{
bFinPartie = 1;
afficherEtat(ecran, "Partie Nulle", true);
}
else
afficherEtat(ecran, "Tour au", false);
SDL_Flip(ecran);
//Si ce n'est pas la fin de la partie
if (!bFinPartie)
{
do
{
iYDestination = -1;
do
{
//Si c'est le tour du joueur courant
if (iTour == iJoueur[0] || iTour == iJoueur[1])
{
selectionPiece(ecran, sTabPlateau, iXPiece, iYPiece, iTour,
sockTransmition, sock, cSock);
selectionDestination(ecran, sTabPlateau, iXPiece, iYPiece,
iXDestination, iYDestination, iTour,
bAction, sockTransmition, sock, cSock);
}
else
{
iAction = -1;
pthread_create(&threadReception, NULL, recevoirCoordonnee,
&coords);
//Tant que iAction n'a pa �t� re�u
while (iAction == -1)
{
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quitter(sTabPlateau, sock, cSock);
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
quitter(sTabPlateau, sock, cSock);
break;
}
}
}
effacerSurlignement(
ecran, sTabPlateau[iYPiece][iXPiece].posCase,
sTabPlateau[iYPiece][iXPiece].fond,
sTabPlateau[iYPiece][iXPiece].pieceEchec->getImage());
//Si bAction est vrais
if (bAction)
//Tant que iYDestination n,a pa �t� re�u
while (iYDestination == -1)
{
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quitter(sTabPlateau, sock, cSock);
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
quitter(sTabPlateau, sock, cSock);
break;
}
}
}while (!bAction || iYDestination == -1); //Tant que le joueur annule ou clique � un mauvais endroit ou
//Si prise en passant
if (sTabPlateau[iYPiece][iXPiece].pieceEchec->type() == 'P' && sTabPlateau[iYDestination][iXDestination].pieceEchec->getEquipe()
== 0
&& iYDestination == iYPiece - iTour
&& abs(iXDestination - iXPiece) == 1)
{
if (iXDestination == iXPassant && iYDestination == iYPassant)
{
temp = copierPiece(
sTabPlateau[iYDestination][iXDestination].pieceEchec);
bAction = verifierMouvement(sTabPlateau, iXPiece, iYPiece,
iXDestination, iYDestination, iTour,
temp, iXRoiBlanc, iYRoiBlanc,
iXRoiNoir, iYRoiNoir);
if (bAction)
{
sTabPlateau[iYDestination + iTour][iXDestination].pieceEchec =
new piece;
SDL_FillRect(
ecran,
&sTabPlateau[iYDestination + iTour][iXDestination].posCase,
sTabPlateau[iYDestination + iTour][iXDestination].fond);
SDL_BlitSurface(
sTabPlateau[iYDestination + iTour][iXDestination].pieceEchec->getImage(),
NULL,
ecran,
&sTabPlateau[iYDestination + iTour][iXDestination].posCase);
}
}
else
bAction = false;
}
//Si Roque
else if (sTabPlateau[iYPiece][iXPiece].pieceEchec->type() == 'K'
&& sTabPlateau[iYDestination][iXDestination].pieceEchec->type() == 'T'
&& sTabPlateau[iYDestination][iXDestination].pieceEchec->getEquipe() == iTour
&& sTabPlateau[iYDestination][iXDestination].pieceEchec->getBouger() == false
&& sTabPlateau[iYPiece][iXPiece].pieceEchec->getBouger() == false)
bAction = roque(ecran, sTabPlateau, iTour, iXPiece, iYPiece,
iXDestination, iYDestination, iXRoiBlanc,
iYRoiBlanc, iXRoiNoir, iYRoiNoir);
//Si c'est un mouvement normal
else
{
temp = copierPiece(
sTabPlateau[iYDestination][iXDestination].pieceEchec);
bAction = verifierMouvement(sTabPlateau, iXPiece, iYPiece,
iXDestination, iYDestination, iTour,
temp, iXRoiBlanc, iYRoiBlanc,
iXRoiNoir, iYRoiNoir);
}
}while (!bAction); //Tant que le joueur se trouve en �chec apres le mouvement
conclureDeplacement(ecran, sTabPlateau, iXPiece, iYPiece,
iXDestination, iYDestination);
//Si le pion est � l'extr�mit� adverse
//conversion en int pour arrondir � l'unit� inf�rieure
if (iYDestination == (3.5 - 3.5 * iTour))
{
//Si la pi�ce est un pion
if (sTabPlateau[iYDestination][iXDestination].pieceEchec->type() == 'P')
{
//Si c'est le tour du joueur courant
if (iTour == iJoueur[0] || iTour == iJoueur[1])
{
iPromotion = choixPromotion(ecran, iTour, sockTransmition,
sTabPlateau, sock, cSock);
envoyerInt(sockTransmition, iPromotion);
}
else
{
iPromotion = -1;
pthread_create(&threadReception, NULL, recevoirPromotion,
&prom);
//Tant que iPromotion n'a pa �t� re�u
while (iPromotion == -1)
{
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quitter(sTabPlateau, sock, cSock);
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
quitter(sTabPlateau, sock, cSock);
break;
}
}
}
appliquerPromotion(ecran,
sTabPlateau[iYDestination][iXDestination],
iPromotion);
}
}
//Si un pion vient d'avancer de 2, s'ouvrant � une prise en passant
if (sTabPlateau[iYDestination][iXDestination].pieceEchec->type() == 'P' && abs(
iYDestination - iYPiece)
== 2
&& iXDestination == iXPiece)
{
iXPassant = iXDestination;
iYPassant = iYDestination + iTour;
}
//Sinon
else
iXPassant = iYPassant = -1;
iTour *= -1;
}
}while (!bFinPartie); //Tant que pas la fin
bPiecePrete = 0;
bZoneSaisie = 0;
attenteClique(sTabPlateau, sock, cSock);
afficherImage(ecran, "Menu.PNG");
deconnecterSocket(sock, cSock);
if (iJoueur[0] == 0)
{
cSock = accept(sock, (SOCKADDR*)&cSin, &recsize);
deconnecterSocket(sock, cSock);
}
iJoueur[0] = 0;
iJoueur[1] = 0;
configurationServeur(sin, sock, recsize);
}
else if (iMenu == 3)
{
iMenu = 1;
afficherImage(ecran, "Instructions.PNG");
attenteClique(sTabPlateau, sock, cSock);
afficherImage(ecran, "Menu.PNG");
}
}while (iMenu); //Tant que pas choisi quitter
//Fermeture ------------------------- 2012
quitter(sTabPlateau, sock, cSock);
return EXIT_SUCCESS;
}