Saturday, November 3, 2012

Code Kata: Bowling Game

Problem:

Write a program, to score a game of Ten-Pin Bowling.

The scoring rules:

Each game, or "line" of bowling, includes ten turns, or "frames" for the bowler.

In each frame, the bowler gets up to two tries to knock down all ten pins.

If the first ball in a frame knocks down all ten pins, this is called a "strike". The frame is over. The score for the frame is ten plus the total of the pins knocked down in the next two balls.

If the second ball in a frame knocks down all ten pins, this is called a "spare". The frame is over. The score for the frame is ten plus the number of pins knocked down in the next ball.

If, after both balls, there is still at least one of the ten pins standing the score for that frame is simply the total number of pins knocked down in those two balls.

If you get a spare in the last (10th) frame you get one more bonus ball. If you get a strike in the last (10th)
frame you get two more bonus balls. These bonus throws are taken as part of the same turn. If a bonus ball knocks down all the pins, the process does not repeat. The bonus balls are only used to calculate the score of the final frame.

The game score is the total of all frame scores.

Examples:

X indicates a strike
/ indicates a spare
- indicates a miss
| indicates a frame boundary

X|X|X|X|X|X|X|X|X|X||XX
Ten strikes on the first ball of all ten frames.
Two bonus balls, both strikes.
Score for each frame == 10 + score for next two
balls == 10 + 10 + 10 == 30
Total score == 10 frames x 30 == 300

9-|9-|9-|9-|9-|9-|9-|9-|9-|9-||
Nine pins hit on the first ball of all ten frames.
Second ball of each frame misses last remaining pin.
No bonus balls.
Score for each frame == 9
Total score == 10 frames x 9 == 90

5/|5/|5/|5/|5/|5/|5/|5/|5/|5/||5
Five pins on the first ball of all ten frames.
Second ball of each frame hits all five remaining pins, a spare.
One bonus ball, hits five pins.
Score for each frame == 10 + score for next one
ball == 10 + 5 == 15
Total score == 10 frames x 15 == 150

Solution:

The written program allows the user to input their bowling score into the console ball by ball. Then the program prints out the summary tally along with the final score.

Source Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _02_TenPinBowling
{
    public class TenPinBowling
    {
        static void Main(string[] args)
        {
            ScoreKeeper scoring = new ScoreKeeper();
            scoring.Start();
            scoring.PrintTally();
            WaitForEnterToExit();
        }

        sealed class ScoreKeeper
        {
            private string[] _frameNumbers = {  "1st", "2nd", "3rd", "4th", "5th", 
                              "6th", "7th", "8th", "9th", "10th (last)" };
            private string[] _frameResults = new string[TotalFrames];
            private List<string> _ballResults = new List<string>();
            private string _bonusBallResults;

            public void Start()
            {
                PrintWelcomeMessage();
                InputPlayerScores();
            }

            private static void PrintWelcomeMessage()
            {
                Console.WriteLine("Welcome to Ten Pin Bowling Alley DownUnder!");
            }

            private void InputPlayerScores()
            {
                for (int i = 0; i < _frameNumbers.Length; i++)
                {
                    Console.WriteLine("Pins knocked down on first ball in {0} frame? [number, X, /]", 
                    _frameNumbers[i]);
                    InputBallScoresInFrame(i);
                }
            }

            private void InputBallScoresInFrame(int index)
            {
                string firstBallResult = Console.ReadLine();
                _ballResults.Add(firstBallResult);
                string secondBallResult = InputSecondBallScoreInFrame(index, firstBallResult);
                _frameResults[index] = firstBallResult + secondBallResult;
                InputBonusBallScores(index, firstBallResult, secondBallResult);
            }

            private string InputSecondBallScoreInFrame(int index, string firstBallResult)
            {
                string secondBallResult = string.Empty;
                if (!firstBallResult.Equals(Strike, StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("Pins knocked down on second ball in {0} frame? [number, X, /]", 
                    _frameNumbers[index]);
                    secondBallResult = Console.ReadLine();
                    _ballResults.Add(secondBallResult);
                }
                return secondBallResult;
            }

            private void InputBonusBallScores(int index, string firstBallResult, string secondBallResult)
            {
                if (index == _frameNumbers.Length - 1)
                {
                    if (firstBallResult.Equals(Strike, StringComparison.InvariantCultureIgnoreCase) 
                    || secondBallResult == Spare)
                    {
                        Console.WriteLine("Pins knocked down on first bonus ball? [number, X]");
                        string firstBonusBallResult = Console.ReadLine();
                        _ballResults.Add(firstBonusBallResult);
                        string secondBonusBallResult = GetSecondBonusBallScore(firstBallResult);
                        _bonusBallResults = firstBallResult + secondBonusBallResult;
                    }
                }
            }

            private string GetSecondBonusBallScore(string firstBallResult)
            {
                string secondBonusBallResult = string.Empty;
                if (firstBallResult.Equals(Strike, StringComparison.InvariantCultureIgnoreCase))
                {
                    Console.WriteLine("Pins knocked down on second bonus ball? [number, X]");
                    secondBonusBallResult = Console.ReadLine();
                    _ballResults.Add(secondBonusBallResult);
                }
                return secondBonusBallResult;
            }

            public void PrintTally()
            {
                Console.WriteLine("The tally of your game: ");
                for (int i = 0; i < _frameNumbers.Length; i++)
                    Console.Write(_frameResults[i] + "|");
                Console.WriteLine("||" + _bonusBallResults);
                Console.WriteLine("Final score is {0}", GetFinalScore());
            }

            private int GetFinalScore()
            {
                int finalScore = 0;
                for (int i = 0; i < _ballResults.Count; i++)
                {
                    finalScore += GetBallScore(i) + GetScoreForNextBalls(ref i);
                }
                return finalScore;
            }

            private int GetScoreForNextBalls(ref int index)
            {
                int currentIndex = index;
                string result = _ballResults[index];
                if (result.Equals(Strike, StringComparison.InvariantCultureIgnoreCase))
                {
                    if (index == _ballResults.Count - 3)
                        index += 2;
                    return GetBallScore(currentIndex + 1) + GetBallScore(currentIndex + 2);
                }
                else if (result == Spare)
                {
                    if (index == _ballResults.Count - 2)
                        index++;
                    return GetBallScore(currentIndex + 1);
                }
                return 0;
            }

            private int GetBallScore(int index)
            {
                string score = _ballResults[index];
                if (score.Equals(Strike, StringComparison.InvariantCultureIgnoreCase))
                    return TotalPins;
                else if (score == "/")
                    return TotalPins - Convert.ToInt32(_ballResults[index - 1]);
                return Convert.ToInt32(score);
            }
        }

        private const string Strike = "x";
        private const string Spare = "/";
        private const int TotalPins = 10;
        private const int TotalFrames = 10;

        private static void WaitForEnterToExit()
        {
            Console.WriteLine("Press enter to exit");
            Console.Read();
        }
    }
}