Search in sources :

Example 1 with Piece

use of core.mino.Piece in project solution-finder by knewjade.

the class CheckmateUsingHoldReuse method search.

@Override
public List<Result> search(Field initFieldOrigin, Piece[] pieces, Candidate<T> candidate, int maxClearLine, int maxDepth) {
    Field initField = initFieldOrigin.freeze(maxClearLine);
    int deleteLine = initField.clearLine();
    int height = maxClearLine - deleteLine;
    TreeSet<Order> orders = new TreeSet<>();
    // 最初の探索開始depthとordersを調整
    int startDepth;
    if (!equalsField(lastField, initField) || lastPieces == null) {
        // mementoの初期化
        // 初めから
        memento = new ArrayList<>();
        orders.add(new NormalOrder(initField, pieces[0], height, maxDepth));
        startDepth = 1;
        memento.add(new TreeSet<>(orders));
    } else {
        int reuseIndex = -1;
        int minLength = lastPieces.length < pieces.length ? lastPieces.length : pieces.length;
        int max = maxDepth + 1 < minLength ? maxDepth + 1 : minLength;
        for (int index = 0; index < max; index++) {
            if (lastPieces[index] == pieces[index])
                reuseIndex = index;
            else
                break;
        }
        if (reuseIndex < 0) {
            memento = new ArrayList<>();
            orders.add(new NormalOrder(initField, pieces[0], height, maxDepth));
            startDepth = 1;
            memento.add(new TreeSet<>(orders));
        } else if (reuseIndex == maxDepth) {
            return dataPool.getResults();
        } else {
            orders.addAll(memento.get(reuseIndex));
            startDepth = reuseIndex + 1;
            memento = memento.subList(0, reuseIndex + 1);
        }
    }
    dataPool.resetResults();
    for (int depth = startDepth; depth <= maxDepth; depth++) {
        dataPool.initEachDepth();
        boolean isLast = depth == maxDepth;
        if (depth < pieces.length) {
            Piece drawn = pieces[depth];
            for (int count = 0, size = orders.size(); count < size; count++) {
                Order order = orders.pollFirst();
                searcherCore.stepWithNext(candidate, drawn, order, isLast);
            }
        } else {
            for (int count = 0, size = orders.size(); count < size; count++) {
                Order order = orders.pollFirst();
                searcherCore.stepWhenNoNext(candidate, order, isLast);
            }
        }
        orders = dataPool.getNexts();
        memento.add(new TreeSet<>(orders));
    }
    lastPieces = pieces;
    lastField = initField;
    return dataPool.getResults();
}
Also used : Order(common.datastore.order.Order) NormalOrder(common.datastore.order.NormalOrder) SmallField(core.field.SmallField) Field(core.field.Field) NormalOrder(common.datastore.order.NormalOrder) TreeSet(java.util.TreeSet) Piece(core.mino.Piece)

Example 2 with Piece

use of core.mino.Piece in project solution-finder by knewjade.

the class AllSeparableMinoFactory method create.

public Set<SeparableMino> create() {
    HashSet<SeparableMino> pieces = new HashSet<>();
    HashSet<Mino> createdCheckSet = new HashSet<>();
    for (Piece piece : Piece.values()) {
        for (Rotate originRotate : Rotate.values()) {
            Rotate rotate = minoShifter.createTransformedRotate(piece, originRotate);
            Mino mino = minoFactory.create(piece, rotate);
            // 追加済みかチェック
            if (createdCheckSet.contains(mino))
                continue;
            createdCheckSet.add(mino);
            // ミノの高さを計算
            int minoHeight = mino.getMaxY() - mino.getMinY() + 1;
            // フィールドの高さ以上にミノを使う場合はおけない
            if (fieldHeight < minoHeight)
                continue;
            // 行候補をリストにする
            ArrayList<Integer> lineIndexes = getLineIndexes(fieldHeight);
            // リストアップ
            ArrayList<SeparableMino> piecesEachMino = generatePiecesEachMino(mino, lineIndexes, minoHeight);
            // 追加
            pieces.addAll(piecesEachMino);
        }
    }
    return pieces;
}
Also used : Rotate(core.srs.Rotate) Piece(core.mino.Piece) Mino(core.mino.Mino) HashSet(java.util.HashSet)

