Search in sources :

Example 6 with ColorType

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

the class Tetfu method decodeMain.

private List<TetfuPage> decodeMain(String str) {
    LinkedList<Integer> values = str.replace("?", "").chars().boxed().map(c -> decodeData((char) c.intValue())).collect(Collectors.toCollection(LinkedList::new));
    ArrayList<TetfuPage> pages = new ArrayList<>();
    ColoredField prevField = ColoredFieldFactory.createField(TETFU_MAX_HEIGHT);
    ColoredField currentField = ColoredFieldFactory.createField(TETFU_MAX_HEIGHT);
    int[] blockUp = new int[FILED_WIDTH];
    int repeatCount = -1;
    while (!values.isEmpty()) {
        if (repeatCount <= 0) {
            int index = 0;
            boolean isChange = false;
            while (index < TETFU_FIELD_BLOCKS) {
                int diffBlock = pollValues(values, 2);
                int diff = diffBlock / TETFU_FIELD_BLOCKS;
                int block = diffBlock % TETFU_FIELD_BLOCKS;
                if (block != TETFU_FIELD_BLOCKS - 1)
                    isChange = true;
                for (int b = 0; b < block + 1; b++) {
                    int x = index % 10;
                    int y = TETFU_FIELD_TOP - (index / 10) - 1;
                    if (0 <= y) {
                        int prevBlockNumber = prevField.getBlockNumber(x, y);
                        currentField.setBlockNumber(x, y, diff + prevBlockNumber - 8);
                    } else {
                        blockUp[x] += diff - 8;
                    }
                    index += 1;
                }
            }
            if (!isChange)
                repeatCount = pollValues(values, 1);
        } else {
            currentField = prevField;
            repeatCount -= 1;
        }
        int action = pollValues(values, 3);
        ActionDecoder actionDecoder = new ActionDecoder(action);
        String escapedComment = "";
        if (actionDecoder.isComment) {
            List<Integer> commentValues = new ArrayList<>();
            int commentLength = pollValues(values, 2);
            for (int commentCounter = 0; commentCounter < (commentLength + 3) / 4; commentCounter++) {
                int commentValue = pollValues(values, 5);
                commentValues.add(commentValue);
            }
            CommentDecoder commentDecoder = new CommentDecoder(commentLength, commentValues);
            escapedComment = commentDecoder.getEscapedComment();
        }
        TetfuPage tetfuPage = new DecodedTetfuPage(actionDecoder, escapedComment, currentField);
        pages.add(tetfuPage);
        ColorType colorType = actionDecoder.colorType;
        if (actionDecoder.isLock) {
            if (ColorType.isMinoBlock(colorType)) {
                Rotate rotate = actionDecoder.rotate;
                Coordinate coordinate = actionDecoder.coordinate;
                Piece piece = converter.parseToBlock(colorType);
                Mino mino = minoFactory.create(piece, rotate);
                currentField.putMino(mino, coordinate.x, coordinate.y);
            }
            currentField.clearLine();
            if (actionDecoder.isBlockUp) {
                currentField.blockUp();
                for (int x = 0; x < TETFU_FIELD_WIDTH; x++) currentField.setBlockNumber(x, 0, blockUp[x]);
            }
            if (actionDecoder.isMirror)
                currentField.mirror();
        }
        prevField = currentField;
    }
    return pages;
}
Also used : CommentDecoder(common.tetfu.decorder.CommentDecoder) Piece(core.mino.Piece) Arrays(java.util.Arrays) ColorType(common.tetfu.common.ColorType) ActionFlags(common.tetfu.common.ActionFlags) ActionDecoder(common.tetfu.decorder.ActionDecoder) CommentEncoder(common.tetfu.encorder.CommentEncoder) FinderParseException(exceptions.FinderParseException) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) ENCODE_TABLE_SIZE(common.tetfu.TetfuTable.ENCODE_TABLE_SIZE) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) FieldEncoder(common.tetfu.encorder.FieldEncoder) Coordinate(common.tetfu.common.Coordinate) LinkedList(java.util.LinkedList) ColoredField(common.tetfu.field.ColoredField) TetfuTable.decodeData(common.tetfu.TetfuTable.decodeData) ActionEncoder(common.tetfu.encorder.ActionEncoder) Mino(core.mino.Mino) ColoredField(common.tetfu.field.ColoredField) ActionDecoder(common.tetfu.decorder.ActionDecoder) Rotate(core.srs.Rotate) ArrayList(java.util.ArrayList) CommentDecoder(common.tetfu.decorder.CommentDecoder) Coordinate(common.tetfu.common.Coordinate) Piece(core.mino.Piece) ColorType(common.tetfu.common.ColorType) Mino(core.mino.Mino)

Example 7 with ColorType

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

the class Tetfu method encode.

