Break my code!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScoreMaster {
public enum Action {Reset, Tidy, EndTurn, EndGame};

private int currentBowl = 0;
private const int totalBowlsAllowed = 21;
private int[] bowls = new int[totalBowlsAllowed];

public Action Roll (int pinsStruck)
{
    if (pinsStruck < 0 || pinsStruck > 10)
    {
        throw new UnityException("invalid number of pins struck: " + pinsStruck);
    }

    // Enter the pin count into the array of bowls
    bowls[currentBowl] = pinsStruck;

    // Strike
    if (pinsStruck == 10)
    {
        return HandleStrike();
    }

    // First bowl of the frame
    if (currentBowl % 2 == 0)
    {
        return HandleFirstBall();
    }
    else
    // Second bowl of the frame
    {
        return HandleSecondBall();
    }

    throw new UnityException("unsure which action to return");
}

private Action HandleSecondBall()
{
    // Not a strike.
    currentBowl += 1;

    // If last two balls add up to 10, it's a spare
    if (bowls[currentBowl-1] + bowls[currentBowl-2] == 10)
    {
        // Spare on second ball of last frame allows one more ball.
        if (currentBowl == 20)
        {
            return Action.Reset;
        }

        // If it's a spare, end the turn.
        return Action.EndTurn;
    }

    // Reset, unless we bowled the 20th ball already.
    // In this case, a non-spare means end of the game.
    if (currentBowl == 20)
    {
        return Action.EndGame;
    }

    return Action.Reset;
}

private Action HandleFirstBall()
{
    // Not a strike. Can't be a spare.
    currentBowl += 1;
    return Action.Tidy;
}

private Action HandleStrike()
{
    // If a strike is bowled on the first or second ball of the
    // last frame, you get another ball.
    if (currentBowl == 18 || currentBowl == 19) {
        currentBowl++;
        return Action.Reset;
    } else if (currentBowl == 20)
    {
        // Last possible frame, strike was bowled, game over.
        currentBowl++;
        return Action.EndGame;
    }

    currentBowl += 2;
    return Action.EndTurn;
}

}

This looks like the ActionMaster.cs code. But it says ScoreMaster.cs .
I guess that was the joke or maybe not.

I think I deviated from the naming scheme Ben had in the course a little, but it does seem to be ActionMaster, you are right. But if you can find any logic errors that would be great!

Privacy & Terms