Example 3 with Piece

use of core.mino.Piece in project solution-finder by knewjade.

the class ActionParser method parseToOperation.

static Operation parseToOperation(int value) {
    int x = value % 10;
    value /= 10;
    int y = value % 24;
    value /= 24;
    Rotate rotate = Rotate.getRotate(value % 4);
    value /= 4;
    Piece piece = Piece.getBlock(value);
    return new SimpleOperation(piece, rotate, x, y);
}
Also used : Rotate(core.srs.Rotate) Piece(core.mino.Piece) SimpleOperation(common.datastore.SimpleOperation)

Example 4 with Piece

use of core.mino.Piece in project solution-finder by knewjade.

the class ResultHelper method createOperationStream.

public static Stream<Operation> createOperationStream(Result result) {
    Order order = result.getOrder();
    OperationHistory history = order.getHistory();
    Stream<Operation> operationStream = history.getOperationStream();
    Piece lastPiece = result.getLastPiece();
    Action lastAction = result.getLastAction();
    SimpleOperation lastOperation = new SimpleOperation(lastPiece, lastAction.getRotate(), lastAction.getX(), lastAction.getY());
    return Stream.concat(operationStream, Stream.of(lastOperation));
}
Also used : Order(common.datastore.order.Order) Action(common.datastore.action.Action) Piece(core.mino.Piece) SimpleOperation(common.datastore.SimpleOperation) SimpleOperation(common.datastore.SimpleOperation) Operation(common.datastore.Operation)

Example 5 with Piece

use of core.mino.Piece in project solution-finder by knewjade.

the class BuildUp method existsValidByOrder.

// block順番で組み立てられる手順が存在するかチェックする
// operationsで使用するミノとblocksが一致していること
public static boolean existsValidByOrder(Field field, Stream<MinoOperationWithKey> operations, List<Piece> pieces, int height, Reachable reachable) {
    EnumMap<Piece, LinkedList<MinoOperationWithKey>> eachBlocks = operations.sequential().collect(() -> new EnumMap<Piece, LinkedList<MinoOperationWithKey>>(Piece.class), (blockLinkedListEnumMap, operationWithKey) -> {
        Piece piece = operationWithKey.getPiece();
        LinkedList<MinoOperationWithKey> operationWithKeys = blockLinkedListEnumMap.computeIfAbsent(piece, b -> new LinkedList<>());
        operationWithKeys.add(operationWithKey);
    }, EnumMap::putAll);
    return existsValidByOrder(field.freeze(height), eachBlocks, pieces, height, reachable, 0);
}
Also used : MinoOperationWithKey(common.datastore.MinoOperationWithKey) Piece(core.mino.Piece) EnumMap(java.util.EnumMap) LinkedList(java.util.LinkedList)

Aggregations

Piece (core.mino.Piece)187 Test (org.junit.jupiter.api.Test)97 Field (core.field.Field)81 Randoms (lib.Randoms)61 LongTest (module.LongTest)60 Action (common.datastore.action.Action)56 LockedCandidate (core.action.candidate.LockedCandidate)44 LongPieces (common.datastore.blocks.LongPieces)41 List (java.util.List)36 MinoFactory (core.mino.MinoFactory)35 ArrayList (java.util.ArrayList)35 Collectors (java.util.stream.Collectors)34 LockedReachable (core.action.reachable.LockedReachable)32 Rotate (core.srs.Rotate)28 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 MinoShifter (core.mino.MinoShifter)25 Stream (java.util.stream.Stream)25 Mino (core.mino.Mino)21 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)19 FieldFactory (core.field.FieldFactory)18