Advertisement
Guest User

ActionMaster.cs

a guest
Apr 28th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ActionMaster {
  6.  
  7. public enum Action {Tidy, EndTurn, EndGame, Reset};
  8.  
  9. private int currentFrame = 1;
  10. private int currentPlayerIndex = 0;
  11. private string currentPlayer;
  12.  
  13. private SortedList<string, Frame[]> scoreCard;
  14.  
  15. // requires a list of player names
  16. public ActionMaster(string[] newPlayers) {
  17. scoreCard = new SortedList<string, Frame[]>();
  18. foreach (string player in newPlayers) {
  19. Frame[] frames = new Frame[10];
  20. for (int i = 0; i < 9; i++) {
  21. frames[i] = new Frame(Frame.FrameType.Normal, (i+1));
  22. }
  23. frames[9] = new Frame(Frame.FrameType.Last, 10);
  24. scoreCard[player] = frames;
  25. }
  26. //set the current player name to first player
  27. currentPlayer = scoreCard.Keys[currentPlayerIndex];
  28. }
  29.  
  30. public string GetCurrentPlayer() {
  31. return currentPlayer;
  32. }
  33.  
  34. //return the score
  35. public int GetPlayerScore(string playerName) {
  36. Frame[] frames = scoreCard[playerName];
  37. int score = 0;
  38. for (int i = 0; i < currentFrame; i++) {
  39. Frame nextFrame = null, twoAfterFrame = null;
  40. if ((i + 1) < currentFrame) nextFrame = frames[i+1];
  41. if ((i + 2) < currentFrame) twoAfterFrame = frames[i+2];
  42. int frameScore = frames[i].GetScore(nextFrame, twoAfterFrame);
  43. if (frameScore > 0) {
  44. //int frameON = i + 1;
  45. //Debug.Log ("score for player: " + playerName + ", on frame: " + frameON + ": " + frameScore);
  46. score += frameScore;
  47. }
  48.  
  49. }
  50. return score;
  51. }
  52.  
  53. protected bool IsLastPlayer() {
  54. int playerNum = currentPlayerIndex + 1;
  55. if (playerNum < scoreCard.Count) return false;
  56. else return true;
  57. }
  58.  
  59. protected void StartNewRound() {
  60. currentPlayerIndex = 0;
  61. currentPlayer = scoreCard.Keys[currentPlayerIndex];
  62. currentFrame += 1;
  63. }
  64.  
  65. protected void AdvanceToNextPlayer() {
  66. currentPlayerIndex++;
  67. currentPlayer = scoreCard.Keys[currentPlayerIndex];
  68. }
  69.  
  70. public Action Bowl (int pins) {
  71.  
  72. if ((pins < 0) || (pins > 10)) throw new UnityException ("Pin count is invalid!");
  73.  
  74. int frameIndex = currentFrame - 1;
  75. Frame[] frames = scoreCard[currentPlayer];
  76. Frame currentPlayerFrame = frames[frameIndex];
  77.  
  78. currentPlayerFrame.AddBowl(pins);
  79.  
  80. // check to see if the player's frame is finished
  81. if (currentPlayerFrame.IsFrameFinished()) {
  82. //check to see if this is the last player
  83. if (IsLastPlayer()) {
  84. //if it is the last frame, end the game
  85. if (currentFrame == 10) return Action.EndGame;
  86. // otherwise advance the current frame and player
  87. StartNewRound();
  88. } else {
  89. AdvanceToNextPlayer();
  90. }
  91. return Action.EndTurn;
  92. } else {
  93. // handle the special case of a strike or spare on the last frame
  94. if (currentFrame == 10) {
  95.  
  96. if (currentPlayerFrame.IsSpare()) return Action.Reset;
  97. else if (currentPlayerFrame.IsStrike()) {
  98. if(currentPlayerFrame.GetBowlCount() == 2) return Action.Tidy;
  99. else if (currentPlayerFrame.GetBowlCount() == 1) return Action.Reset;
  100. }
  101. }
  102. // frame is not finished so tidy
  103. return Action.Tidy;
  104. }
  105.  
  106. }
  107.  
  108. // protected inner class to hold frame data
  109. protected class Frame {
  110. public enum FrameType {Normal, Last};
  111.  
  112. // will include 3 on last frame
  113. private int[] bowls;
  114.  
  115. //private int frameNumber;
  116.  
  117. private int bowlsTaken = 0;
  118.  
  119. private FrameType type;
  120.  
  121. public Frame(FrameType type, int number) {
  122. if (type.Equals(FrameType.Last)) bowls = new int[3];
  123. else bowls = new int[2];
  124. // initialize array to zero values
  125. for (int i = 0; i < bowls.Length; i++) {
  126. bowls[i] = 0;
  127. }
  128. //frameNumber = number;
  129. this.type = type;
  130. }
  131.  
  132. // clean up the code in other calls
  133. public bool IsStrikeOrSpare() {
  134. return (IsStrike() || IsSpare());
  135. }
  136.  
  137. // ok to pass a null next frame on last frame
  138. // if the score cannot yet be calculated, -1 is
  139. // returned
  140. public int GetScore(Frame nextFrame, Frame secondNextFrame) {
  141. if (IsStrike()) return CalculateStrikeScore(nextFrame, secondNextFrame);
  142. else if (IsSpare()) return CalculateSpareScore(nextFrame);
  143. else return CalculateNormalScore();
  144. }
  145.  
  146. /**
  147. * If two bowls have been taken, a score is returned. Otherwise, -1 is returned.
  148. */
  149. private int CalculateNormalScore() {
  150. if (bowlsTaken < 2) return -1;
  151. else return (bowls[0] + bowls[1]);
  152. }
  153.  
  154. /**
  155. * Calculates the score for a spare. If there has not been a next frame bowled yet,
  156. * this method is null safe so just pass in null. Will return -1 if there has not
  157. * been enough bowls to calculate
  158. */
  159. private int CalculateSpareScore(Frame nextFrame) {
  160. if (type.Equals(FrameType.Last)) {
  161. // last frame we use the 3 bowls to calculate
  162. if (bowlsTaken < 3) return -1;
  163. else return (bowls[0] + bowls[1] + bowls[2]);
  164. } else {
  165. if ((nextFrame == null) || (nextFrame.GetBowlCount() < 1)) return -1;
  166. else return (10 + nextFrame.GetFirstBowl());
  167.  
  168. }
  169. }
  170.  
  171. /**
  172. * Use this to calculate the score for a strike. It is ok to pass null values in
  173. * for input frames if they have not yet occured. A return of -1 indicates that
  174. * there is not enough data to calculate a score
  175. */
  176. private int CalculateStrikeScore(Frame nextFrame, Frame secondNextFrame) {
  177. if (type.Equals(FrameType.Last)) {
  178. // have to bowl 3 times on the last frame for a strike
  179. if (bowlsTaken < 3) return -1;
  180. else {
  181. return (bowls[0] + bowls[1] + bowls[2]);
  182. }
  183. } else {
  184. if (nextFrame == null) return -1;
  185. // case of a strike with the next bowl being normal
  186. if ((nextFrame.GetBowlCount() > 1) && (!nextFrame.IsStrike())) return (10 + nextFrame.GetFirstBowl() + nextFrame.GetSecondBowl());
  187. // ninth round special case
  188. if (nextFrame.type.Equals(FrameType.Last) && (nextFrame.bowlsTaken > 1)) {
  189. return (10 + nextFrame.GetFirstBowl() + nextFrame.GetSecondBowl());
  190. }
  191. // case of a strike and another strike
  192. if (nextFrame.IsStrike()) {
  193. //need the next frame to calculate
  194. if ((secondNextFrame == null) || (secondNextFrame.GetBowlCount() < 1)) return -1;
  195. else return (20 + secondNextFrame.GetFirstBowl());
  196. }
  197. //not enough frames to calculate yet
  198. return -1;
  199. }
  200. }
  201.  
  202. // true if all the bowls associated with this frame are completed.
  203. // note that in the case of a strike or spare a score may not yet be available
  204. public bool IsFrameFinished() {
  205. // deal with special cases first
  206. if (type.Equals(FrameType.Last)) {
  207. if (!IsStrikeOrSpare() && (GetBowlCount() > 1)) return true;
  208. else if (GetBowlCount() > 2) return true;
  209. else return false;
  210. } else {
  211. //normal frame
  212. if (IsStrike() || (GetBowlCount() > 1)) return true;
  213. else return false;
  214. }
  215. }
  216.  
  217. public void AddBowl(int pinCount) {
  218. bowls[bowlsTaken] = pinCount;
  219. //index by 1
  220. bowlsTaken++;
  221. }
  222.  
  223. public int GetFirstBowl() {
  224. return bowls[0];
  225. }
  226.  
  227. public int GetSecondBowl() {
  228. return bowls[1];
  229. }
  230.  
  231. public int GetBowlCount() {
  232. return bowlsTaken;
  233. }
  234.  
  235. public bool IsStrike() {
  236. if (bowls[0] == 10) return true;
  237. else return false;
  238. }
  239.  
  240. public bool IsSpare() {
  241. if (bowls[0] == 10) return false;
  242. int total = bowls[0] + bowls[1];
  243. if (total == 10) return true;
  244. return false;
  245. }
  246.  
  247.  
  248. }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement