Un simple jeu Taquin, il faut remetre les chiffres dans l'ordre
Source / Exemple :
/* Le but du jeu c de remettre les chiffres dans le bon ordre
*
*
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define taille 3
void init_mat(int tmp[taille][taille],int n);
void init_lst(int tmp[taille*taille]);
void lecture(int tmp[taille][taille],int size);
void background();
void keypress(int tmp[taille][taille],int key);
void visionner(char fichier[128]);
void genere_mat();
// Programme principal
int coup=0;
void main()
{
genere_mat();
}
// Initialise la matrice
void init_mat(int tmp[taille][taille],int n)
{
int i,j,k=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
k++;
if(k==9)
tmp[j][i]=0;
else
tmp[j][i]=k;
}
}
// dessine le fond d'écran
void background()
{
int k;
for(k=1;k<=25;k++)
{
gotoxy(25,k);
printf("%c",179);
gotoxy(55,k);
printf("%c",179);
}
for(k=1;k<=80;k++)
{
gotoxy(k,7);
printf("%c",196);
gotoxy(k,18);
printf("%c",196);
}
gotoxy(25,7);
printf("%c",197);
gotoxy(25,18);
printf("%c",197);
gotoxy(55,7);
printf("%c",197);
gotoxy(55,18);
printf("%c",197);
}
// Test la matrice
void lecture(int tmp[taille][taille],int size)
{
int a,b,c=0;
for(a=0;a<size;a++)
for(b=0;b<size;b++)
{
c++;
switch(c)
{
case 1 : gotoxy(12,4);break;
case 2 : gotoxy(40,4);break;
case 3 : gotoxy(67,4);break;
case 4 : gotoxy(12,12);break;
case 5 : gotoxy(40,12);break;
case 6 : gotoxy(67,12);break;
case 7 : gotoxy(12,22);break;
case 8 : gotoxy(40,22);break;
case 9 : gotoxy(67,22);break;
}
if(tmp[b][a]==0)
printf(" ");
else
printf("%d",tmp[b][a]);
}
}
void init_lst(int tmp[taille*taille])
{
int i;
for(i=0;i<(taille*taille);i++)
tmp[i]=i+1;
}
// Si pression sur une touche
void keypress(int tmp[taille][taille],int key)
{
int echange;
int i,j,c;
int z_l,z_c;
int mat_ini[taille][taille];
FILE *stream;
char player[32];
char points_c[5];
init_mat(mat_ini,taille);
for(i=0;i<taille;i++)
for(j=0;j<taille;j++)
if(tmp[j][i]==0)
{
z_l=j;
z_c=i;
}
switch(key)
{
case 27 : exit(0);
case 59 : for(c=1;c<=taille*taille;c++)
{
switch(c)
{
case 1 : gotoxy(12,4);break;
case 2 : gotoxy(40,4);break;
case 3 : gotoxy(67,4);break;
case 4 : gotoxy(12,12);break;
case 5 : gotoxy(40,12);break;
case 6 : gotoxy(67,12);break;
case 7 : gotoxy(12,22);break;
case 8 : gotoxy(40,22);break;
case 9 : gotoxy(67,22);break;
}
if(c!=9)
printf("%d",c);
}
gotoxy(67,22);
for(i=0;i<5;i++)
for(j=0;j<5;j++)
printf(" ");
gotoxy(63,20);printf("NOTICE");
gotoxy(63,21);printf("COUP(S):");
gotoxy(72,21);printf("%d",coup);
case 80 : if(z_c>0)
{
coup++;
echange=tmp[z_l][z_c-1];
tmp[z_l][z_c]=echange;
tmp[z_l][z_c-1]=0;
lecture(tmp,taille);
}
break;
case 72 : if(z_c<2)
{
coup++;
echange=tmp[z_l][z_c+1];
tmp[z_l][z_c]=echange;
tmp[z_l][z_c+1]=0;
lecture(tmp,taille);
}
break;
case 77 : if(z_l>0)
{
coup++;
echange=tmp[z_l-1][z_c];
tmp[z_l][z_c]=echange;
tmp[z_l-1][z_c]=0;
lecture(tmp,taille);
}
break;
case 75 : if(z_l<2)
{
coup++;
echange=tmp[z_l+1][z_c];
tmp[z_l][z_c]=echange;
tmp[z_l+1][z_c]=0;
lecture(tmp,taille);
}
break;
default : gotoxy(63,20);printf(" ");
gotoxy(63,21);printf(" ");
lecture(tmp,taille);
}
if(tmp[0][0]==1 && tmp[0][1]==4 && tmp[0][2]==7 &&
tmp[1][0]==2 && tmp[1][1]==5 && tmp[1][2]==8 &&
tmp[2][0]==3 && tmp[2][1]==6 && tmp[2][2]==0 )
{
clrscr();
gotoxy(32,11);
printf("GAGNE !\n");
gotoxy(32,13);
printf("EN %d COUP(S)",coup);
gotoxy(1,15);printf("NOM: ");scanf(" %[^'\n']",player);
stream=fopen("score.dat","ab");
fprintf(stream,player); // On inscrit le nom du joueur
fprintf(stream,"\t:\t\t"); // Les ":" et les tabulations "\t"
fprintf( stream,itoa(coup,points_c,10)); // On inscrit les points comme une
// chaine de caractere avec "itoa"
fputc('\n',stream); // On inscrit un saut de ligne
fclose(stream); // Fermeture du fichier
printf("\n\nAppuyer sur une touche quelconque pour voir les scores.");
getch();
clrscr();
visionner("score.dat");
printf("\n\nAppuyez sur:\n\n"
"[1] pour reinitialiser les scores.\n"
"[2] pour rejouer.\n"
"[3] pour quitter.\n");
GETKEY:
i=getch();
switch(i)
{
case 49 : gotoxy(1,25);
stream=fopen("score.dat","wb"); // On ouvre le fichier en
fclose(stream); // tant que nouveau fichier.
printf("Score reinitialise."); // Donc le fichier est vide.
goto GETKEY;
break;
case 50 : genere_mat();
break;
case 51 : exit(0);
break;
default : goto GETKEY;
}
}
}
void visionner(char fichier[128])
{
int car;
int nbr_saut=0;
double nbr_car=0;
FILE *stream;
stream=fopen(fichier,"rb");
clrscr();
if(stream!=NULL) /* Si le fichier est existant */
{
printf("\nLES SCORES:\n\n");getch();
while( (car=fgetc(stream)) !=EOF )
{
nbr_car++;
if( char(car)=='\n' ) /* Si le caractere rencontre est un saut de ligne */
nbr_saut++; /* alors on incremente le saut */
if( nbr_saut==20 ) /* Tous les vingt sauts il y a une attente */
{
nbr_saut=0;
getch();
}
printf("%c",car); /* On affiche le caractere lu dans le fichier */
}
}
printf("\n");
}
void genere_mat() // Permet de generer une grille
{
int matrix[taille][taille];
int list1[taille*taille];
int i,j,x,getkey;
time_t s;
init_mat(matrix,taille);
textbackground(BLUE);
textcolor(YELLOW);
clrscr();
background();
srand( (unsigned) time (&s) );
lecture(matrix,taille);
gotoxy(67,22);
printf("NOTICE"); // combinaison à faire
getch();
gotoxy(67,22);
printf(" "); // on efface le mot NOTICE
init_lst(list1);
for(i=0;i<taille;i++) // pour la matrice
for(j=0;j<taille;j++)
{
x=(rand()%(taille*taille)-1); // on pioche un nombre au hazard
if(list1[x]!=10) // on teste si on peut prendre le nombre
{
matrix[i][j]=list1[x];
list1[x]=10; // on met 10 pour ne pas pouvoir retomber sur
} // le nombre aléatoire
else
{
if(j==0)
{
j=taille; // en recule d'une case
i--; //
} // | |
else // --+--+--
j--; // | |
} // --+--+--
} // | |
lecture(matrix,taille);
for(;;)
{
getkey=getch();
keypress(matrix,getkey);
}
}
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.