Advertisement
Sweetsour

Mike's ActionMaster Script

Jun 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. public class ActionMaster {
  2.     public enum Action { Tidy, Reset, EndTurn, EndGame };
  3.  
  4.     private int[] bowls = new int[21];
  5.     private int bowl = 1;
  6.  
  7.     public Action Bowl (int pins) {
  8.         if (pins < 0 || pins > 10 ) { throw new UnityException("Invalid number of pins"); }
  9.  
  10.         bowls[bowl-1] = pins;
  11.  
  12.         if (bowl == 21) {
  13.             return Action.EndGame;
  14.         }
  15.  
  16.         if (bowl >= 19) {
  17.             if (bowl == 20 && IsBowl19Strike()) {
  18.                 bowl++;
  19.                 return Action.Tidy;
  20.             } else if (Bowl21Awarded()) {
  21.                 bowl++;
  22.                 return Action.Reset;
  23.             } else if (bowl == 20 && !Bowl21Awarded()) {
  24.                 return Action.EndGame;
  25.             }
  26.         }
  27.  
  28.         if (pins == 10) {
  29.             bowl += 2;
  30.             return Action.EndTurn;
  31.         }
  32.  
  33.         if (bowl % 2 != 0) { // Mid-Frame (or last frame)
  34.             bowl++;
  35.             return Action.Tidy;
  36.         } else if (bowl % 2 == 0) { // End of Frame
  37.             bowl++;
  38.             return Action.EndTurn;
  39.         }
  40.  
  41.         throw new UnityException("No action to return");
  42.     }
  43.  
  44.     private bool Bowl21Awarded() {
  45.         return (bowls[19-1] + bowls [20-1] >= 10);
  46.     }
  47.  
  48.     private bool IsBowl19Strike() {
  49.         return (bowls[19-1] == 10);
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement