Search in sources :

Example 6 with Rotate

use of core.srs.Rotate in project solution-finder by knewjade.

the class OperationInterpreterTest method parseRandom.

@Test
void parseRandom() throws Exception {
    Randoms randoms = new Randoms();
    for (int size = 1; size < 20; size++) {
        List<Operation> operationList = Stream.generate(() -> {
            Piece piece = randoms.block();
            Rotate rotate = randoms.rotate();
            int x = randoms.nextInt(10);
            int y = randoms.nextInt(4);
            return new SimpleOperation(piece, rotate, x, y);
        }).limit(size).collect(Collectors.toList());
        Operations operations = new Operations(operationList);
        String str = OperationInterpreter.parseToString(operations);
        Operations actual = OperationInterpreter.parseToOperations(str);
        assertThat(actual).isEqualTo(operations);
    }
}
Also used : Randoms(lib.Randoms) Rotate(core.srs.Rotate) Piece(core.mino.Piece) SimpleOperation(common.datastore.SimpleOperation) SimpleOperation(common.datastore.SimpleOperation) Operation(common.datastore.Operation) Operations(common.datastore.Operations) Test(org.junit.jupiter.api.Test)

Example 7 with Rotate

use of core.srs.Rotate in project solution-finder by knewjade.

the class TetfuTest method random.

@Test
@LongTest
void random() throws Exception {
    // Initialize
    Randoms randoms = new Randoms();
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    MinoRotation minoRotation = new MinoRotation();
    ColorConverter colorConverter = new ColorConverter();
    // Define size
    int height = 4;
    int basicWidth = 3;
    SizedBit sizedBit = new SizedBit(basicWidth, height);
    SeparableMinos separableMinos = SeparableMinos.createSeparableMinos(minoFactory, minoShifter, sizedBit);
    // Create basic solutions
    TaskResultHelper taskResultHelper = new Field4x10MinoPackingHelper();
    LockedReachableThreadLocal lockedReachableThreadLocal = new LockedReachableThreadLocal(minoFactory, minoShifter, minoRotation, height);
    Predicate<ColumnField> memorizedPredicate = (columnField) -> true;
    OnDemandBasicSolutions basicSolutions = new OnDemandBasicSolutions(separableMinos, sizedBit, memorizedPredicate);
    for (int count = 0; count < 20; count++) {
        System.out.println(count);
        // Create field
        int numOfMinos = randoms.nextIntClosed(6, 10);
        Field field = randoms.field(height, numOfMinos);
        // Search
        List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(basicWidth, height, field);
        SolutionFilter solutionFilter = new SRSValidSolutionFilter(field, lockedReachableThreadLocal, sizedBit);
        PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper);
        Optional<Result> resultOptional = searcher.findAny();
        BuildUpStream buildUpStream = new BuildUpStream(lockedReachableThreadLocal.get(), height);
        // If found solution
        resultOptional.ifPresent(result -> {
            List<MinoOperationWithKey> list = result.getMemento().getSeparableMinoStream(basicWidth).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toList());
            Optional<List<MinoOperationWithKey>> validOption = buildUpStream.existsValidBuildPattern(field, list).findAny();
            validOption.ifPresent(operationWithKeys -> {
                Operations operations = OperationTransform.parseToOperations(field, operationWithKeys, height);
                List<TetfuElement> elements = operations.getOperations().stream().map(operation -> {
                    ColorType colorType = colorConverter.parseToColorType(operation.getPiece());
                    Rotate rotate = operation.getRotate();
                    int x = operation.getX();
                    int y = operation.getY();
                    String comment = randoms.string() + randoms.string() + randoms.string();
                    return new TetfuElement(colorType, rotate, x, y, comment);
                }).collect(Collectors.toList());
                String encode = new Tetfu(minoFactory, colorConverter).encode(elements);
                List<TetfuPage> decode = decodeTetfu(minoFactory, colorConverter, encode);
                assertThat(decode).hasSize(elements.size());
                for (int index = 0; index < decode.size(); index++) {
                    TetfuElement element = elements.get(index);
                    assertThat(decode.get(index)).returns(element.getColorType(), TetfuPage::getColorType).returns(element.getRotate(), TetfuPage::getRotate).returns(element.getX(), TetfuPage::getX).returns(element.getY(), TetfuPage::getY).returns(element.getComment(), TetfuPage::getComment);
                }
            });
        });
    }
}
Also used : TaskResultHelper(searcher.pack.task.TaskResultHelper) Randoms(lib.Randoms) Arrays(java.util.Arrays) ColorType(common.tetfu.common.ColorType) OperationTransform(common.parser.OperationTransform) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ColumnField(core.column_field.ColumnField) MinoOperationWithKey(common.datastore.MinoOperationWithKey) FinderParseException(exceptions.FinderParseException) SizedBit(searcher.pack.SizedBit) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) SeparableMinos(searcher.pack.SeparableMinos) Tag(org.junit.jupiter.api.Tag) MinoRotation(core.srs.MinoRotation) BuildUpStream(common.buildup.BuildUpStream) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) MinoShifter(core.mino.MinoShifter) Piece(core.mino.Piece) L(core.mino.Piece.L) Predicate(java.util.function.Predicate) Result(searcher.pack.task.Result) J(core.mino.Piece.J) InOutPairField(searcher.pack.InOutPairField) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Operations(common.datastore.Operations) SolutionFilter(searcher.pack.memento.SolutionFilter) List(java.util.List) Field(core.field.Field) SeparableMino(searcher.pack.separable_mino.SeparableMino) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) Optional(java.util.Optional) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) LongTest(module.LongTest) Collections(java.util.Collections) ArrayColoredField(common.tetfu.field.ArrayColoredField) ColoredField(common.tetfu.field.ColoredField) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) Rotate(core.srs.Rotate) TaskResultHelper(searcher.pack.task.TaskResultHelper) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) Result(searcher.pack.task.Result) ColumnField(core.column_field.ColumnField) InOutPairField(searcher.pack.InOutPairField) Field(core.field.Field) ArrayColoredField(common.tetfu.field.ArrayColoredField) ColoredField(common.tetfu.field.ColoredField) MinoOperationWithKey(common.datastore.MinoOperationWithKey) SeparableMinos(searcher.pack.SeparableMinos) InOutPairField(searcher.pack.InOutPairField) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) ColorConverter(common.tetfu.common.ColorConverter) ColorType(common.tetfu.common.ColorType) MinoFactory(core.mino.MinoFactory) List(java.util.List) BuildUpStream(common.buildup.BuildUpStream) Operations(common.datastore.Operations) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) ColumnField(core.column_field.ColumnField) MinoRotation(core.srs.MinoRotation) Randoms(lib.Randoms) SizedBit(searcher.pack.SizedBit) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) SolutionFilter(searcher.pack.memento.SolutionFilter) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) MinoShifter(core.mino.MinoShifter) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Example 8 with Rotate

