use of common.tetfu.TetfuPage in project solution-finder by knewjade.
the class PngWriter method write.
@Override
public void write(List<TetfuPage> tetfuPages) throws IOException {
int page = startPageIndex + 1;
for (TetfuPage tetfuPage : tetfuPages) {
String path = String.format("%s_%03d.png", prefix, page);
// リセット
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();
// 画像の出力
ImageIO.write(image, "png", new File(path));
page++;
}
}
use of common.tetfu.TetfuPage 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;
}
use of common.tetfu.TetfuPage in project solution-finder by knewjade.
the class FigUtilEntryPoint method createGif.
private FigWriter createGif(MinoFactory minoFactory, ColorConverter colorConverter, FrameType frameType, File originalOutputFile, List<TetfuPage> usingTetfuPages) throws FinderException {
String outputFilePath = getRemoveExtensionFromPath(getCanonicalPath(originalOutputFile));
if (outputFilePath.isEmpty())
outputFilePath = "fig";
outputFilePath += ".gif";
File outputFile = new File(outputFilePath);
if (outputFile.isDirectory())
throw new FinderInitializeException("Cannot specify directory as output file path: Output=" + settings.getOutputFilePath());
if (outputFile.exists() && !outputFile.canWrite())
throw new FinderInitializeException("Cannot write output file: Output=" + settings.getOutputFilePath());
output(" .... Output to " + getCanonicalPath(outputFile));
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");
}
int nextBoxCount = settings.getNextBoxCount();
int delay = settings.getDelay();
boolean isInfiniteLoop = settings.getInfiniteLoop();
return new GifWriter(minoFactory, colorConverter, figGenerator, bag, nextBoxCount, delay, outputFile, isInfiniteLoop);
}
use of common.tetfu.TetfuPage in project solution-finder by knewjade.
the class FigUtilEntryPoint method useTetfuPages.
private List<TetfuPage> useTetfuPages() {
List<TetfuPage> tetfuPages = settings.getTetfuPages();
int startPageIndex = settings.getStartPageIndex();
int endPage = settings.getEndPage();
return tetfuPages.subList(startPageIndex, endPage);
}
use of common.tetfu.TetfuPage 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));
}
Aggregations