Code :
/***************************************************************************** * projet.c ***************************************************************************** */ #include <allegro.h> #include <math.h> #include <tpinfo.h> #include <stdio.h> #include <stdlib.h> int puissance4(int); void presentation(void); void grille (void); //void ordinateur(void); int bleu,blanc,noir,jaune,rouge,gris, bleuclair; const int X = 2*500;//1100; const int Y = 2*400;//900; int x,k; int q; int ordi; int ligne1,ligne2,ligne3,ligne4,ligne5,ligne6,ligne7; int colonne1,colonne2,colonne3,colonne4,colonne5,colonne6,colonne7; int n1, n2,n3,n4,n5,n6,n7; int i,j,copiei, copiej; int couleur, choix; int modulo, compteur; int tab[6][7]={{0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0}}; int A[4]={0,-1,-1,-1}; int B[4]={1,0,1,-1}; int k; int main(void) { int puissance=1; int touche; allegro_init(); install_keyboard(); set_color_depth(32); set_gfx_mode(GFX_AUTODETECT_WINDOWED, X, Y, 0, 0); ligne1=ligne2=ligne3=ligne4=ligne5=ligne6=ligne7=5; colonne1=colonne2=colonne3=colonne4=colonne5=colonne6=colonne7=0; n1=n2=n3=n4=n5=n6=n7=700; bleu = makecol(0, 0, 255); blanc = makecol(255, 255, 255); noir = makecol(0, 0, 0); jaune = makecol(255, 255, 0); rouge = makecol(255, 0, 0); gris=makecol(40,35,40); bleuclair=makecol(125,125,255); clear_to_color(screen, noir); presentation(); grille(); while(1 || (compteur>42) || (puissance == 4)) { for(compteur=0;compteur<=42;compteur++) { modulo = compteur % 2; touche = readkey(); if(modulo==0) { couleur=jaune; if(((touche & 0xff) =='a')&&(ligne1>=0)&&(ligne1<=5)) { circlefill(screen, 200, n1, 40, couleur); circle(screen, 200, n1, 35, noir); n1 = n1 - 100; tab[ligne1][colonne1]=1; i=ligne1; j=colonne1; ligne1--; rest(100); } if(((touche & 0xff) =='z')&&(ligne2>=0)&&(ligne2<=5)) { circlefill(screen, 300, n2, 40, couleur); circle(screen, 300, n2, 35, noir); n2 = n2 - 100; tab[ligne2][colonne2]=1; i=ligne2; j=colonne2; ligne2--; rest(100); } if(((touche & 0xff) =='e')&&(ligne3>=0)&&(ligne3<=5)) { circlefill(screen, 400, n3, 40, couleur); circle(screen, 400, n3, 35, noir); n3 = n3 - 100; tab[ligne3][colonne3]=1; i=ligne3; j=colonne3; ligne3--; rest(100); } if(((touche & 0xff) =='r')&&(ligne4>=0)&&(ligne4<=5)) { circlefill(screen, 500, n4, 40, couleur); circle(screen, 500, n4, 35, noir); n4 = n4 - 100; tab[ligne4][colonne4]=1; i=ligne4; j=colonne4; ligne4--; rest(100); } if(((touche & 0xff) =='t')&&(ligne5>=0)&&(ligne5<=5)) { circlefill(screen, 600, n5, 40, couleur); circle(screen, 600, n5, 35, noir); n5 = n5 - 100; tab[ligne5][colonne5]=1; i=ligne5; j=colonne5; ligne5--; rest(100); } if(((touche & 0xff) =='y')&&(ligne6>=0)&&(ligne6<=5)) { circlefill(screen, 700, n6, 40, couleur); circle(screen, 700, n6, 35, noir); n6 = n6 - 100; tab[ligne6][colonne6]=1; i=ligne6; j=colonne6; ligne6--; rest(100); } if(((touche & 0xff) =='u')&&(ligne7>=0)&&(ligne7<=5)) { circlefill(screen, 800, n7, 40, couleur); circle(screen, 800, n7, 35, noir); n7 = n7 - 100; tab[ligne7][colonne7]=1; i=ligne7; j=colonne7; ligne7--; rest(100); } if(((touche & 0xff) !='a')&&((touche & 0xff) !='z')&&((touche & 0xff) !='e')&&((touche & 0xff) !='r')&&((touche & 0xff) !='t')&&((touche & 0xff) !='y')&&((touche & 0xff) !='u')) { compteur--; } } else { couleur=rouge; if(((touche & 0xff) =='a')&&(ligne1>=0)&&(ligne1<=5)) { circlefill(screen, 200, n1, 40, couleur); circle(screen, 200, n1, 35, noir); n1 = n1 - 100; tab[ligne1][colonne1]=2; i=ligne1; j=colonne1; ligne1--; rest(100); } if(((touche & 0xff) =='z')&&(ligne2>=0)&&(ligne2<=5)) { circlefill(screen, 300, n2, 40, couleur); circle(screen, 300, n2, 35, noir); n2 = n2 - 100; tab[ligne2][colonne2]=2; i=ligne2; j=colonne2; ligne2--; rest(100); } if(((touche & 0xff) =='e')&&(ligne3>=0)&&(ligne3<=5)) { circlefill(screen, 400, n3, 40, couleur); circle(screen, 400, n3, 35, noir); n3 = n3 - 100; tab[ligne3][colonne3]=2; i=ligne3; j=colonne3; ligne3--; rest(100); } if(((touche & 0xff) =='r')&&(ligne4>=0)&&(ligne4<=5)) { circlefill(screen, 500, n4, 40, couleur); circle(screen, 500, n4, 35, noir); n4 = n4 - 100; tab[ligne4][colonne4]=2; i=ligne4; j=colonne4; ligne4--; rest(100); } if(((touche & 0xff) =='t')&&(ligne5>=0)&&(ligne5<=5)) { circlefill(screen, 600, n5, 40, couleur); circle(screen, 600, n5, 35, noir); n5 = n5 - 100; tab[ligne5][colonne5]=2; i=ligne5; j=colonne5; ligne5--; rest(100); } if(((touche & 0xff) =='y')&&(ligne6>=0)&&(ligne6<=5)) { circlefill(screen, 700, n6, 40, couleur); circle(screen, 700, n6, 35, noir); n6 = n6 - 100; tab[ligne6][colonne6]=2; i=ligne6; j=colonne6; ligne6--; rest(100); } if(((touche & 0xff) =='u')&&(ligne7>=0)&&(ligne7<=5)) { circlefill(screen, 800, n7, 40, couleur); circle(screen, 800, n7, 35, noir); n7 = n7 - 100; tab[ligne7][colonne7]=2; i=ligne7; j=colonne7; ligne7--; rest(100); } puissance = puissance4(puissance); } if(((touche & 0xff) !='a')&&((touche & 0xff) !='z')&&((touche & 0xff) !='e')&&((touche & 0xff) !='r')&&((touche & 0xff) !='t')&&((touche & 0xff) !='y')&&((touche & 0xff) !='u')) { compteur--; } if(puissance==4) // si puissance = 4 alors le joueur 1 a gagné { textout_ex(screen,font,"Le joueur 1 a gagne",500,400,rouge,10); rest(1000); return 0; } if(puissance==0) // si puissance = 0 alors le joueur 1 a gagné { textout_ex(screen,font,"Le joueur 2 a gagne",x-1,15,rouge,10); rest(1000); return 0; } if(key[KEY_ESC]) // a tout moment on peut appuyer sur Echap pour quitter { return(0); } } } return 0; } END_OF_MAIN() void presentation(void) { int q,degrade; rectfill(screen, 150, 150, X-150, Y-50, gris); q=0; for(x=0;x<430;x++) { degrade=makecol(q,q,q); textout_ex(screen,font,"Flo Production",x-1,15,noir,10); textout_ex(screen,font,"Flo Production",x,15,rouge,10); textout_ex(screen,font,"presente :",-x+881,35,noir,10); textout_ex(screen,font,"presente :",-x+880,35,rouge,10); textout_ex(screen,font,"Le Puissance 4",450,75,degrade,10); rest(4); } textout_ex(screen,font,"L",420,70,blanc,10); rest(185); textout_ex(screen,font,"e",430,70,blanc,10); rest(185); textout_ex(screen,font," ",440,70,blanc,10); rest(185); textout_ex(screen,font,"P",450,70,blanc,10); rest(185); textout_ex(screen,font,"u",460,70,blanc,10); rest(185); textout_ex(screen,font,"i",470,70,blanc,10); rest(185); textout_ex(screen,font,"s",480,70,blanc,10); rest(185); textout_ex(screen,font,"s",490,70,blanc,10); rest(185); textout_ex(screen,font,"a",500,70,blanc,10); rest(185); textout_ex(screen,font,"n",510,70,blanc,10); rest(185); textout_ex(screen,font,"c",520,70,blanc,10); rest(185); textout_ex(screen,font,"e",530,70,blanc,10); rest(185); textout_ex(screen,font," ",540,70,blanc,10); rest(185); textout_ex(screen,font,"4",550,70,blanc,10); rest(185); textout_ex(screen,font," Pour jouer seul taper S, pour jouer a deux taper D :",146,170,bleuclair,-1); choix=readkey(); if((choix & 0xff) =='d') { textout_ex(screen,font,"Vous avez choisit de jouer a deux joueurs",155,210,blanc,-1); rest(700); textout_ex(screen,font,"La partie commence dans quelques secondes",155,250,bleuclair,-1); } if((choix & 0xff) =='s') { textout_ex(screen,font,"Vous avez choisit de jouer contre l'ordinateur",155,210,blanc,-1); rest(700); textout_ex(screen,font,"Tapez F pour jouer en facile, I pour intermediaire ou D pour difficile",155,250,bleuclair,-1); choix=readkey(); if((choix & 0xff) =='f') { textout_ex(screen,font,"Vous avez choisit de jouer en mode facile",155,290,blanc,-1); } if((choix & 0xff) =='i') { textout_ex(screen,font,"Vous avez choisit de jouer en mode intermediaire",155,290,blanc,-1); } if((choix & 0xff) =='d') { textout_ex(screen,font,"Vous avez choisit de jouer en mode difficile",155,290,blanc,-1); } rest(700); textout_ex(screen,font,"La partie commence dans quelques secondes",155,330,bleuclair,-1); } rest(2000); } void grille(void) { rectfill(screen, 150, 150, X-150, Y-50, bleu); for(x = 200; x < X-100; x = x + 100) { for(q = 200; q < Y-50; q = q + 100) { circlefill(screen, x, q, 40, noir); } } } int puissance4( int puissance) { int k; int a,b; int i,j; if(puissance!=4) { puissance=1; i=5; j=0; // for(i=0;i<=5;i++) //{ // for(j=0;j<=6;j++) // { if(tab[i][j]==1) { for(k=0;k<=3;k++) { a=A[k]; b=B[k]; while((tab[i+a][j+b]==1)&&(puissance<4)) { i+=a; j+=b; } while((tab[i-a][j-b]==1)&&(puissance<4)) { puissance++; i-=a; j-=b; } } } // } //} } if((puissance!=4)) { puissance=3; i=5; j=1; //for(i=0;i<=5;i++) //{ // for(j=0;j<=6;j++) // { if(tab[i][j]==2) { for(k=0;k<=3;k++) { a=A[k]; b=B[k]; while((tab[i+a][j+b]==2)&&(puissance<4)) { i+=a; j+=b; } while((tab[i-a][j-b]==2)&&(puissance<4)) { puissance--; i-=a; j-=b; } } } //} // } } return(puissance); } /* void ordinateur(void) { ordi=aleatoire(1,7); if(ordi==1) { circlefill(screen, 200, n1, 40, couleur); circle(screen, 200, n1, 35, noir); n1 = n1 - 100; tab[ligne1][colonne1]=1; i=ligne1; j=colonne1; ligne1--; rest(100); } if(ordi==2) { circlefill(screen, 300, n2, 40, couleur); circle(screen, 300, n2, 35, noir); n2 = n2 - 100; tab[ligne2][colonne2]=1; i=ligne2; j=colonne2; ligne2--; rest(100); } if(ordi==3) { circlefill(screen, 400, n3, 40, couleur); circle(screen, 400, n3, 35, noir); n3 = n3 - 100; tab[ligne3][colonne3]=1; i=ligne3; j=colonne3; ligne3--; rest(100); } if(ordi==4) { circlefill(screen, 500, n4, 40, couleur); circle(screen, 500, n4, 35, noir); n4 = n4 - 100; tab[ligne4][colonne4]=1; i=ligne4; j=colonne4; ligne4--; rest(100); } if(ordi==5) { circlefill(screen, 600, n5, 40, couleur); circle(screen, 600, n5, 35, noir); n5 = n5 - 100; tab[ligne5][colonne5]=1; i=ligne5; j=colonne5; ligne5--; rest(100); } if(ordi==6) { circlefill(screen, 700, n6, 40, couleur); circle(screen, 700, n6, 35, noir); n6 = n6 - 100; tab[ligne6][colonne6]=1; i=ligne6; j=colonne6; ligne6--; rest(100); } if(ordi==7) { circlefill(screen, 800, n7, 40, couleur); circle(screen, 800, n7, 35, noir); n7 = n7 - 100; tab[ligne7][colonne7]=1; i=ligne7; j=colonne7; ligne7--; rest(100); } } */
|