use of common.tetfu.Tetfu in project solution-finder by knewjade.
the class MoveEntryPoint method encodeOnePage.
private String encodeOnePage(MinoFactory minoFactory, ColorConverter colorConverter, TetfuElement tetfuElement) {
Tetfu tetfu = new Tetfu(minoFactory, colorConverter);
List<TetfuElement> elementOnePage = Collections.singletonList(tetfuElement);
return "v115@" + tetfu.encode(elementOnePage);
}
use of common.tetfu.Tetfu in project solution-finder by knewjade.
the class SetupSettingParser method encodeTetfu.
private List<TetfuPage> encodeTetfu(String encoded) throws FinderParseException {
MinoFactory minoFactory = new MinoFactory();
ColorConverter colorConverter = new ColorConverter();
Tetfu tetfu = new Tetfu(minoFactory, colorConverter);
String data = Tetfu.removePrefixData(encoded);
if (data == null)
throw new FinderParseException("Unsupported tetfu: data=" + encoded);
return tetfu.decode(data);
}
use of common.tetfu.Tetfu in project solution-finder by knewjade.
the class FigUtilSettingParser method encodeTetfu.
private List<TetfuPage> encodeTetfu(String encoded) throws FinderParseException {
MinoFactory minoFactory = new MinoFactory();
ColorConverter colorConverter = new ColorConverter();
Tetfu tetfu = new Tetfu(minoFactory, colorConverter);
String data = Tetfu.removePrefixData(encoded);
if (data == null)
throw new FinderParseException("Unsupported tetfu: data=" + encoded);
return tetfu.decode(data);
}
use of common.tetfu.Tetfu in project solution-finder by knewjade.
the class SetupSettingParser method parse.
public Optional<SetupSettings> parse() throws FinderParseException {
Options options = createOptions();
CommandLineParser parser = new DefaultParser();
CommandLine commandLine = parseToCommandLine(options, parser, commands);
CommandLineWrapper wrapper = new NormalCommandLineWrapper(commandLine);
SetupSettings settings = new SetupSettings();
// help
if (wrapper.hasOption("help")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("percent [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 {
// フィールドファイルから
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 removeDomainData = Tetfu.removeDomainData(fieldLines.get(0));
if (Tetfu.isDataLater115(removeDomainData)) {
// テト譜から
wrapper = loadTetfu(removeDomainData, parser, options, wrapper, settings);
} else {
// 固定ピースの指定があるか
Optional<Boolean> reservedOption = wrapper.getBoolOption("reserved");
reservedOption.ifPresent(settings::setReserved);
// 最大削除ラインの設定
int maxHeightForce = -1;
try {
maxHeightForce = Integer.valueOf(fieldLines.peekFirst());
// 読み込みに成功したときだけ進める
fieldLines.pollFirst();
} catch (Exception ignore) {
}
// フィールドの設定
String fieldMarks = String.join("", fieldLines);
parseField(fieldMarks, settings, maxHeightForce);
}
} catch (IOException e) {
throw new FinderParseException("Cannot open field file", e);
}
}
// ログファイルの設定
Optional<String> logFilePath = wrapper.getStringOption("log-path");
logFilePath.ifPresent(settings::setLogFilePath);
// アウトプットファイルの設定
Optional<String> outputBaseFilePath = wrapper.getStringOption("output-base");
outputBaseFilePath.ifPresent(settings::setOutputBaseFilePath);
// ドロップの設定
Optional<String> dropType = wrapper.getStringOption("drop");
try {
dropType.ifPresent(type -> {
String key = dropType.orElse("softdrop");
try {
settings.setDropType(key);
} catch (FinderParseException e) {
throw new RuntimeException(e);
}
});
} catch (Exception e) {
throw new FinderParseException("Unsupported format: format=" + dropType.orElse("<empty>"));
}
// 探索パターンの設定
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);
}
use of common.tetfu.Tetfu 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);
}
Aggregations