Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ActionMaster {
- public enum Action { Tidy, Reset, EndTurn, EndGame };
- private int[] bowls = new int[21];
- private int bowl = 1;
- public Action Bowl (int pins) {
- if (pins < 0 || pins > 10 ) { throw new UnityException("Invalid number of pins"); }
- bowls[bowl-1] = pins;
- if (bowl == 21) {
- return Action.EndGame;
- }
- if (bowl >= 19) {
- if (bowl == 20 && IsBowl19Strike()) {
- bowl++;
- return Action.Tidy;
- } else if (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("No action to return");
- }
- private bool Bowl21Awarded() {
- return (bowls[19-1] + bowls [20-1] >= 10);
- }
- private bool IsBowl19Strike() {
- return (bowls[19-1] == 10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement