Match-case for 10th frame seems too tricky. A state machine idea is gumption-trapped by a dash where there should be an underbar. Also: Resist fascism, and take care of one another.

I had a little time to fiddle, and came up with this test and code for the three small boxes on tenth frame:

    def test_tenth(self):
        self.check_case(None, None, None, '   ')
        self.check_case(10, None, None, 'X  ')
        self.check_case(10, 10, None, 'XX ')
        self.check_case(10, 5, None, 'X5 ')
        self.check_case(10, 0, None, 'X- ')
        self.check_case(6, 4, None, '6/ ')
        self.check_case(3, 5, None, '35 ')
        self.check_case(10, 10, 10, 'XXX')
        self.check_case(10, 10, 5, 'XX5')
        self.check_case(10, 6, 4, 'X6/')
        self.check_case(10, 0, 10, 'X-/')
        self.check_case(10, 0, 0, 'X--')
        self.check_case(0, 10, 10, '-/X')
        self.check_case(6, 4, 10, '6/X')
        self.check_case(6, 4, 3, '6/3')
        # cannot occur but work
        self.check_case(6, 3, 3, '633')
        self.check_case(6, 3, 3, '633')


    def get_tenth_frame_boxes(self):
        first, second, third = self.first, self.second, self.third
        match first, second, third:
            case a, None, None:
                return self.box_display(a), ' ', ' '
            case 10, b, None:
                return 'X', self.box_display(b), ' '
            case a, b, None if a + b == 10:
                return self.box_display(a), '/', ' '
            case a, b, None:
                return self.box_display(a), self.box_display(b), ' '
            # case 10, b, c if b + c == 10:
            #     return 'X', self.box_display(b), '/'
            case a, b, c if a + b == 10 and b != 0:
                return self.box_display(a), '/', self.box_display(c)
            case a, b, c if b + c == 10:
                return self.box_display(a), self.box_display(b), '/'
            case a, b, c:
                return self.box_display(a), self.box_display(b), self.box_display(c)

I’ve tried removing each of those cases and the tests don’t run without them, except for the commented one. And the tests are quite comprehensive, I think, so I am satisfied that this method works. There were some minor changes to the normal frame and test because I decided that I prefer to return a space rather than an empty string, for testing purposes.

I felt that the normal frame match-case was reasonably intelligible:

    def get_normal_boxes(self):
        first, second = self.first, self.second
        match first, second:
            case 10, b:
                return 'X', ' '
            case a, None:
                return self.box_display(a), ' '
            case a, b if a + b == 10:
                return self.box_display(a), '/'
            case a, b:
                return self.box_display(a), self.box_display(b)

But the tenth frame is not obviously correct, and took me a long time to get right. Changing it in any substantial way would be difficult, though, of course, there is little likelihood that we’ll need a change. But as a rule, one does not like to leave code in the system that will be hard to change.

So I’m still not satisfied with how we do this. My latest mad scheme is to have a state machine. We’ll feed it three values, one at a time, and it will, somehow, possibly magically, return our results. We have a great test for it in the one just shown. I don’t want to try to wire that up just yet, so I’ll start with a simpler test, planning to wind up with a new one with all the examples of the old one in it.

I can imagine setting up a list of inputs and outputs, and if this goes on much longer, I suppose that I might. In fact, having said it, let’s start on that path for the new test.

That said, I’m not really sure how this machine is going to work. I think I want a new test file to work in. This sends me down a brief rat hole setting up a new run config for PyCharm, which I never know how to do.

Gumption Trap!1

I have just spent over half an hour trying to convince PyCharm and pytest to run both my test files. The first file had been running just fine and now it was saying it couldn’t find any tests, after I added the new test file and made a config that looked just like the config from a project that works.

The problem was that my test files were named like ‘test-*.py’, and by default pytest only looks for ‘test_*.py’ and ‘*_test.py’. I think I have learned this at least once before.

I’ve been sitting here for over an hour with no progress on the new state-machine idea. I am out of gumption and will now go rest without further progress. I’ll pick this up later. Grumble!

Resist fascism, and take care of one another.



  1. See Pirsig, Zen and the Art of Motorcycle Maintenance for this concept and many interesting ideas.