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; }
/*Add legal Moves for give Piece at Location to Linked List*/ int getMovesSinglePiece(Game *game, Location *loc, ListNode *temp){ char c = game->board[loc->column][loc->row]; switch (c) { case BLACK_P:case WHITE_P:return getPawnMoves(game, loc, temp); break; case BLACK_N:case WHITE_N: return getKnightMoves(game, loc, temp); break; case BLACK_Q:case BLACK_K:case BLACK_B:case BLACK_R:case WHITE_Q:case WHITE_K:case WHITE_B:case WHITE_R:return getBRQKMoves(game, loc, temp); break; } return 0; }
/* * 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 } } }
/* Get all the moves of the piece in the specified square */ void getMoves(Board *brd, MoveList *list, int square) { int piece; list->numMoves = 0; piece = brd->piecesBySquare[square]; if(piece == wPawn || piece == bPawn) { getPawnMoves(brd, list, square); } else if(piece == wKnight || piece == bKnight) { getDiscMoves(brd, list, square); } else if (piece == wKing || piece == bKing) { getCastlingMoves(brd, list, square); getDiscMoves(brd, list, square); } else if(piece == wRook || piece == bRook) { getCastlingMoves(brd, list, square); getContMoves(brd, list, square); } else if(piece == wQueen || piece == wBishop || piece == bQueen || piece == bBishop) { getContMoves(brd, list, square); } }
/* Create a list of a player's all possible moves */ void makeMoveList(Board *brd, MoveList *list) { int piece, square, pieceNum, i, pieceIndex; list->numMoves = 0; /* Pawns */ if(brd->side == WHITE) { for(pieceNum = 0; pieceNum < brd->pieceNum[wPawn]; pieceNum++) { getPawnMoves(brd, list, brd->squaresByPieces[wPawn][pieceNum]); } } else { /* same, but for black */ for(pieceNum = 0; pieceNum < brd->pieceNum[bPawn]; pieceNum++) { getPawnMoves(brd, list, brd->squaresByPieces[bPawn][pieceNum]); } } /* Castling */ if(brd->side == WHITE) { getCastlingMoves(brd, list, E1); } else { /* same, but for black */ getCastlingMoves(brd, list, E8); } /* Continuous pieces for both colors */ if (brd->side == WHITE) { pieceIndex = 0; } else { pieceIndex = 3; } piece = contPieces[pieceIndex]; for(i = 0; i < 3; i++) { for(pieceNum = 0; pieceNum < brd->pieceNum[piece]; pieceNum++) { square = brd->squaresByPieces[piece][pieceNum]; getContMoves(brd, list, square); } piece = contPieces[++pieceIndex]; } /* Discrete Pieces */ if (brd->side == WHITE) { pieceIndex = 0; } else { pieceIndex = 2; } piece = discPieces[pieceIndex]; for(i = 0; i < 2; i++) { for(pieceNum = 0; pieceNum < brd->pieceNum[piece]; pieceNum++) { square = brd->squaresByPieces[piece][pieceNum]; getDiscMoves(brd, list, square); } piece = discPieces[++pieceIndex]; } }