Esempio n. 1
0
void Game::saveToDatabase() const
{
    time_t now = time(0);
    struct tm tstruct;
    char buff[80];
    localtime_s(&tstruct, &now);

    strftime(buff, sizeof(buff), "%Y-%m-%d", &tstruct);
    string strDate = buff;

    strftime(buff, sizeof(buff), "%X", &tstruct);
    string strTime = buff;

    string strMovesString;
    string strPromotionsString;
    for (Move m : vecPreviousMoves)
    {
        if (m.isPawnPromotionMove())
        {
            strPromotionsString += m.getInitialSquare().getPieceAtSquare()->getAbbreviation();
        }
        strMovesString += to_string(m.getInitialSquare().getRow());
        strMovesString += to_string(m.getInitialSquare().getCol());
        strMovesString += to_string(m.getFinalSquare().getRow());
        strMovesString += to_string(m.getFinalSquare().getCol());
    }

    DatabaseManager dbManager;
    dbManager.connectToDatabase();
    dbManager.addGame(strDate, strTime, ptrWhitePlayer->getName(), ptrBlackPlayer->getName(), strMovesString, strPromotionsString);
    dbManager.disconnectFromDatabase();
}
Esempio n. 2
0
Game::Game(int intGameID)
{
    DatabaseManager dbManager;
    dbManager.connectToDatabase();
    vector<string> vecGame = dbManager.getGame(intGameID);
    dbManager.disconnectFromDatabase();

    ptrWhitePlayer = new Player(vecGame.at(3), WHITE);
    ptrBlackPlayer = new Player(vecGame.at(4), BLACK);

    int intPromotionsStringIndex = 0;
    for (unsigned int i = 0; i < vecGame.at(5).size(); i += 4)
    {
        int intInitialRow = static_cast<int>(vecGame.at(5).at(i) - '0');
        int intInitialCol = static_cast<int>(vecGame.at(5).at(i + 1) - '0');
        int intFinalRow = static_cast<int>(vecGame.at(5).at(i + 2) - '0');
        int intFinalCol = static_cast<int>(vecGame.at(5).at(i + 3) - '0');
        Move aMove(currentBoard.getSquare(intInitialRow, intInitialCol), currentBoard.getSquare(intFinalRow, intFinalCol));

        if (currentBoard.getPieceAt(intInitialRow, intInitialCol)->getType() == PAWN && (intFinalRow == 0 || intFinalRow == 7))
        {
            aMove.setIsPawnPromotionMove(true);
        }
        makeMove(aMove);
        generatePGNString();

        if (aMove.isPawnPromotionMove())
        {
            char charPromotionAbbreviation = vecGame.at(6).at(intPromotionsStringIndex);
            PieceType promotionType;
            if (toupper(charPromotionAbbreviation) == 'N')
            {
                promotionType = KNIGHT;
            }
            else if (toupper(charPromotionAbbreviation) == 'B')
            {
                promotionType = BISHOP;
            }
            else if (toupper(charPromotionAbbreviation) == 'R')
            {
                promotionType = ROOK;
            }
            else
            {
                promotionType = QUEEN;
            }
            promotePawn(intFinalRow, intFinalCol, promotionType);
            intPromotionsStringIndex++;
        }
    }
}