Search in sources :

Example 1 with ColorType

use of common.tetfu.common.ColorType in project solution-finder by knewjade.

the class SequenceFumenParser method parse.

@Override
public String parse(List<MinoOperationWithKey> operations, Field field, int maxClearLine) {
    Operations operations2 = OperationTransform.parseToOperations(field, operations, maxClearLine);
    List<? extends Operation> operationsList = operations2.getOperations();
    // ブロック順に変換
    List<Piece> pieceList = operationsList.stream().map(Operation::getPiece).collect(Collectors.toList());
    // テト譜を作成
    String quiz = Tetfu.encodeForQuiz(pieceList);
    ArrayList<TetfuElement> tetfuElements = new ArrayList<>();
    // 最初のelement
    Operation firstKey = operationsList.get(0);
    ColorType colorType1 = colorConverter.parseToColorType(firstKey.getPiece());
    ColoredField coloredField = createInitColoredField(field, maxClearLine);
    TetfuElement firstElement = new TetfuElement(coloredField, colorType1, firstKey.getRotate(), firstKey.getX(), firstKey.getY(), quiz);
    tetfuElements.add(firstElement);
    // 2番目以降のelement
    if (1 < operationsList.size()) {
        operationsList.subList(1, operationsList.size()).stream().map(operation -> {
            ColorType colorType = colorConverter.parseToColorType(operation.getPiece());
            return new TetfuElement(colorType, operation.getRotate(), operation.getX(), operation.getY(), quiz);
        }).forEach(tetfuElements::add);
    }
    Tetfu tetfu = new Tetfu(minoFactory, colorConverter);
    return tetfu.encode(tetfuElements);
}
Also used : Piece(core.mino.Piece) ColorType(common.tetfu.common.ColorType) OperationTransform(common.parser.OperationTransform) MinoOperationWithKey(common.datastore.MinoOperationWithKey) Tetfu(common.tetfu.Tetfu) TetfuElement(common.tetfu.TetfuElement) ColorConverter(common.tetfu.common.ColorConverter) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Operations(common.datastore.Operations) List(java.util.List) Field(core.field.Field) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) Operation(common.datastore.Operation) ColoredField(common.tetfu.field.ColoredField) ColoredField(common.tetfu.field.ColoredField) ArrayList(java.util.ArrayList) Operation(common.datastore.Operation) Piece(core.mino.Piece) ColorType(common.tetfu.common.ColorType) Operations(common.datastore.Operations) Tetfu(common.tetfu.Tetfu) TetfuElement(common.tetfu.TetfuElement)

Example 2 with ColorType

use of common.tetfu.common.ColorType 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 3 with ColorType

use of common.tetfu.common.ColorType in project solution-finder by knewjade.

the class PathSettings method setFieldWithReserved.

void setFieldWithReserved(ColoredField coloredField, int height) {
    Field field = FieldFactory.createField(height);
    BlockField blockField = new BlockField(height);
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < 10; x++) {
            ColorConverter colorConverter = new ColorConverter();
            ColorType colorType = colorConverter.parseToColorType(coloredField.getBlockNumber(x, y));
            switch(colorType) {
                case Empty:
                    break;
                case Gray:
                    field.setBlock(x, y);
                    break;
                default:
                    Piece piece = colorConverter.parseToBlock(colorType);
                    blockField.setBlock(piece, x, y);
                    break;
            }
        }
    }
    setField(field);
    setReservedBlock(blockField);
}
Also used : Field(core.field.Field) BlockField(common.datastore.BlockField) ColoredField(common.tetfu.field.ColoredField) BlockField(common.datastore.BlockField) Piece(core.mino.Piece) ColorConverter(common.tetfu.common.ColorConverter) ColorType(common.tetfu.common.ColorType)

Example 4 with ColorType

use of common.tetfu.common.ColorType in project solution-finder by knewjade.

the class FigUtilSettings method adjust.

