Search in sources :

Example 26 with ColoredField

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

the class OneFumenParser method createTetfuElement.

private TetfuElement createTetfuElement(Field initField, BlockField blockField, String comment, int maxClearLine) {
    ColoredField coloredField = createInitColoredField(initField, maxClearLine);
    for (Piece piece : Piece.values()) {
        Field target = blockField.get(piece);
        ColorType colorType = this.colorConverter.parseToColorType(piece);
        fillInField(coloredField, colorType, target, maxClearLine);
    }
    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 27 with ColoredField

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

the class OneFumenParser method createInitColoredField.

private ColoredField createInitColoredField(Field initField, int maxClearLine) {
    ColoredField coloredField = ColoredFieldFactory.createField(24);
    fillInField(coloredField, ColorType.Gray, initField, maxClearLine);
    return coloredField;
}
Also used : ColoredField(common.tetfu.field.ColoredField)

Example 28 with ColoredField

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

the class PercentSettingParser method loadTetfu.

private CommandLineWrapper loadTetfu(String data, CommandLineParser parser, Options options, CommandLineWrapper wrapper, PercentSettings 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, settings.getMaxClearLine());
    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) 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) 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 29 with ColoredField

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

the class DevRandomEntryPoint method run.

@Override
public void run() throws FinderException {
    PatternGenerator generator = createBlockGenerator(pattern);
    List<Pieces> blocks = generator.blocksStream().collect(Collectors.toList());
    int index = new Random().nextInt(blocks.size());
    Pieces selected = blocks.get(index);
    String quiz = Tetfu.encodeForQuiz(selected.getPieces());
    MinoFactory minoFactory = new MinoFactory();
    ColorConverter converter = new ColorConverter();
    ColoredField coloredField = getTetfu(minoFactory, converter);
    Tetfu tetfu = new Tetfu(minoFactory, converter);
    TetfuElement element = new TetfuElement(coloredField, quiz);
    String encode = tetfu.encode(Collections.singletonList(element));
    System.out.println("v115@" + encode);
}
Also used : PatternGenerator(common.pattern.PatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) ArrayColoredField(common.tetfu.field.ArrayColoredField) ColoredField(common.tetfu.field.ColoredField) Random(java.util.Random) ColorConverter(common.tetfu.common.ColorConverter) MinoFactory(core.mino.MinoFactory) Tetfu(common.tetfu.Tetfu) EntryPoint(entry.EntryPoint) Pieces(common.datastore.blocks.Pieces) TetfuElement(common.tetfu.TetfuElement)

Example 30 with ColoredField

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

the class MoveSettingParser method parse.

public Optional<MoveSettings> parse() throws FinderParseException {
    Options options = createOptions();
    CommandLineParser parser = new DefaultParser();
    CommandLine commandLine = parseToCommandLine(options, parser, commands);
    CommandLineWrapper wrapper = new NormalCommandLineWrapper(commandLine);
    MoveSettings settings = new MoveSettings();
    // help
    if (wrapper.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("path [options]", options);
        return Optional.empty();
    }
    // フィールド・最大削除ラインの設定
    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, parser, options, wrapper, settings);
    } else if (wrapper.hasOption("field-path")) {
        // フィールドファイルから
        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 clear-line & field-definition in field file");
            String removeDomainData = Tetfu.removeDomainData(fieldLines.get(0));
            if (Tetfu.isDataLater115(removeDomainData)) {
                // テト譜から
                wrapper = loadTetfu(removeDomainData, parser, options, wrapper, settings);
            } else {
                // フィールドの設定
                String fieldMarks = String.join("", fieldLines);
                ColoredField coloredField = ColoredFieldFactory.createColoredField(fieldMarks);
                settings.setField(coloredField);
            }
        } catch (NumberFormatException e) {
            throw new FinderParseException("Cannot read clear-line from " + fieldPath);
        } catch (IOException ignored) {
        }
    }
    // ログファイルの設定
    Optional<String> logFilePath = wrapper.getStringOption("log-path");
    logFilePath.ifPresent(settings::setLogFilePath);
    // アウトプットファイルの設定
    Optional<String> outputBaseFilePath = wrapper.getStringOption("output-base");
    outputBaseFilePath.ifPresent(settings::setOutputBaseFilePath);
    // 探索パターンの設定
    if (wrapper.hasOption("patterns")) {
        // パターン定義から
        Optional<String> patternOption = wrapper.getStringOption("patterns");
        assert patternOption.isPresent();
        String patternValue = patternOption.get();
        List<String> patterns = Arrays.stream(patternValue.split(PATTERN_DELIMITER)).collect(Collectors.toList());
        settings.setPatterns(patterns);
    } else {
        // パターンファイルから
        Optional<String> patternPathOption = wrapper.getStringOption("patterns-path");
        String patternPath = patternPathOption.orElse(DEFAULT_PATTERNS_TXT);
        Path path = Paths.get(patternPath);
        Charset charset = Charset.forName(CHARSET_NAME);
        try {
            List<String> patterns = Files.lines(path, charset).collect(Collectors.toList());
            settings.setPatterns(patterns);
        } catch (IOException e) {
            throw new FinderParseException("Cannot open patterns file", e);
        }
    }
    return Optional.of(settings);
}
Also used : Path(java.nio.file.Path) ColoredField(common.tetfu.field.ColoredField) Optional(java.util.Optional) Charset(java.nio.charset.Charset) IOException(java.io.IOException) LinkedList(java.util.LinkedList) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) FinderParseException(exceptions.FinderParseException) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper) CommandLineWrapper(entry.CommandLineWrapper)

Aggregations

ColoredField (common.tetfu.field.ColoredField)30 ColorType (common.tetfu.common.ColorType)19 Field (core.field.Field)12 Piece (core.mino.Piece)11 ColorConverter (common.tetfu.common.ColorConverter)10 Mino (core.mino.Mino)10 MinoFactory (core.mino.MinoFactory)9 Rotate (core.srs.Rotate)9 TetfuPage (common.tetfu.TetfuPage)8 List (java.util.List)8 Tetfu (common.tetfu.Tetfu)7 FinderParseException (exceptions.FinderParseException)7 LinkedList (java.util.LinkedList)7 Collectors (java.util.stream.Collectors)7 TetfuElement (common.tetfu.TetfuElement)6 ColoredFieldFactory (common.tetfu.field.ColoredFieldFactory)6 CommandLineWrapper (entry.CommandLineWrapper)6 NormalCommandLineWrapper (entry.NormalCommandLineWrapper)6 PriorityCommandLineWrapper (entry.PriorityCommandLineWrapper)6 IOException (java.io.IOException)6