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 possible moves for current Queen piece. * Input: * board ~ The game board. * possibleMoves ~ A list of possible moves by the current player, we aggregate it as we check * possible eat / position change moves. * isMovesForBlackPlayer ~ True if current player is black. False if white. * startPos ~ Where the piece is currently located. * kingPos ~ Current position of the current player's king. */ void getQueenMoves(char board[BOARD_SIZE][BOARD_SIZE], LinkedList* possibleMoves, bool isMovesForBlackPlayer, Position* startPos, Position* kingPos) { // The queen combines the power of a bishop and a rook getBishopMoves(board, possibleMoves, isMovesForBlackPlayer, startPos, kingPos); if (g_memError) return; getRookMoves(board, possibleMoves, isMovesForBlackPlayer, startPos, kingPos); }
set<int> getQueenMoves(const int* board, const int & loc, Simulator& sim, bool enable) { set<int> r = getRookMoves(board, loc, sim, enable); set<int> b = getBishopMoves(board, loc, sim, enable); set<int> u = getUnicornMoves(board, loc, sim, enable); for (set<int>::iterator it=b.begin(); it!=b.end(); ++it) { r.insert(*it); } for (set<int>::iterator it=u.begin(); it!=u.end(); ++it) { r.insert(*it); } return r; }
/* * 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 } } }
void addPossibleMoves(const Piece* board, const Point& pos, vector<Point>& moves) { Piece type = board[pos.ind]; bool pieceIsWhite = isWhite(type); switch (type) { case BKnight: case WKnight: { moves.push_back(Point(pos.x + 1, pos.y + 2)); moves.push_back(Point(pos.x - 1, pos.y + 2)); moves.push_back(Point(pos.x + 2, pos.y + 1)); moves.push_back(Point(pos.x - 2, pos.y + 1)); moves.push_back(Point(pos.x + 1, pos.y - 2)); moves.push_back(Point(pos.x - 1, pos.y - 2)); moves.push_back(Point(pos.x + 2, pos.y - 1)); moves.push_back(Point(pos.x - 2, pos.y - 1)); } break; case BRook: case WRook: { getRookMoves(board, pos, moves); } break; case BBishop: case WBishop: { getBishopMoves(board, pos, moves); } break; case WQueen: case BQueen: { getRookMoves(board, pos, moves); getBishopMoves(board, pos, moves); } break; case WPawn: case BPawn: { int dir = type == BPawn ? 1 : -1; Point next(pos.x, pos.y + dir); if (next.y >= 0 && next.y < 8 && board[next.ind] == Empty) { moves.push_back(next); Point fwd = Point(pos.x, pos.y+(dir*2)); if (((pos.y == 1 && type == BPawn) || (pos.y == 6 && type == WPawn)) && board[fwd.ind] == Empty) moves.push_back(fwd); } Point right = Point(pos.x+1, pos.y+dir); if (right.y >= 0 && right.y < 8 && right.x >= 0 && right.x < 8 && board[right.ind] != Empty && isWhite(board[right.ind]) != pieceIsWhite) moves.push_back(right); Point left = Point(pos.x-1, pos.y+dir); if (left.y >= 0 && left.y < 8 && left.x >= 0 && left.x < 8 && board[left.ind] != Empty && isWhite(board[left.ind]) != pieceIsWhite) moves.push_back(left); } break; case WKing: case BKing: { moves.push_back(Point(pos.x + 1, pos.y)); moves.push_back(Point(pos.x - 1, pos.y)); moves.push_back(Point(pos.x + 1, pos.y - 1)); moves.push_back(Point(pos.x, pos.y - 1)); moves.push_back(Point(pos.x - 1, pos.y - 1)); moves.push_back(Point(pos.x + 1, pos.y + 1)); moves.push_back(Point(pos.x, pos.y + 1)); moves.push_back(Point(pos.x - 1, pos.y + 1)); } break; case Empty: break; } moves.erase( remove_if(moves.begin(), moves.end(), [board,pieceIsWhite](const Point& p) { return !(p.x >= 0 && p.x < 8 && p.y >= 0 && p.y < 8 && (isWhite(board[p.ind]) != pieceIsWhite || board[p.ind] == Empty)); } ), moves.end() ); }