Isolate, Test. Fix
Hello, loves!
Let’s fix the defect. Bear bite costs me two hours.
First a test, basically this layout, with each number a one-cell room, added in order 0 1 2 3:
1_3_0_2
def test_ensure_connected(self):
layout = DungeonLayout(7, 7)
c1 = Cell(0,0)
c3 = Cell(2,0)
c0 = Cell(4, 0)
c2 = Cell(6,0)
r0 = Room([c0], '0')
r1 = Room([c1], '1')
r2 = Room([c2], '2')
r3 = Room([c3], '3')
layout.add_room(r1)
layout.add_room(r3)
layout.add_room(r0)
layout.add_room(r2)
layout.ensure_connected()
assert layout.is_fully_connected()
Irritatingly, this passes. Did I configure it wrong? I think I did. I confused myself with the numbers.
I don’t want to tell you how long I chased this until I realized that I had made the layout, not a straight line, but a square, so there was room to draw more paths than I expected. I’m still not entirely sure how it did it. The test change:
def test_ensure_connected(self):
layout = DungeonLayout(7, 1)
Now I can make the allegedly simple change, from:
class DungeonLayout:
def _make_path_rooms(self):
suites = self.define_suites()
for s1, s2 in zip(suites, suites[1:]):
To:
class DungeonLayout:
def _make_path_rooms(self):
suites = self.define_suites()
for s1, s2 in zip(suites, suites[1:] + suites[:1]):
The test passes, but another fails, this one:
def test_create_path_room(self):
layout = DungeonLayout(10, 10)
layout.add_room(Room([(Cell(0, 0))], 'room_00'))
layout.add_room(Room([(Cell(4, 0))], 'room_40'))
layout.add_room(Room([(Cell(4, 4))], 'room_44'))
layout.add_room(Room([(Cell(0, 4))], 'room_04'))
layout._make_path_rooms()
path_rooms = [room for room in layout.rooms if room.name == 'path']
assert len(path_rooms) == 3
for path in path_rooms:
assert len(path.cells) == 3
The first assert is failing, we are getting four path rooms now. I suspect we are actually getting an additional connection in some cases, and this is one of them.
The map is a square with a room in each corner. When we zipped 0->1->2->3 we got three paths. With the new zip we’ll add a fourth path, 3->0.
I think we’ll allow it. Change that test.
Now before I do anything like commit this change, I want to run the loop looking for disconnected layouts. I’ll run it for a good long time. Our formerly failing case passes now, I am not surprised.
I’ll take a long break … 56,000 runs later, no failures. I think we can call it fixed.
Wrapup
Can’t commit this, too many hacks. I’ll do it right tomorrow. Defect slain, and would have taken no time at all had I not written that malformed test. Sometimes the bear bites you. Today it bit me.
Some thoughts:
- Is it possible to arrange rooms such that this serial attempt to draw paths can still fail?
- The current scheme can now draw more paths than are strictly required. Should we come up with a new algorithm that does just the minimum? Or are extra paths actually part of the fun.
- If that’s the case maybe we should connect all unique pairs of suites?
See you next time!