Search in sources :

Example 41 with Rotate

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

the class SetupSettingParser method loadTetfu.

private CommandLineWrapper loadTetfu(String data, CommandLineParser parser, Options options, CommandLineWrapper wrapper, SetupSettings settings) throws FinderParseException {
    // テト譜面のエンコード
    List<TetfuPage> decoded = encodeTetfu(data);
    // 指定されたページを抽出
    int page = wrapper.getIntegerOption("page").orElse(1);
    TetfuPage tetfuPage = extractTetfuPage(decoded, page);
    // コメントの抽出
    String comment = 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);
        wrapper = new PriorityCommandLineWrapper(Arrays.asList(wrapper, newWrapper));
    } catch (FinderParseException ignore) {
    }
    // 固定ピースの指定があるか
    // Optional<Boolean> reservedOption = wrapper.getBoolOption("reserved");
    // reservedOption.ifPresent(settings::setReserved);
    // マージン色の指定があるか
    Optional<String> fillColorOption = wrapper.getStringOption("fill");
    if (fillColorOption.isPresent()) {
        settings.setFillColorType(fillColorOption.get());
    }
    // マージン色の指定があるか
    Optional<String> marginColorOption = wrapper.getStringOption("margin");
    if (marginColorOption.isPresent()) {
        settings.setMarginColorType(marginColorOption.get());
    }
    // フィールドを設定
    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);
    }
    // 最大削除ラインの設定
    Optional<Integer> maxHeightOption = wrapper.getIntegerOption("line");
    int maxHeight = maxHeightOption.orElse(coloredField.getUsingHeight());
    if (settings.isReserved()) {
        Field initField = FieldFactory.createField(maxHeight);
        Field needFilledField = FieldFactory.createField(maxHeight);
        Field notFilledField = FieldFactory.createField(maxHeight);
        ColorType marginColorType = settings.getMarginColorType();
        ColorType fillColorType = settings.getFillColorType();
        for (int y = 0; y < maxHeight; y++) {
            for (int x = 0; x < 10; x++) {
                ColorType colorType = coloredField.getColorType(x, y);
                if (colorType.equals(marginColorType)) {
                    coloredField.setColorType(ColorType.Empty, x, y);
                } else if (colorType.equals(fillColorType)) {
                    coloredField.setColorType(ColorType.Empty, x, y);
                    needFilledField.setBlock(x, y);
                } else {
                    switch(colorType) {
                        case Gray:
                            initField.setBlock(x, y);
                            notFilledField.setBlock(x, y);
                            coloredField.setColorType(ColorType.Empty, x, y);
                            break;
                        case Empty:
                            notFilledField.setBlock(x, y);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        settings.setFieldWithReserved(initField, needFilledField, notFilledField, coloredField, maxHeight);
    } else {
        Field initField = FieldFactory.createField(maxHeight);
        Field needFilledField = FieldFactory.createField(maxHeight);
        Field notFilledField = FieldFactory.createField(maxHeight);
        ColorType marginColorType = settings.getMarginColorType();
        ColorType fillColorType = settings.getFillColorType();
        for (int y = 0; y < maxHeight; y++) {
            for (int x = 0; x < 10; x++) {
                ColorType colorType = coloredField.getColorType(x, y);
                if (colorType.equals(marginColorType)) {
                // skip
                } else if (colorType.equals(fillColorType)) {
                    needFilledField.setBlock(x, y);
                } else {
                    switch(colorType) {
                        case Empty:
                            notFilledField.setBlock(x, y);
                            break;
                        case Gray:
                        default:
                            initField.setBlock(x, y);
                            notFilledField.setBlock(x, y);
                            break;
                    }
                }
            }
        }
        settings.setField(initField, needFilledField, notFilledField, maxHeight);
    }
    return wrapper;
}
Also used : TetfuPage(common.tetfu.TetfuPage) Arrays(java.util.Arrays) ColorType(common.tetfu.common.ColorType) org.apache.commons.cli(org.apache.commons.cli) Tetfu(common.tetfu.Tetfu) FinderParseException(exceptions.FinderParseException) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) Charset(java.nio.charset.Charset) FieldFactory(core.field.FieldFactory) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) Files(java.nio.file.Files) IOException(java.io.IOException) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) Collectors(java.util.stream.Collectors) List(java.util.List) Field(core.field.Field) Paths(java.nio.file.Paths) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper) Optional(java.util.Optional) CommandLineWrapper(entry.CommandLineWrapper) ColoredField(common.tetfu.field.ColoredField) Mino(core.mino.Mino) TetfuPage(common.tetfu.TetfuPage) ColoredField(common.tetfu.field.ColoredField) Rotate(core.srs.Rotate) Field(core.field.Field) ColoredField(common.tetfu.field.ColoredField) 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)

