set<int> getPossibleMoves(const int* board, const int & loc, bool checkingEnabled) { Simulator sim(checkingEnabled); sim.setBoard(board); int pos = board[loc] & 7; if (loc==-1) pos = 0; switch ( pos ) { case 0: return set<int>(); case 1: return getPawnMoves(board, loc, sim, checkingEnabled); case 2: return getRookMoves(board, loc, sim, checkingEnabled); case 3: return getKnightMoves(board, loc, sim, checkingEnabled); case 4: return getBishopMoves(board, loc, sim, checkingEnabled); case 5: return getUnicornMoves(board, loc, sim, checkingEnabled); case 6: return getQueenMoves(board, loc, sim, checkingEnabled); case 7: return getKingMoves(board, loc, sim, checkingEnabled); } return set<int>(); }
std::vector<Move> Chessboard::getMoves(char color) { std::vector<Move> moves; char kingInCheck = 0; for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { if (getColor(row, col) == color) { std::vector<Move> m; char piece = get(row, col); if (isPawn(piece)) { m = getPawnMoves(*this, row, col); } else if (isKnight(piece)) { m = getKnightMoves(*this, row, col); } else if (isBishop(piece)) { m = getBishopMoves(*this, row, col); } else if (isRook(piece)) { m = getRookMoves(*this, row, col); } else if (isQueen(piece)) { m = getQueenMoves(*this, row, col); } else if (isKing(piece)) { m = getKingMoves(*this, row, col); } moves.insert(moves.end(), m.begin(), m.end()); } } } buildCheckBoard(moves); return moves; }
/* * Get all possible position / eat moves of any soldier. * Input: * board ~ The game board. * possibleMoves ~ A list of possible moves by the current player, the list will be filled with possible moves for * the piece. * isMovesForBlackPlayer ~ True if current player is black. False if white. * startPos ~ Where the soldier is currently located. * kingPos ~ Current position of the current player's king. */ void getPieceMove(char board[BOARD_SIZE][BOARD_SIZE], LinkedList* possibleMoves, bool isMovesForBlackPlayer, Position* startPos, Position* kingPos) { // Search for moves only if the piece on the square belongs to the current player. if (isSquareOccupiedByCurrPlayer(board, isMovesForBlackPlayer, startPos->x, startPos->y)) { switch (board[startPos->x][startPos->y]) // Get move by piece type { case (WHITE_P): case (BLACK_P): { getPawnMoves(board, possibleMoves, isMovesForBlackPlayer, startPos, kingPos); break; } case (WHITE_B) : case (BLACK_B) : { getBishopMoves(board, possibleMoves, isMovesForBlackPlayer, startPos, kingPos); break; } case (WHITE_R) : case (BLACK_R) : { getRookMoves(board, possibleMoves, isMovesForBlackPlayer, startPos, kingPos); break; } case (WHITE_N) : case (BLACK_N) : { getKnightMoves(board, possibleMoves, isMovesForBlackPlayer, startPos, kingPos); break; } case (WHITE_Q) : case (BLACK_Q) : { getQueenMoves(board, possibleMoves, isMovesForBlackPlayer, startPos, kingPos); break; } case (WHITE_K) : case (BLACK_K) : { getKingMoves(board, possibleMoves, isMovesForBlackPlayer, startPos); break; } default: break; // Illegal board piece } } }
MoveList* getMoves(char** board, int player) { MoveList* result = NULL; for (int i = 0; i < BOARD_SIZE; ++i) for (int j = 0; j < BOARD_SIZE; ++j) { Position position = {.x = i, .y = j}; if (playerInPosition(position, board, player)) { MoveList* discMoves = isKing(getValueInPosition(position, board)) ? getKingMoves(position, board, player) : getManMoves(position, board, player); result = bestMoveList(result, discMoves); } } return result; }