Search in sources :

Example 86 with Randoms

use of lib.Randoms in project solution-finder by knewjade.

the class ColumnFieldViewTest method testRandom.

@Test
void testRandom() {
    Randoms randoms = new Randoms();
    String lineSeparator = System.lineSeparator();
    for (int count = 0; count < 10000; count++) {
        int width = randoms.nextIntClosed(1, 6);
        int height = randoms.nextIntClosed(1, 10);
        SizedBit sizedBit = new SizedBit(width, height);
        // create fields
        boolean[][] fields = new boolean[height][width];
        for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) fields[y][x] = randoms.nextBoolean();
        // parse to long
        long board = 0L;
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                boolean isEmpty = fields[y][x];
                board += isEmpty ? 0 : (1L << (x * height + y));
            }
        }
        ColumnSmallField field = ColumnFieldFactory.createField(board);
        // parse to strings
        StringBuilder builder = new StringBuilder();
        for (int y = height - 1; 0 <= y; y--) {
            for (int x = 0; x < width; x++) {
                boolean isEmpty = fields[y][x];
                builder.append(isEmpty ? '_' : 'X');
            }
            if (y != 0)
                builder.append(lineSeparator);
        }
        String expect = builder.toString();
        assertThat(ColumnFieldView.toString(field, sizedBit)).isEqualTo(expect);
    }
}
Also used : Randoms(lib.Randoms) SizedBit(searcher.pack.SizedBit) Test(org.junit.jupiter.api.Test)

Example 87 with Randoms

use of lib.Randoms in project solution-finder by knewjade.

the class AnalyzeTreeTest method randomLong.

@Test
@LongTest
void randomLong() throws SyntaxException {
    Randoms randoms = new Randoms();
    for (int size = 8; size <= 11; size++) {
        PatternGenerator generator = new LoadedPatternGenerator("*p7, *p" + (size - 7));
        AnalyzeTree tree = new AnalyzeTree();
        HashSet<LongPieces> success = new HashSet<>();
        HashSet<LongPieces> failed = new HashSet<>();
        generator.blocksStream().forEach(blocks -> {
            boolean flag = randoms.nextBoolean();
            List<Piece> pieceList = blocks.getPieces();
            tree.set(flag, pieceList);
            LongPieces longPieces = new LongPieces(pieceList);
            if (flag) {
                success.add(longPieces);
            } else {
                failed.add(longPieces);
            }
        });
        boolean isSucceed = success.stream().allMatch(pieces -> {
            List<Piece> blocks = pieces.getPieces();
            return tree.isVisited(blocks) && tree.isSucceed(blocks);
        });
        assertThat(isSucceed).isTrue();
        boolean isFailed = failed.stream().allMatch(pieces -> {
            List<Piece> blocks = pieces.getPieces();
            return tree.isVisited(blocks) && !tree.isSucceed(blocks);
        });
        assertThat(isFailed).isTrue();
        double percent = (double) success.size() / (success.size() + failed.size());
        assertThat(tree.getSuccessPercent()).isCloseTo(percent, offset(0.0001));
    }
}
Also used : Randoms(lib.Randoms) PatternGenerator(common.pattern.PatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) LongPieces(common.datastore.blocks.LongPieces) Piece(core.mino.Piece) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Example 88 with Randoms

use of lib.Randoms in project solution-finder by knewjade.

the class AnalyzeTreeTest method random.

@Test
void random() throws SyntaxException {
    Randoms randoms = new Randoms();
    for (int size = 1; size <= 7; size++) {
        PatternGenerator generator = new LoadedPatternGenerator("*p" + size);
        AnalyzeTree tree = new AnalyzeTree();
        HashSet<LongPieces> success = new HashSet<>();
        HashSet<LongPieces> failed = new HashSet<>();
        generator.blocksStream().forEach(blocks -> {
            boolean flag = randoms.nextBoolean();
            List<Piece> pieceList = blocks.getPieces();
            tree.set(flag, pieceList);
            LongPieces longPieces = new LongPieces(pieceList);
            if (flag) {
                success.add(longPieces);
            } else {
                failed.add(longPieces);
            }
        });
        boolean isSucceed = success.stream().allMatch(pieces -> {
            List<Piece> blocks = pieces.getPieces();
            return tree.isVisited(blocks) && tree.isSucceed(blocks);
        });
        assertThat(isSucceed).isTrue();
        boolean isFailed = failed.stream().allMatch(pieces -> {
            List<Piece> blocks = pieces.getPieces();
            return tree.isVisited(blocks) && !tree.isSucceed(blocks);
        });
        assertThat(isFailed).isTrue();
        double percent = (double) success.size() / (success.size() + failed.size());
        assertThat(tree.getSuccessPercent()).isCloseTo(percent, offset(0.0001));
    }
}
Also used : Randoms(lib.Randoms) PatternGenerator(common.pattern.PatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) LongPieces(common.datastore.blocks.LongPieces) Piece(core.mino.Piece) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest)

