def test_color_state(self): generator = PuzzleGenerator(5) generator.generate_filled_field() generator.field_state.color_state() for cell in generator.field_state.field.get_all_cells(): self.assertTrue(generator.field_state._colored_cells[cell] in generator.field_state.COLORS)
def test_field_to_string(self): generator = PuzzleGenerator(4) generator.generate_filled_field() state = generator.field_state state_to_string = str(state) state_from_string = FieldState.from_string_to_state(state_to_string) self.assertDictEqual(state_from_string.get_full_state(), state.get_full_state())
def test_cells_involved(self): string = ''' 3 5 1 2 3 5 5 2 3 1 5 5 0 4 4 4 0 0 4 ''' generator = PuzzleGenerator(3) generator.field_state = FieldState.from_string_to_state(string) for number in (3, 4, 6): self.assertFalse(generator._cells_involved((3, 0), number)) self.assertTrue(generator._cells_involved((4, 1), 2)) self.assertTrue(generator._cells_involved((3, 0), 1))
def test_find_next_cells(self): string = ''' 3 5 1 3 3 5 5 2 0 5 4 4 3 5 1 4 3 0 4 ''' generator = PuzzleGenerator(3) generator.field_state = FieldState.from_string_to_state(string) for cell, next_cells in (((0, 0), []), ((2, 0), [(2, 1)]), ((3, 0), []), ((4, 0), [(4, 1)])): self.assertEqual( next_cells, list( generator._find_next_cells( cell, generator.field_state.get_state(cell))))
def main(): args = parse_args() if args.size: try: if args.maxvalue: generator = PuzzleGenerator(args.size, args.maxvalue) else: generator = PuzzleGenerator(args.size) generator.generate_filled_field() if args.empty: generator.generate_field_for_game(bool(args.unity), args.empty) else: generator.generate_field_for_game(bool(args.unity)) write_result(args.puzzle, generator.game_field, args.color) write_result(args.solution, generator.field_state, args.color) except Exception as e: print('Error while generating puzzle\n{}'.format(e), file=sys.stderr) sys.exit(ERROR_GENERATING_PUZZLE)
def test_generate_field_for_game_with_unities(self): generator = PuzzleGenerator(4) generator.generate_filled_field() generator.generate_field_for_game(False) for cell, value in filter( lambda cell_state: cell_state[1] == 1, generator.field_state.get_full_state().items()): self.assertTrue(generator.game_field.get_state(cell) == 1)
def test_neighbours_differ(self): string = ''' 3 5 1 1 3 5 5 2 0 0 5 5 3 4 4 4 3 0 4 ''' generator = PuzzleGenerator(3) generator.field_state = FieldState.from_string_to_state(string) for cell, prev_cell in (((2, 2), (1, 1)), ((2, 1), (2, 0)), ((4, 1), (4, 0))): self.assertTrue( generator.field_state.neighbours_differ( cell, [prev_cell], generator.field_state.get_state(prev_cell))) for cell, prev_cell in (((2, 1), (1, 1)), ((2, 1), (3, 0))): self.assertFalse( generator.field_state.neighbours_differ( cell, [prev_cell], generator.field_state.get_state(prev_cell)))
def test_generate_field_for_game_empty_unities(self): percent = 50 unity = True generator = PuzzleGenerator(4) generator.generate_filled_field() generator.generate_field_for_game(unity, percent) empty_cells = list( filter(lambda cell: generator.game_field.get_state(cell) == 0, generator.field.get_all_cells())) min_empty_count = 0 for group in generator.groups: min_empty_count += int(group.get_value() * percent / 100) max_empty_count = min_empty_count + len(generator.groups) self.assertTrue(min_empty_count <= len(empty_cells) <= max_empty_count) for group in filter(lambda g: g.get_value() != 1, generator.groups): self.assertTrue( any( generator.field_state.get_state(cell) != 0 for cell in group.initial_cells))
def test_generate_filled_field(self): generator = PuzzleGenerator(3) generator.generate_filled_field() for number in generator.field_state.get_full_state().values(): self.assertNotEqual(number, 0)