Last active
September 25, 2015 23:52
-
-
Save joshalanwagner/951a4b905aad854347cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class ActionMaster { | |
| public enum Action {Tidy, Reset, EndTurn, EndGame}; | |
| public int[] bowlScore = new int[21]; // leave bowlScore[0] empty so i can check backwards. | |
| public int[] frameScore = new int[12]; // leave frameScore[0] empty. | |
| private int bowl = 1; | |
| private int frame = 1; | |
| public string result = ""; | |
| private int ball = 1; | |
| private int thisScore; | |
| private int lastScore; | |
| private int scoreBeforeLast; | |
| // Because you can bowl three strikes on the last 'frame' I'm considering | |
| // them all 'frames' so the one strike per frame rule holds. | |
| // I simply ignore those 'frames' when reporting the score. | |
| // Strike can only happen on ball 1 according to my rules, since I count | |
| // additional strikes past frame 10 as additional frames. | |
| // | |
| // I consider this a reasonable alternative to having a special case for having | |
| // a ball 3 and checking for strikes on ball 2 and ball 3 of frame 10. | |
| public Action Bowl (int pins) | |
| { | |
| if (pins < 0 || pins > 10) {throw new UnityException ("Invalid Pins");} | |
| bowlScore[bowl] = pins; | |
| thisScore = bowlScore[bowl]; | |
| lastScore = bowlScore[bowl - 1]; | |
| if (bowl > 2) {scoreBeforeLast = bowlScore[bowl - 2];} | |
| // check what the quality is first. | |
| if (thisScore == 10) {result = "strike";} | |
| else if (ball == 2 && (lastScore + thisScore == 10)) {result = "spare";} | |
| else {result = "";} | |
| AddFrameScore (); | |
| if (frame == 12) {return Action.EndGame;} | |
| if (ball == 1) // First ball in frame | |
| { | |
| if (result == "strike") | |
| { | |
| if (frame == 10) | |
| { | |
| EndOfFrame (); | |
| return Action.Reset; | |
| } | |
| if (frame == 11 && lastScore == 10) | |
| { | |
| EndOfFrame (); | |
| return Action.Reset; | |
| } | |
| EndOfFrame (); | |
| return Action.EndTurn; | |
| } | |
| bowl += 1; | |
| ball = 2; | |
| return Action.Tidy; | |
| } | |
| else if (ball == 2) // Second ball in frame | |
| { | |
| if (frame == 10) | |
| { | |
| if (result == "spare") {return Action.Reset;} | |
| if (result == "" && lastScore == 10) {return Action.Tidy;} | |
| if (result == "") {return Action.EndGame;} | |
| } | |
| EndOfFrame (); | |
| ball = 1; | |
| return Action.EndTurn; | |
| } | |
| throw new UnityException ("Something wrong happened"); | |
| } | |
| private void EndOfFrame () | |
| { | |
| bowl += 1; | |
| frame += 1; | |
| } | |
| private void AddFrameScore () | |
| { | |
| // STRIKE? | |
| if (scoreBeforeLast == 10) // can only score a strike after two more bowls. | |
| { | |
| if (lastScore == 10) // if last TWO bowls were strikes the first strike was two frames ago. | |
| { | |
| frameScore[frame - 2] = frameScore[frame - 3] + thisScore + lastScore + scoreBeforeLast; | |
| } | |
| else // two bowls ago was a strike, but last one wasn't then it's only one frame back. | |
| { | |
| frameScore[frame - 1] = frameScore[frame - 2] + thisScore + lastScore + scoreBeforeLast; | |
| } | |
| } | |
| // SPARE? | |
| else if (ball == 1) // can only score a spare after one more bowl and only on ball 1. | |
| { | |
| if (scoreBeforeLast + lastScore == 10) // if last roll was a spare | |
| { | |
| frameScore[frame - 1] = frameScore[frame - 2] + thisScore + lastScore + scoreBeforeLast; | |
| } | |
| } | |
| // NO strike or spare | |
| if (result == "" && ball == 2) | |
| { | |
| frameScore[frame] = frameScore[frame - 1] + thisScore + lastScore; | |
| } | |
| // If no conditions met - can't record a frame score yet. | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment