Search in sources :

Example 1 with FrameType

use of util.fig.FrameType 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 FrameType

use of util.fig.FrameType in project solution-finder by knewjade.

the class FigUtilEntryPoint method run.

@Override
public void run() throws FinderException {
    output("# Setup");
    output("Version = " + FinderConstant.VERSION);
    MinoFactory minoFactory = new MinoFactory();
    ColorConverter colorConverter = new ColorConverter();
    FrameType frameType = settings.getFrameType();
    File outputFile = new File(settings.getOutputFilePath());
    output();
    output("# Generate");
    output("  -> Stopwatch start");
    Stopwatch stopwatch = Stopwatch.createStartedStopwatch();
    List<TetfuPage> usingTetfuPages = useTetfuPages();
    FigFormat figFormat = settings.getFigFormat();
    FigWriter figWriter = createFigWriter(minoFactory, colorConverter, frameType, outputFile, figFormat, usingTetfuPages);
    generate(figWriter, usingTetfuPages);
    stopwatch.stop();
    output("  -> Stopwatch stop : " + stopwatch.toMessage(TimeUnit.MILLISECONDS));
}
Also used : TetfuPage(common.tetfu.TetfuPage) FigWriter(util.fig.output.FigWriter) FrameType(util.fig.FrameType) Stopwatch(lib.Stopwatch) ColorConverter(common.tetfu.common.ColorConverter) MinoFactory(core.mino.MinoFactory) File(java.io.File)

Example 3 with FrameType

use of util.fig.FrameType in project solution-finder by knewjade.

the class FigUtilSettingParser method parse.

