use of common.tetfu.common.ColorType in project solution-finder by knewjade.
the class SequenceFumenParser method parse.
@Override
public String parse(List<MinoOperationWithKey> operations, Field field, int maxClearLine) {
Operations operations2 = OperationTransform.parseToOperations(field, operations, maxClearLine);
List<? extends Operation> operationsList = operations2.getOperations();
// ブロック順に変換
List<Piece> pieceList = operationsList.stream().map(Operation::getPiece).collect(Collectors.toList());
// テト譜を作成
String quiz = Tetfu.encodeForQuiz(pieceList);
ArrayList<TetfuElement> tetfuElements = new ArrayList<>();
// 最初のelement
Operation firstKey = operationsList.get(0);
ColorType colorType1 = colorConverter.parseToColorType(firstKey.getPiece());
ColoredField coloredField = createInitColoredField(field, maxClearLine);
TetfuElement firstElement = new TetfuElement(coloredField, colorType1, firstKey.getRotate(), firstKey.getX(), firstKey.getY(), quiz);
tetfuElements.add(firstElement);
// 2番目以降のelement
if (1 < operationsList.size()) {
operationsList.subList(1, operationsList.size()).stream().map(operation -> {
ColorType colorType = colorConverter.parseToColorType(operation.getPiece());
return new TetfuElement(colorType, operation.getRotate(), operation.getX(), operation.getY(), quiz);
}).forEach(tetfuElements::add);
}
Tetfu tetfu = new Tetfu(minoFactory, colorConverter);
return tetfu.encode(tetfuElements);
}
use of common.tetfu.common.ColorType in project solution-finder by knewjade.
the class TetfuTest method random.
@Test
@LongTest
void random() throws Exception {
// Initialize
Randoms randoms = new Randoms();
MinoFactory minoFactory = new MinoFactory();
MinoShifter minoShifter = new MinoShifter();
MinoRotation minoRotation = new MinoRotation();
ColorConverter colorConverter = new ColorConverter();
// Define size
int height = 4;
int basicWidth = 3;
SizedBit sizedBit = new SizedBit(basicWidth, height);
SeparableMinos separableMinos = SeparableMinos.createSeparableMinos(minoFactory, minoShifter, sizedBit);
// Create basic solutions
TaskResultHelper taskResultHelper = new Field4x10MinoPackingHelper();
LockedReachableThreadLocal lockedReachableThreadLocal = new LockedReachableThreadLocal(minoFactory, minoShifter, minoRotation, height);
Predicate<ColumnField> memorizedPredicate = (columnField) -> true;
OnDemandBasicSolutions basicSolutions = new OnDemandBasicSolutions(separableMinos, sizedBit, memorizedPredicate);
for (int count = 0; count < 20; count++) {
System.out.println(count);
// Create field
int numOfMinos = randoms.nextIntClosed(6, 10);
Field field = randoms.field(height, numOfMinos);
// Search
List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(basicWidth, height, field);
SolutionFilter solutionFilter = new SRSValidSolutionFilter(field, lockedReachableThreadLocal, sizedBit);
PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper);
Optional<Result> resultOptional = searcher.findAny();
BuildUpStream buildUpStream = new BuildUpStream(lockedReachableThreadLocal.get(), height);
// If found solution
resultOptional.ifPresent(result -> {
List<MinoOperationWithKey> list = result.getMemento().getSeparableMinoStream(basicWidth).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toList());
Optional<List<MinoOperationWithKey>> validOption = buildUpStream.existsValidBuildPattern(field, list).findAny();
validOption.ifPresent(operationWithKeys -> {
Operations operations = OperationTransform.parseToOperations(field, operationWithKeys, height);
List<TetfuElement> elements = operations.getOperations().stream().map(operation -> {
ColorType colorType = colorConverter.parseToColorType(operation.getPiece());
Rotate rotate = operation.getRotate();
int x = operation.getX();
int y = operation.getY();
String comment = randoms.string() + randoms.string() + randoms.string();
return new TetfuElement(colorType, rotate, x, y, comment);
}).collect(Collectors.toList());
String encode = new Tetfu(minoFactory, colorConverter).encode(elements);
List<TetfuPage> decode = decodeTetfu(minoFactory, colorConverter, encode);
assertThat(decode).hasSize(elements.size());
for (int index = 0; index < decode.size(); index++) {
TetfuElement element = elements.get(index);
assertThat(decode.get(index)).returns(element.getColorType(), TetfuPage::getColorType).returns(element.getRotate(), TetfuPage::getRotate).returns(element.getX(), TetfuPage::getX).returns(element.getY(), TetfuPage::getY).returns(element.getComment(), TetfuPage::getComment);
}
});
});
}
}
use of common.tetfu.common.ColorType in project solution-finder by knewjade.
the class PathSettings method setFieldWithReserved.
void setFieldWithReserved(ColoredField coloredField, int height) {
Field field = FieldFactory.createField(height);
BlockField blockField = new BlockField(height);
for (int y = 0; y < height; y++) {
for (int x = 0; x < 10; x++) {
ColorConverter colorConverter = new ColorConverter();
ColorType colorType = colorConverter.parseToColorType(coloredField.getBlockNumber(x, y));
switch(colorType) {
case Empty:
break;
case Gray:
field.setBlock(x, y);
break;
default:
Piece piece = colorConverter.parseToBlock(colorType);
blockField.setBlock(piece, x, y);
break;
}
}
}
setField(field);
setReservedBlock(blockField);
}
use of common.tetfu.common.ColorType 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));
}
use of common.tetfu.common.ColorType in project solution-finder by knewjade.
the class EasyTetfu method parseBlockFieldToTetfuElement.
private TetfuElement parseBlockFieldToTetfuElement(Field initField, ColorConverter colorConverter, BlockField blockField, String comment) {
ColoredField coloredField = ColoredFieldFactory.createGrayField(initField);
for (Piece piece : Piece.values()) {
Field target = blockField.get(piece);
ColorType colorType = colorConverter.parseToColorType(piece);
fillInField(coloredField, colorType, target);
}
return new TetfuElement(coloredField, ColorType.Empty, Rotate.Reverse, 0, 0, comment);
}
Aggregations