aiplayer.cpp

来自「Source code (C++) of the Amoebax game fo」· C++ 代码 · 共 689 行 · 第 1/2 页

CPP
689
字号
////// \brief Computes the score of the next move.///voidAIPlayer::computeNextMove (void){    switch ( getWhichPairToCheck () )    {        case CheckingCurrentFallingPair:            checkCurrentFallingPair ();            break;        case CheckingNextFallingPair:            checkNextFallingPair ();            break;        case CheckingFollowingFallingPair:            checkFollowingFallingPair ();            break;        default:            setPairToCheck (CheckingCurrentFallingPair);            break;    }}////// \brief Gets the found best move.////// \return The best move found.///inline AIPlayer::MoveAIPlayer::getBestMove (void) const{    return m_BestMove;}////// \brief Gets the score of the best move.////// \return The score of the best move.///inline int32_tAIPlayer::getBestScore (void) const{    return m_BestScore;}////// \brief Tells if the player has the final move computed.////// \return \a true if the player has the move computed, \a false otherwise.///inline boolAIPlayer::hasFinalMove (void) const{    return m_HaveFinalMove;}////// \brief Tells if the pair is already at the final position.////// \return \a true if the pair is at its final position, \a false otherwise.///inline boolAIPlayer::hasPairAtFinalPosition (void) const{    return m_FallingPairAtPosition;}////// \brief Initializes an state to a left rotation.////// \param state The state to initialize./// \param gridState The initial grid state to use to compute the score./// \param parentScore The score of the parent state.///voidAIPlayer::initializeState (State &state, const GridStatus &gridState,                           int32_t parentScore) const{    state.move.rotation = RotationLeft;    state.move.main.x = 0;    state.move.satellite.x = -1;    state.currentX = 0;    state.endX = Grid::k_GridWidth - 1;    state.gridState = gridState;    GridStatus::PositionResult result;    state.gridState.checkPositions (state.move.main, state.move.satellite,                                    result);    state.score = parentScore + computeScore (result);}////// \brief Tells if the player is still for a pair to fall into the grid.////// \return \a true if the player is waiting, \a false otherwise.///inline boolAIPlayer::isWaitingNextPair (void) const{    return m_WaitingNextPair;}voidAIPlayer::joyMotion (uint8_t joystick, uint8_t axis, int16_t value){    // Computer players don't have joystick. Nothing to do.}voidAIPlayer::joyDown (uint8_t joystick, uint8_t button){    // Computer players don't have joystick. Nothing to do.}voidAIPlayer::joyUp (uint8_t joystick, uint8_t button){    // Computer players don't have joystick. Nothing to do.}#if !defined (IS_GP2X_HOST)|| defined (__SYMBIAN32__)voidAIPlayer::keyDown (uint32_t key){    // Computer players don't have keyboard. Nothing to do.}voidAIPlayer::keyUp (uint32_t key){    // Computer players don't have keyboard. Nothing to do.}#endif // !IS_GP2X_HOSTvoidAIPlayer::loadOptions (void){    // Nothing to do.}////// \brief Moves Grid's falling pair to the left.///voidAIPlayer::moveLeft (void){    if ( canMove () )    {        getGrid ()->moveLeft ();        updateTimeOfNextMove ();    }}////// \brief Moves the falling pair an step closer to its final position.///voidAIPlayer::movePairToPosition (void){    Move currentPosition;    currentPosition.main = getGrid ()->getFallingMainAmoeba ();    currentPosition.satellite = getGrid ()->getFallingSatelliteAmoeba ();    if ( shouldSatelliteBeVisible () && currentPosition.satellite.y >= Grid::k_FirstVisibleHeight ||         !shouldSatelliteBeVisible () && currentPosition.main.y >= Grid::k_FirstVisibleHeight )    {        if ( RotationLeft == getBestMove ().rotation &&             currentPosition.main.x <= currentPosition.satellite.x )        {            rotateCounterClockwise ();        }        else if ( RotationRight == getBestMove ().rotation &&                  currentPosition.main.x >= currentPosition.satellite.x )        {            rotateClockwise ();        }        else if ( RotationTop == getBestMove ().rotation &&                  currentPosition.main.y <= currentPosition.satellite.y ||                  RotationBottom == getBestMove ().rotation &&                  currentPosition.main.y >= currentPosition.satellite.y )        {            rotateCounterClockwise ();        }        else        {            if ( getBestMove ().main.x == currentPosition.main.x &&                 getBestMove ().satellite.x == currentPosition.satellite.x )            {                pairIsAtPosition ();            }            else if ( getBestMove ().main.x < currentPosition.main.x )            {                moveLeft ();            }            else            {                moveRight ();            }        }    }}////// \brief Moves Grid's falling pair to the right.///voidAIPlayer::moveRight (void){    if ( canMove () )    {        getGrid ()->moveRight ();        updateTimeOfNextMove ();    }}////// \brief Tells the player that the pair is already at its position.///inline voidAIPlayer::pairIsAtPosition (void){    m_FallingPairAtPosition = true;}////// \brief Rotates Grid's falling pair clockwise.///voidAIPlayer::rotateClockwise (void){    if ( canMove () )    {        getGrid ()->rotateClockwise ();        updateTimeOfNextMove ();    }}////// \brief Rotates Grid's falling pair counter clockwise.///voidAIPlayer::rotateCounterClockwise (void){    if ( canMove () )    {        getGrid ()->rotateCounterClockwise ();        updateTimeOfNextMove ();    }}////// \brief Sets the best move and its score.////// \param bestMove The best move to set./// \param bestScore The score of \p bestMove.///inline voidAIPlayer::setBestMove (const Move &bestMove, int32_t bestScore){    m_BestMove = bestMove;    m_BestScore = bestScore;}////// \brief Tells to the player that it has the final move.///inline voidAIPlayer::setHasFinalMove (void){    m_HaveFinalMove = true;}////// \brief Sets the pair to check its moves.////// \param pairToCheck The pair to check its moves.///inline voidAIPlayer::setPairToCheck (Checking pairToCheck){    m_PairToCheck = pairToCheck;}////// \brief Sets the player into waiting the next falling pair mode.///inline voidAIPlayer::setToWaitingNextPair (void){    m_WaitingNextPair = true;}voidAIPlayer::update (uint32_t elapsedTime){    IPlayer::update (elapsedTime);    if ( isWaitingNextPair () )    {        checkIfPairIsAvailable ();    }    else if ( !hasFinalMove () )    {        computeNextMove ();    }    else if ( !hasPairAtFinalPosition () && canMove () )    {        movePairToPosition ();    }    else if ( hasPairAtFinalPosition () && canMove () )    {        if ( shouldThePairFall () )        {            getGrid ()->setMaxFallingSpeed ();        }        setToWaitingNextPair ();    }}////// \brief Updates the time of the next move.///voidAIPlayer::updateTimeOfNextMove (void){    m_TimeOfNextMove = FrameManager::getCurrentTime () +                       m_TimeToWaitForNextMove -                       ((rand () % (m_TimeDeviation << 1)) -                       m_TimeDeviation);}////// \brief Tells if the state is at the last position.////// \return \a true if the state is at the last position, \a false otherwise.///boolAIPlayer::State::isAtLastPosition (void) const{    return RotationBottom == move.rotation && currentX >= endX;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?