diff --git a/DEV/Makefile b/DEV/Makefile new file mode 100644 index 0000000..9e2b9f0 --- /dev/null +++ b/DEV/Makefile @@ -0,0 +1,11 @@ +jeu : jeu_principal.o + gcc -o jeu jeu_principal.o -lgraph + +jeu_principal.o : jeu_principal.c jeu_principal.h + gcc -c jeu_principal.c -lgraph + +clean : + rm *.o + +run : jeu + ./jeu \ No newline at end of file diff --git a/DEV/img/p4.img b/DEV/img/p4.img new file mode 100644 index 0000000..e81ac8c Binary files /dev/null and b/DEV/img/p4.img differ diff --git a/DEV/jeu b/DEV/jeu new file mode 100755 index 0000000..c37535f Binary files /dev/null and b/DEV/jeu differ diff --git a/DEV/jeu_principal.c b/DEV/jeu_principal.c new file mode 100644 index 0000000..a6231ce --- /dev/null +++ b/DEV/jeu_principal.c @@ -0,0 +1,324 @@ +#include +#include +#include +#include + +void affiche_grille(int grille[8][8], int nb_de_lignes, int nb_de_colonnes) { + int i, j; + + for (i = 0; i < nb_de_lignes; i++) { + for (j = 0; j < nb_de_colonnes; j++) { + printf("%d ", grille[i][j]); + } + putchar('\n'); + } +} + +int verifie_si_taquin_complet(int grille[8][8], int nb_de_lignes, int nb_de_colonnes) { + int compteur = 0; + int i, j; + + for (i = 0; i < nb_de_lignes; i++) { + for (j = 0; j < nb_de_colonnes; j++) { + if (grille[i][j] != compteur) { + return 0; /* Pas complelt */ + } + compteur++; + } + } + + return 1; /* Complet */ +} + +int peut_decaler_en_haut(int grille[8][8], int nb_de_lignes, int nb_de_colonnes) { + int i; + + for (i = 0; i != nb_de_lignes; i++) { + if (grille[nb_de_colonnes-1][i] == 0) { + return 0; + } + } + + return 1; +} + +int peut_decaler_en_bas(int grille[8][8], int nb_de_lignes, int nb_de_colonnes) { + int i; + + for (i = 0; i != nb_de_lignes; i++) { + if (grille[0][i] == 0) { + return 0; + } + } + + return 1; +} + + +int peut_decaler_a_gauche(int grille[8][8], int nb_de_lignes, int nb_de_colonnes) { + int i; + + for (i = 0; i != nb_de_colonnes; i++) { + if (grille[i][nb_de_lignes-1] == 0) { + return 0; + } + } + + return 1; +} + + +int peut_decaler_a_droite(int grille[8][8], int nb_de_lignes, int nb_de_colonnes) { + int i; + + for (i = 0; i != nb_de_colonnes; i++) { + if (grille[i][0] == 0) { + return 0; + } + } + + return 1; +} + + +void mettre_a_jour_grille(int tab_image[8][8], int nb_de_lignes, int nb_de_colonnes, int decalage, int indice_case_vide_x, int indice_case_vide_y, int direction_decalage, int largeur_image, int hauteur_image) { + int pos_img_x; + int pos_img_y; + int pos_x; + int pos_y; + + switch (direction_decalage) { + case 1: /* haut */ + pos_img_x = (tab_image[indice_case_vide_x][indice_case_vide_y] % nb_de_colonnes) * largeur_image / nb_de_colonnes; + pos_img_y = tab_image[indice_case_vide_x][indice_case_vide_y] / nb_de_colonnes * hauteur_image / nb_de_lignes; + pos_x = indice_case_vide_y * (largeur_image / nb_de_colonnes); + pos_y = indice_case_vide_x * (hauteur_image / nb_de_lignes); + + ChoisirCouleurDessin(CouleurParNom("white")); + + RemplirRectangle( + pos_x + decalage*(indice_case_vide_y+1), pos_y + decalage*(indice_case_vide_x+1), largeur_image / nb_de_colonnes, hauteur_image / nb_de_lignes + ); + + pos_img_x = (tab_image[indice_case_vide_x-1][indice_case_vide_y] % nb_de_colonnes) * largeur_image / nb_de_colonnes; + pos_img_y = tab_image[indice_case_vide_x-1][indice_case_vide_y] / nb_de_colonnes * hauteur_image / nb_de_lignes; + pos_x = (indice_case_vide_y) * (largeur_image / nb_de_colonnes); + pos_y = (indice_case_vide_x-1) * (hauteur_image / nb_de_lignes); + + ChargerImage("./img/p4.img", + pos_x + decalage * indice_case_vide_y + 10, pos_y + decalage * (indice_case_vide_x-1) + 10, + pos_img_x, pos_img_y, + largeur_image / nb_de_colonnes, + hauteur_image / nb_de_lignes + ); + + break; + + case 2: /* bas */ + pos_img_x = (tab_image[indice_case_vide_x][indice_case_vide_y] % nb_de_colonnes) * largeur_image / nb_de_colonnes; + pos_img_y = tab_image[indice_case_vide_x][indice_case_vide_y] / nb_de_colonnes * hauteur_image / nb_de_lignes; + pos_x = indice_case_vide_y * (largeur_image / nb_de_colonnes); + pos_y = indice_case_vide_x * (hauteur_image / nb_de_lignes); + + ChoisirCouleurDessin(CouleurParNom("white")); + + RemplirRectangle( + pos_x + decalage*(indice_case_vide_y+1), pos_y + decalage*(indice_case_vide_x+1), largeur_image / nb_de_colonnes, hauteur_image / nb_de_lignes + ); + + pos_img_x = (tab_image[indice_case_vide_x+1][indice_case_vide_y] % nb_de_colonnes) * largeur_image / nb_de_colonnes; + pos_img_y = tab_image[indice_case_vide_x+1][indice_case_vide_y] / nb_de_colonnes * hauteur_image / nb_de_lignes; + pos_x = (indice_case_vide_y) * (largeur_image / nb_de_colonnes); + pos_y = (indice_case_vide_x+1) * (hauteur_image / nb_de_lignes); + + ChargerImage("./img/p4.img", + pos_x + decalage * indice_case_vide_y + 10, pos_y + decalage * (indice_case_vide_x+1) + 10, + pos_img_x, pos_img_y, + largeur_image / nb_de_colonnes, + hauteur_image / nb_de_lignes + ); + break; + + case 3: /* gauche */ + + printf("Coord case vide : %d | %d", indice_case_vide_x, indice_case_vide_y); + pos_img_x = (tab_image[indice_case_vide_x][indice_case_vide_y] % nb_de_colonnes) * largeur_image / nb_de_colonnes; + pos_img_y = tab_image[indice_case_vide_x][indice_case_vide_y] / nb_de_colonnes * hauteur_image / nb_de_lignes; + pos_x = indice_case_vide_y * (largeur_image / nb_de_colonnes); + pos_y = indice_case_vide_x * (hauteur_image / nb_de_lignes); + + ChoisirCouleurDessin(CouleurParNom("white")); + + RemplirRectangle( + pos_x + decalage*(indice_case_vide_y+1), pos_y + decalage*(indice_case_vide_x+1), largeur_image / nb_de_colonnes, hauteur_image / nb_de_lignes + ); + + pos_img_x = (tab_image[indice_case_vide_x][indice_case_vide_y-1] % nb_de_colonnes) * largeur_image / nb_de_colonnes; + pos_img_y = tab_image[indice_case_vide_x][indice_case_vide_y-1] / nb_de_colonnes * hauteur_image / nb_de_lignes; + pos_x = (indice_case_vide_y-1) * (largeur_image / nb_de_colonnes); + pos_y = (indice_case_vide_x) * (hauteur_image / nb_de_lignes); + + ChargerImage("./img/p4.img", + pos_x + decalage * (indice_case_vide_y-1) + 10, pos_y + decalage * (indice_case_vide_x) + 10, + pos_img_x, pos_img_y, + largeur_image / nb_de_colonnes, + hauteur_image / nb_de_lignes + ); + break; + + case 4: /* droite */ + pos_img_x = (tab_image[indice_case_vide_x][indice_case_vide_y] % nb_de_colonnes) * largeur_image / nb_de_colonnes; + pos_img_y = tab_image[indice_case_vide_x][indice_case_vide_y] / nb_de_colonnes * hauteur_image / nb_de_lignes; + pos_x = indice_case_vide_y * (largeur_image / nb_de_colonnes); + pos_y = indice_case_vide_x * (hauteur_image / nb_de_lignes); + + ChoisirCouleurDessin(CouleurParNom("white")); + + RemplirRectangle( + pos_x + decalage*(indice_case_vide_y+1), pos_y + decalage*(indice_case_vide_x+1), largeur_image / nb_de_colonnes, hauteur_image / nb_de_lignes + ); + + pos_img_x = (tab_image[indice_case_vide_x][indice_case_vide_y+1] % nb_de_colonnes) * largeur_image / nb_de_colonnes; + pos_img_y = tab_image[indice_case_vide_x][indice_case_vide_y+1] / nb_de_colonnes * hauteur_image / nb_de_lignes; + pos_x = (indice_case_vide_y+1) * (largeur_image / nb_de_colonnes); + pos_y = (indice_case_vide_x) * (hauteur_image / nb_de_lignes); + + ChargerImage("./img/p4.img", + pos_x + decalage * (indice_case_vide_y+1) + 10, pos_y + decalage * (indice_case_vide_x) + 10, + pos_img_x, pos_img_y, + largeur_image / nb_de_colonnes, + hauteur_image / nb_de_lignes + ); + break; + + } +} + + +int main(void) { + int nb_de_lignes = 3; + int nb_de_colonnes = 3; + int decalage = 10; + int largeur_image = 1710; + int hauteur_image = 900; + int tab_image[8][8]; + int i; + int j; + int compteur = 0; + int indice_case_vide_x = 0; + int indice_case_vide_y = 0; + + srand(time(NULL)); + + for (i = 0; i != nb_de_lignes; i++) { + for (j = 0; j != nb_de_colonnes; j++) { + tab_image[i][j] = compteur; + compteur++; + } + } + + for (i = 0; i != 100; i++) { + int rand_1_1 = (rand() % nb_de_lignes); + int rand_1_2 = (rand() % nb_de_colonnes); + + while (rand_1_1 + rand_1_2 == 0) { + rand_1_1 = (rand() % nb_de_lignes); + rand_1_2 = (rand() % nb_de_colonnes); + } + + int rand_2_1 = (rand() % nb_de_lignes); + int rand_2_2 = (rand() % nb_de_colonnes); + + while (rand_2_1 + rand_2_2 == 0) { + rand_2_1 = (rand() % nb_de_lignes); + rand_2_2 = (rand() % nb_de_colonnes); + } + + int temp = tab_image[rand_1_1][rand_1_2]; + tab_image[rand_1_1][rand_1_2] = tab_image[rand_2_1][rand_2_2]; + tab_image[rand_2_1][rand_2_2] = temp; + } + + + affiche_grille(tab_image, nb_de_lignes, nb_de_colonnes); + + + InitialiserGraphique(); + CreerFenetre(10, 10, largeur_image + decalage * (nb_de_lignes+1), hauteur_image + decalage * (nb_de_colonnes+1)); + + for (i = 0; i < nb_de_lignes; i++) { + for (j = 0; j < nb_de_colonnes; j++) { + if (j == 0 && i == 0){ + j++; + } + int pos_img_x = (tab_image[i][j] % nb_de_colonnes) * (largeur_image / nb_de_colonnes); + int pos_img_y = (tab_image[i][j] / nb_de_colonnes) * (hauteur_image / nb_de_lignes); + int pos_x = j * (largeur_image / nb_de_colonnes); + int pos_y = i * (hauteur_image / nb_de_lignes); + + + ChargerImage("./img/p4.img", + pos_x + decalage * j + 10, pos_y + decalage * i + 10, + pos_img_x, pos_img_y, + largeur_image / nb_de_colonnes, + hauteur_image / nb_de_lignes + ); + + } + } + + while (!verifie_si_taquin_complet(tab_image, nb_de_lignes, nb_de_colonnes)) { + if (ToucheEnAttente()) { + int touche = Touche(); + + if (touche == XK_Up && peut_decaler_en_haut(tab_image, nb_de_lignes, nb_de_colonnes)) { + int temp = tab_image[indice_case_vide_x+1][indice_case_vide_y]; + tab_image[indice_case_vide_x+1][indice_case_vide_y] = 0; + tab_image[indice_case_vide_x][indice_case_vide_y] = temp; + + indice_case_vide_x++; + + mettre_a_jour_grille(tab_image, nb_de_lignes, nb_de_colonnes, decalage, indice_case_vide_x, indice_case_vide_y, 1, largeur_image, hauteur_image); + } else if (touche == XK_Down && peut_decaler_en_bas(tab_image, nb_de_lignes, nb_de_colonnes)) { + int temp = tab_image[indice_case_vide_x-1][indice_case_vide_y]; + tab_image[indice_case_vide_x-1][indice_case_vide_y] = 0; + tab_image[indice_case_vide_x][indice_case_vide_y] = temp; + + indice_case_vide_x--; + + mettre_a_jour_grille(tab_image, nb_de_lignes, nb_de_colonnes, decalage, indice_case_vide_x, indice_case_vide_y, 2, largeur_image, hauteur_image); + } else if (touche == XK_Left && peut_decaler_a_gauche(tab_image, nb_de_lignes, nb_de_colonnes)) { + int temp = tab_image[indice_case_vide_x][indice_case_vide_y+1]; + tab_image[indice_case_vide_x][indice_case_vide_y+1] = 0; + tab_image[indice_case_vide_x][indice_case_vide_y] = temp; + + indice_case_vide_y++; + + mettre_a_jour_grille(tab_image, nb_de_lignes, nb_de_colonnes, decalage, indice_case_vide_x, indice_case_vide_y, 3, largeur_image, hauteur_image); + } else if (touche == XK_Right && peut_decaler_a_droite(tab_image, nb_de_lignes, nb_de_colonnes)) { + int temp = tab_image[indice_case_vide_x][indice_case_vide_y-1]; + tab_image[indice_case_vide_x][indice_case_vide_y-1] = 0; + tab_image[indice_case_vide_x][indice_case_vide_y] = temp; + + indice_case_vide_y--; + + mettre_a_jour_grille(tab_image, nb_de_lignes, nb_de_colonnes, decalage, indice_case_vide_x, indice_case_vide_y, 4, largeur_image, hauteur_image); + + } + + affiche_grille(tab_image, nb_de_lignes, nb_de_colonnes); + } + } + + ChargerImage("./img/p4.img", + 10, 10, + 0, 0, + largeur_image, hauteur_image + ); + + Touche(); + FermerGraphique(); + return EXIT_SUCCESS; +} + + diff --git a/DEV/jeu_principal.h b/DEV/jeu_principal.h new file mode 100644 index 0000000..0f7cbc2 --- /dev/null +++ b/DEV/jeu_principal.h @@ -0,0 +1,28 @@ +#ifndef JEU_PRINCIPAL_H +#define JEU_PRINCIPAL_H + +void affiche_grille(int grille[8][8], int nb_de_lignes, int nb_de_colonnes); +/* Affichage de la grille dans la console */ + +int verifie_si_taquin_complet(int grille[8][8], int nb_de_lignes, int nb_de_colonnes); +/* Vérifie la condition de victoire */ + +int peut_decaler_en_haut(int grille[8][8], int nb_de_lignes, int nb_de_colonnes); +/* Vérifie si on peut décaler l'image en dessous de la case vide vers le haut */ + +int peut_decaler_en_bas(int grille[8][8], int nb_de_lignes, int nb_de_colonnes); +/* Vérifie si on peut décaler l'image au dessus de la case vide vers le bas */ + +int peut_decaler_a_gauche(int grille[8][8], int nb_de_lignes, int nb_de_colonnes); +/* Vérifie si on peut décaler l'image à droite de la case vide vers la gauche */ + +int peut_decaler_a_droite(int grille[8][8], int nb_de_lignes, int nb_de_colonnes); +/* Vérifie si on peut décaler l'image à gauche de la case vide vers la droite */ + +void mettre_a_jour_grille(int tab_image[8][8], int nb_de_lignes, int nb_de_colonnes, int decalage, int indice_case_vide_x, int indice_case_vide_y, int direction_decalage, int largeur_image, int hauteur_image); +/* Met à jour l'affichage des 2 cases concernées dans le cas d'un déplacement */ + +int main(void); +/* LANCEMENT DU JEU */ + +#endif \ No newline at end of file