# Solving Daryl's failing test case

So here is my solution. Anyone see any problems with it?

It passes all the tests, but ideally we should be going to the rules and work from there, but that isn’t in TDDs methodology.

Also, I prefer to be very specific about the condition I’m checking for, because if you try to combine too many conditions, you might get undesirable results if you don’t have a lot of good test cases written, which I don’t think we do.

``````    public Action Bowl(int pins)
{
if (pins < 0 && pins > 10)
throw new UnityException("Invalid pins!");

bowls[bowl - 1] = pins;

if (bowl == 21)
{
return Action.EndGame;
}

// We are ensuring that this is the 20th bowl, and it is just
// clearer to use the variable pins, than indexing the bowls array
if (bowl == 20 && bowls[19 - 1] == 10 && pins < 10)
{
bowl++;
return Action.Tidy;
}

// Handle last frame special cases
if (bowl >= 19 && Bowl21Awarded())
{
bowl++;
return Action.Reset;
}
else if (bowl == 20 && !Bowl21Awarded())
{
return Action.EndGame;
}

if (pins == 10)
{
bowl += 2;
return Action.EndTurn;
}

if (bowl % 2 != 0) // mid frame (or last frame)
{
bowl++;
return Action.Tidy;
}
else if (bowl % 2 == 0) // end of frame
{
bowl++;
return Action.EndTurn;
}

throw new UnityException("Not sure what action to return!");
}``````

I did a similar thing, I kept the original code as I didn’t want to completely change mine to be like Daryl’s (I thought it looked confusing to be honest)

So I ended up with this myself

``````//Handle last frame special cases
if (bowl >= 19 && Bowl21Awarded()) {
if (bowls[19-1] == 10 && pins < 10) {
bowl += 1;
return Action.Tidy;
}
else {
bowl += 1;
return Action.Reset;
}
} else if (bowl == 20 && !Bowl21Awarded()) {
return Action.EndGame;
}
``````

(Note: I omitted the bowl 21 handling which is above this, so it won’t ****** up on bowl 21 lol, but it’s identical to yours )

It is definitely the same idea, but will accept slightly different trigger cases. In your case, it will accept additional cases. For instance, if you are on bowl 19, and pin is say 5, then it will trigger your clause, whereas in mine it does not.

In my code, it triggers it below, where I put a comment “// mid frame (or last frame)”.

Here is a test case, of what I mean:

``````[Test]
public void T12()
{
for (int i = 0; i < 18; i++)
actionMaster.Bowl(1);

Assert.AreEqual(tidy, actionMaster.Bowl(5));
}
``````

Now, why am I pointing that out since they both work?

It’s because, when you have multiple code paths with the same result, it is harder to understand and debug code like that. You want to be as specific as possible in my opinion.

In some way, I feel TDD applied in this way sort of encourages hacking things together as in I haven’t thought about the problem, but I know a few results so I’m coding towards that and patching it up as I go along. The finalized code will likely be lower quality in terms of logic.

PS, I also was confused about Daryl’s code and since I skimmed the rules (as it wasn’t important to me), I thought maybe it was because of that.

Yes indeed I understand where you’re coming from in a debugging perspective, where I have gone down the “as much common code as possible” route, repetition of code can get out of control after a while, so I’ve kept it very much to the exceptions to the rules that are handled differently

To be frank, I sort of didn’t, because I’m not entirely sure about the bowling rules nor do I care enough about that to look it up.

How far along in the class are you? Did you finish the course?

I could use a study buddy.

Lol fair enough xD I watched that video on the rules been recommended and got the basic idea, although I didn’t know about all the 21 awarded rules from that lol.

I’m currently doing bowlmaster, I actually got as far as the scoremaster complete refactor, cried and ran away to do other courses lol. Nearly a year later I have returned and started the bowlmaster part again from scratch I’m currently at the video on making bug reports or something!

Not sure if I’m going to just do this then jump ship to the rpg course, I should really finish this one first lol