예제 #1
0
 def test_all_no_swaps(self):
     """No swaps on all symbols"""
     for symbol in SYMBOLS:
         with self.subTest(symbol=symbol):
             grid = sg(".{0}.\n..{0}\n{0}{0}{0}".format(symbol))
             grid2 = swap(grid, ())
             self.assertEqual(grid2, grid)
예제 #2
0
 def test_wall_to_rock(self):
     """Swap wall into a rock"""
     grid = sg(".W.\n..W\nWWW")
     target = sg(".R.\n..R\nRRR")
     swaps = (wir, )
     grid2 = swap(grid, swaps)
     self.assertEqual(grid2, target)
예제 #3
0
 def test_no_byproducts(self):
     """Wall is rock does not mess anything else up"""
     for e in filterfalse(lambda x: x == "W", ENTITIES):
         with self.subTest(entity=e):
             grid = sg(f".W{e}\n..W\nW{e}W")
             target = sg(f".R{e}\n..R\nR{e}R")
             swaps = (wir, )
             grid2 = swap(grid, swaps)
             self.assertEqual(grid2, target)
예제 #4
0
 def test_all_swaps_but_stationary(self):
     """A is B, but B is B"""
     for a, b in permutations(ENTITIES, 2):
         with self.subTest(entity_a=a, entity_b=b):
             grid = sg(f".{a}.\n..{a}\n{a}{a}{a}")
             target = sg(f".{b}.\n..{b}\n{b}{b}{b}")
             swaps = ((a, b), (a, a))
             grid2 = swap(grid, ())
             self.assertEqual(grid2, grid)
예제 #5
0
def animate_play(grid, sequence):
    """Play a game, given the sequence of moves and make an animated gif of it"""

    isvalidgrid(grid)
    behaviours, _ = ruleparser(rulefinder(grid))

    images = []
    images.append(draw_frame(grid, behaviours))  # First frame

    # Animate sequence
    youwon, youlost = (False, False)
    youlost = False
    for step in (*sequence, None):
        rules = rulefinder(grid)
        behaviours, swaps = ruleparser(rules)

        # Check for you is win condition
        for noun in behaviours:
            if behaviours[noun]["y"] and behaviours[noun]["n"]:
                youwon = True

        # Do the swap
        grid = swap(grid, swaps)
        images.append(draw_frame(grid, behaviours))

        entities_present = {j.lower() for j in flatten(grid) if isentity(j)}
        if not any(behaviours[e]["y"] for e in entities_present):
            youlost = True

        # Timestep the grid
        if step:
            (grid, yw) = timestep(grid, behaviours, step)
            if yw:
                youwon = True
                images.append(draw_frame(grid, behaviours))

        if youwon or youlost:
            break

    if youlost:
        add_lose_to_frame(images[-1])
    elif youwon:
        add_win_to_frame(images[-1])

    images.append(images[-1].copy())
    images.append(images[-1].copy())

    return images
예제 #6
0
 def test_but_wall_is_wall(self):
     """Would swap wall to rock, but wall is wall"""
     grid = sg(".W.\n..W\nWWW")
     swaps = (wir, wiw)
     grid2 = swap(grid, swaps)
     self.assertEqual(grid2, grid)
예제 #7
0
 def test_no_swaps(self):
     """Lots of walls but no swap rule"""
     grid = sg(".W.\n..W\nWWW")
     grid2 = swap(grid, ())
     self.assertEqual(grid2, grid)