public Optional<FigUtilSettings> parse() throws FinderParseException {
    Options options = createOptions();
    CommandLineParser parser = new DefaultParser();
    CommandLine commandLine = parseToCommandLine(options, parser);
    CommandLineWrapper wrapper = new NormalCommandLineWrapper(commandLine);
    FigUtilSettings settings = new FigUtilSettings();
    // help
    if (wrapper.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("util fig [options]", options);
        return Optional.empty();
    }
    // ホールドの設定
    Optional<Boolean> isUsingHold = wrapper.getBoolOption("hold");
    isUsingHold.ifPresent(settings::setUsingHold);
    // ループの設定
    Optional<Boolean> isInfiniteLoop = wrapper.getBoolOption("loop");
    isInfiniteLoop.ifPresent(settings::setInfiniteLoop);
    // アウトプットファイルの設定
    Optional<String> outputPath = wrapper.getStringOption("output");
    outputPath.ifPresent(settings::setOutputFilePath);
    // ディレイタイムの設定
    Optional<Integer> delay = wrapper.getIntegerOption("delay");
    try {
        delay.ifPresent(value -> {
            if (value <= 0)
                throw new RuntimeException("Delay should be positive: delay=" + value);
            settings.setDelay(value);
        });
    } catch (RuntimeException e) {
        throw new FinderParseException(e.getMessage());
    }
    // フレームの設定
    Optional<String> frameTypeName = wrapper.getStringOption("frame");
    try {
        Optional<FrameType> frameType = frameTypeName.map(frame -> {
            try {
                return parseFrameType(frame);
            } catch (UnsupportedDataTypeException e) {
                throw new RuntimeException(e);
            }
        });
        frameType.ifPresent(settings::setFrameType);
    } catch (RuntimeException e) {
        throw new FinderParseException("Unsupported frame: frame=" + frameTypeName.orElse("<empty>"));
    }
    // フォーマットの設定
    Optional<String> formatName = wrapper.getStringOption("format");
    try {
        Optional<FigFormat> figFormat = formatName.map(format -> {
            try {
                return parseFigFormat(format);
            } catch (UnsupportedDataTypeException e) {
                throw new RuntimeException(e);
            }
        });
        figFormat.ifPresent(settings::setFigFormat);
    } catch (RuntimeException e) {
        throw new FinderParseException("Unsupported format: format=" + formatName.orElse("<empty>"));
    }
    // 高さの設定
    Optional<Integer> height = wrapper.getIntegerOption("line");
    try {
        height.ifPresent(value -> {
            if (value == 0)
                throw new RuntimeException("Line should be positive or -1: line=" + value);
            settings.setHeight(value);
        });
    } catch (RuntimeException e) {
        throw new FinderParseException(e.getMessage());
    }
    // テト譜の設定
    if (wrapper.hasOption("tetfu")) {
        // パラメータから
        Optional<String> tetfuData = wrapper.getStringOption("tetfu");
        if (!tetfuData.isPresent())
            throw new FinderParseException("Should specify option value: --tetfu");
        String encoded = Tetfu.removeDomainData(tetfuData.get());
        wrapper = loadTetfu(encoded, wrapper, settings);
    } else {
        // フィールドファイルから
        Optional<String> fieldPathOption = wrapper.getStringOption("field-path");
        String fieldPath = fieldPathOption.orElse(DEFAULT_FIELD_TXT);
        Path path = Paths.get(fieldPath);
        Charset charset = Charset.forName(CHARSET_NAME);
        try {
            LinkedList<String> fieldLines = Files.lines(path, charset).map(str -> {
                if (str.contains("#"))
                    return str.substring(0, str.indexOf('#'));
                return str;
            }).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toCollection(LinkedList::new));
            if (fieldLines.isEmpty())
                throw new FinderParseException("Should specify field-definition in field file");
            String encoded = fieldLines.get(0);
            String removeDomainData = Tetfu.removeDomainData(encoded);
            wrapper = loadTetfu(removeDomainData, wrapper, settings);
        } catch (IOException e) {
            throw new FinderParseException("Cannot open field file", e);
        }
    }
    // ネクストの設定
    Optional<Integer> next = wrapper.getIntegerOption("next");
    Optional<Integer> positiveNext = next.map(integer -> 0 < integer ? integer : 0);
    positiveNext.ifPresent(settings::setNextBoxCount);
    return Optional.of(settings);
}
Also used : TetfuPage(common.tetfu.TetfuPage) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) Files(java.nio.file.Files) org.apache.commons.cli(org.apache.commons.cli) FrameType(util.fig.FrameType) IOException(java.io.IOException) Tetfu(common.tetfu.Tetfu) FinderParseException(exceptions.FinderParseException) ColorConverter(common.tetfu.common.ColorConverter) Collectors(java.util.stream.Collectors) List(java.util.List) MinoFactory(core.mino.MinoFactory) Charset(java.nio.charset.Charset) Paths(java.nio.file.Paths) Optional(java.util.Optional) UnsupportedDataTypeException(javax.activation.UnsupportedDataTypeException) CommandLineWrapper(entry.CommandLineWrapper) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) FrameType(util.fig.FrameType) UnsupportedDataTypeException(javax.activation.UnsupportedDataTypeException) Path(java.nio.file.Path) Charset(java.nio.charset.Charset) IOException(java.io.IOException) FinderParseException(exceptions.FinderParseException) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) CommandLineWrapper(entry.CommandLineWrapper)

Aggregations

TetfuPage (common.tetfu.TetfuPage)3 ColorConverter (common.tetfu.common.ColorConverter)3 MinoFactory (core.mino.MinoFactory)3 FrameType (util.fig.FrameType)3 List (java.util.List)2 Tetfu (common.tetfu.Tetfu)1 ColorType (common.tetfu.common.ColorType)1 ColoredField (common.tetfu.field.ColoredField)1 Mino (core.mino.Mino)1 Piece (core.mino.Piece)1 Rotate (core.srs.Rotate)1 CommandLineWrapper (entry.CommandLineWrapper)1 NormalCommandLineWrapper (entry.NormalCommandLineWrapper)1 FinderParseException (exceptions.FinderParseException)1 File (java.io.File)1 IOException (java.io.IOException)1 Charset (java.nio.charset.Charset)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1