Search in sources :

Example 1 with Order

use of common.datastore.order.Order 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 Order

use of common.datastore.order.Order in project solution-finder by knewjade.

the class SimpleSearcherCore method step.

private void step(Candidate<T> candidate, Piece drawn, Piece nextHold, Order order, boolean isLast) {
    Field currentField = order.getField();
    int max = order.getMaxClearLine();
    Set<T> candidateList = candidate.search(currentField, drawn, max);
    OperationHistory history = order.getHistory();
    for (T action : candidateList) {
        Field field = currentField.freeze(max);
        Mino mino = minoFactory.create(drawn, action.getRotate());
        field.put(mino, action.getX(), action.getY());
        int clearLine = field.clearLine();
        int maxClearLine = max - clearLine;
        if (!validator.validate(field, maxClearLine))
            continue;
        if (validator.satisfies(field, maxClearLine)) {
            Result result = new Result(order, drawn, action, nextHold);
            dataPool.addResult(result);
            continue;
        }
        if (isLast)
            continue;
        OperationHistory nextHistory = history.recordAndReturnNew(drawn, action);
        Order nextOrder = new NormalOrder(field, nextHold, maxClearLine, nextHistory);
        dataPool.addOrder(nextOrder);
    }
}
Also used : Order(common.datastore.order.Order) NormalOrder(common.datastore.order.NormalOrder) Field(core.field.Field) NormalOrder(common.datastore.order.NormalOrder) Mino(core.mino.Mino) OperationHistory(common.OperationHistory) Result(common.datastore.Result)

Example 3 with Order

use of common.datastore.order.Order in project solution-finder by knewjade.

the class PutterNoHold method first.

public TreeSet<Order> first(Field initField, Piece[] headPieces, Candidate<T> candidate, int maxClearLine, int maxDepth) {
    Field freeze = initField.freeze(maxClearLine);
    int deleteLine = freeze.clearLine();
    dataPool.initFirst(new NormalOrder(freeze, null, maxClearLine - deleteLine, maxDepth));
    for (int depth = 0; depth < headPieces.length; depth++) {
        TreeSet<Order> orders = dataPool.getNexts();
        dataPool.initEachDepth();
        boolean isLast = depth == maxDepth - 1;
        for (int count = 0, size = orders.size(); count < size; count++) {
            Order order = orders.pollFirst();
            searcherCore.stepWithNextNoHold(candidate, headPieces[depth], order, isLast);
        }
    }
    return dataPool.getNexts();
}
Also used : Order(common.datastore.order.Order) NormalOrder(common.datastore.order.NormalOrder) Field(core.field.Field) NormalOrder(common.datastore.order.NormalOrder)

Example 4 with Order

use of common.datastore.order.Order in project solution-finder by knewjade.

the class CheckerNoHold method check.

@Override
public boolean check(Field initField, Piece[] pieces, Candidate<T> candidate, int maxClearLine, int maxDepth) {
    Field freeze = initField.freeze(maxClearLine);
    int deleteLine = freeze.clearLine();
    dataPool.initFirst();
    dataPool.addOrder(new DepthOrder(freeze, null, maxClearLine - deleteLine, maxDepth));
    while (!dataPool.getNexts().isEmpty() && dataPool.getResults().isEmpty()) {
        Order order = dataPool.getNexts().pollLast();
        int depth = order.getHistory().getNextIndex();
        boolean isLast = depth == maxDepth;
        assert depth < pieces.length : depth;
        searcherCore.stepWithNextNoHold(candidate, pieces[depth], order, isLast);
    }
    return !dataPool.getResults().isEmpty();
}
Also used : Order(common.datastore.order.Order) DepthOrder(common.datastore.order.DepthOrder) Field(core.field.Field) DepthOrder(common.datastore.order.DepthOrder)

Example 5 with Order

use of common.datastore.order.Order 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)

Aggregations

Order (common.datastore.order.Order)11 Field (core.field.Field)10 NormalOrder (common.datastore.order.NormalOrder)7 Piece (core.mino.Piece)4 OperationHistory (common.OperationHistory)2 Operation (common.datastore.Operation)2 Action (common.datastore.action.Action)2 DepthOrder (common.datastore.order.DepthOrder)2 SmallField (core.field.SmallField)2 TreeSet (java.util.TreeSet)2 BlockField (common.datastore.BlockField)1 MinoOperationWithKey (common.datastore.MinoOperationWithKey)1 Operations (common.datastore.Operations)1 Result (common.datastore.Result)1 SimpleOperation (common.datastore.SimpleOperation)1 Pieces (common.datastore.blocks.Pieces)1 PatternGenerator (common.pattern.PatternGenerator)1 ColorConverter (common.tetfu.common.ColorConverter)1 ColoredField (common.tetfu.field.ColoredField)1 LockedCandidate (core.action.candidate.LockedCandidate)1