void adjust() {
    // Quizがない場合はホールドは使えない
    boolean isUsingQuiz = tetfuPages.subList(0, startPage).stream().map(TetfuPage::getComment).anyMatch(s -> s.startsWith("#Q="));
    if (!isUsingQuiz)
        setUsingHold(false);
    // 高さの指定がないときは最も高い場所 + 1とする
    if (this.height == -1) {
        MinoFactory minoFactory = new MinoFactory();
        ColorConverter colorConverter = new ColorConverter();
        OptionalInt maxHeight = tetfuPages.subList(startPage - 1, endPage).stream().mapToInt(page -> {
            ColoredField field = page.getField();
            int fieldHeight = field.getUsingHeight();
            ColorType colorType = page.getColorType();
            if (ColorType.isMinoBlock(colorType)) {
                Piece piece = colorConverter.parseToBlock(colorType);
                Rotate rotate = page.getRotate();
                Mino mino = minoFactory.create(piece, rotate);
                int minoHeight = page.getY() + mino.getMaxY() + 1;
                return fieldHeight < minoHeight ? minoHeight : fieldHeight;
            } else {
                return fieldHeight;
            }
        }).max();
        this.height = maxHeight.orElse(0) + 1;
        if (height <= 0)
            this.height = 1;
        else if (23 <= height)
            this.height = 23;
    }
    // ホールドを使わない場合はRightに変更
    if (!this.isUsingHold && frameType == FrameType.Basic) {
        frameType = FrameType.Right;
    }
    // ネクストがない場合はチェックは必要がない
    if (frameType == FrameType.NoFrame) {
        return;
    }
    // フィールドの高さを計算し、その高さで置けるネクスト数を計算
    int fieldHeight = 34 * this.height + 2;
    int canPutCount = (fieldHeight - 5) / 52;
    // ネクスト (& ホールド)で必要な個数を算出し、ネクストを置けるならチェック終了
    int count = this.isUsingHold && frameType == FrameType.Right ? nextBoxCount + 1 : nextBoxCount;
    if (count <= canPutCount)
        return;
    // ネクストを置けるようにフィールドの高さを調整
    int needHeightPx = 52 * count + 5;
    setHeight((int) Math.ceil((needHeightPx - 2) / 34.0));
}
Also used : TetfuPage(common.tetfu.TetfuPage) Piece(core.mino.Piece) List(java.util.List) ColorType(common.tetfu.common.ColorType) MinoFactory(core.mino.MinoFactory) FrameType(util.fig.FrameType) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) OptionalInt(java.util.OptionalInt) ColoredField(common.tetfu.field.ColoredField) Mino(core.mino.Mino) ArrayList(java.util.ArrayList) ColoredField(common.tetfu.field.ColoredField) Rotate(core.srs.Rotate) Piece(core.mino.Piece) ColorConverter(common.tetfu.common.ColorConverter) ColorType(common.tetfu.common.ColorType) Mino(core.mino.Mino) MinoFactory(core.mino.MinoFactory) OptionalInt(java.util.OptionalInt)

Example 5 with ColorType

use of common.tetfu.common.ColorType in project solution-finder by knewjade.

the class EasyTetfu method parseBlockFieldToTetfuElement.

private TetfuElement parseBlockFieldToTetfuElement(Field initField, ColorConverter colorConverter, BlockField blockField, String comment) {
    ColoredField coloredField = ColoredFieldFactory.createGrayField(initField);
    for (Piece piece : Piece.values()) {
        Field target = blockField.get(piece);
        ColorType colorType = colorConverter.parseToColorType(piece);
        fillInField(coloredField, colorType, target);
    }
    return new TetfuElement(coloredField, ColorType.Empty, Rotate.Reverse, 0, 0, comment);
}
Also used : BlockField(common.datastore.BlockField) Field(core.field.Field) ColoredField(common.tetfu.field.ColoredField) ColoredField(common.tetfu.field.ColoredField) Piece(core.mino.Piece) ColorType(common.tetfu.common.ColorType) TetfuElement(common.tetfu.TetfuElement)

Aggregations

ColorType (common.tetfu.common.ColorType)27 ColoredField (common.tetfu.field.ColoredField)19 Piece (core.mino.Piece)15 Mino (core.mino.Mino)11 Rotate (core.srs.Rotate)11 ColorConverter (common.tetfu.common.ColorConverter)10 Field (core.field.Field)9 MinoFactory (core.mino.MinoFactory)8 List (java.util.List)8 TetfuPage (common.tetfu.TetfuPage)7 Collectors (java.util.stream.Collectors)7 ColoredFieldFactory (common.tetfu.field.ColoredFieldFactory)6 FinderParseException (exceptions.FinderParseException)6 Arrays (java.util.Arrays)6 BlockField (common.datastore.BlockField)5 Tetfu (common.tetfu.Tetfu)5 TetfuElement (common.tetfu.TetfuElement)5 LinkedList (java.util.LinkedList)5 Optional (java.util.Optional)5 FigColor (util.fig.FigColor)5