Refactored switch statement for NextAction (no need to instantiate an Action Master to get actions)
using UnityEngine;
using System.Collections.Generic;
public class ActionMaster
{
public enum Action { Tidy, Reset, EndTurn, EndGame };
public static Action NextAction (List<int> pinFall)
{
// Guard
if (pinFall.Count <= 0 || pinFall.Count > 21) throw new UnityException("Can not accept more than 21 or less than 0 pinFall list entries in ActionMaster.NextAction()");
// Get the most recent pinFall entry
var lastIndex = pinFall.Count - 1;
var lastPinFall = pinFall[lastIndex];
// Last frame special cases
//
// [19] [20] [21]
// 0 - tidy 0 - end
// 5 - tidy 1 - end
// 5 - tidy 5 - reset x
// 10 - reset 0 - tidy x
// 10 - reset 5 - tidy x
// 10 - reset 10 - reset x
switch (lastIndex)
{
// zero indexed 19 -> 18
case 18:
if (lastPinFall != 10) return Action.Tidy;
else return Action.Reset;
// zero indexed 20 -> 19
case 19:
var previousScore = pinFall[lastIndex - 1];
if (previousScore != 10 && previousScore + lastPinFall != 10) return Action.EndGame;
if (previousScore != 10 && previousScore + lastPinFall == 10) return Action.Reset;
if (previousScore == 10 && lastPinFall != 10) return Action.Tidy;
else return Action.Reset;
// zero indexed 21 -> 20
case 20:
return Action.EndGame;
}
// Otherwise if you bowl a stike you end the turn
// unless it was the second ball of the frame (so a spare) and still end the turn
if (lastPinFall == 10) return Action.EndTurn;
// All other cases -> if the last index is an odd number tidy, other wise end turn
if (lastIndex % 2 == 0) return Action.Tidy;
else return Action.EndTurn;
}
}