2025-11-30 16:16:27 +01:00
|
|
|
#include "partie.h"
|
2025-11-30 19:40:22 +01:00
|
|
|
/* On inclut stdio au cas où */
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
/* Codes des touches avec les fleches (standards X11) */
|
|
|
|
|
#define FL_GAUCHE 65361
|
|
|
|
|
#define FL_HAUT 65362
|
|
|
|
|
#define FL_DROITE 65363
|
|
|
|
|
#define FL_BAS 65364
|
|
|
|
|
|
2025-11-30 16:16:27 +01:00
|
|
|
void initialiser_plateau(int grille[NB_LIGNES][NB_COLS]){
|
|
|
|
|
int i, j;
|
|
|
|
|
int compteur = 1;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < NB_LIGNES; i++){
|
|
|
|
|
for(j = 0; j < NB_COLS; j++){
|
|
|
|
|
grille[i][j] = compteur;
|
|
|
|
|
compteur++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*On laisse la dernière case du tableu vide pour le taquin*/
|
|
|
|
|
grille[NB_LIGNES - 1 ][NB_COLS - 1] = 0;
|
|
|
|
|
}
|
2025-11-30 19:40:22 +01:00
|
|
|
|
|
|
|
|
void deplacer(int grille[NB_LIGNES][NB_COLS], int touche) {
|
|
|
|
|
int i, j;
|
|
|
|
|
int vide_i, vide_j;
|
|
|
|
|
int cible_i, cible_j;
|
|
|
|
|
int temp;
|
|
|
|
|
|
|
|
|
|
/* Trouver la case vide */
|
|
|
|
|
vide_i = -1;
|
|
|
|
|
for(i = 0; i < NB_LIGNES; i++) {
|
|
|
|
|
for(j = 0; j< NB_COLS; j++) {
|
|
|
|
|
if (grille[i][j] == 0) {
|
|
|
|
|
vide_i = i;
|
|
|
|
|
vide_j = j;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* INiitialisation cible */
|
|
|
|
|
cible_i = vide_i;
|
|
|
|
|
cible_j = vide_j;
|
|
|
|
|
|
|
|
|
|
/* Calculer la cible */
|
|
|
|
|
if(touche == FL_GAUCHE) cible_j = vide_j + 1;
|
|
|
|
|
if(touche == FL_DROITE) cible_j = vide_j - 1;
|
|
|
|
|
if(touche == FL_HAUT) cible_i = vide_i + 1;
|
|
|
|
|
if(touche == FL_BAS) cible_i = vide_i - 1;
|
|
|
|
|
|
|
|
|
|
/* Vérifier les murs */
|
|
|
|
|
if (cible_i < 0 || cible_i >= NB_LIGNES ||
|
|
|
|
|
cible_j < 0 || cible_j >= NB_COLS) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Echanger */
|
|
|
|
|
temp = grille[cible_i][cible_j];
|
|
|
|
|
grille[cible_i][cible_j] = 0;
|
|
|
|
|
grille[vide_i][vide_j] = temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void melanger_plateau(int grille[NB_LIGNES][NB_COLS]) {
|
|
|
|
|
int i;
|
|
|
|
|
int touche_aleatoire;
|
|
|
|
|
int direction;
|
|
|
|
|
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
|
|
|
|
|
/* On fais 500 mouvements aléatoires */
|
|
|
|
|
for (i = 0; i < 500; i++) {
|
|
|
|
|
/* tirage d'un nombre entre 0 et 3 */
|
|
|
|
|
direction = rand() % 4;
|
|
|
|
|
|
|
|
|
|
/* On convertit ce nombre en code de touche fléchée */
|
|
|
|
|
/* Rappel : FL_GAUCHE vaut 65361 donc 65361 + 0, 1, 2 ou 3 donne les 4 flèches */
|
|
|
|
|
touche_aleatoire = FL_GAUCHE + direction;
|
|
|
|
|
|
|
|
|
|
if (i < 5) {
|
|
|
|
|
printf("DEBUG: Coup %d, Direction %d, Touche %d\n", i, direction, touche_aleatoire);
|
|
|
|
|
}
|
|
|
|
|
/* On joue le coup */
|
|
|
|
|
deplacer(grille, touche_aleatoire);
|
|
|
|
|
}
|
|
|
|
|
printf("---DEBUG: Fin du mélange ! ---\n");
|
|
|
|
|
}
|