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 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
#define JOUEUR_1 1
#define JOUEUR_0 0
#define JOUEUR_VIDE -1
#define PIECE_NON_PROMUE 0
#define PIECE_PROMUE 1
#define FAUX 0
#define VRAI 1
#define VIDE -1
#define T 11
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct piece_s
{
int joueur;
int type;
int statut;
}; typedef struct piece_s piece_t;
struct coordonnees_s
{
int x; // ligne du tableau;
int y; // colonne du tableau;
}; typedef struct coordonnees_s coordonnees_t;
typedef struct coup_s coup_t; //maillon de coups
struct coup_s
{
coordonnees_t depart; //sauvegarder les coordonees de la piece de depart
coordonnees_t arrivee; //sauvegarder les coordonnees d'arriver la piece
int promotion; //booleen
int capture; //booleen
coup_t *suivant;
coup_t *precedant;
};
struct liste_s
{
coup_t *debut;
int nb_de_coups;
coup_t *fin; //ladresse de fin du dernier maillon
}; typedef struct liste_s liste_t;
struct partie_s
{
piece_t tablier[T][T]; // T=taille
liste_t coup_joue;
int bool_joueur;
int joueur;
}; typedef struct partie_s partie_t;
typedef struct maillon_capture_s maillon_capture_t;
struct maillon_capture_s
{
coordonnees_t depart; //sauvegarder les coordonees de la piece de depart
coordonnees_t arrivee; //sauvegarder les coordonnees d'arriver la piece
int promotion; //booleen
int capture; //booleen
coordonnees_t coord_capture;
maillon_capture_t *suivant;
};
typedef struct liste_capture_s liste_capture_t;
struct liste_capture_s
{
liste_capture_t *liste_capture;
maillon_capture_t *debut;
int taille;
};
//c'est un pointeur qui nous permetra de sauvegarder la position de la derniere piece mise dans la reserve
liste_capture_t *liste_capture;
//prend en arguments un joueur, un type et un statut, et renvoie une piece.
piece_t piece_creer(int joueur, int type, int statut);
//prend en argument une piece et renvoie son joueur.
int piece_joueur(piece_t piece);
//prend en argument un caractere et renvoie la piece correspondante.
piece_t piece_identifier(char type);
//prend en argument une piece et renvoie le caractere qui lui est associe.
char piece_caractere(piece_t piece);
//prend en argument une piece et affiche le caractere qui lui est associe .
void piece_afficher(piece_t piece);
//
void changer_joueur(partie_t *partie);
//
void changer_statut(piece_t *piece);
//
int case_vide(piece_t caase);
//
void modifier_case(partie_t *partie, piece_t piece, coordonnees_t coord );
//
void affiche_plateau(partie_t *partie);
//
void deplacemet(partie_t *partie, coordonnees_t coord_de_depart, coordonnees_t coord_darrivee);
//
void annuler_deplacement(partie_t *partie);
//
int* saisie_case();
//
partie_t *partie_creer();
//
coup_t *extraire_debut_liste(liste_t *liste);
//
void partie_detruire(partie_t *partie);
//
void partie_sauvegarder(partie_t* partie, const char* chemain);
//
char partie_charger(partie_t* partie, const char* chemain);
//
partie_t *partie_nouvelle();
piece_t piece_creer(int joueur, int type, int statut)
{
piece_t piece;
piece.joueur=joueur;
piece.type=type;
piece.statut=statut;
return piece;
}
//**********************************************_________________________************************************************
int piece_joueur(piece_t piece)
{
return piece.joueur;
}
//**********************************************_________________________*************************************************
piece_t piece_identifier(char type)
{
int i=0;
char tab_type[]= "malloc(15*sizeof(char))";
piece_t piece=piece_creer(JOUEUR_VIDE,'.',VIDE);
strcpy(tab_type,"plnbrsgkdjcfta");
if(isupper(type))
{
for(i=0;i<8; i++)
{
if(toupper(tab_type[i])==type)
{
return piece_creer(JOUEUR_0,type,PIECE_PROMUE);
}
}
for(i=0; i<14; i++)
{
if (toupper(tab_type[i])==type)
{
return piece_creer(JOUEUR_0, type, PIECE_PROMUE);
}
}
}
else
{
for (i = 0; i < 8; ++i)
{
return piece_creer(JOUEUR_1, type, PIECE_PROMUE);
}
}
return piece;
}
//*********************************************_________________________**************************************************
char piece_caractere(piece_t piece)
{
return piece.type;
}
//*********************************************_________________________****************************************************
void piece_afficher(piece_t piece)
{
printf("%3c",piece.type );
}
//***********************************************________________________*******************************************************//
int case_vide(piece_t caase)
{
if (caase.type == '.')
{
return 1;
}
else
{
return 0;
}
}
//*************************************___________________________________**********************************/
void modifier_case(partie_t *partie, piece_t piece, coordonnees_t coord )
{
partie->tablier[coord.x][coord.y]=piece;
}
//*************************************_____________________________________********************//
void changer_joueur ( partie_t *partie)
{
if ( partie->joueur ==0)
{
partie->joueur=1;
}
else
{
partie->joueur=0;
}
}
//**************************************________________________________________******************************//
void affiche_plateau(partie_t *partie)
{
int i,j;
printf(" ");
for(i=0; i<T; i++)
{
printf("%3d", i);
}
printf("\n");
for (i = 0; i < T; ++i)
{
printf("%2d\n", i);
for ( j = 0; i < T; ++i)
{
piece_afficher(partie->tablier[i][j]);
}
printf("\n");
}
}
//***********************************______________________________*****************************//
void changer_statut(piece_t *piece)
{
char *tab1=malloc(7*sizeof(char));
char *tab2=malloc(7*sizeof(char));
int i=0;
strcpy(tab1, "djcfta");
strcpy(tab2, "plnbrs");
if (isupper(piece->type))
{
for (i = 0; i <6; ++i)
{
tab1[i]=toupper(tab1[i]);
tab2[i]=toupper(tab2[i]);
}
}
if (piece->statut ==PIECE_PROMUE)
{
for ( i = 0; i < 6; ++i)
{
if (tab1[i]==piece->type)
{
piece->type=tab2[i];
break;
}
}
}
if (isupper(piece->type))
{
for (i = 0; i < 6; ++i)
{
tab1[i]=toupper(tab1[i]);
tab2[i]=toupper(tab2[i]);
}
}
if (piece->statut==PIECE_PROMUE)
{
for ( i = 0; i < 6; ++i)
{
if (tab1[i]==piece->type)
{
piece->type=tab2[i];
break;
}
}
piece->statut=PIECE_NON_PROMUE;
}
else
{
for (i = 0; i < 6; ++i)
{
if (tab2[i]==piece->type)
{
piece->type=tab1[i];
break;
}
}
piece->statut=PIECE_PROMUE;
}
free(tab1);
free(tab2);
}
//****************************************************_________________________________________*******************************//
void deplacemet(partie_t *partie, coordonnees_t coord_de_depart, coordonnees_t coord_darrivee)
{
int i=0,j=0;
int bool_capture=FAUX, bool_promotion=FAUX, reponse=FAUX;
coup_t *nv_coup=malloc(sizeof(coup_t));
if(!case_vide(partie->tablier[coord_darrivee.x][coord_darrivee.y]))
{
if (partie->bool_joueur==JOUEUR_1)
{
j=1;
i=T-1;
while(!case_vide(partie->tablier[i][j]) && (j<T-1))
{
j++;
}
while(!case_vide(partie->tablier[i][j]) && (i>0))
{
i--;
}
partie->tablier[coord_darrivee.x][coord_darrivee.y].joueur=JOUEUR_0;
}
else
{
i=0;
j=T-2;
while(!case_vide(partie->tablier[i][j]) && (j>0))
{
j--;
}
while(!case_vide(partie->tablier[i][j]) && (i<T-1))
{
i++;
}
partie->tablier[coord_darrivee.x][coord_darrivee.y].joueur=JOUEUR_1;
}
bool_capture=VRAI;
maillon_capture_t *nv_coup=malloc(sizeof(nv_coup));
nv_coup->coord_capture.x=i;
nv_coup->coord_capture.y=j;
if (liste_capture->T != 0)
{
nv_coup->suivant = liste_capture->debut;
}
else
{
nv_coup->suivant=NULL;
}
liste_capture->debut=nv_coup;
liste_capture->T=T++;
if(isupper(partie->tablier[i][j].type))
{
partie->tablier[i][j].type=tolower(partie->tablier[i][j].type);
}
else
{
partie->tablier[i][j].type=toupper(partie->tablier[i][j].type);
}
modifier_case(partie,partie->tablier[coord_de_depart.x][coord_de_depart.y],coord_de_depart);
modifier_case(partie,partie->tablier[T-1][0],coord_de_depart);
if (partie->tablier[coord_darrivee.x][coord_darrivee.y].statut == PIECE_NON_PROMUE)
{
if (partie->tablier[coord_darrivee.x][coord_darrivee.y].type != 'G' && partie->tablier[coord_darrivee.x][coord_darrivee.y] != 'K' && partie->tablier[coord_darrivee.x][coord_darrivee.y].type != 'g' && partie->tablier[coord_darrivee.x][coord_darrivee.y] != 'k')
{
if ((partie->bool_joueur == JOUEUR_1 && coord_darrivee.x < 4) || (partie->bool_joueur == JOUEUR_0 && coord_darrivee.x > 6))
{
if (tolower(partie->tablier[coord_darrivee.x][coord_darrivee.y].type == 'p') && (coord_darrivee.x == 1 || coord_darrivee.x ==9))
{
changer_statut(&partie->tablier[coord_darrivee.x][coord_darrivee.y]);
bool_promotion = VRAI;
}
else if ((partie->tablier[coord_darrivee.x][coord_darrivee.y].type == 'l') && (coord_darrivee.x == 1) || (partie->tablier[coord_darrivee.x][coord_darrivee.y].type == 'L' && coord_darrivee.x == 9))
{
changer_statut(&partie->tablier[coord_darrivee.x][coord_darrivee.y]);
bool_promotion = VRAI;
}
else if ((partie->tablier[coord_darrivee.x][coord_darrivee.y].type == 'n' && coord_darrivee.x < 3) || (partie->tablier[coord_darrivee.x][coord_darrivee.y].type == 'N' && coord_darrivee.x > 7))
{
changer_statut(&partie->tablier[coord_darrivee.x][coord_darrivee.y]);
bool_promotion = VRAI;
}
else
{
do
{
printf("voulez vous proumouvoire la piece jouee ? \n si oui tappez : 1\n si non tappez : 0\n");
scanf("%d",&reponse);
}while(reponse != VRAI && reponse != FAUX);
if (reponse)
{
changer_statut(&partie->tablier[coord_darrivee.x][coord_darrivee.y]);
bool_promotion = VRAI;
}
}
}
}
}
nv_coup->promotion=bool_promotion;
nv_coup->capture=bool_capture;
nv_coup->depart=coord_de_depart;
nv_coup->arrivee=coord_darrivee;
ajouter_coup_fin(partie,nv_coup);
}
}
/************************************************_____________________________******************************************/
void annuler_deplacement(partie_t *partie)
{
coup_t *dernier_coup=partie->coup_joue.fin;
if (partie->coup_joue.nb_de_coups != 1)
{
dernier_coup->precedant->suivant=NULL;
partie->coup_joue.fin=dernier_coup->precedant;
}
partie->coup_joue.nb_de_coups--;
modifier_case(partie,partie->tablier[dernier_coup->arrivee.x][dernier_coup->arrivee.y], dernier_coup->depart);
modifier_case(partie,partie->tablier[T-1][0],dernier_coup->arrivee);
if (dernier_coup->capture == VRAI)
{
dernier_coup = liste_capture->debut;
modifier_case(partie,partie->tablier[liste_capture->debut->coord_capture.x][liste_capture->debut->coord_capture.y],dernier_coup->arrivee);
modifier_case(partie,partie->tablier[T-1][0],liste_capture->debut->coord_capture);
liste_capture->debut=dernier_coup->suivant;
dernier_coup->suivant=NULL;
liste_capture->T--;
free(dernier_coup);
partie->tablier[dernier_coup->arrivee.x][dernier_coup->arrivee.y].joueur=(partie->bool_joueur+1)%2;
}
if (isupper(partie->tablier[dernier_coup->arrivee.x][dernier_coup->arrivee.y].type))
{
partie->tablier[dernier_coup->arrivee.x][dernier_coup->arrivee.y].type=tolower(partie->tablier[dernier_coup->arrivee.x][dernier_coup->arrivee.y].type);
}
else
{
partie->tablier[dernier_coup->arrivee.x][dernier_coup->arrivee.y].type=toupper(partie->tablier[dernier_coup->arrivee.x][dernier_coup->arrivee.y].type);
}
if (dernier_coup->promotion==PIECE_PROMUE)
{
changer_statut(&partie->tablier[dernier_coup->depart.x][dernier_coup->depart.y]);
}
free(dernier_coup);
while(partie->bool_joueur==partie->tablier[dernier_coup->arrivee.x][dernier_coup->arrivee.y].joueur)
{
dernier_coup=extraire_fin_liste(&partie->coup_joue);
changer_statut(&partie->tablier[dernier_coup->arrivee.x][dernier_coup->arrivee.y]);
free(dernier_coup);
}
}
//**************************************************_________________________________***************************//
int* saisie_case() {
int* saisie = calloc(6, sizeof(int));
printf("Veuillez entrer l'un des choix suivants :\n\t1. Effectuer un deplacement\n\t2. Annuler le dernier deplacement\n\t3. Quitter la partie\n...? ");
scanf("%u", &saisie[0]);
while (saisie[0] < 1 || saisie[0] > 3)
{ printf("Erreur, aucun numero correspondant, veuillez recommencer\n...? ");
scanf("%u", &saisie[0]);
}
if (saisie[0] == 1) {
printf("Entrez les coordonnees de depart et d'arrivee sous forme de quatre entiers consecutifs et de 1 pour une demande de promotion avant le deplacement, qui sera attribue seulement si possible, 0 sinon\nExemple : 2 5 3 6 1 deplace la piece situee en (2, 5) en (3, 6) avec une demande de promotion\n...? ");
scanf("%u %u %u %u %u", &saisie[1], &saisie[2], &saisie[3], &saisie[4], &saisie[5]);
while (saisie[1] > 10 || saisie[2] > 10 || saisie[3] > 10 || saisie[4] > 10 || saisie[5] > 1) {
printf("Erreur toutes les coordonnees doivent etre comprises entre 0 et 10 et la demande de promotion 1 ou 0, entrez a nouveau\n...? ");
scanf("%u %u %u %u %u", &saisie[1], &saisie[2], &saisie[3], &saisie[4], &saisie[5]);
}
}
return saisie;
}
//********************************************______________________________________************************************//
partie_t *partie_creer()
{
partie_t *partie = malloc(sizeof(*partie_t));
partie->coup_joue.debut=NULL;
partie->coup_joue.fin=NULL;
partie->coup_joue.nb_de_coups=0;
return partie;
} |