Skip to main content

Hands on Test Driven Development Katas

In these days, Test Driven Development (TDD) is one of the most growing things in the technical world. Most of us are following Agile methodology where we would like to test our code within code.In this article, we will discuss all about TDD Katas and how we can get hands-on with

C# (10.0 MB)
 
 
 
 
 
5 Star
(1)
223 times
Add to favorites
12/24/2015
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
namespace TDD_Katas_project.BowlingGame
{
    public class Game
    {
        #region Private members
        private int[] rolls = new int[21];
        private int _currentRoll = 0;

        #endregion
        #region Public Methods
        public void Roll(int pins)
        {
            rolls[_currentRoll++] = pins;
        }
        public int Score()
        {
            var score = 0;
            var frameIndex = 0;
            for (var frame = 0; frame < 10; frame++)
            {
                if (IsStrike(frameIndex))
                {
                    score += StrikeBonus(frameIndex);
                    frameIndex++;
                }
                else if (IsSpare(frameIndex))
                {
                    score += 10 + SpareBonus(frameIndex);
                    frameIndex += 2;
                }

                else
                {
                    score += SumOfBallsInFrames(frameIndex);
                    frameIndex += 2;
                }
            }
            return score;
        }

        private bool IsStrike(int frameIndex)
        {
            return rolls[frameIndex] == 10;
        }

        #endregion
        #region Private Methods
        private int SumOfBallsInFrames(int frameIndex)
        {
            return rolls[frameIndex] + rolls[frameIndex + 1];
        }
        private int SpareBonus(int frameIndex)
        {
            return rolls[frameIndex + 2];
        }
        private bool IsSpare(int frameIndex)
        {
            return rolls[frameIndex] + rolls[frameIndex + 1] == 10;
        }
        private int StrikeBonus(int frameIndex)
        {
            return 10 + rolls[frameIndex + 1] + rolls[frameIndex + 2];
        }
        #endregion
    }
}