// calculate and set the spare score for the given frame public void SetSpareScore(int frame) { BowlScore framescore = scores[frame]; // the score is the score of both rolls in this frame and teh first roll of the next framescore.total = framescore.ball1 + framescore.ball2 + scores[frame + 1].ball1; }
public BowlScore[] scores; // all 10 frames of the game // constructor public BowlPlayer() { scores = new BowlScore[10]; for (int i = 0; i < scores.Length; ++i) { scores[i] = new BowlScore(); } ClearScore(); }
// set the ball 3 score of the current frame (final frame) public void SetBall3Score(int frame, int pinsDown) { BowlScore framescore = scores[frame]; if (IsStrike(frame) && framescore.ball2 < 10) { framescore.ball3 = pinsDown - framescore.ball2; } else { framescore.ball3 = pinsDown; // spare or two strikes } framescore.total = framescore.ball1 + framescore.ball2 + framescore.ball3; }
// calculate and set the strike score for the given frame // not called for the final frame public void SetStrikeScore(int frame) { BowlScore framescore = scores[frame]; framescore.total = framescore.ball1; // always add the score from first roll of the next frame framescore.total += scores[frame + 1].ball1; // if not the ninth or tenth frame, and the next frame is a strike, then add the the ball1 score from the frame after that if (frame < 8 && IsStrike(frame + 1)) { framescore.total += scores[frame + 2].ball1; } else { // for the ninth frame (frame 8) add the second ball from the next (final) frame (there always is one) framescore.total += scores[frame + 1].ball2; } }
// set the ball 2 score of the current frame public void SetBall2Score(int frame, int pinsDown) { BowlScore framescore = scores[frame]; if (IsStrike(frame)) // we must be in the final frame { framescore.ball2 = pinsDown; } else { framescore.ball2 = pinsDown - framescore.ball1; } // calculate this frame's total score if it isn't a spare or strike if (!IsSpare(frame) && !IsStrike(frame)) { framescore.total = pinsDown; } // if previous frame was a strike then set that frame's score if (frame > 0 && IsStrike(frame - 1)) { SetStrikeScore(frame - 1); } }
public float baseScreenWidth = 320.0f; // for iOS, the screen width we think we're rendering on void OnGUI() { useGUILayout = false; #if UNITY_IPHONE || UNITY_ANDROID float guiScale = Screen.width / baseScreenWidth; GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(guiScale, guiScale, 1)); #endif for (int f = 0; f < 10; f++) { string score = ""; BowlScore framescore = Bowl.player.scores[f]; int roll1 = framescore.ball1; int roll2 = framescore.ball2; int roll3 = framescore.ball3; switch (roll1) { case -1: score += " "; break; case 10: score += "X"; break; default: score += roll1; break; } score += "/"; if (Bowl.player.IsSpare(f)) { score += "I"; } else { switch (roll2) { case -1: score += " "; break; case 10: score += "X"; break; default: score += roll2; break; } } if (f == 9) { score += "/"; if (10 == roll2 + roll3) { score += "I"; } else { switch (roll3) { case -1: score += " "; break; case 10: score += "X"; break; default: score += roll3; break; } } } var y = 25; GUI.Label(new Rect(f * 30 + 5, y, 50, 20), score, style); int total = Bowl.player.GetScore(f); if (total != -1) { GUI.Label(new Rect(f * 30 + 5, y + 15, 50, 20), " " + total, style); } } }