Example #1
0
void Game::play()
{
    while(continueGame()){
        Step step = (turn == true) ? whitePlayer->makeStep(board.get()) :
                                     blackPlayer->makeStep(board.get());
        if(step.isStepOnBoard()){
            history.push_back(step);
            Piece* piece = board->getCellValue(*step.getFrom());
            if(piece->isStepValid(step, board.get())){
                board->movePiece(step);
                if (Pawn* pawn = dynamic_cast<Pawn*>(piece))
                    if(pawn->isFirstStep())
                        pawn->setFirstStep(false);
            }
        }
        turn = !turn;
    }
}
Example #2
0
bool King::isStepValid(const Step& step, ChessBoard* board) const
{
    const Position* from = step.getFrom();
    const Position* to = step.getTo();

    if(board->isColorTheSame(*from,*to))
        return false;

    switch (std::abs(from->first - to->first)) {
    case 0:
        if(std::abs(from->second - to->second)!=1)
            return false;
        break;
    case 1:
        if(std::abs(from->second - to->second)>1)
            return false;
        break;
    default:
        return false;
        break;
    }

    return true;
}