def test_blocky(): block = generate_board(0, 750) blocky = _block_to_squares(block) assert isinstance(blocky, list) assert blocky[0][0] in COLOUR_LIST assert blocky[0][1] == (0, 0) assert blocky[0][2] == 750 print(block) main_block = Block((0, 0), 200, colour=None, level=0, max_depth=2) child_block_1 = Block((100, 0), 100, colour=COLOUR_LIST[0], level=1, max_depth=2) child_block_2 = Block((0, 0), 100, colour=COLOUR_LIST[1], level=1, max_depth=2) child_block_3 = Block((0, 100), 100, colour=COLOUR_LIST[2], level=1, max_depth=2) child_block_4 = Block((100, 100), 100, colour=COLOUR_LIST[3], level=1, max_depth=2) main_block.children = [child_block_1, child_block_2, child_block_3, child_block_4] blocky = _block_to_squares(main_block) assert len(blocky) == 4 assert blocky[0][0] in COLOUR_LIST assert blocky[0][1] == (100, 0) assert blocky[0][2] == 100 assert blocky[1][0] in COLOUR_LIST assert blocky[1][1] == (0, 0) assert blocky[1][2] == 100 assert blocky[2][0] in COLOUR_LIST assert blocky[2][1] == (0, 100) assert blocky[2][2] == 100 assert blocky[3][0] in COLOUR_LIST assert blocky[3][1] == (100, 100) assert blocky[3][2] == 100
def test_game_board(game_board, renderer): renderer.draw_board(_block_to_squares(game_board)) renderer.save_to_file('game_board1.png') game_board.rotate(1) renderer.clear() renderer.draw_board(_block_to_squares(game_board)) renderer.save_to_file('game_board_rotate_clockwise.png')
def test__block_to_squares() -> None: """Test _block_to_squares. - test on a leaf block at level 0 - test on a block with children """ b = Block((-375, 375), 750, (1, 128, 181), 0, 1) assert _block_to_squares(b) == [((1, 128, 181), (-375, 375), 750)] assert b.smash() squares = _block_to_squares(b) assert len(squares) >= 4
def test_combine_game_board(game_board, renderer): renderer.draw_board(_block_to_squares(game_board)) renderer.save_to_file('game_board1.png') game_board.children[0].children[3].combine() _flatten(game_board) renderer.clear() goal = PerimeterGoal(COLOUR_LIST[0]) goal.score(game_board) renderer.draw_board(_block_to_squares(game_board)) renderer.save_to_file('game_board_rotate_combine.png')
def test_render_reference_board_rotate1(self, renderer, board_16x16, board_16x16_rotate1) -> None: """Render the reference board to a file so that you can view it on your computer.""" # Render the reference board swapped renderer.draw_board(_block_to_squares(board_16x16_rotate1)) renderer.save_to_file('reference-rotate-1.png') # Render what your swap does to the reference board board_16x16.swap(0) renderer.clear() renderer.draw_board(_block_to_squares(board_16x16)) renderer.save_to_file('your-rotate-1.png')
def test__block_to_squares() -> None: """ Test the _block_to_squares function. """ b1 = Block((0, 0), 500, (255, 0, 0), 0, 3) assert _block_to_squares(b1) == [((255, 0, 0), (0, 0), 500)] b2 = Block((500, 0), 1000, None, 0, 2) b2.children = [Block((500, 0), 500, (255, 0, 0), 1, 2), Block((0, 0), 500, (255, 255, 0), 1, 2), Block((0, 500), 500, (255, 0, 255), 1, 2), Block((500, 500), 500, (0, 0, 255), 1, 2)] squares2 = _block_to_squares(b2) assert len(squares2) == 4 assert ((255, 0, 0), (500, 0), 500) in squares2 assert ((255, 255, 0), (0, 0), 500) in squares2 assert ((255, 0, 255), (0, 500), 500) in squares2 assert ((0, 0, 255), (500, 500), 500) in squares2 b2.children[1].colour = None b2.children[1].children = [Block((250, 0), 250, (0, 0, 0), 1, 2), Block((0, 0), 250, (0, 255, 0), 1, 2), Block((0, 250), 250, (255, 255, 255), 1, 2), Block((250, 250), 250, (0, 255, 255), 1, 2)] squares3 = _block_to_squares(b2) assert len(squares3) == 7 assert ((255, 0, 0), (500, 0), 500) in squares3 assert ((0, 0, 0), (250, 0), 250) in squares3 assert ((0, 255, 0), (0, 0), 250) in squares3 assert ((255, 255, 255), (0, 250), 250) in squares3 assert ((0, 255, 255), (250, 250), 250) in squares3 assert ((255, 0, 255), (0, 500), 500) in squares3 assert ((0, 0, 255), (500, 500), 500) in squares3
def test_block_to_squares_leaf(child_block) -> None: """Test that a board with only one block can be correctly trasnlated into a square that would be rendered onto the screen. """ squares = _block_to_squares(child_block) expected = [(COLOUR_LIST[0], (0, 0), 750)] assert squares == expected
def test_blocks_to_squares(board_16x16) -> None: "Test blocks to squares method" bts = _block_to_squares(board_16x16) assert isinstance(bts, list) is True assert isinstance(bts[0][1], tuple) is True assert isinstance(bts[0][2], int) is True for block in bts: assert len(block) == 3 assert len(bts) == 7
def test_block_to_squares_reference1(board_2x2) -> None: """Test that the reference board can be correctly translated into a set of squares that would be rendered onto the screen. """ squares = set(_block_to_squares(board_2x2)) expected = {((199, 44, 58), (375, 0), 375), ((138, 151, 71), (0, 0), 375), ((255, 211, 92), (0, 375), 375), ((1, 128, 181), (375, 375), 375)} assert squares == expected
def test_depth_5_board(renderer): random.seed(1002) board = generate_board(5, BOARD_SIZE) renderer.draw_board(_block_to_squares(board)) renderer.save_to_file('board_5_ref.png') renderer.clear() goal1 = BlobGoal(COLOUR_LIST[0]) # blue goal2 = BlobGoal(COLOUR_LIST[1]) # red player1 = RandomPlayer(1, goal1) player2 = RandomPlayer(2, goal2) player1._proceed = True move1 = player1.generate_move(board) move1_block = move1[2] to_do = _get_block(board, move1_block.position, move1_block.level) assert move1[0] == "swap" and move1[1] == 0 assert to_do.swap(0) renderer.draw_board(_block_to_squares(board)) renderer.save_to_file('board_5_move1.png') renderer.clear() afterfirst1 = goal1.score(board) afterfirst2 = goal2.score(board) player2._proceed = True move2 = player2.generate_move(board) move2_block = move2[2] to_do_2 = _get_block(board, move2_block.position, move2_block.level) assert move2[0] == "smash" assert to_do_2.smash() renderer.draw_board(_block_to_squares(board)) renderer.save_to_file('board_5_move2.png') renderer.clear() aftersecond1 = goal1.score(board) aftersecond2 = goal2.score(board) player1._proceed = True move3 = player1.generate_move(board) move3_block = move3[2] to_do_3 = _get_block(board, move3_block.position, move3_block.level) assert move3[0] == "rotate" and move3[1] == 3 assert to_do_3.rotate(3) renderer.draw_board(_block_to_squares(board)) renderer.save_to_file('board_5_move3.png') renderer.clear() afterthird1 = goal1.score(board) afterthird2 = goal2.score(board)
def test_block_2_squares_a(self, board_16x16) -> None: squares = set(_block_to_squares(board_16x16)) expected = {((1, 128, 181), (563, 0), 188), ((199, 44, 58), (375, 0), 188), ((199, 44, 58), (375, 188), 188), ((255, 211, 92), (563, 188), 188), ((138, 151, 71), (0, 0), 375), ((199, 44, 58), (0, 375), 375), ((255, 211, 92), (375, 375), 375)} assert squares == expected
def test_blocks_to_squares(self) -> None: borde = standard_borde() # print(borde) expected = ((PACIFIC_POINT, (300, 0), 100), (REAL_RED, (200, 0), 100), (OLD_OLIVE, (200, 100), 100), (DAFFODIL_DELIGHT, (300, 100), 100), (DAFFODIL_DELIGHT, (0, 0), 200), (REAL_RED, (0, 200), 100), (PACIFIC_POINT, (100, 200), 100), (OLD_OLIVE, (0, 300), 100), (PACIFIC_POINT, (100, 300), 100), (PACIFIC_POINT, (200, 200), 100)) for item in _block_to_squares(borde): assert item in expected
def test_get_block_2(self): """ Test for a nested Block Level 0: root([i1, l1, l2, l3]) / | | \ Level1: i1([l4, l5, l6, l7]), l1([]), l2([]), l3([])) / | | \ Level2: l4([]), l5([]), l6 ([]), l7([]) root: (0, 0), 100, None, 0, 2 i1:(50, 0), 50, None, 1, 2 l1:(0, 0), 50, (80, 80, 80), 1, 2 l2:(0, 50), 50, (70, 70, 70), 1, 2 l3:(50, 50), 50, (60, 60, 60), 1, 2 l4:(75, 0), 25, (40, 40, 40), 2, 2 l5:(50, 0), 25, (30, 30, 30), 2, 2 l6:(50, 25), 25, (20, 20, 20), 2, 2 l7:(75, 25), 25, (10, 10, 10),2, 2 Expected return value: [((10, 10, 10), (75, 25), 25), ((20, 20, 20), (50, 25), 25), ((30, 30, 30), (50, 0), 25), ((40, 40, 40), (75, 0), 25), ((80, 80, 80), (0, 0), 50), ((70, 70, 70), (0, 50), 50), ((60, 60, 60), (50, 50), 50)] """ act1 = _block_to_squares(self.one_internal.children[0]) exp1 = [((10, 10, 10), (75, 25), 25), ((20, 20, 20), (50, 25), 25), ((30, 30, 30), (50, 0), 25), ((40, 40, 40), (75, 0), 25)] self.assertCountEqual(exp1, act1) act2 = _block_to_squares(self.one_internal) exp2 = exp1 + [((80, 80, 80), (0, 0), 50), ((70, 70, 70), (0, 50), 50), ((60, 60, 60), (50, 50), 50)] self.assertCountEqual(exp2, act2)
def test_block_to_squares(): b1 = Block((0, 0), 750, COLOUR_LIST[1], 0, 1) assert _block_to_squares(b1) == [((COLOUR_LIST[1]), (0, 0), 750)] b2 = Block((0, 0), 375, COLOUR_LIST[0], 1, 1) b3 = Block((0, 0), 375, COLOUR_LIST[1], 1, 1) b4 = Block((0, 0), 375, COLOUR_LIST[2], 1, 1) b5 = Block((0, 0), 375, COLOUR_LIST[3], 1, 1) b1.children = [b2, b3, b4, b5] assert set(_block_to_squares(b1)) == {((COLOUR_LIST[0]), (0, 0), 375), ((COLOUR_LIST[1]), (0, 0), 375), ((COLOUR_LIST[2]), (0, 0), 375), ((COLOUR_LIST[3]), (0, 0), 375)} b2.children = [b2.create_copy(), b3.create_copy(), b4.create_copy(), b5.create_copy()] assert set(_block_to_squares(b1.create_copy())) == {((COLOUR_LIST[0]), (0, 0), 375), ((COLOUR_LIST[1]), (0, 0), 375), ((COLOUR_LIST[2]), (0, 0), 375), ((COLOUR_LIST[3]), (0, 0), 375), ((COLOUR_LIST[1]), (0, 0), 375), ((COLOUR_LIST[2]), (0, 0), 375), ((COLOUR_LIST[3]), (0, 0), 375)}
def test_block_to_squares_reference(board_16x16) -> None: """Test that the reference board can be correctly translated into a set of squares that would be rendered onto the screen. """ # The order the squares appear may differ based on the implementation, so # we use a set here. squares = set(_block_to_squares(board_16x16)) expected = {((1, 128, 181), (563, 0), 188), ((199, 44, 58), (375, 0), 188), ((199, 44, 58), (375, 188), 188), ((255, 211, 92), (563, 188), 188), ((138, 151, 71), (0, 0), 375), ((199, 44, 58), (0, 375), 375), ((255, 211, 92), (375, 375), 375)} assert squares == expected
def test_get_block_1(self): """ Test for a single block root((10, 10), 10, (10, 10, 10), 0, 0, []) Expected Return Value [((10, 10, 10), (10, 10), 10)] """ leaf_block = self.leaf_block exp_list = [getattr(leaf_block, attr_name) for attr_name in self.block_attrs] exp_list[-1] = 0 act = _block_to_squares(leaf_block) self.assertTrue(isinstance(act, list)) self.assertTrue(len(act) == 1) self.assertEqual([((10, 10, 10), (10, 10), 10)], act) self.assertBlock(leaf_block, exp_list, color_check=True, is_leaf=True)
def test_block_to_squares() -> None: block1 = Block((0, 0), 16, None, 0, 2) c1 = Block((8, 0), 8, (1, 128, 181), 1, 2) c2 = Block((0, 0), 8, (199, 44, 58), 1, 2) c3 = Block((0, 8), 8, None, 1, 2) c4 = Block((8, 8), 8, None, 1, 2) block1.children = [c1, c2, c3, c4] b31 = Block((4, 8), 4, (1, 128, 181), 2, 2) b32 = Block((0, 8), 4, (199, 44, 58), 2, 2) b33 = Block((0, 12), 4, (255, 211, 92), 2, 2) b34 = Block((4, 12), 4, (199, 44, 58), 2, 2) c3.children = [b31, b32, b33, b34] b41 = Block((12, 8), 4, (255, 211, 92), 2, 2) b42 = Block((8, 8), 4, (199, 44, 58), 2, 2) b43 = Block((8, 12), 4, (255, 211, 92), 2, 2) b44 = Block((12, 12), 4, (199, 44, 58), 2, 2) c4.children = [b41, b42, b43, b44] lst = _block_to_squares(block1) assert ((1, 128, 181), (4, 8), 4) in lst
def test_render_reference_board(self, renderer, board_16x16) -> None: """Render the reference board to a file so that you can view it on your computer.""" renderer.draw_board(_block_to_squares(board_16x16)) renderer.save_to_file('reference-board.png')
def test_smash_3(self, mocked_random): """ Before: Level 0: root([]) After: Level 0: root([i1, l1, l2, l3]) / | | \ Level1: i1([l5, l6, l7, l8]), i2([l9, l10, l11, l12]), i3([l13, l14, l15, l16]), i4([l17, l18, l19, l20])) / | | \ Level2: l5([]), l6([]), l7([]), l8([]) l9([]),l10([]), l11([]), l12([]) l13([]), l14([]), l15([]), l16)([]). l17([]), l18([]), l19([]), l20([]) l9 l10, l11 root:(0, 0), 100, None, 0, 2 i1:(50, 0), 50, None, 1, 2 i2:(0, 0), 50, None, 1, 2 i3:(0, 50), 50, None, 1, 2 i4:(50, 50), 50, None, 1, 2 l5:(75, 0), 25, XXX, 2, 2 l6:(50, 0), 25, XXX, 2, 2 l7:(50, 25), 25, XXX, 2, 2 l8:(75, 25), 25, XXX, 2, 2 l9:(25, 0), 25, XXX, 2, 2 l10:(0, 0), 25, XXX, 2, 2 l11:(0, 25), 25, XXX, 2, 2 l12:(25, 25), 25, XXX, 2, 2 l13:(25, 50), 25, XXX, 2, 2 l14:(0, 50), 25, XXX, 2, 2 l15:(0, 75), 25, XXX, 2, 2 l16:(25, 75), 25, XXX, 2, 2 l17:(75, 50), 25, XXX, 2, 2 l18:(50, 50), 25, XXX, 2, 2 l19:(50, 75), 25, XXX, 2, 2 l20:(75, 75), 25, XXX, 2, 2 """ mocked_random.return_value = 0 temp = Block((0, 0), 100, None, 0, 2) self.assertTrue(temp.smash()) bfs = block_bfs(temp) self.assertTrue( len(bfs) == 21, "You should have exactly 21 nodes after smashing") check_color = True is_leaf = True parameter_list = [ [[(0, 0), 100, None, 0, 2, 4], not check_color, not is_leaf], [[(50, 0), 50, None, 1, 2, 4], not check_color, not is_leaf], [[(0, 0), 50, None, 1, 2, 4], not check_color, not is_leaf], [[(0, 50), 50, None, 1, 2, 4], not check_color, not is_leaf], [[(50, 50), 50, None, 1, 2, 4], not check_color, not is_leaf], [[(75, 0), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(50, 0), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(50, 25), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(75, 25), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(25, 0), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(0, 0), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(0, 25), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(25, 25), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(25, 50), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(0, 50), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(0, 75), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(25, 75), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(75, 50), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(50, 50), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(50, 75), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf], [[(75, 75), 25, COLOUR_LIST, 2, 2, 0], not check_color, is_leaf] ] for i in range(len(bfs)): block = bfs[i] parameters = parameter_list[i] self.assertBlock(block, parameters[0], parameters[1], parameters[2]) for j in range(5, 21): block = bfs[j] block.colour = (j * 10, j * 10, j * 10) squares = _block_to_squares(temp) squares.sort(key=lambda x: x[0][0]) exp_squares = [ ((50, 50, 50), (75, 0), 25), ((60, 60, 60), (50, 0), 25), ((70, 70, 70), (50, 25), 25), ((80, 80, 80), (75, 25), 25), ((90, 90, 90), (25, 0), 25), ((100, 100, 100), (0, 0), 25), ((110, 110, 110), (0, 25), 25), ((120, 120, 120), (25, 25), 25), ((130, 130, 130), (25, 50), 25), ((140, 140, 140), (0, 50), 25), ((150, 150, 150), (0, 75), 25), ((160, 160, 160), (25, 75), 25), ((170, 170, 170), (75, 50), 25), ((180, 180, 180), (50, 50), 25), ((190, 190, 190), (50, 75), 25), ((200, 200, 200), (75, 75), 25) ] self.assertCountEqual(squares, exp_squares)
def test_block_2_squares_no_children(self) -> None: board = Block((0, 0), 750, (138, 151, 71), 0, 3) squares = set(_block_to_squares(board)) expected = {((138, 151, 71), (0, 0), 750)} assert squares == expected
def blocky_playground(): from blocky import _block_to_squares # _block_to_squares b = block_two_level1() print(_block_to_squares(b))
def test_moves_game_board2(game_board2, renderer): goal1 = PerimeterGoal(COLOUR_LIST[1]) goal2 = BlobGoal(COLOUR_LIST[1]) renderer.draw_board(_block_to_squares(game_board2)) renderer.save_to_file('game_board2_ref.png') assert goal1.score(game_board2) == 26 assert goal2.score(game_board2) == 4 * 3 + 3 * 2 + 4 * 4 + 2 * 4**3 lst_of_game_boards = [] for i in range(10): lst_of_game_boards.append(game_board2.create_copy()) lst_of_game_boards[0].rotate(1) renderer.clear() renderer.draw_board(_block_to_squares(lst_of_game_boards[0])) renderer.save_to_file("game_board_2_rotate1.png") assert goal1.score(lst_of_game_boards[0]) == 26 assert goal2.score( lst_of_game_boards[0]) == 3 * 2 + 4 * 3 + 4**2 + 2 * 4**3 renderer.clear() lst_of_game_boards[1].rotate(3) renderer.draw_board(_block_to_squares(lst_of_game_boards[1])) renderer.save_to_file("game_board_2_rotate3.png") assert goal1.score(lst_of_game_boards[1]) == 26 assert goal2.score( lst_of_game_boards[1]) == 3 * 2 + 4 * 3 + 4**2 + 2 * 4**3 renderer.clear() lst_of_game_boards[2].children[1].smash() renderer.draw_board(_block_to_squares(lst_of_game_boards[2])) renderer.save_to_file("game_board_2_smash.png") assert goal1.score(lst_of_game_boards[2]) == sum( [1, 2, 1, 1, 8, 7, 7, 2, 8]) assert goal2.score( lst_of_game_boards[2]) == 2 * 4**3 + 4 + 6 + 4**2 + 4 + 4 renderer.clear() lst_of_game_boards[3].swap(1) renderer.draw_board(_block_to_squares(lst_of_game_boards[3])) renderer.save_to_file("game_board_2_swap1.png") assert goal1.score(lst_of_game_boards[3]) == sum([8, 8, 8, 1, 1, 2, 1, 1]) assert goal2.score(lst_of_game_boards[3]) == 4 * 4 * 4 + 4 * 4 * 2 + 4 + 3 renderer.clear() lst_of_game_boards[4].swap(0) renderer.draw_board(_block_to_squares(lst_of_game_boards[4])) renderer.save_to_file("game_board_2_swap0.png") assert goal1.score(lst_of_game_boards[4]) == sum( [3, 3, 2, 4, 8, 8, 8, 4, 2]) assert goal2.score( lst_of_game_boards[4]) == 4 * 2 + 3 * 2 + 4**2 + 2 * 4**3 renderer.clear() assert lst_of_game_boards[5].children[0].children[0].children[0].children[ 0].paint(COLOUR_LIST[3]) == False lst_of_game_boards[5].children[0].children[0].children[0].children[ 0].children[0].paint(COLOUR_LIST[3]) renderer.draw_board(_block_to_squares(lst_of_game_boards[5])) renderer.save_to_file("game_board_2_paint.png") renderer.clear() lst_of_game_boards[6].children[2].children[3].children[1].children[ 3].combine() lst_of_game_boards[6].children[2].children[3].children[1].children[ 2].combine() renderer.draw_board(_block_to_squares(lst_of_game_boards[6])) renderer.save_to_file("game_board_2_combine.png") assert goal1.score(lst_of_game_boards[6]) == 26 assert goal2.score( lst_of_game_boards[6]) == 4 * 4 * 4 * 2 + 4 * 4 + 4 + 4 + 3 + 3 + 4