Example 42 with Rotate

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

the class OperationHistoryTest method random.

@Test
void random() throws ExecutionException, InterruptedException {
    Randoms randoms = new Randoms();
    for (int count = 0; count < 1000; count++) {
        int size = randoms.nextInt(1, 10);
        ArrayList<Operation> operations = new ArrayList<>();
        OperationHistory history = new OperationHistory(size);
        for (int index = 0; index < size; index++) {
            Piece piece = randoms.block();
            Rotate rotate = randoms.rotate();
            int y = randoms.nextInt(4);
            int x = randoms.nextInt(10);
            MinimalAction action = MinimalAction.create(x, y, rotate);
            history = history.recordAndReturnNew(piece, action);
            operations.add(new SimpleOperation(piece, rotate, x, y));
        }
        List<Operation> actual = history.getOperationStream().collect(Collectors.toList());
        assertThat(actual).isEqualTo(operations);
    }
}
Also used : Randoms(lib.Randoms) MinimalAction(common.datastore.action.MinimalAction) Rotate(core.srs.Rotate) Piece(core.mino.Piece) ArrayList(java.util.ArrayList) SimpleOperation(common.datastore.SimpleOperation) SimpleOperation(common.datastore.SimpleOperation) Operation(common.datastore.Operation) Test(org.junit.jupiter.api.Test)

Example 43 with Rotate

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

the class OperationWithKeyInterpreterTest method parseRandom.

@Test
void parseRandom() throws Exception {
    Randoms randoms = new Randoms();
    MinoFactory minoFactory = new MinoFactory();
    for (int size = 1; size < 20; size++) {
        List<OperationWithKey> operations = Stream.generate(() -> {
            Piece piece = randoms.block();
            Rotate rotate = randoms.rotate();
            int x = randoms.nextInt(10);
            int y = randoms.nextInt(4);
            long deleteKey = randoms.key();
            long usingKey = randoms.key();
            return new FullOperationWithKey(minoFactory.create(piece, rotate), x, y, deleteKey, usingKey);
        }).limit(size).collect(Collectors.toList());
        String str = OperationWithKeyInterpreter.parseToString(operations);
        List<MinoOperationWithKey> actual = OperationWithKeyInterpreter.parseToList(str, minoFactory);
        assertThat(actual).isEqualTo(operations);
    }
}
Also used : Randoms(lib.Randoms) MinoOperationWithKey(common.datastore.MinoOperationWithKey) OperationWithKey(common.datastore.OperationWithKey) FullOperationWithKey(common.datastore.FullOperationWithKey) MinoOperationWithKey(common.datastore.MinoOperationWithKey) Rotate(core.srs.Rotate) Piece(core.mino.Piece) MinoFactory(core.mino.MinoFactory) FullOperationWithKey(common.datastore.FullOperationWithKey) Test(org.junit.jupiter.api.Test)

Example 44 with Rotate

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

the class GifWriter method write.

