Search in sources :

Example 1 with TetfuPage

use of common.tetfu.TetfuPage 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 2 with TetfuPage

use of common.tetfu.TetfuPage 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)

Example 3 with TetfuPage

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

the class FigUtilEntryPoint method createPng.

private FigWriter createPng(MinoFactory minoFactory, ColorConverter colorConverter, FrameType frameType, List<TetfuPage> usingTetfuPages) throws FinderException {
    // 日付から新しいディレクトリ名を生成
    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
    String dateDirName = format.format(date);
    // 出力先の親ディレクトリを取得
    File originOutputFile = new File(settings.getOutputFilePath());
    File parentDirectory = originOutputFile.getParentFile();
    // 出力先ディレクトリを作成
    String baseName = getCanonicalPath(parentDirectory) + File.separatorChar + dateDirName;
    File outputDirectoryFile = new File(baseName);
    for (int suffix = 0; outputDirectoryFile.exists(); suffix++) {
        outputDirectoryFile = new File(baseName + "_" + suffix);
    }
    // ファイル名の取得
    String outputFileName = getRemoveExtensionFromPath(originOutputFile.getName());
    if (outputFileName.isEmpty())
        outputFileName = "fig";
    // 出力先ディレクトリがない場合は作成
    if (!outputDirectoryFile.exists()) {
        boolean mkdirsSuccess = outputDirectoryFile.mkdirs();
        if (!mkdirsSuccess) {
            throw new FinderInitializeException("Failed to make output directory: OutputFilePath=" + originOutputFile.getName());
        }
    }
    output("  .... Output to " + getCanonicalPath(outputDirectoryFile));
    Quiz quiz = parseQuiz();
    // generatorの準備
    boolean usingHold = settings.isUsingHold();
    FigGenerator figGenerator = createFigGenerator(frameType, usingHold, minoFactory, colorConverter);
    // Bagの作成
    List<TetfuPage> tetfuPages = settings.getTetfuPages();
    int startPageIndex = settings.getStartPageIndex();
    int endPage = settings.getEndPage();
    Bag bag = createBag(colorConverter, startPageIndex, tetfuPages, quiz, usingTetfuPages);
    // もし開始ページ以降にQuizが含まれるときは無視することを警告
    if (tetfuPages.subList(startPageIndex + 1, endPage).stream().map(TetfuPage::getComment).anyMatch(s -> s.startsWith("#Q="))) {
        output("#### WARNING: Contains Quiz in tetfu after start page. ignored");
    }
    String path = String.format("%s" + File.separatorChar + "%s", getCanonicalPath(outputDirectoryFile), outputFileName);
    int nextBoxCount = settings.getNextBoxCount();
    return new PngWriter(minoFactory, colorConverter, figGenerator, bag, nextBoxCount, path, startPageIndex);
}
Also used : TetfuPage(common.tetfu.TetfuPage) FinderInitializeException(exceptions.FinderInitializeException) Bag(util.fig.Bag) Date(java.util.Date) EntryPoint(entry.EntryPoint) PngWriter(util.fig.output.PngWriter) FieldOnlyFigGenerator(util.fig.generator.FieldOnlyFigGenerator) NoHoldFigGenerator(util.fig.generator.NoHoldFigGenerator) AllFigGenerator(util.fig.generator.AllFigGenerator) FigGenerator(util.fig.generator.FigGenerator) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File)

Example 4 with TetfuPage

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

the class FigUtilEntryPoint method parseQuiz.

private Quiz parseQuiz() {
    // 開始ページまでにQuizが含まれているかを確認する
    int startPageIndex = settings.getStartPageIndex();
    List<TetfuPage> tetfuPages = settings.getTetfuPages();
    Quiz quiz = Quiz.EMPTY;
    for (int index = startPageIndex; 0 <= index; index--) {
        TetfuPage tetfuPage = tetfuPages.get(index);
        String comment = tetfuPage.getComment();
        if (comment.startsWith("#Q="))
            return new Quiz(comment, index);
    }
    return quiz;
}
Also used : TetfuPage(common.tetfu.TetfuPage) EntryPoint(entry.EntryPoint)

Example 5 with TetfuPage

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

the class FigUtilSettingParser method loadTetfu.

private CommandLineWrapper loadTetfu(String data, CommandLineWrapper wrapper, FigUtilSettings settings) throws FinderParseException {
    // テト譜面のエンコード
    List<TetfuPage> tetfuPages = encodeTetfu(data);
    // 指定されたページを抽出
    // // 開始ページ
    int startPage = wrapper.getIntegerOption("start").orElse(1);
    if (startPage <= 0)
        throw new FinderParseException(String.format("Tetfu-start-page should be 1 <= page: StartPage=%d", startPage));
    if (tetfuPages.size() < startPage)
        throw new FinderParseException(String.format("Tetfu-start-page is over max page: StartPage=%d", startPage));
    // // 終了ページ
    int endPage = wrapper.getIntegerOption("end").orElse(-1);
    if (endPage == -1)
        endPage = tetfuPages.size();
    if (endPage < startPage)
        throw new FinderParseException(String.format("Tetfu-end-page should be %d <= page: EndPage=%d", startPage, endPage));
    if (tetfuPages.size() < endPage)
        throw new FinderParseException(String.format("Tetfu-end-page is over max page: EndPage=%d", endPage));
    settings.setTetfuPages(tetfuPages, startPage, endPage);
    return wrapper;
}
Also used : TetfuPage(common.tetfu.TetfuPage) FinderParseException(exceptions.FinderParseException)

Aggregations

TetfuPage (common.tetfu.TetfuPage)14 ColorType (common.tetfu.common.ColorType)7 ColoredField (common.tetfu.field.ColoredField)7 Mino (core.mino.Mino)7 Rotate (core.srs.Rotate)7 ColorConverter (common.tetfu.common.ColorConverter)6 MinoFactory (core.mino.MinoFactory)6 Tetfu (common.tetfu.Tetfu)5 FinderParseException (exceptions.FinderParseException)5 List (java.util.List)5 CommandLineWrapper (entry.CommandLineWrapper)4 EntryPoint (entry.EntryPoint)4 NormalCommandLineWrapper (entry.NormalCommandLineWrapper)4 PriorityCommandLineWrapper (entry.PriorityCommandLineWrapper)4 File (java.io.File)4 IOException (java.io.IOException)4 Charset (java.nio.charset.Charset)4 Files (java.nio.file.Files)4 Path (java.nio.file.Path)4 Paths (java.nio.file.Paths)4