void PuzzlePosition::moveZeroDown() { int x, y; findZeroPosition(x, y); if (!zeroIsOverLowerEdge(y)) { findZeroPosition(x, y); _puzzle[y][x] = _puzzle[y + 1][x]; _puzzle[y + 1][x] = 0; } }
void PuzzlePosition::moveZeroLeft() { int x, y; findZeroPosition(x, y); if (!zeroIsOverLeftEdge(x)) { findZeroPosition(x, y); _puzzle[y][x] = _puzzle[y][x - 1]; _puzzle[y][x - 1] = 0; } }
void PuzzlePosition::moveZeroUp() { int x, y; findZeroPosition(x, y); if (!zeroIsOverUpperEdge(y)) { _puzzle[y][x] = _puzzle[y - 1][x]; _puzzle[y - 1][x] = 0; } }
void PuzzlePosition::moveZeroRight() { int x, y; findZeroPosition(x, y); if (!zeroIsOverRightEdge(x)) { _puzzle[y][x] = _puzzle[y][x + 1]; _puzzle[y][x + 1] = 0; } }
int aStar(tNodeQueue *root, int heuristica, int *answer) { tQueue *a = malloc(sizeof(tQueue)); tQueue *f = malloc(sizeof(tQueue)); *answer = -1; printf("%d\n",a); initialize(a); initialize(f); insertAtEnd(a, root); while (!isEmpty(a)) { tNodeQueue *v = removeMin(a); insertAtEnd(f, v); if (isFinalState(v->elem->matrix)) { *answer = v->elem->f; return 1; } else { tNodeQueue *top = malloc(sizeof (tNodeQueue)); tNodeBoard *topBoard = malloc(sizeof (tNodeBoard)); top->elem = topBoard; tNodeQueue *right = malloc(sizeof (tNodeQueue)); tNodeBoard *rightBoard = malloc(sizeof (tNodeBoard)); right->elem = rightBoard; tNodeQueue *bottom = malloc(sizeof (tNodeQueue)); tNodeBoard *bottomBoard = malloc(sizeof (tNodeBoard)); bottom->elem = bottomBoard; tNodeQueue *left = malloc(sizeof (tNodeQueue)); tNodeBoard *leftBoard = malloc(sizeof (tNodeBoard)); left->elem = leftBoard; int i; int j; findZeroPosition(v->elem->matrix, &i, &j); if (moveTop(v->elem->matrix, top->elem->matrix, i, j)) { validSuccessor(a, f, v, top, heuristica); } else { free(topBoard); free(top); } if (moveRight(v->elem->matrix, right->elem->matrix, i, j)) { validSuccessor(a, f, v, right, heuristica); } else { free(rightBoard); free(right); } if (moveBottom(v->elem->matrix, bottom->elem->matrix, i, j)) { validSuccessor(a, f, v, bottom, heuristica); } else { free(bottomBoard); free(bottom); } if (moveLeft(v->elem->matrix, left->elem->matrix, i, j)) { validSuccessor(a, f, v, left, heuristica); } else { free(leftBoard); free(left); } } } if (isEmpty(a)) { return 0; } }