Pastebin
Retrouvez, créez et partagez vos snippets en temps réel.
Rechercher un Pastebin
Aucun paste trouvé.
Créer un paste
Pastebin
Blog
t
#include "resolver.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #define MOVE_UP 'U' #define MOVE_DOWN 'D' #define MOVE_LEFT 'L' #define MOVE_RIGHT 'R' #define MOVE_ROTATE_R 'R' #define MOVE_ROTATE_L 'L' char deduce_roll_move(const GameCore* fromState, const GameCore* toState) { if (toState->m_player.row < fromState->m_player.row) return MOVE_UP; if (toState->m_player.row > fromState->m_player.row) return MOVE_DOWN; if (toState->m_player.col < fromState->m_player.col) return MOVE_LEFT; if (toState->m_player.col > fromState->m_player.col) return MOVE_RIGHT; return 0; } char deduce_rotation_move(const GameCore* fromState, const GameCore* toState) { if (fromState->m_player.row != toState->m_player.row || fromState->m_player.col != toState->m_player.col) { return 0; } if (fromState->m_player.dice.top == toState->m_player.dice.top && fromState->m_player.dice.front == toState->m_player.dice.front) { return 0; } float diff = toState->m_player.orientation - fromState->m_player.orientation; if (diff < 0) diff += 360.0f; #define EPSILON 0.001f if (diff > 90.0f - EPSILON && diff < 90.0f + EPSILON) return MOVE_ROTATE_R; if (diff > 270.0f - EPSILON && diff < 270.0f + EPSILON) return MOVE_ROTATE_L; return 0; } void Resolver_printPath(const GameHashmap* hashmap, int goalIndex) { char* path = (char*)malloc(hashmap->m_size + 1); if (!path) { fprintf(stderr, "Erreur d'allocation pour le chemin.\n"); return; } int pathLength = 0; int currentIndex = goalIndex; while (currentIndex > 0) { GameHashmapEntry* currentEntry = &hashmap->m_entries[currentIndex]; int previousIndex = currentEntry->prevStateIndex; if (previousIndex == -1) { break; } GameHashmapEntry* previousEntry = &hashmap->m_entries[previousIndex]; char move = 0; if (currentEntry->state.m_player.row != previousEntry->state.m_player.row || currentEntry->state.m_player.col != previousEntry->state.m_player.col) { move = deduce_roll_move(&previousEntry->state, ¤tEntry->state); } else { move = deduce_rotation_move(&previousEntry->state, ¤tEntry->state); } if (move != 0) { path[pathLength++] = move; } else { fprintf(stderr, "Erreur: Impossible de déduire le mouvement entre l'index %d et %d.\n", previousIndex, currentIndex); } currentIndex = previousIndex; } printf("\n--- Solution trouvée (Longueur: %d) ---\n", pathLength); printf("Chemin: "); for (int i = pathLength - 1; i >= 0; i--) { printf("%c", path[i]); if (i > 0) { printf(" -> "); } } printf("\n--------------------------------------\n"); free(path); }
Créé il y a 1 mois.