use of common.datastore.Operations in project solution-finder by knewjade.
the class BuildUpStreamTest method buildUp1.
@Test
void buildUp1() {
// Create LockedReachable
int height = 4;
MinoFactory minoFactory = new MinoFactory();
MinoShifter minoShifter = new MinoShifter();
MinoRotation minoRotation = new MinoRotation();
LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, height);
// Create OperationWithKey List
Field field = FieldFactory.createField("" + "____XXXXXX" + "____XXXXXX" + "____XXXXXX" + "____XXXXXX");
Operations operations = OperationInterpreter.parseToOperations("J,0,1,0;S,L,1,2;O,0,2,1;J,2,2,1");
List<MinoOperationWithKey> operationWithKeys = OperationTransform.parseToOperationWithKeys(field, operations, minoFactory, height);
// Create Pieces
Set<String> valid = new BuildUpStream(reachable, height).existsValidBuildPattern(field, operationWithKeys).map(op -> op.stream().map(OperationWithKey::getPiece).map(Piece::name).collect(Collectors.joining())).collect(Collectors.toSet());
// Assertion
assertThat(valid).hasSize(2).contains("JSOJ", "JOSJ");
}
use of common.datastore.Operations 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.datastore.Operations in project solution-finder by knewjade.
the class OperationInterpreterTest method parseRandom.
@Test
void parseRandom() throws Exception {
Randoms randoms = new Randoms();
for (int size = 1; size < 20; size++) {
List<Operation> operationList = Stream.generate(() -> {
Piece piece = randoms.block();
Rotate rotate = randoms.rotate();
int x = randoms.nextInt(10);
int y = randoms.nextInt(4);
return new SimpleOperation(piece, rotate, x, y);
}).limit(size).collect(Collectors.toList());
Operations operations = new Operations(operationList);
String str = OperationInterpreter.parseToString(operations);
Operations actual = OperationInterpreter.parseToOperations(str);
assertThat(actual).isEqualTo(operations);
}
}
use of common.datastore.Operations in project solution-finder by knewjade.
the class OperationTransformTest method parseToOperationWithKeys.
@Test
void parseToOperationWithKeys() throws Exception {
Field field = FieldFactory.createField("" + "____XXXXXX" + "____XXXXXX" + "____XXXXXX" + "____XXXXXX" + "");
String base = "L,0,2,0;Z,R,2,2;O,0,0,1;L,2,1,1";
Operations operations = OperationInterpreter.parseToOperations(base);
MinoFactory minoFactory = new MinoFactory();
List<MinoOperationWithKey> operationWithKeys = OperationTransform.parseToOperationWithKeys(field, operations, minoFactory, 4);
String line = OperationWithKeyInterpreter.parseToString(operationWithKeys);
assertThat(line).isEqualTo("L,0,2,0,0,1025;Z,R,2,2,0,1074791424;O,0,0,1,0,1049600;L,2,1,1,1049600,1073741825");
}
use of common.datastore.Operations 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);
}
});
});
}
}
Aggregations