Thanks for testing! Sorry, I didn’t post the whole code. But yes, I did have code to end on Bowl 21. Here’s the snippet of the whole thing:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ActionMaster {
public enum Action { Tidy, Reset, EndTurn, EndGame };
private int[] bowls = new int[21];
private int bowl = 1;
public static Action NextAction(List<int> pinFalls) {
ActionMaster actionMaster = new ActionMaster();
Action currentAction = new Action();
foreach(int pinFall in pinFalls) {
currentAction = actionMaster.Bowl(pinFall);
}
return currentAction;
}
public Action Bowl(int pins) { //TODO make private
//Check pin validity
if(pins < 0 || pins > 10) {
throw new UnityException("Invalid pin count.");
}
bowls[bowl - 1] = pins;
//Very last frame
if(bowl == 21) {
return Action.EndGame;
}
//Last frame special cases
if(bowl >= 19 && Bowl21Awarded()) {
if(bowl > 19 && bowls[19-1] == 10 && bowls[20-1] < 10) { //Strike at 19, Not at 20
bowl += 1;
return Action.Tidy;
}
bowl += 1;
return Action.Reset;
} else if(bowl == 20 && !Bowl21Awarded()) {
return Action.EndGame;
}
//Normal bowl cases
if(bowl % 2 != 0) { //First bowl of frame
if(pins == 10) { //Strike
bowl += 2;
return Action.EndTurn;
} else {
bowl += 1;
return Action.Tidy;
}
} else if (bowl % 2 == 0) { //Second bowl of frame
bowl += 1;
return Action.EndTurn;
}
throw new UnityException("Not sure what action to return.");
}
private bool Bowl21Awarded() {
return (bowls[19-1] + bowls [20-1] >= 10);
}
}