use of core.srs.Rotate in project solution-finder by knewjade.

the class MinimalLockedCacheTest method assertCache.

private void assertCache(int height) {
    MinimalLockedCache cache = new MinimalLockedCache(height);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < 10; x++) {
            for (Rotate rotate : Rotate.values()) {
                assertThat(cache.isVisit(x, y, rotate)).isFalse();
                cache.visit(x, y, rotate);
                assertThat(cache.isVisit(x, y, rotate)).isTrue();
            }
        }
    }
    cache.clear();
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < 10; x++) {
            for (Rotate rotate : Rotate.values()) {
                assertThat(cache.isVisit(x, y, rotate)).isFalse();
            }
        }
    }
}
Also used : Rotate(core.srs.Rotate)

Example 9 with Rotate

use of core.srs.Rotate in project solution-finder by knewjade.

the class LimitIterationCandidate method search.

@Override
public Set<Action> search(Field field, Piece piece, int appearY) {
    // temporaryの初期化
    this.appearY = appearY;
    HashSet<Action> actions = new HashSet<>();
    for (Rotate rotate : Rotate.values()) {
        Mino mino = minoFactory.create(piece, rotate);
        for (int x = -mino.getMinX(); x < FIELD_WIDTH - mino.getMaxX(); x++) {
            for (int y = appearY - mino.getMaxY() - 1; -mino.getMinY() <= y; y--) {
                if (field.canPut(mino, x, y) && field.isOnGround(mino, x, y)) {
                    if (check(field, mino, x, y, From.None, 0)) {
                        Action action = minoShifter.createTransformedAction(piece, rotate, x, y);
                        actions.add(action);
                    }
                }
            }
        }
    }
    return actions;
}
Also used : Action(common.datastore.action.Action) Rotate(core.srs.Rotate) Mino(core.mino.Mino) HashSet(java.util.HashSet)

Example 10 with Rotate

use of core.srs.Rotate in project solution-finder by knewjade.

the class LockedCandidate method search.

@Override
public Set<Action> search(Field field, Piece piece, int appearY) {
    // temporaryの初期化
    this.appearY = appearY;
    lockedCache.clear();
    HashSet<Action> actions = new HashSet<>();
    for (Rotate rotate : Rotate.values()) {
        Mino mino = minoFactory.create(piece, rotate);
        for (int x = -mino.getMinX(); x < FIELD_WIDTH - mino.getMaxX(); x++) {
            for (int y = appearY - mino.getMaxY() - 1; -mino.getMinY() <= y; y--) {
                if (field.canPut(mino, x, y) && field.isOnGround(mino, x, y)) {
                    if (check(field, mino, x, y, From.None)) {
                        Action action = minoShifter.createTransformedAction(piece, rotate, x, y);
                        actions.add(action);
                    } else {
                        lockedCache.visit(x, y, rotate);
                    }
                    lockedCache.resetTrail();
                }
            }
        }
    }
    return actions;
}
Also used : Action(common.datastore.action.Action) Rotate(core.srs.Rotate) Mino(core.mino.Mino) HashSet(java.util.HashSet)

Aggregations

Rotate (core.srs.Rotate)55 Mino (core.mino.Mino)25 Piece (core.mino.Piece)25 Test (org.junit.jupiter.api.Test)14 Action (common.datastore.action.Action)13 ColorType (common.tetfu.common.ColorType)11 Randoms (lib.Randoms)11 HashSet (java.util.HashSet)10 ColoredField (common.tetfu.field.ColoredField)9 MinoFactory (core.mino.MinoFactory)9 List (java.util.List)9 MinimalAction (common.datastore.action.MinimalAction)7 TetfuPage (common.tetfu.TetfuPage)7 ColorConverter (common.tetfu.common.ColorConverter)7 Arrays (java.util.Arrays)7 Collectors (java.util.stream.Collectors)7 Field (core.field.Field)6 FinderParseException (exceptions.FinderParseException)6 SimpleOperation (common.datastore.SimpleOperation)5 ColoredFieldFactory (common.tetfu.field.ColoredFieldFactory)5