// コメント・フィールドは初期設定のみ設定可能
public String encode(List<TetfuElement> elements) {
    ColoredField prevField = ColoredFieldFactory.createField(TETFU_MAX_HEIGHT);
    String prevComment = "";
    for (int index = 0; index < elements.size(); index++) {
        TetfuElement element = elements.get(index);
        ColoredField field = element.getField().orElse(prevField);
        // field settings
        // prevFieldは、ひとつ前のミノを置いてできたフィールド
        // fieldは次に表示させたいフィールド。今回は、最初をのぞいてひとつ前のミノを置いてできたフィールドをそのまま利用
        encodeField(prevField, field);
        String comment = element.getEscapedComment();
        ActionFlags flags = new ActionFlags(comment, prevComment, index, element);
        parseAction(element, flags);
        ColorType colorType = element.getColorType();
        if (flags.isLock) {
            if (ColorType.isMinoBlock(colorType)) {
                Piece piece = converter.parseToBlock(colorType);
                Mino mino = minoFactory.create(piece, element.getRotate());
                field.putMino(mino, element.getX(), element.getY());
            }
            field.clearLine();
            if (flags.isBlockUp) {
                throw new UnsupportedOperationException();
            // currentField.blockUp();
            // for (int x = 0; x < TETFU_FIELD_WIDTH; x++)
            // currentField.setBlockNumber(x, 0, blockUp[x]);
            }
            if (flags.isMirror) {
                throw new UnsupportedOperationException();
            // currentField.mirror();
            }
        }
        // next field
        prevField = field;
        prevComment = comment;
    }
    StringBuilder builder = new StringBuilder();
    for (int index = 0; index < encodedValues.size(); index++) {
        Integer value = encodedValues.get(index);
        String encoded = TetfuTable.encodeData(value);
        builder.append(encoded);
        if (index % 47 == 41)
            builder.append('?');
    }
    return builder.toString();
}
Also used : ColoredField(common.tetfu.field.ColoredField) Piece(core.mino.Piece) ColorType(common.tetfu.common.ColorType) Mino(core.mino.Mino) ActionFlags(common.tetfu.common.ActionFlags)

Example 8 with ColorType

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

the class MoveEntryPoint method parseColorElement.

private TetfuElement parseColorElement(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)

Example 9 with ColorType

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

the class ActionEncoder method parseRotate.

private int parseRotate(TetfuElement element) {
    ColorType type = element.getColorType();
    Rotate rotate = element.getRotate();
    if (!ColorType.isMinoBlock(type))
        return 0;
    switch(rotate) {
        case Reverse:
            return 0;
        case Right:
            return type != ColorType.I ? 1 : 3;
        case Spawn:
            return 2;
        case Left:
            return type != ColorType.I ? 3 : 1;
    }
    throw new IllegalStateException("No reachable");
}
Also used : Rotate(core.srs.Rotate) ColorType(common.tetfu.common.ColorType)

Example 10 with ColorType

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

the class MoveSettingParser method loadTetfu.

private CommandLineWrapper loadTetfu(String data, CommandLineParser parser, Options options, CommandLineWrapper wrapper, MoveSettings settings) throws FinderParseException {
    // テト譜面のエンコード
    List<TetfuPage> decoded = encodeTetfu(data);
    // 指定されたページを抽出
    int page = wrapper.getIntegerOption("page").orElse(1);
    TetfuPage tetfuPage = extractTetfuPage(decoded, page);
    // コメントの抽出
    // 先頭が数字ではない(--clear-line -p *p7のようになる)場合でも、parserはエラーにならない
    // データ取得時にOptional.emptyがかえるだけ
    String comment = "--clear-line " + tetfuPage.getComment();
    List<String> splitComment = Arrays.stream(comment.split(" ")).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toList());
    // コマンド引数を配列に変換
    String[] commentArgs = new String[splitComment.size()];
    splitComment.toArray(commentArgs);
    // オプションとして読み込む
    try {
        CommandLine commandLineTetfu = parseToCommandLine(options, parser, commentArgs);
        CommandLineWrapper newWrapper = new NormalCommandLineWrapper(commandLineTetfu);
        // 削除ラインが読み取れればOK
        newWrapper.getIntegerOption("clear-line");
        wrapper = new PriorityCommandLineWrapper(Arrays.asList(wrapper, newWrapper));
    } catch (FinderParseException ignore) {
    }
    // 最大削除ラインの設定
    Optional<Integer> maxClearLineOption = wrapper.getIntegerOption("clear-line");
    maxClearLineOption.ifPresent(settings::setMaxClearLine);
    // フィールドを設定
    ColoredField coloredField = tetfuPage.getField();
    if (tetfuPage.isPutMino()) {
        ColorType colorType = tetfuPage.getColorType();
        Rotate rotate = tetfuPage.getRotate();
        int x = tetfuPage.getX();
        int y = tetfuPage.getY();
        ColorConverter colorConverter = new ColorConverter();
        Mino mino = new Mino(colorConverter.parseToBlock(colorType), rotate);
        coloredField.putMino(mino, x, y);
    }
    settings.setField(coloredField);
    return wrapper;
}
Also used : TetfuPage(common.tetfu.TetfuPage) Arrays(java.util.Arrays) ColorType(common.tetfu.common.ColorType) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) Files(java.nio.file.Files) org.apache.commons.cli(org.apache.commons.cli) IOException(java.io.IOException) Tetfu(common.tetfu.Tetfu) FinderParseException(exceptions.FinderParseException) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) Collectors(java.util.stream.Collectors) List(java.util.List) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) Charset(java.nio.charset.Charset) Paths(java.nio.file.Paths) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper) Optional(java.util.Optional) CommandLineWrapper(entry.CommandLineWrapper) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) ColoredField(common.tetfu.field.ColoredField) Mino(core.mino.Mino) TetfuPage(common.tetfu.TetfuPage) ColoredField(common.tetfu.field.ColoredField) Rotate(core.srs.Rotate) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) FinderParseException(exceptions.FinderParseException) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper) CommandLineWrapper(entry.CommandLineWrapper) ColorType(common.tetfu.common.ColorType) ColorConverter(common.tetfu.common.ColorConverter) Mino(core.mino.Mino) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper)

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