Example #1
0
/**
    Print solution in this format:
        moveNumber. [movedTile] MOVE_DIRECTION
*/
void printSolution(struct GameState *queue)
{
    int i, j;
    struct GameState *goal = queue;
    int moves[queue->distance];
    int movedTiles[queue->distance];
    /* fill arays of moves and tiles, which moved - we'll have to go through them
       from the end */
    for (i = 0; goal != NULL; i++)
    {
        moves[i] = getLastMove(goal);
        movedTiles[i] = getMovedTile(goal);
        goal = goal->prev;
    }
    char *move;
    for (i = queue->distance - 1, j = 1; i >= 0; i--, j++)
    {
        /* moves are named after moving of space (empty tile), but we want to know, 
           which direction moved tile to swap with */
        if (moves[i] == MOVE_LEFT)
        {
            move = "RIGHT";
        }
        if (moves[i] == MOVE_UP)
        {
            move = "DOWN";
        }
        if (moves[i] == MOVE_RIGHT)
        {
            move = "LEFT";
        }
        if (moves[i] == MOVE_DOWN)
        {
            move = "UP";
        }
        printf("%d: [%d] %s\n", j, movedTiles[i], move);
    }
    printf("GOAL\n");
    system("PAUSE");
}
/* Updates the observations and the mc filter if the robot is on the move or 
 * changed it's position since last update 
 */
void Localization::update() {
  if (!isDestinationSet() && !isGlobalCameraAvailable() ){
    //cout << "updating observations" << endl;
    updateObservations();
  }

  //if ( obs.size() > 0 ) 
  //  displayObservationSummary();
  
  Move lastMove = getLastMove();
  //if ( lastMove.getX() + lastMove.getY() + lastMove.getTheta() != 0 ) 
  //  cout << "lastMove(" << lastMove.getX() << "," << lastMove.getY() << "," << lastMove.getTheta() << ")" << endl ;
  if ( isGlobalCameraAvailable() ){
    if ( lastMove.getX() + lastMove.getY() + lastMove.getTheta() != 0 ){
      currentPos.moveRelative(lastMove);
    }
  }
  else if (obs.size() > 0 || lastMove.getX() + lastMove.getY() + lastMove.getTheta() != 0) {
    cout << "applying filter" << endl;
    mc->updateFilter(lastMove, obs);
  }
}
Example #3
0
/**
    Searches for solution
*/
int solveFifteen()
{
    struct GameState *queue;
    int i;
    
    while (notEmptyPQ())
    {
        queue = getQueueTop();
        
        if (queue->manhattanDistance == 0)
        {
            printSolution(queue);
            
            for (i = 0; i < rows; i++)
            {
                free(queue->tilesPosition[i]);
            }
            free(queue->tilesPosition);
            free(queue->prev);
            queue->prev = NULL;
            
            /* deallocate memory */
            int pocet = 0;
            while (notEmptyPQ())
            {
                pocet++;
                free(queue);
                queue = NULL;
                queue = getQueueTop();
                for (i = 0; i < rows; i++)
                {
                    free(queue->tilesPosition[i]);
                }
                free(queue->tilesPosition);
                free(queue->prev);
                queue->prev = NULL;
            }
            printf("Ve fronte bylo %d\n", pocet);
            system("PAUSE");
            return 0;
        }
        else
        {
            int lastMove = getLastMove(queue);
            if (canMoveLeft(queue) && lastMove != MOVE_RIGHT)
            {
                struct GameState *s = createNewState(queue);
                if (s == NULL)
                {
                    return OUT_OF_MEMORY;    
                }
                moveLeft(s);
                s->manhattanDistance = getManhattanDistance(*s);
                insertPQ(s);
            }
            if (canMoveRight(queue) && lastMove != MOVE_LEFT)
            {
                struct GameState *s = createNewState(queue);
                if (s == NULL)
                {
                    return OUT_OF_MEMORY;    
                }
                moveRight(s);
                s->manhattanDistance = getManhattanDistance(*s);
                insertPQ(s);
            }
            if (canMoveUp(queue) && lastMove != MOVE_DOWN)
            {
                struct GameState *s = createNewState(queue);
                if (s == NULL)
                {
                    return OUT_OF_MEMORY;    
                }
                moveUp(s);
                s->manhattanDistance = getManhattanDistance(*s);
                insertPQ(s);
            }
            if (canMoveDown(queue) && lastMove != MOVE_UP)
            {
                struct GameState *s = createNewState(queue);
                if (s == NULL)
                {
                    return OUT_OF_MEMORY;    
                }
                moveDown(s);
                s->manhattanDistance = getManhattanDistance(*s);
                insertPQ(s);
            }
        }
        
    }
    return 1;
}
Example #4
0
bool Board::isValidNewMove(Coords& move) {
    Coords lastMove = getLastMove();
    if (lastMove.isNotAMove()) return true;
    return this->isValidMove(lastMove, move);
}