@Override
public void write(List<TetfuPage> tetfuPages) throws IOException {
    boolean isLoop = isInfiniteLoop;
    try (ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputFile)) {
        // imageWriterの準備
        ImageWriter imageWriter = getGifImageWriter();
        imageWriter.setOutput(imageOutputStream);
        imageWriter.prepareWriteSequence(null);
        for (TetfuPage tetfuPage : tetfuPages) {
            // リセット
            figGenerator.reset();
            // 現在のミノを取得
            ColorType colorType = tetfuPage.getColorType();
            Rotate rotate = tetfuPage.getRotate();
            Mino mino = ColorType.isMinoBlock(colorType) ? minoFactory.create(colorConverter.parseToBlock(colorType), rotate) : null;
            int x = tetfuPage.getX();
            int y = tetfuPage.getY();
            // フィールドの更新
            ColoredField field = tetfuPage.getField();
            figGenerator.updateField(field, mino, x, y);
            // ミノを置くかチェック
            if (ColorType.isMinoBlock(colorType)) {
                // 現在のミノの更新
                figGenerator.updateMino(colorType, rotate, x, y);
                // bagの更新
                Piece piece = colorConverter.parseToBlock(colorType);
                bag.use(piece);
            }
            // ネクストの更新
            figGenerator.updateNext(bag.getNext(nextBoxCount));
            // ホールドの更新
            figGenerator.updateHold(bag.getHold());
            // 画像の生成
            BufferedImage image = figGenerator.fix();
            // メタデータの作成
            IIOMetadata metadata = createMetadata(imageWriter, image, delay, isLoop);
            IIOImage iioImage = new IIOImage(image, null, metadata);
            imageWriter.writeToSequence(iioImage, null);
            // 無限ループの設定は最大1度までで十分
            isLoop = false;
        }
        // imageWriterの終了処理
        imageWriter.endWriteSequence();
    }
}
Also used : TetfuPage(common.tetfu.TetfuPage) ColoredField(common.tetfu.field.ColoredField) Rotate(core.srs.Rotate) BufferedImage(java.awt.image.BufferedImage) IIOMetadata(javax.imageio.metadata.IIOMetadata) Piece(core.mino.Piece) ColorType(common.tetfu.common.ColorType) Mino(core.mino.Mino) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 45 with Rotate

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

the class DeleteKeyParser method initializeMaps.

private EnumMap<Piece, EnumMap<Rotate, List<DeleteKey>>> initializeMaps(MinoFactory minoFactory, int maxClearLine) {
    EnumMap<Piece, EnumMap<Rotate, List<DeleteKey>>> maps = new EnumMap<>(Piece.class);
    for (Piece piece : Piece.values()) {
        EnumMap<Rotate, List<DeleteKey>> rotateMaps = maps.computeIfAbsent(piece, blk -> new EnumMap<>(Rotate.class));
        for (Rotate rotate : Rotate.values()) {
            Mino mino = minoFactory.create(piece, rotate);
            // ミノの高さを計算
            int minoHeight = mino.getMaxY() - mino.getMinY() + 1;
            // 行候補をリストにする
            ArrayList<Integer> lineIndexes = new ArrayList<>();
            for (int index = 0; index < maxClearLine; index++) lineIndexes.add(index);
            // ブロックが置かれる行を選択する
            CombinationIterable<Integer> combinationIterable = new CombinationIterable<>(lineIndexes, minoHeight);
            // リストアップ
            ArrayList<DeleteKey> deleteLimitedMinos = new ArrayList<>();
            for (List<Integer> indexes : combinationIterable) {
                // ソートする
                indexes.sort(Integer::compare);
                // 一番下の行と一番上の行を取得
                int lowerY = indexes.get(0);
                int upperY = indexes.get(indexes.size() - 1);
                // ミノに挟まれる全ての行を含むdeleteKey
                long deleteKey = KeyOperators.getMaskForKeyAboveY(lowerY) & KeyOperators.getMaskForKeyBelowY(upperY + 1);
                long usingKey = 0L;
                assert Long.bitCount(deleteKey) == upperY - lowerY + 1;
                for (Integer index : indexes) {
                    long bitKey = KeyOperators.getDeleteBitKey(index);
                    // ブロックのある行のフラグを取り消す
                    deleteKey &= ~bitKey;
                    // ブロックのある行のフラグをたてる
                    usingKey |= bitKey;
                }
                assert Long.bitCount(deleteKey) + indexes.size() == upperY - lowerY + 1;
                deleteLimitedMinos.add(DeleteKey.create(mino, deleteKey, usingKey, lowerY, upperY));
            }
            rotateMaps.put(rotate, deleteLimitedMinos);
        }
    }
    return maps;
}
Also used : Rotate(core.srs.Rotate) ArrayList(java.util.ArrayList) CombinationIterable(common.iterable.CombinationIterable) Piece(core.mino.Piece) Mino(core.mino.Mino) List(java.util.List) ArrayList(java.util.ArrayList) EnumMap(java.util.EnumMap)

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