diff --git a/Makefile b/Makefile index ad42cc26a96f67c2958a79605157b96db436deef..33f9ad30164c13e49fbf51a71e1b958b92697ab8 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CC=gcc -Wall -Wextra -ansi -g -std=c99 +CC=gcc -Wall -Wextra -ansi -g -lm -std=c99 stackchess : main.o tableau.o affichage.o action.o deplacement.o cd obj && $(CC) $^ -o ../$@ diff --git a/obj/affichage.o b/obj/affichage.o index 20c46e1b16f07270976f7bfbed2c5ea672a6bf84..43f031287aee45fca6cab0f46347031313b4d86d 100644 Binary files a/obj/affichage.o and b/obj/affichage.o differ diff --git a/src/action.c b/src/action.c index eb9787377c46de63beebe4e31f7a185735e8a31c..bff66898603abc4917b11a3d34d2c805fdb2354c 100644 --- a/src/action.c +++ b/src/action.c @@ -15,6 +15,7 @@ void realiser(char action,pile **tableau,int N,char *cases,int *tour) case 'c' : affichage(tableau,N,cases,*tour,"Entrez les coordonnées de la case à selectionner >"); scanf("%3s",sel); + viderbuffer(); if (sel[1] != '\0'){ strcpy(cases,sel); } diff --git a/src/affichage.c b/src/affichage.c index 4cb7deee64c67ac33548c3ccb09bf87146570281..023f94bf8d473cdd3caa9a6a5e69dad952174c5c 100644 --- a/src/affichage.c +++ b/src/affichage.c @@ -2,7 +2,15 @@ #include"tableau.h" #include"affichage.h" #include"couleur.h" +#include<math.h> /*affichage.c*/ +void viderbuffer() +{ + char buff=getchar(); + while (buff != '\n'){ + buff=getchar(); + } +} void affiche_pile_a_cote(pile **tableau,char *sel,int x,int y,int N, int ligne,int nb) { diff --git a/src/affichage.h b/src/affichage.h index 4c5d7d17e1930dbf3603ce038e3149cc52957f1f..284776ac25fea4ac3b1f9445868c7bce1c752491 100644 --- a/src/affichage.h +++ b/src/affichage.h @@ -5,6 +5,13 @@ /*affichage.h*/ +/* + * @require + * @assign + * @ensure : vide le buffer en input + */ +void viderbuffer(); + /* * @require * @assign diff --git a/src/deplacement.c b/src/deplacement.c index 33a62c97083cb9b66de6a43d4d1b91363ce89834..aaf4e632f975d07e30e4ccf56c54b97f82bcc398 100644 --- a/src/deplacement.c +++ b/src/deplacement.c @@ -2,7 +2,8 @@ #include"affichage.h" #include"deplacement.h" #include<stdlib.h> - +#include<math.h> +//#include<unistd.h> /*deplacement.c*/ void deplacement(pile **tableau, int N, char *sel, int *tour) @@ -22,11 +23,9 @@ void deplacement(pile **tableau, int N, char *sel, int *tour) } } else printf("\nDéplacement non autorisé"); - char c; printf("\n"); - scanf("%1s",&c); - fflush(stdin); - //read(0,&c,1);//attend la touche entrer + getchar(); + viderbuffer(); } } @@ -54,7 +53,7 @@ void case_destination(pile **tableau,int N,int i_src,int j_src, int *i_dest,int for (i=0 ; i<nb ; i++){ switch (tmp->chaine[0]){ case 'P' : - valide=pions(i_src,j_src,*i_dest,*j_dest); + valide=pions(tableau,i_src,j_src,*i_dest,*j_dest,*tour); break; case 'R' : valide=roi(i_src,j_src,*i_dest,*j_dest); @@ -64,10 +63,13 @@ void case_destination(pile **tableau,int N,int i_src,int j_src, int *i_dest,int break; case 'T' : valide=tours(i_src,j_src,*i_dest,*j_dest); + break; case 'F' : valide=fous(i_src,j_src,*i_dest,*j_dest); + break; case 'C' : valide=cavaliers(i_src,j_src,*i_dest,*j_dest); + break; default : valide=0; } @@ -81,17 +83,35 @@ void case_destination(pile **tableau,int N,int i_src,int j_src, int *i_dest,int } else { printf("Déplacement illégal pour au moins un pion de la pile"); } + } else if (*i_dest>=0 && *i_dest<N && *j_dest>=0) { + printf("Coordonnées invalides pour la case de destination"); + } else if (*j_dest<N && nb>nb_de_pion(tableau[*i_dest][*j_dest],"BN"[*tour%2+1])){ + printf("Le nombre de pions à déplacer doit être strictement plus grand que le nombre de pions adverses sur la case de destination"); } } } -int pions(int i_src,int j_src,int i_dest,int j_dest) +int pions(pile **tableau,int i_src,int j_src,int i_dest,int j_dest,int tour) { + int k=0; + if ("BN"[tour%2] == 'N'){ + k=1; + } else { + k=-1; + } + if (nb_de_pion(tableau[i_dest][j_dest],"BN"[tour%2+1])==0 && i_dest==i_src+k && j_dest==j_src){ + return 1; + } else if (i_dest==i_src+k && (j_dest==j_src+1 && j_dest==j_src-1)){ + return 1; + } return 0; } int dames(int i_src,int j_src,int i_dest,int j_dest) { + if ((j_dest==j_src && i_dest!=i_src) || (i_dest==i_src && j_dest!=j_src) || (abs(i_dest-i_src)==abs(j_dest-j_src) && i_dest!=i_src)){ + return 1; + } return 0; } @@ -107,16 +127,31 @@ int roi(int i_src,int j_src,int i_dest,int j_dest) } int tours(int i_src,int j_src,int i_dest,int j_dest) { + if ((i_dest == i_src && j_dest!=j_src) || (j_dest ==j_src && i_dest!=i_src)){ + return 1; + } return 0; } int fous(int i_src,int j_src,int i_dest, int j_dest) { + if (abs(j_dest-j_src)==abs(i_dest-i_src) && i_dest!=i_src){ + return 1; + } return 0; } int cavaliers(int i_src,int j_src,int i_dest,int j_dest) { + if (i_dest==i_src+2 || i_dest==i_src-2){ + if (j_dest==j_src+1 || j_dest==j_src-1){ + return 1; + } + } else if (j_dest==j_src+2 || j_dest==j_src-2){ + if (i_dest==i_src+1 || i_dest==i_src-1){ + return 1; + } + } return 0; } diff --git a/src/deplacement.h b/src/deplacement.h index 1d8032cf5c71fed9ab664fe727fe03d099f98e8e..d162db63be38f2a6c810a26f879f627fbd406f74 100644 --- a/src/deplacement.h +++ b/src/deplacement.h @@ -30,7 +30,7 @@ void case_destination(pile **tableau,int N,int i_src,int j_src,int *i_dest,int * * @assign * @ensure : verifie que le déplacement est valide pour un pion */ -int pions(int i_src,int j_src,int i_dest,int j_dest); +int pions(pile **tableau, int i_src,int j_src,int i_dest,int j_dest, int tour); /* * @require diff --git a/stackchess b/stackchess index 154ea31e394eb5a9234e15493fd5c56c53f78b8d..f9fdf53180ba9dec72463d0f5e66f3cd0f3ab6fc 100755 Binary files a/stackchess and b/stackchess differ diff --git a/test.c b/test.c deleted file mode 100644 index cc0c5fb6851fde22624de27cba926fbfe9809306..0000000000000000000000000000000000000000 --- a/test.c +++ /dev/null @@ -1,8 +0,0 @@ -#include<stdio.h> - -int main() -{ - char *sel="a"; - printf("%s",sel+1); - return 0; -}