示例#1
0
文件: Table.cpp 项目: bapi23/Checkers
bool Table::shouldFinishGame(PieceColor pieceColor)
{
    if(isGameFinished(pieceColor) || getAllPossibleMoves(m_array, getOppositeColor(pieceColor)).empty())
    {
        return true;
    }
    return false;
}
示例#2
0
void AIMediocrePlayer::requestMove(PieceColor moveType)
{
    assert(m_array);
    auto gameLogic = m_gameLogic.lock();

    std::vector<int> scores;
    auto lockedGameLogic = m_gameLogic.lock();
    std::vector<PieceMove> moves = lockedGameLogic->getAllPossibleMoves(*m_array, moveType);

    std::vector<std::future<int>> futures;
    for(const auto& move: moves)
    {
        Array copyOfArray = *m_array;
        copyOfArray.move(move.source().m_row, move.source().m_col, move.destination().m_row, move.destination().m_col);
        for(const auto& pos: move.capturedPieces())
        {
            copyOfArray.erase(pos.m_row, pos.m_col);
        }

        int newScore = move.numberOfCapturedPieces();

        auto score = std::async(std::launch::async,
                                &minMaxWithAlphaBetaPruning,
                                lockedGameLogic,
                                copyOfArray,
                                m_maxDepth,
                                newScore,
                                getOppositeColor(moveType),
                                moveType,
                                gMinusInfinity,
                                gPlusInfinity);
        futures.push_back(std::move(score));
    }

    for(auto& future: futures)
    {
        auto result = future.get();
        scores.push_back(result);
    }

    auto it = std::max_element(scores.begin(), scores.end());

    PieceMove move = moves.at(std::distance(scores.begin(), it));
    std::vector<Position> subMoves{move.subMoves().begin()+1, move.subMoves().end()};

    lockedGameLogic->movePiece(move.source(), subMoves, moveType);
}
示例#3
0
bool CheckerBoard::isSimpleKillTurn(BoardCoord oldCoord, BoardCoord newCoord)
{
        if(isChecker(newCoord)) return false;

        boost::shared_ptr<Checker> current = getChecker(oldCoord);

        int dY = newCoord.y-oldCoord.y;
        int dX = newCoord.x-oldCoord.x;
        int possible_dY;
        if(!(((dX == -2)||(dX == 2))&&(((dY == -2)||(dY == 2))))) return false;


        int enemyPos_X = (newCoord.x+oldCoord.x)/2;
        int enemyPos_Y = (newCoord.y+oldCoord.y)/2;
        if(!isChecker(BoardCoord(enemyPos_X,enemyPos_Y))) return false;

        boost::shared_ptr<Checker> enemy = getChecker(BoardCoord(enemyPos_X, enemyPos_Y));
        if(enemy->getColor() != getOppositeColor(current->getColor())) return false;

        return true;
}
示例#4
0
void AIBestPlayer::requestMove(PieceColor moveType)
{
    assert(m_array);
    auto gameLogic = m_gameLogic.lock();


    std::vector<int> scores;
    auto lockedGameLogic = m_gameLogic.lock();
    std::vector<PieceMove> moves = lockedGameLogic->getAllPossibleMoves(*m_array, moveType);

    std::experimental::optional<std::pair<std::vector<Position>, int>> foundMove = bestMoves::findMove(*m_array, moveType);

    if(!foundMove || (foundMove && foundMove->second < m_maxDepth))
    {
        std::cout << "computation of best move" << std::endl;
        std::vector<std::future<int>> futures;
        for(const auto& move: moves)
        {
            Array copyOfArray = *m_array;
            copyOfArray.move(move.source().m_row, move.source().m_col, move.destination().m_row, move.destination().m_col);
            for(const auto& pos: move.capturedPieces())
            {
                copyOfArray.erase(pos.m_row, pos.m_col);
            }

            int newScore = move.numberOfCapturedPieces();

            auto score = std::async(std::launch::async,
                                    &minMaxWithAlphaBetaPruning,
                                    lockedGameLogic,
                                    copyOfArray,
                                    m_maxDepth,
                                    newScore,
                                    getOppositeColor(moveType),
                                    moveType,
                                    gMinusInfinity,
                                    gPlusInfinity);
            futures.push_back(std::move(score));
        }

        for(auto& future: futures)
        {
            auto result = future.get();
            scores.push_back(result);
        }

        auto it = std::max_element(scores.begin(), scores.end());

        PieceMove move = moves.at(std::distance(scores.begin(), it));
        std::vector<Position> subMoves{move.subMoves().begin()+1, move.subMoves().end()};
        lockedGameLogic->movePiece(move.source(), subMoves, moveType);
    }
    else
    {
        std::vector<Position>& positions = foundMove->first;
        for(const auto& move: moves)
        {
            if(positions == move.subMoves())
            {
                std::cout << "used saved best move" << std::endl;
                std::vector<Position> subMoves{move.subMoves().begin()+1, move.subMoves().end()};
                lockedGameLogic->movePiece(move.source(), subMoves, moveType);
            }
        }
    }
}