How much time did you take?

It took me like 4 hours researching about bowling and thinking about how to code the scoring rules. Then I spent more 3 hours to write the code and make it pass all the tests.

Honestly, I do not remember how long I took. I know it was longer than that. Then after the bowling project I moved to blender for a break. I have been a hobbyist programmer for a very long time now, and TDD was very different for me. Took awhile to wrap my head around thinking backwards, but once I did I found creating that script much easier.

Thanks for answer! I just got curious because the rules are so simple, but it was so hard to implement it.

Yea. I found the same thing. I saw in the udemy Q&A that others felt that way too. I assume you figured it out though?

Yeah. But my code isn’t so concise and elegant as Ben’s.

I took about 3 hours to get the solution and then I refactored and messed it all up and it took another 2 hours to fix it. However, I don’t like Ben’s solution. I like mine better - but that is just me. I use a foreach structure instead of the for loops.

public static List<int> ScoreFrames(List<int> rolls) {
     List<int> frameList = new List<int>();
     int framecount = 1;
     int rollcount = 0;
     int firstball = 0;
     bool firstbowl = true;

    foreach (int bowl in rolls) {          
        rollcount++; // running count of all rolls
        if (firstbowl)
        {
            firstball = bowl;
            firstbowl = false;
            // will need to handle a strike here
            if (bowl == 10) {
                //need to look ahead to get score
                firstball = 0;
                firstbowl = true;
                framecount++;
                if (rolls.Count > rollcount + 1)
                {
                    //this should mean there are two rolls after the current one available
                    frameList.Add(bowl + rolls[rollcount] + rolls[rollcount + 1]);
                    
                }
            }
        }
        else {
            //must be the second ball
            //Need to handle spare first
            firstbowl = true;
            if (firstball + bowl == 10) {
                framecount++;
                //spare - now look ahead to see if there is one more roll if not we can have a score
                if (rolls.Count > rollcount)
                {
                    //there is one more roll at least
                    frameList.Add(firstball + bowl + rolls[rollcount]);
                    firstball = 0;
                }
            }else {
                // not a spare
                //should not count the last two balls if in the 11 frame
                 if (framecount != 11)
                {
                    frameList.Add(firstball + bowl);
                    firstball = 0;
                    framecount++;
                    firstbowl = true;

                }
                }
        }
    }
    return frameList;
}

}

[SPOILER] - my beautiful code. It just doesn’t get nicer than this.

About 7 hours over here, including a decision to start again about 2 hours in, not so much re-factoring as deciding to totally change the way I approached the problem. But even my finished code is really not pretty. I think I did a few things “interestingly”…

  1. Created a newRolls and fleshed it out with a 0 after each strike, to help me tell strikes and spares apart. So %2 would always tell me whether I was on a first or second bowl. I realised later on that I didn’t really need to, but I liked the logical flow with this And Surprisingly it didn’t interfere with the final frame fixes at all! Worked just great.

  2. Perhaps this one isn’t weird at all, but I found that printing a string-converted version of the lists to Debug.Log was incredibly helpful for getting some transparency on what was going on.

I also thought another few hours in that it could be cool to actually code the whole thing backwards, so for strikes and spares you can just add the previous stored scores(s) (plural for the strikes).

Anyway, kudos to Ben for his amazing code. Beautiful to watch it being written, so thanks so much for the valuable lesson.

Liam

oh and here’s the rest - it was too much for instacode all at once. Weird, since it’s so concise.

I think it took me about four hours total. I had to break out pen and paper to help me with this one. Here’s my solution:

//returns list of individual frame scores. Not cumulative
public static List<int> ScoreFrames(List<int> rolls)
{
    var frameList = new List<int>();

    //only go up to the second to last roll since going to the last one would 
    //result in an index out of range operation
    for (int i = 0; i < rolls.Count - 1; i++)
    {
        var currentRoll = rolls[i];
        var nextRoll = rolls[i + 1];
        var strike = currentRoll == 10 || nextRoll == 10;
        var spare = currentRoll + nextRoll == 10;

        if (strike || spare)
        {
            if (!calculatedBonus(i, rolls, frameList, currentRoll, nextRoll))
                return frameList;

            //only skip a roll if we got a spare
            if (!strike)
                i++;

            continue;
        }

        //regular frame
        if (currentRoll != 10 && nextRoll != 10 && currentRoll + nextRoll != 10 && frameList.Count < 10)
        {
            frameList.Add(currentRoll + nextRoll);
            i++;
        }
    }

    return frameList;
}

private static bool calculatedBonus(int i, List<int> rolls, List<int> frameList, int currentRoll, int nextRoll)
{
    //there aren't enough rolls to score frame with bonus
    if (i + 2 == rolls.Count)
        return false;

    var bonusRoll = rolls[i + 2];

    frameList.Add(currentRoll + nextRoll + bonusRoll);
    return true;
}

Took me 45-50 minutes. It was an interesting problem.

Here is my code:

	public static List<int> ScoreFrames(List<int> turnScores) {
	List<int> frameScores = new List<int>();
	int totalTurns = turnScores.Count;

	int idx = 0;
	int currentFrame = 1;
	while (idx < totalTurns) {
		if (turnScores[idx] == MAX_PINS) {
			// Handle Strike.
			if ((idx + 2) >= totalTurns) break;
			frameScores.Add(turnScores[idx] + turnScores[idx + 1] + turnScores[idx + 2]);
			if (currentFrame == LAST_FRAME) break;
			++idx;
		} else {
			if ((idx + 1) >= totalTurns) break;
			if ((turnScores[idx] + turnScores[idx + 1]) == MAX_PINS) {
				// Handle Spare.
				if ((idx + 2) >= totalTurns) break;
				frameScores.Add(turnScores[idx] + turnScores[idx + 1] + turnScores[idx + 2]);
			} else {
				// Handle Open Frames.
				frameScores.Add(turnScores[idx] + turnScores[idx + 1]);
			}
			idx += 2;
		}
		++currentFrame;
	}

	return frameScores;
}

I just watched the video and Ben handles the frame counting slightly different and I am using a 0 index and everything looks ahead, but all in all similar solutions.

Privacy & Terms