/** @param piece The piece to test. @param x The X coordinate to move the piece to. @param y The Y coordinate to move the piece to. */ bool Board::resultsInCheck(Piece* piece, int x, int y) { bool check = false; Position oldPos = piece->getPos(); Piece* capturedPiece = getPieceAt(x, y); piece->setPos(x, y); // Find the kings position Position kingPos; kingPos = getPiece(KING, piece->getColor())->getPos(); if(capturedPiece != NULL) capturedPiece->setCaptured(true); // Find out if the king gets pinned for(auto iter = mPieces.begin(); iter != mPieces.end(); iter++) { Piece* p = (*iter); if(p->getColor() != piece->getColor() && p->pinning(kingPos.x, kingPos.y) && !p->getCaptured()) { check = true; break; } } if(capturedPiece != NULL) capturedPiece->setCaptured(false); piece->setPos(oldPos); return check; }
// explore les déplacements possibles void Board::exploreMoveTree(Piece const * opponent, Explorer & explorer) const { for(int j = 0; j != N; ++j) { for(int i = 0; i != N; ++i) { Piece const * item = getPieceAt(i, j); if(item && !item->isSameTeam(opponent)) { item->explorePossibleMoves(explorer, *this, i, j); } } } }
// affichage de l'échiquier sur la console void Board::print() const { std::cout << " abcdefgh " << std::endl; // affiche les pièces noires puis les blanches for(int j = N; j != 0; --j) { std::cout << j; for(int i = 0; i != N; ++i) { Piece const * item = getPieceAt(i, j - 1); if(item) { std::cout << item->getNotation(); } else { std::cout << ' '; } } // complète la ligne std::cout << j << std::endl; } std::cout << " abcdefgh " << std::endl << std::endl; }
void RockPaperScissors::setMove(int lineNumber, const string& line, int playerNumber, Status& currentStatus) { //split string by white spaces istringstream buf(line); istream_iterator<string> beg(buf), end; vector<string> tokens(beg, end); int fromRow, fromCol, toRow, toCol; if (isMoveFormatCorrect(tokens)) { fromRow = stoi(tokens[0]) - 1; fromCol = stoi(tokens[1]) - 1; toRow = stoi(tokens[2]) - 1; toCol = stoi(tokens[3]) - 1; //player doesn't have a piece at source if (getPlayerOwningCell(fromRow, fromCol) != playerNumber) { cout << "Player " << playerNumber << " has no pieces to move at " << fromRow + 1 << "," << fromCol + 1 << " when executing line number " << lineNumber + 1 << ":" << endl \ << line << endl; currentStatus.setStatus(playerNumber, PossibleStatus::Move_File_Error, lineNumber, line); return; } //piece at source isn't a moving piece else if (getPieceAt(fromRow, fromCol).getrpc() != RPC::Rock && \ getPieceAt(fromRow, fromCol).getrpc() != RPC::Paper && \ getPieceAt(fromRow, fromCol).getrpc() != RPC::Scissors) { cout << "Player " << playerNumber << " has no mobile pieces at " << fromRow + 1 << "," << fromCol + 1 << " when executing line " << lineNumber + 1 << ":" << endl \ << line << endl; currentStatus.setStatus(playerNumber, PossibleStatus::Move_File_Error, lineNumber+1, line); return; } //player already has a piece at destination else if (getPlayerOwningCell(toRow, toCol) == playerNumber) { cout << "Player " << playerNumber << " already has a " << getPieceAt(toRow, toCol) << " piece at " << toRow + 1 << "," << toCol + 1 << " when executing line number " << lineNumber + 1 << ":" << endl \ << line << endl; currentStatus.setStatus(playerNumber, PossibleStatus::Move_File_Error, lineNumber, line); return; } } //illegal format else { cout << "Player " << playerNumber << " has a faulty format in line " << lineNumber + 1 << " : " << endl \ << line << endl \ << "Correct format is:" << endl \ << "<FROM_X> <FROM_Y> <TO_X> <TO_Y> [J: <Joker_X> <Joker_Y> <NEW_REP>]" << endl; currentStatus.setStatus(playerNumber, PossibleStatus::Move_File_Error, lineNumber, line); return; } //move the piece //place piece at new cell if (!placePiece(playerNumber, Piece(), fromRow, fromCol, toRow, toCol, lineNumber, line, currentStatus)) return; if (tokens.size() == 8) { Cell& c = board[stoi(tokens[5]) - 1][stoi(tokens[6]) - 1]; if (c.getPlayerOwning() == playerNumber && c.getCellPiece().getisJoker() == true) { Piece pTo; pTo.setJoker(true); switch (tokens[7].at(0)) { case 'R': pTo.setrpc(RPC::Rock); break; case 'P': pTo.setrpc(RPC::Paper); break; case 'S': pTo.setrpc(RPC::Scissors); break; case 'B': pTo.setrpc(RPC::Bomb); break; } c.setCell(pTo, playerNumber); } else { cout << "Player " << playerNumber << " has no joker piece at " << stoi(tokens[5]) << "," << stoi(tokens[6]) << " when executing line number " << lineNumber + 1 << ":" << endl \ << line << endl; currentStatus.setStatus(playerNumber, PossibleStatus::Move_File_Error, lineNumber+1, line); } } }