use of core.srs.Rotate 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 core.srs.Rotate in project solution-finder by knewjade.
the class OperationHistoryTest method random.
@Test
void random() throws ExecutionException, InterruptedException {
Randoms randoms = new Randoms();
for (int count = 0; count < 1000; count++) {
int size = randoms.nextInt(1, 10);
ArrayList<Operation> operations = new ArrayList<>();
OperationHistory history = new OperationHistory(size);
for (int index = 0; index < size; index++) {
Piece piece = randoms.block();
Rotate rotate = randoms.rotate();
int y = randoms.nextInt(4);
int x = randoms.nextInt(10);
MinimalAction action = MinimalAction.create(x, y, rotate);
history = history.recordAndReturnNew(piece, action);
operations.add(new SimpleOperation(piece, rotate, x, y));
}
List<Operation> actual = history.getOperationStream().collect(Collectors.toList());
assertThat(actual).isEqualTo(operations);
}
}
use of core.srs.Rotate in project solution-finder by knewjade.
the class OperationWithKeyInterpreterTest method parseRandom.
@Test
void parseRandom() throws Exception {
Randoms randoms = new Randoms();
MinoFactory minoFactory = new MinoFactory();
for (int size = 1; size < 20; size++) {
List<OperationWithKey> operations = Stream.generate(() -> {
Piece piece = randoms.block();
Rotate rotate = randoms.rotate();
int x = randoms.nextInt(10);
int y = randoms.nextInt(4);
long deleteKey = randoms.key();
long usingKey = randoms.key();
return new FullOperationWithKey(minoFactory.create(piece, rotate), x, y, deleteKey, usingKey);
}).limit(size).collect(Collectors.toList());
String str = OperationWithKeyInterpreter.parseToString(operations);
List<MinoOperationWithKey> actual = OperationWithKeyInterpreter.parseToList(str, minoFactory);
assertThat(actual).isEqualTo(operations);
}
}
use of core.srs.Rotate in project solution-finder by knewjade.
the class GifWriter method write.
@Override
public void write(List<TetfuPage> tetfuPages) throws IOException {
boolean isLoop = isInfiniteLoop;
try (ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputFile)) {
// imageWriterの準備
ImageWriter imageWriter = getGifImageWriter();
imageWriter.setOutput(imageOutputStream);
imageWriter.prepareWriteSequence(null);
for (TetfuPage tetfuPage : tetfuPages) {
// リセット
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();
// メタデータの作成
IIOMetadata metadata = createMetadata(imageWriter, image, delay, isLoop);
IIOImage iioImage = new IIOImage(image, null, metadata);
imageWriter.writeToSequence(iioImage, null);
// 無限ループの設定は最大1度までで十分
isLoop = false;
}
// imageWriterの終了処理
imageWriter.endWriteSequence();
}
}
use of core.srs.Rotate in project solution-finder by knewjade.
the class DeleteKeyParser method initializeMaps.
private EnumMap<Piece, EnumMap<Rotate, List<DeleteKey>>> initializeMaps(MinoFactory minoFactory, int maxClearLine) {
EnumMap<Piece, EnumMap<Rotate, List<DeleteKey>>> maps = new EnumMap<>(Piece.class);
for (Piece piece : Piece.values()) {
EnumMap<Rotate, List<DeleteKey>> rotateMaps = maps.computeIfAbsent(piece, blk -> new EnumMap<>(Rotate.class));
for (Rotate rotate : Rotate.values()) {
Mino mino = minoFactory.create(piece, rotate);
// ミノの高さを計算
int minoHeight = mino.getMaxY() - mino.getMinY() + 1;
// 行候補をリストにする
ArrayList<Integer> lineIndexes = new ArrayList<>();
for (int index = 0; index < maxClearLine; index++) lineIndexes.add(index);
// ブロックが置かれる行を選択する
CombinationIterable<Integer> combinationIterable = new CombinationIterable<>(lineIndexes, minoHeight);
// リストアップ
ArrayList<DeleteKey> deleteLimitedMinos = new ArrayList<>();
for (List<Integer> indexes : combinationIterable) {
// ソートする
indexes.sort(Integer::compare);
// 一番下の行と一番上の行を取得
int lowerY = indexes.get(0);
int upperY = indexes.get(indexes.size() - 1);
// ミノに挟まれる全ての行を含むdeleteKey
long deleteKey = KeyOperators.getMaskForKeyAboveY(lowerY) & KeyOperators.getMaskForKeyBelowY(upperY + 1);
long usingKey = 0L;
assert Long.bitCount(deleteKey) == upperY - lowerY + 1;
for (Integer index : indexes) {
long bitKey = KeyOperators.getDeleteBitKey(index);
// ブロックのある行のフラグを取り消す
deleteKey &= ~bitKey;
// ブロックのある行のフラグをたてる
usingKey |= bitKey;
}
assert Long.bitCount(deleteKey) + indexes.size() == upperY - lowerY + 1;
deleteLimitedMinos.add(DeleteKey.create(mino, deleteKey, usingKey, lowerY, upperY));
}
rotateMaps.put(rotate, deleteLimitedMinos);
}
}
return maps;
}
Aggregations