void Game::configure(GameConfiguration config) { if (!config.isValid()) { throw std::invalid_argument("Invalid configuration: " + config.str()); } config.clean(); reset(); curTurn = config.getTurn(); FOR_POSITION_64(pos) { setPieceAt(pos, config.getPieceAt(pos)); if (config.getPieceAt(pos) != Piece::EMPTY()) { toggleBit(config.getOwnerAt(pos), pos, config.getPieceAt(pos)); } } cur = Game_UndoData(); cur.halfMoveClock = config.getHalfMoveClock(); fullMoveCount = config.getMoveNumber(); cur.check = posAttackedBy(getKingPosition(getTurn()), !getTurn()); cur.hash = Game_Hash(config); integrityCheck(); }
/* * Get all possible moves for the given square. * If the square is illegal or vacant, empty list is returned. * Otherwise we query the square to find out which player occupies the square, and returns the possible moves for that * player's piece. * Input: * board ~ The game board. * x, y ~ The position on board to search for moves. */ LinkedList* getMovesForSquare(char board[BOARD_SIZE][BOARD_SIZE], int x, int y) { LinkedList* possibleMoves = createList(deleteMove); // <-- This list contains the results of moves available. // Note we change this list in the following service functions. if (g_memError) return NULL; if (!isSquareOnBoard(x, y) || isSquareVacant(board, x, y)) return possibleMoves; bool isMovesForBlackPlayer = isSquareOccupiedByBlackPlayer(board, x, y); Position kingPos = getKingPosition(board, isMovesForBlackPlayer); // Position of current player's king Position startPos; startPos.x = x; startPos.y = y; getPieceMove(board, possibleMoves, isMovesForBlackPlayer, &startPos, &kingPos); if (g_memError) { deleteList(possibleMoves); return NULL; } return possibleMoves; }
/* * Iterates the board and returns a list of moves the player can make with each piece * Input: * board ~ The game board. * isMovesForBlackPlayer ~ True if the function returns moves for the black player. * False if the function returns moves for the white player. */ LinkedList* getMoves(char board[BOARD_SIZE][BOARD_SIZE], bool isMovesForBlackPlayer) { LinkedList* possibleMoves = createList(deleteMove); // <-- This list contains the results of moves available. // Note we change this list in the following service functions. if (g_memError) return NULL; Position kingPos = getKingPosition(board, isMovesForBlackPlayer); // Position of current player's king int i, j; // i = row, j = column for (i = 0; i < BOARD_SIZE; i++) { for (j = 0; j < BOARD_SIZE; j++) { Position startPos; startPos.x = i; startPos.y = j; getPieceMove(board, possibleMoves, isMovesForBlackPlayer, &startPos, &kingPos); if (g_memError) { deleteList(possibleMoves); return NULL; } } } return possibleMoves; }
/* * Returns either whether the black player (isTestForBlackPlayer == true) is in check, * or the white player (isTestForBlackPlayer == false) is in check. */ bool isCheck(char board[BOARD_SIZE][BOARD_SIZE], bool isTestForBlackPlayer) { Position kingPos = getKingPosition(board, isTestForBlackPlayer); return isKingUnderCheck(board, isTestForBlackPlayer, &kingPos); }