Exemplo n.º 1
0
bool MarbleGraphics::makeMove(const Move move){
    GImage * movingMarble = marbles[move.startRow][move.startCol];
    int jumpedRow = move.startRow+(move.endRow-move.startRow)/2;
    int jumpedCol = move.startCol+(move.endCol-move.startCol)/2;
    GImage * jumpedMarble = marbles[jumpedRow][jumpedCol];
    if (movingMarble == NULL || jumpedMarble == NULL){
        return false;
    }

    //Move empty spot from end to start of move
    GOval* endSpot = spaces[move.endRow][move.endCol];
    double tempX = endSpot->getX();
    double tempY = endSpot->getY();
    endSpot->setLocation(movingMarble->getX(), movingMarble->getY());
    spaces[move.startRow][move.startCol] = endSpot;
    spaces[move.endRow][move.endCol] = NULL;
    spaceCoords[endSpot] = Coord(move.startRow, move.startCol);

    //Move image at start location to image at end location
    movingMarble->setLocation(tempX, tempY);
    marbles[move.endRow][move.endCol] = movingMarble;
    marbles[move.startRow][move.startCol] = NULL;
    marbleCoords[movingMarble] = Coord(move.endRow, move.endCol);

    //Delete marble for jumped space and create empty space there instead
    marbles[jumpedRow][jumpedCol] = NULL;
    marbleCoords.remove(jumpedMarble);
    remove(jumpedMarble);
    delete jumpedMarble;
    GOval* jumpedSpace = new GOval(kMarbleDimension, kMarbleDimension);
    spaces[jumpedRow][jumpedCol] = jumpedSpace;
    spaceCoords[jumpedSpace] = Coord(jumpedRow, jumpedCol);
    add(jumpedSpace, jumpedCol*(kMarbleDimension+kMarbleSpacingWidth)+kMarbleSpacingWidth,
        jumpedRow*(kMarbleDimension+kMarbleSpacingWidth)+kMarbleSpacingWidth);

    return true;
}