Search in sources :

Example 41 with Mino

use of core.mino.Mino 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 42 with Mino

use of core.mino.Mino 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 43 with Mino

use of core.mino.Mino 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 44 with Mino

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

the class Neighbors method createNeighbors.

private Neighbor[][][][] createNeighbors(OriginalPieceFactory pieceFactory, int maxHeight) {
    int maxBlock = Piece.values().length;
    int maxRotate = Rotate.values().length;
    Neighbor[][][][] neighbors = new Neighbor[maxBlock][maxRotate][maxHeight][FIELD_WIDTH];
    Collection<OriginalPiece> pieces = pieceFactory.create();
    for (OriginalPiece piece : pieces) {
        Mino mino = piece.getMino();
        Piece block = mino.getPiece();
        Rotate rotate = mino.getRotate();
        int x = piece.getX();
        int y = piece.getY();
        Neighbor neighbor = new Neighbor(piece);
        neighbors[block.getNumber()][rotate.getNumber()][y][x] = neighbor;
    }
    return neighbors;
}
Also used : Rotate(core.srs.Rotate) Piece(core.mino.Piece) Mino(core.mino.Mino)

Example 45 with Mino

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

the class Neighbors method updateNeighbor.

private void updateNeighbor(MinoFactory minoFactory, MinoRotation minoRotation, Neighbor current) {
    OriginalPiece piece = current.getPiece();
    Mino mino = piece.getMino();
    int x = piece.getX();
    int y = piece.getY();
    // 上と左右を更新
    current.updateUp(getNeighbor(mino, x, y + 1));
    current.updateLeft(getNeighbor(mino, x - 1, y));
    current.updateRight(getNeighbor(mino, x + 1, y));
    // 左回転でジャンプする先を更新
    int[][] patternsLeftDest = minoRotation.getLeftPatternsFrom(mino);
    Mino afterLeft = minoFactory.create(mino.getPiece(), mino.getRotate().getLeftRotate());
    ArrayList<Neighbor> neighborsLeftDest = createDestinations(afterLeft, x, y, patternsLeftDest);
    current.updateLeftRotateDestination(neighborsLeftDest);
    // 右回転でジャンプする先を更新
    int[][] patternsRightDest = minoRotation.getRightPatternsFrom(mino);
    Mino afterRight = minoFactory.create(mino.getPiece(), mino.getRotate().getRightRotate());
    ArrayList<Neighbor> neighborsRightDest = createDestinations(afterRight, x, y, patternsRightDest);
    current.updateRightRotateDestination(neighborsRightDest);
    // 左回転でここにジャンプしてくる元を更新
    Mino beforeLeft = minoFactory.create(mino.getPiece(), mino.getRotate().getRightRotate());
    int[][] patternsLeftSrc = minoRotation.getLeftPatternsFrom(beforeLeft);
    ArrayList<Neighbor> neighborsLeftSrc = createSources(beforeLeft, x, y, patternsLeftSrc);
    current.updateLeftRotateSource(neighborsLeftSrc);
    // 右回転でここにジャンプしてくる元を更新
    Mino beforeRight = minoFactory.create(mino.getPiece(), mino.getRotate().getLeftRotate());
    int[][] patternsRightSrc = minoRotation.getRightPatternsFrom(beforeRight);
    ArrayList<Neighbor> neighborsRightSrc = createSources(beforeRight, x, y, patternsRightSrc);
    current.updateRightRotateSource(neighborsRightSrc);
}
Also used : Mino(core.mino.Mino)

Aggregations

Mino (core.mino.Mino)103 Test (org.junit.jupiter.api.Test)46 Rotate (core.srs.Rotate)27 Field (core.field.Field)24 Piece (core.mino.Piece)20 Randoms (lib.Randoms)16 MinoFactory (core.mino.MinoFactory)14 ColorType (common.tetfu.common.ColorType)12 ColoredField (common.tetfu.field.ColoredField)12 ArrayList (java.util.ArrayList)12 FullOperationWithKey (common.datastore.FullOperationWithKey)9 HashSet (java.util.HashSet)9 ColorConverter (common.tetfu.common.ColorConverter)8 OriginalPiece (core.neighbor.OriginalPiece)8 List (java.util.List)8 MinoOperationWithKey (common.datastore.MinoOperationWithKey)7 OperationWithKey (common.datastore.OperationWithKey)7 TetfuPage (common.tetfu.TetfuPage)7 Arrays (java.util.Arrays)7 Collectors (java.util.stream.Collectors)7