I guess I’ll set up an approval test with a random seed. Then see if we can create a better test.

This whole scheme is essentially random in nature, which makes it hard to test some key aspects. If we provide a random seed in a test, it should at least provide consistent behavior, which we can manually check once and then automate, until it doesn’t and we check it again.

And there are at least a few simple checks we can make.

Here’s our latest print test:

    def test_random_growth(self):
        bank = CellBank(10, 10)
        room = Room()
        room.add_cell(5,5)
        print()
        for _ in range(5):
            room.grow_randomly(bank)
        room.print_cells()
        bank2 = CellBank(10, 10)
        room2 = Room()
        room2.add_cell(5,5)
        for _ in range(5):
            room2.grow_randomly(bank2)
        room2.print_cells()

Hm. I was going to just check the cells list but maybe it would be more useful to have a string return from the Room and check that, still with a random seed of course.

    def test_random_growth(self):
        bank = CellBank(10, 10)
        room = Room()
        room.add_cell(5,5)
        print()
        for _ in range(5):
            room.grow_randomly(bank)
        print(room.cell_string())
        bank2 = CellBank(10, 10)
        room2 = Room()
        room2.add_cell(5,5)
        for _ in range(5):
            room2.grow_randomly(bank2)
        print(room2.cell_string())

And …

    def cell_string(self):
        return ", ".join([str(c) for c in sorted(self.cells)])

That prints:

(5, 5), (5, 5), (5, 6), (5, 7), (6, 7), (6, 8)
(4, 4), (4, 5), (5, 4), (5, 5), (5, 5), (5, 6)

Add a seed, run again, change to checks. I got this far and then noticed the duplicated (5,5):

    def test_random_growth(self):
        random.seed(123)
        bank = CellBank(10, 10)
        room = Room()
        room.add_cell(5,5)
        # print()
        for _ in range(5):
            room.grow_randomly(bank)
        s = room.cell_string()
        assert s == '(5, 4), (5, 5), (5, 5), (5, 6), (6, 5), (7, 5)'

That’s because I forgot to remove (5,5) from the bank when I allocated it at the beginning. Improve the test:

    def test_random_growth(self):
        random.seed(123)
        bank = CellBank(10, 10)
        room = Room()
        bank.take(5,5)
        room.add_cell(5,5)
        # print()
        for _ in range(5):
            room.grow_randomly(bank)
        s = room.cell_string()
        assert s == '(5, 4), (5, 5), (5, 5), (5, 6), (6, 5), (7, 5)'
        print(s)
        bank2 = CellBank(10, 10)
        room2 = Room()
        bank.take(5,5)
        room2.add_cell(5,5)
        for _ in range(5):
            room2.grow_randomly(bank2)
        s2 = room2.cell_string()
        print(s2)

One more run and I should have good values.

    def test_random_growth(self):
        random.seed(123)
        bank = CellBank(10, 10)
        room = Room()
        bank.take(5,5)
        room.add_cell(5,5)
        for _ in range(5):
            room.grow_randomly(bank)
        s = room.cell_string()
        assert s == '(4, 4), (5, 4), (5, 5), (5, 6), (6, 5), (7, 5)'
        bank2 = CellBank(10, 10)
        room2 = Room()
        bank2.take(5,5)
        room2.add_cell(5,5)
        for _ in range(5):
            room2.grow_randomly(bank2)
        s2 = room2.cell_string()
        assert s2 != s
        assert s2 == '(4, 3), (4, 4), (5, 4), (5, 5), (6, 4), (6, 5)'

I don’t think the second half is bearing much weight, but I guess it does show lack of repetition. Let’s do slightly better names and move on.

    def test_random_growth(self):
        random.seed(123)
        bank_1 = CellBank(10, 10)
        room_1 = Room()
        bank_1.take(5,5)
        room_1.add_cell(5,5)
        for _ in range(5):
            room_1.grow_randomly(bank_1)
        s_1 = room_1.cell_string()
        assert s_1 == '(4, 4), (5, 4), (5, 5), (5, 6), (6, 5), (7, 5)'
        bank_2 = CellBank(10, 10)
        room_2 = Room()
        bank_2.take(5,5)
        room_2.add_cell(5,5)
        for _ in range(5):
            room_2.grow_randomly(bank_2)
        s_2 = room_2.cell_string()
        assert s_2 != s_1
        assert s_2 == '(4, 3), (4, 4), (5, 4), (5, 5), (6, 4), (6, 5)'

What would be better would be to remove this duplication and save the checks. Extract method.

    def test_random_growth(self):
        random.seed(123)
        s_1 = self.random_6_cell_room()
        assert s_1 == '(4, 4), (5, 4), (5, 5), (5, 6), (6, 5), (7, 5)'
        s_2 = self.random_6_cell_room()
        assert s_2 != s_1
        assert s_2 == '(4, 3), (4, 4), (5, 4), (5, 5), (6, 4), (6, 5)'

    @staticmethod
    def random_6_cell_room() -> str:
        bank = CellBank(10, 10)
        room = Room()
        bank.take(5, 5)
        room.add_cell(5, 5)
        for _ in range(5):
            room.grow_randomly(bank)
        return room.cell_string()

OK, that’ll do. Green. Commit: convert print test to fixed seed test.

Summary

I’m out of free time, as GeePaw has opened his zoom and I want to pair with him on his more interesting solution, which I hope he’ll write up at some point, no promises, we’re just doing this for fun and he doesn’t write contemporaneously as I do.

I think we have my scheme for growing a random room implemented correctly, just randomly grab cells on the edges until the room is big enough. We’ll set “big enough” as a parameter in some range that we’ll adjust to make things look good. Then we’ll see what we get.

Before us, I think, looms the drawing. I might undertake that tomorrow, depending on variables outside my control.

See you next time!