Example 89 with Randoms

use of lib.Randoms in project solution-finder by knewjade.

the class ConcurrentVisitedTreeTest method randomLong.

@Test
@LongTest
void randomLong() throws SyntaxException {
    Randoms randoms = new Randoms();
    for (int size = 8; size <= 11; size++) {
        PatternGenerator generator = new LoadedPatternGenerator("*p7, *p" + (size - 7));
        ConcurrentVisitedTree tree = new ConcurrentVisitedTree();
        Set<LongPieces> success = Collections.synchronizedSet(new HashSet<>());
        Set<LongPieces> failed = Collections.synchronizedSet(new HashSet<>());
        List<Pieces> piecesList = generator.blocksStream().collect(Collectors.toList());
        piecesList.parallelStream().forEach(pieces -> {
            boolean flag = randoms.nextBoolean();
            List<Piece> blocks = pieces.getPieces();
            tree.set(flag, blocks);
            LongPieces longPieces = new LongPieces(blocks);
            if (flag) {
                success.add(longPieces);
            } else {
                failed.add(longPieces);
            }
        });
        boolean isSucceed = success.stream().allMatch(pieces -> {
            List<Piece> blocks = pieces.getPieces();
            return tree.isSucceed(blocks) == ConcurrentVisitedTree.SUCCEED;
        });
        assertThat(isSucceed).isTrue();
        boolean isFailed = failed.stream().allMatch(pieces -> {
            List<Piece> blocks = pieces.getPieces();
            return tree.isSucceed(blocks) == ConcurrentVisitedTree.FAILED;
        });
        assertThat(isFailed).isTrue();
    }
}
Also used : PatternGenerator(common.pattern.PatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) Randoms(lib.Randoms) LongPieces(common.datastore.blocks.LongPieces) Piece(core.mino.Piece) Pieces(common.datastore.blocks.Pieces) LongPieces(common.datastore.blocks.LongPieces) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Example 90 with Randoms

use of lib.Randoms in project solution-finder by knewjade.

the class ConcurrentVisitedTreeTest method random.

@Test
void random() throws SyntaxException {
    Randoms randoms = new Randoms();
    for (int size = 1; size <= 7; size++) {
        PatternGenerator generator = new LoadedPatternGenerator("*p" + size);
        ConcurrentVisitedTree tree = new ConcurrentVisitedTree();
        Set<LongPieces> success = Collections.synchronizedSet(new HashSet<>());
        Set<LongPieces> failed = Collections.synchronizedSet(new HashSet<>());
        List<Pieces> piecesList = generator.blocksStream().collect(Collectors.toList());
        piecesList.parallelStream().forEach(pieces -> {
            boolean flag = randoms.nextBoolean();
            List<Piece> blocks = pieces.getPieces();
            tree.set(flag, blocks);
            LongPieces longPieces = new LongPieces(blocks);
            if (flag) {
                success.add(longPieces);
            } else {
                failed.add(longPieces);
            }
        });
        boolean isSucceed = success.stream().allMatch(pieces -> {
            List<Piece> blocks = pieces.getPieces();
            return tree.isSucceed(blocks) == ConcurrentVisitedTree.SUCCEED;
        });
        assertThat(isSucceed).isTrue();
        boolean isFailed = failed.stream().allMatch(pieces -> {
            List<Piece> blocks = pieces.getPieces();
            return tree.isSucceed(blocks) == ConcurrentVisitedTree.FAILED;
        });
        assertThat(isFailed).isTrue();
    }
}
Also used : PatternGenerator(common.pattern.PatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) Randoms(lib.Randoms) LongPieces(common.datastore.blocks.LongPieces) Piece(core.mino.Piece) Pieces(common.datastore.blocks.Pieces) LongPieces(common.datastore.blocks.LongPieces) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest)

Aggregations

Randoms (lib.Randoms)108 Test (org.junit.jupiter.api.Test)103 Piece (core.mino.Piece)58 LongTest (module.LongTest)32 Field (core.field.Field)29 Action (common.datastore.action.Action)25 LongPieces (common.datastore.blocks.LongPieces)24 MinoFactory (core.mino.MinoFactory)20 ArrayList (java.util.ArrayList)19 LockedCandidate (core.action.candidate.LockedCandidate)17 Mino (core.mino.Mino)16 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)16 MinoShifter (core.mino.MinoShifter)14 MinoRotation (core.srs.MinoRotation)14 Rotate (core.srs.Rotate)13 Collectors (java.util.stream.Collectors)13 LoadedPatternGenerator (common.pattern.LoadedPatternGenerator)12 PatternGenerator (common.pattern.PatternGenerator)12 List (java.util.List)12 SeparableMino (searcher.pack.separable_mino.SeparableMino)12