def test_combine_no_dominant_color() -> None: d = Block((0, 0), 500, None, 0, 1) d.children = [Block(d._children_positions()[0], 250, COLOUR_LIST[1], 1, 1), Block(d._children_positions()[1], 250, COLOUR_LIST[1], 1, 1), Block(d._children_positions()[2], 250, COLOUR_LIST[2], 1, 1), Block(d._children_positions()[3], 250, COLOUR_LIST[2], 1, 1)] assert not d.combine()
def test_smash_block_with_children() -> None: b = Block((0, 0), 500, None, 0, 2) b.children = [Block(b._children_positions()[0], 200, None, 1, 2), Block(b._children_positions()[1], 200, None, 1, 2), Block(b._children_positions()[2], 200, None, 1, 2), Block(b._children_positions()[3], 200, None, 1, 2)] assert not b.smash()
def test_rotate_clockwise_max_level_2() -> None: b = Block((0, 0), 500, None, 0, 2) b.children = [Block(b._children_positions()[0], 250, None, 1, 2), Block(b._children_positions()[1], 250, COLOUR_LIST[1], 1, 2), Block(b._children_positions()[2], 250, COLOUR_LIST[2], 1, 2), Block(b._children_positions()[3], 250, COLOUR_LIST[3], 1, 2)] b.children[0].children = [Block(b.children[0]._children_positions()[0], 125, COLOUR_LIST[2], 2, 2), Block(b.children[0]._children_positions()[1], 125, COLOUR_LIST[0], 2, 2), Block(b.children[0]._children_positions()[2], 125, COLOUR_LIST[2], 2, 2), Block(b.children[0]._children_positions()[3], 125, COLOUR_LIST[3], 2, 2)] assert b.rotate(1) c = Block((0, 0), 500, None, 0, 2) c.children = [Block((250, 0), 250, COLOUR_LIST[1], 1, 2), Block((0, 0), 250, COLOUR_LIST[2], 1, 2), Block((0, 250), 250, COLOUR_LIST[3], 1, 2), Block((250, 250), 250, None, 1, 2)] c.children[3].children = [Block((375, 250), 125, COLOUR_LIST[0], 2, 2), Block((250, 250), 125, COLOUR_LIST[2], 2, 2), Block((250, 375), 125, COLOUR_LIST[3], 2, 2), Block((375, 375), 125, COLOUR_LIST[2], 2, 2)] assert b == c
def test_update_children_position(): # Update regular positions b1 = Block((44, 44), 750, None, 0, 1) b2 = Block((0, 0), 750, COLOUR_LIST[0], 0, 1) b3 = Block((0, 0), 750, COLOUR_LIST[1], 0, 1) b4 = Block((0, 0), 750, COLOUR_LIST[2], 0, 1) b5 = Block((0, 0), 750, COLOUR_LIST[3], 0, 1) b1.children = [b2, b3, b4, b5] b1._update_children_positions((0, 0)) pos = b1._children_positions() lst = [] for child in b1.children: lst.append(child.position) assert lst == pos # Update positions with children b6 = Block((0, 0), 750, COLOUR_LIST[3], 0, 1) b7 = Block((0, 0), 750, COLOUR_LIST[3], 0, 1) b8 = Block((0, 0), 750, COLOUR_LIST[3], 0, 1) b9 = Block((0, 0), 750, COLOUR_LIST[3], 0, 1) b2.children = [b6, b7, b8, b9] b1._update_children_positions((657, 5334)) pos = b2._children_positions() lst = [] for child in b2.children: lst.append(child.position) assert lst == pos
def test_swap_invalid_direction() -> None: b = Block((0, 0), 500, None, 0, 2) b.children = [Block(b._children_positions()[0], 250, None, 1, 2), Block(b._children_positions()[1], 250, None, 1, 2), Block(b._children_positions()[2], 250, None, 1, 2), Block(b._children_positions()[3], 250, None, 1, 2)] assert not b.swap(2) assert not b.swap(5)
def test_combine_4_vs_0() -> None: d = Block((0, 0), 500, None, 0, 1) d.children = [Block(d._children_positions()[0], 250, COLOUR_LIST[1], 1, 1), Block(d._children_positions()[1], 250, COLOUR_LIST[1], 1, 1), Block(d._children_positions()[2], 250, COLOUR_LIST[1], 1, 1), Block(d._children_positions()[3], 250, COLOUR_LIST[1], 1, 1)] assert d.combine() assert d.colour == COLOUR_LIST[1]
def test_paint_invalid_cases() -> None: # Not at max_depth c = Block((0, 0), 500, COLOUR_LIST[1], 0, 1) assert not c.paint(COLOUR_LIST[1]) # Not a leaf b = Block((0, 0), 500, None, 0, 2) b.children = [Block(b._children_positions()[0], 250, None, 1, 2), Block(b._children_positions()[1], 250, COLOUR_LIST[1], 1, 2), Block(b._children_positions()[2], 250, COLOUR_LIST[2], 1, 2), Block(b._children_positions()[3], 250, COLOUR_LIST[3], 1, 2)] assert not c.paint(COLOUR_LIST[1]) # Already has target color a = Block((0, 0), 500, COLOUR_LIST[1], 0, 0) assert not a.paint(COLOUR_LIST[1])
def test_paint_at_level_2() -> None: b = Block((0, 0), 500, None, 0, 2) b.children = [Block(b._children_positions()[0], 250, None, 1, 2), Block(b._children_positions()[1], 250, COLOUR_LIST[1], 1, 2), Block(b._children_positions()[2], 250, COLOUR_LIST[2], 1, 2), Block(b._children_positions()[3], 250, COLOUR_LIST[3], 1, 2)] b.children[0].children = [Block(b.children[0]._children_positions()[0], 125, COLOUR_LIST[2], 2, 2), Block(b.children[0]._children_positions()[1], 125, COLOUR_LIST[0], 2, 2), Block(b.children[0]._children_positions()[2], 125, COLOUR_LIST[2], 2, 2), Block(b.children[0]._children_positions()[3], 125, COLOUR_LIST[3], 2, 2)] assert b.children[0].children[0].paint(COLOUR_LIST[0]) assert b.children[0].children[0].colour == COLOUR_LIST[0] assert not b.children[1].paint(COLOUR_LIST[0])
def test_score_perimeter_goal() -> None: b = Block((0, 0), 500, None, 0, 2) b.children = [Block(b._children_positions()[0], 250, None, 1, 2), Block(b._children_positions()[1], 250, COLOUR_LIST[1], 1, 2), Block(b._children_positions()[2], 250, COLOUR_LIST[2], 1, 2), Block(b._children_positions()[3], 250, COLOUR_LIST[3], 1, 2)] b.children[0].children = [Block(b.children[0]._children_positions()[0], 125, COLOUR_LIST[2], 2, 2), Block(b.children[0]._children_positions()[1], 125, COLOUR_LIST[0], 2, 2), Block(b.children[0]._children_positions()[2], 125, COLOUR_LIST[2], 2, 2), Block(b.children[0]._children_positions()[3], 125, COLOUR_LIST[3], 2, 2)] P = PerimeterGoal(COLOUR_LIST[3]) assert P.score(b) == 5 b.children[0].children[0].paint(COLOUR_LIST[3]) assert P.score(b) == 7
def test_smash(): # Test for when function will NOT smash b1 = Block((44, 44), 750, None, 0, 1) b2 = Block((0, 0), 750, COLOUR_LIST[0], 0, 1) b3 = Block((0, 0), 750, COLOUR_LIST[1], 0, 1) b4 = Block((0, 0), 750, COLOUR_LIST[2], 0, 1) b5 = Block((0, 0), 750, COLOUR_LIST[3], 0, 1) b1.children = [b2, b3, b4, b5] assert b1.smash() is False assert b2.position and b3.position and \ b4.position and b5.position == (0, 0) b6 = Block((44, 44), 750, COLOUR_LIST[0], 1, 1) assert b6.smash() is False # Test for function WILL smash b7 = Block((0, 0), 750, COLOUR_LIST[0], 0, 1) assert len(b7.children) == 0 assert b7.colour is COLOUR_LIST[0] b7.smash() assert len(b7.children) == 4 assert b7.colour is None assert b7.max_depth == 1 assert b7.level == 0 lst = [] for child in b7.children: assert child.level == 1 assert child.max_depth == 1 assert child.size == round(b7.size/2) assert child.smash() is False lst.append(child.position) assert lst == b7._children_positions() assert b7.smash() is False b8 = Block((0, 0), 750, COLOUR_LIST[0], 0, 5) b8.smash() # Level 1 b8.children[1].smash() # 2 b8.children[1].children[1].smash() # 3 b8.children[1].children[1].children[1].smash() # 4 b8.children[1].children[1].children[1].children[1].smash() # 5 assert b8.children[1].children[1].children[1].children[1]\ .children[1].smash() is False # 6 b9 = b8.children[1].children[1].children[1].children[1].children[1] b10 = b8.children[1].children[1].children[1].children[1] assert b9.position == (0, 0) assert b8.colour is None assert b10.colour is None assert b10.level == 4 for child in b10.children: assert child.level == 5 assert child.max_depth == 5 assert child.smash() is False assert child.size == round(b10.size/2)
def test_score_blob_goal_02() -> None: b = Block((0, 0), 500, None, 0, 2) b.children = [Block(b._children_positions()[0], 250, None, 1, 2), Block(b._children_positions()[1], 250, COLOUR_LIST[1], 1, 2), Block(b._children_positions()[2], 250, COLOUR_LIST[2], 1, 2), Block(b._children_positions()[3], 250, COLOUR_LIST[3], 1, 2)] b.children[0].children = [Block(b.children[0]._children_positions()[0], 125, COLOUR_LIST[2], 2, 2), Block(b.children[0]._children_positions()[1], 125, COLOUR_LIST[0], 2, 2), Block(b.children[0]._children_positions()[2], 125, COLOUR_LIST[2], 2, 2), Block(b.children[0]._children_positions()[3], 125, COLOUR_LIST[3], 2, 2)] P = BlobGoal(COLOUR_LIST[3]) assert P.score(b) == 5 b.children[0].children[2].paint(COLOUR_LIST[3]) assert P.score(b) == 6 b.children[1].colour = COLOUR_LIST[3] assert P.score(b) == 10
def set_children(block: Block, colours: List[Optional[Tuple[int, int, int]]]) \ -> None: """Set the children at <level> for <block> using the given <colours>. Precondition: - len(colours) == 4 - block.level + 1 <= block.max_depth """ size = block._child_size() positions = block._children_positions() level = block.level + 1 depth = block.max_depth block.children = [] # Potentially discard children for i in range(4): b = Block(positions[i], size, colours[i], level, depth) block.children.append(b)
def test_swap_v_block_max_level_1() -> None: b = Block((0, 0), 500, None, 0, 1) b.children = [Block(b._children_positions()[0], 250, COLOUR_LIST[0], 1, 1), Block(b._children_positions()[1], 250, COLOUR_LIST[1], 1, 1), Block(b._children_positions()[2], 250, COLOUR_LIST[2], 1, 1), Block(b._children_positions()[3], 250, COLOUR_LIST[3], 1, 1)] assert b.swap(1) assert b.children == [Block(b._children_positions()[0], 250, COLOUR_LIST[3], 1, 1), Block(b._children_positions()[1], 250, COLOUR_LIST[2], 1, 1), Block(b._children_positions()[2], 250, COLOUR_LIST[1], 1, 1), Block(b._children_positions()[3], 250, COLOUR_LIST[0], 1, 1)]
def test_rotate_clockwise_max_level_1() -> None: b = Block((0, 0), 500, None, 0, 1) b.children = [Block(b._children_positions()[0], 250, COLOUR_LIST[0], 1, 1), Block(b._children_positions()[1], 250, COLOUR_LIST[1], 1, 1), Block(b._children_positions()[2], 250, COLOUR_LIST[2], 1, 1), Block(b._children_positions()[3], 250, COLOUR_LIST[3], 1, 1)] assert b.rotate(1) assert b.children == [Block(b._children_positions()[0], 250, COLOUR_LIST[1], 1, 1), Block(b._children_positions()[1], 250, COLOUR_LIST[2], 1, 1), Block(b._children_positions()[2], 250, COLOUR_LIST[3], 1, 1), Block(b._children_positions()[3], 250, COLOUR_LIST[0], 1, 1)]