use of core.srs.Rotate 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 core.srs.Rotate 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 core.srs.Rotate in project solution-finder by knewjade.
the class MinimalLockedCacheTest method assertCache.
private void assertCache(int height) {
MinimalLockedCache cache = new MinimalLockedCache(height);
for (int y = 0; y < height; y++) {
for (int x = 0; x < 10; x++) {
for (Rotate rotate : Rotate.values()) {
assertThat(cache.isVisit(x, y, rotate)).isFalse();
cache.visit(x, y, rotate);
assertThat(cache.isVisit(x, y, rotate)).isTrue();
}
}
}
cache.clear();
for (int y = 0; y < height; y++) {
for (int x = 0; x < 10; x++) {
for (Rotate rotate : Rotate.values()) {
assertThat(cache.isVisit(x, y, rotate)).isFalse();
}
}
}
}
use of core.srs.Rotate in project solution-finder by knewjade.
the class LimitIterationCandidate method search.
@Override
public Set<Action> search(Field field, Piece piece, int appearY) {
// temporaryの初期化
this.appearY = appearY;
HashSet<Action> actions = new HashSet<>();
for (Rotate rotate : Rotate.values()) {
Mino mino = minoFactory.create(piece, rotate);
for (int x = -mino.getMinX(); x < FIELD_WIDTH - mino.getMaxX(); x++) {
for (int y = appearY - mino.getMaxY() - 1; -mino.getMinY() <= y; y--) {
if (field.canPut(mino, x, y) && field.isOnGround(mino, x, y)) {
if (check(field, mino, x, y, From.None, 0)) {
Action action = minoShifter.createTransformedAction(piece, rotate, x, y);
actions.add(action);
}
}
}
}
}
return actions;
}
use of core.srs.Rotate in project solution-finder by knewjade.
the class LockedCandidate method search.
@Override
public Set<Action> search(Field field, Piece piece, int appearY) {
// temporaryの初期化
this.appearY = appearY;
lockedCache.clear();
HashSet<Action> actions = new HashSet<>();
for (Rotate rotate : Rotate.values()) {
Mino mino = minoFactory.create(piece, rotate);
for (int x = -mino.getMinX(); x < FIELD_WIDTH - mino.getMaxX(); x++) {
for (int y = appearY - mino.getMaxY() - 1; -mino.getMinY() <= y; y--) {
if (field.canPut(mino, x, y) && field.isOnGround(mino, x, y)) {
if (check(field, mino, x, y, From.None)) {
Action action = minoShifter.createTransformedAction(piece, rotate, x, y);
actions.add(action);
} else {
lockedCache.visit(x, y, rotate);
}
lockedCache.resetTrail();
}
}
}
}
return actions;
}
Aggregations