grid.cpp

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

CPP
1,733 行
字号
        else        {            main->setX (getQueuePositionX () + getAmoebaSize () * 3);            main->setY (getQueuePositionY () - getAmoebaSize () - lastPairOffsetX);            satellite->setX (getQueuePositionX () + getAmoebaSize () * 2);            satellite->setY (getQueuePositionY () - getAmoebaSize () - lastPairOffsetX);        }    }    // And set up the falling pair structure for when they fall.    FallingPair fallingPair;    fallingPair.main.amoeba = main;    fallingPair.main.x = 3;    fallingPair.main.y = k_FirstVisibleHeight - 2;    fallingPair.satellite.amoeba = satellite;    fallingPair.satellite.x = 3;    fallingPair.satellite.y = k_FirstVisibleHeight - 3;    fallingPair.verticalOffset = 0;    fallingPair.fallingTime = k_DefaultFallingTime;    m_Queue.push_back (fallingPair);    // Request a new pair if we are out of inactive pairs.    if ( m_InactiveAmoebas.empty () )    {        m_Generator->generate ();    }}////// \brief Gets the side of the queue.////// \return The side of the grid where the queue is located at.///Grid::QueueSideGrid::getQueueSide (void) const{    return m_QueueSide;}////// \brief Gets the queue's top-left corner X position.////// \return The X position of queue's top-left corner.///uint16_tGrid::getQueuePositionX (void) const{    return m_QueuePositionX;}////// \brief Gets the queue's top-left corner Y position.////// \return The Y position of queue's top-left corner.///uint16_tGrid::getQueuePositionY (void) const{    return m_QueuePositionY;}////// \brief Gets the current grid's state.////// \return The grid's state.///GridStatusGrid::getState (void) const{    std::vector<Amoeba::Colour> state (k_GridWidth * k_GridHeight,                                       Amoeba::ColourNone);    assert (state.size () == m_Grid.size () &&            "The state's size and the grid's size doesn't match.");    for ( uint16_t currentAmoeba = 0 ; currentAmoeba < state.size () ;          ++currentAmoeba )    {        Amoeba *amoeba = m_Grid.at (currentAmoeba);        if ( 0 != amoeba )        {            state.at (currentAmoeba) = amoeba->getColour ();        }    }    return GridStatus (state);}////// \brief Gets the waiting ghost's top-left corner X position.////// \return The X position of the waitings ghost's top-left corner.///uint16_tGrid::getWaitingGhostPositionX (void) const{    return m_WaitingGhostPositionX;}////// \brief Gets the waiting ghost's top-left corner Y position.////// \return The Y position of the waitings ghost's top-left corner.///uint16_tGrid::getWaitingGhostPositionY (void) const{    return m_WaitingGhostPositionY;}////// \brief Checks if an amoeba at a give position is the same colour of another.////// \param x The grid's X position of the amoeba to check its colour./// \param y The grid's Y position of the amoeba to check its colour./// \param amoeba The amoeba to check if its colour is the same as the///               amoeba at \a x i \a y./// \return \a true if the amoeba at \a x and \a y if of the same colour as///         \a amoeba.///boolGrid::isOfSameColour (int16_t x, int16_t y, Amoeba *amoeba) const{    assert ( 0 != amoeba && "Tried to check the colour of a null amoeba.");    Amoeba *amoebaInGrid = getAmoebaAt (x, y);    if ( 0 != amoebaInGrid )    {        return amoebaInGrid->getColour () == amoeba->getColour ();    }    return false;}////// \brief Tells if the queue is moving and needs to be updated.////// \return \a true if the amoebas in the queue are moving and needs to///         be updated, \a false otherwise.///inline boolGrid::isQueueMoving (void) const{    return m_QueueIsMoving;}////// \brief Tells if the satellite amoeba is rotating.////// \return \a true if the satellite amoeba is rotating.///         \a false otherwise.///inline boolGrid::isSatelliteRotating (void) const{    return 0 != static_cast<int16_t> (m_DegreesUntilRotationDone);}////// \brief Increments the number of ghosts amoebas.////// \param amount The amount to increment the number of ghost amoebas///               that are waiting to fall into the grid.///voidGrid::incrementNumberOfWaitingGhosts (uint8_t amount){    setNumberOfWaitingGhosts (getNumberOfWaitingGhosts () + amount);    updateWaitingGhosts ();}////// \brief Sets the waiting ghost as falling amoebas.///voidGrid::makeGhostsFall (void){    std::vector<uint8_t> emptyPositions (k_GridWidth);    uint8_t remainingGhosts = getNumberOfWaitingGhosts ();    int8_t remainingPositions = 0;    int8_t y = -1;    while ( remainingGhosts > 0 )    {        if ( 0 == remainingPositions )        {            for ( uint8_t currentPosition = 0 ; currentPosition < k_GridWidth ;                  ++currentPosition )            {                emptyPositions.at (currentPosition) = currentPosition;            }            y -= 1;            remainingPositions = k_GridWidth;        }        uint8_t horizontalPositionIndex = rand () % remainingPositions;        int8_t x = emptyPositions.at (horizontalPositionIndex);        emptyPositions.at (horizontalPositionIndex) =        emptyPositions.at (remainingPositions - 1);        --remainingPositions;        FallingAmoeba ghostAmoeba;        ghostAmoeba.amoeba = new Amoeba (Amoeba::ColourGhost);        ghostAmoeba.y = y;        ghostAmoeba.x = x;        m_ActiveAmoebas.push_back (ghostAmoeba.amoeba);        m_FloatingAmoebas.push_back (ghostAmoeba);        --remainingGhosts;    }    setNumberOfWaitingGhosts (0);    updateWaitingGhosts ();}////// \brief Makes a group of equal colour amoebas recursively.////// \param group The group vector to save the members found to./// \param ghosts The ghosts amoebas that are in contact with the group./// \param x The X position to look for a posible group candidate./// \param y The Y position to look for a possible group candidate./// \param visitedPositions The set of the already visited positions by the///                         algorithm. Initially should be empty./// \param colour The colour the amoeba should be to be part of the group./// \param initialAmoeba Tells if the amoeba \a x and \a y should be the///        first amoeba of the group.///voidGrid::makeGroup (std::vector<FallingAmoeba> &group,                 std::vector<FallingAmoeba> &ghosts, int16_t x, int16_t y,                 std::set<std::pair<int16_t, int16_t> > &visitedPositions,                 Amoeba::Colour colour, bool initialAmoeba){    // Only check this possition if we didn't visited it yet.    if ( visitedPositions.end () ==            visitedPositions.find (std::make_pair (x, y)) )    {        // If there's any amoeba at the given position, check if is a member        // of the group.        Amoeba *amoeba = getAmoebaAt (x, y);        if ( 0 != amoeba && !amoeba->isDying () )        {            // The initial amoeba is always a member of the group, so            // we get its colour and make it the group's colour.            if ( initialAmoeba )            {                colour = amoeba->getColour ();            }            // Can't make groups of ghost amoebas. Set it to the            // ghosts groups and don't look any further.            if ( amoeba->getColour () == Amoeba::ColourGhost )            {                FallingAmoeba ghost;                ghost.amoeba = amoeba;                ghost.x = x;                ghost.y = y;                ghosts.push_back (ghost);                // Add the current position to the already visited positions.                visitedPositions.insert (std::make_pair (x, y));            }            else if ( colour == amoeba->getColour () )            {                // Once a member is identified, add it to the group.                FallingAmoeba groupMember;                groupMember.amoeba = amoeba;                groupMember.x = x;                groupMember.y = y;                group.push_back (groupMember);                // Add the current position to the already visited positions.                visitedPositions.insert (std::make_pair (x, y));                // Check neighbours.                makeGroup (group, ghosts, x, y - 1, visitedPositions, colour, false);                makeGroup (group, ghosts, x + 1, y, visitedPositions, colour, false);                makeGroup (group, ghosts, x, y + 1, visitedPositions, colour, false);                makeGroup (group, ghosts, x - 1, y, visitedPositions, colour, false);            }        }    }}////// \brief Checks all grid's position and tries to make chains.////// A chain is nothing more than 4 or more amoebas joined in a single/// group.///voidGrid::makeChain (void){    // First delete all groups.    for ( int16_t row = 0 ; row < k_GridHeight ; ++row )    {        for ( int16_t column = 0 ; column < k_GridWidth ; ++column )        {            std::set<std::pair<int16_t, int16_t> > visitedPositions;            std::vector<FallingAmoeba> group;            std::vector<FallingAmoeba> ghosts;            makeGroup (group, ghosts, column, row, visitedPositions);            if ( 4 <= group.size () )            {                int16_t meanXPosition = 0;                int16_t meanYPosition = 0;                m_DyingTime = k_DefaultDyingTime;                m_BlinkTime = k_DefaultBlinkTime;                for (std::vector<FallingAmoeba>::iterator currentAmoeba =                        group.begin () ;                     currentAmoeba != group.end () ; ++currentAmoeba )                {                    meanXPosition += currentAmoeba->x;                    meanYPosition += currentAmoeba->y;                    currentAmoeba->amoeba->setDying (true);                    m_DyingAmoebas.push_back (*currentAmoeba);                }                for (std::vector<FallingAmoeba>::iterator currentGhost =                        ghosts.begin () ;                     currentGhost != ghosts.end () ; ++currentGhost )                {                    currentGhost->amoeba->setDying (true);                    m_DyingAmoebas.push_back (*currentGhost);                }                meanXPosition /= group.size ();                meanYPosition /= group.size ();                addChainLabel (getCurrentStepChain () + 1,                               getGridPositionX () +                               meanXPosition * getAmoebaSize () +                               getAmoebaSize () / 2,                               getGridPositionY () +                               (meanYPosition - k_FirstVisibleHeight) *                               getAmoebaSize () + getAmoebaSize () / 2);            }        }    }    findFloatingAmoebas ();    // If there's no floating amoebas, set up the falling pair.    if ( m_FloatingAmoebas.empty () && m_DyingAmoebas.empty () )    {        if ( getNumberOfWaitingGhosts () == 0 )        {                // If the positions above the grid of the 3rd column (from                // the right) is occuped by an amoeba, then the grid is                // considered filled.  If any amobea is sitting over the grid in                // any other column, this is not considered filled.                if ( 0 != getAmoebaAt (3, k_FirstVisibleHeight - 1) )                {                    markAsFilled ();                }                else

⌨️ 快捷键说明

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