Exemplo n.º 1
0
        public void Spare_Bonus_Calculation_Provided_One_Next_Roll()
        {
            var roll1           = GetRandomPins(0, 9);
            var roll2           = 10 - roll1;
            var roll3           = GetRandomPins(0, 10);
            var spareFrame      = new SpareFrame(roll1, roll2);
            var calculatedBonus = spareFrame.CalculateBonus(roll3);

            var actualBonus = roll3;

            Assert.AreEqual(actualBonus, calculatedBonus);
        }
Exemplo n.º 2
0
        public void When_InOneFrameKnockDownAllPinsUsingTwoRolls_then_SumScoreFromNextRoll()
        {
            var spareFrame = new SpareFrame(4);
            var nextFrame  = new DefaultFrame(3, 3);

            _theGame.Roll(spareFrame);
            _theGame.Roll(nextFrame);

            int score = _theGame.Score();

            Assert.That(score, Is.EqualTo(19));
        }
        public void ShouldReturnTrueIfSpare()
        {
            //arrange
            Ball   ball1 = new Ball(6);
            Ball   ball2 = new Ball(4);
            IFrame frame = new SpareFrame(ball1, ball2);

            //act
            bool isSpare = frame.IsSpare();

            //assert
            isSpare.Should().BeTrue();
        }
        public void ShouldGetAdditionAllBallScoreForSpare()
        {
            //arrange
            IFrame frame1        = new SpareFrame(new Ball(6), new Ball(4));
            IFrame frame2        = new RegularFrame(new Ball(3), new Ball(3));
            Score  expectedScore = new Score(13);

            frame1.AddNextFrame(frame2);
            //act
            Score actualScore = frame1.TotalScore();

            //assert
            actualScore.Should().Be(expectedScore);
        }
Exemplo n.º 5
0
        /// <inheritdoc />
        public List <Frame> GetFrames(int[] rolls)
        {
            int currentFrameIndex      = 0;
            int currentFrameRollsCount = 0;
            var frames = new List <Frame>();

            for (int i = 0; i < rolls.Length && currentFrameIndex <= FrameConstants.MAX_NUMBER_OF_FRAMES;)
            {
                currentFrameIndex++;
                Frame currentFrame;

                if (IsStrike(rolls[i]))
                {
                    currentFrame           = new StrikeFrame();
                    currentFrameRollsCount = 1;
                }
                else if (IsSpare(rolls[i], rolls[i + 1]))
                {
                    currentFrame           = new SpareFrame(rolls[i], rolls[i + 1]);
                    currentFrameRollsCount = FrameConstants.MAX_ROLLS_PER_NORMAL_FRAME;
                }
                else
                {
                    currentFrame           = new Frame(rolls[i], rolls[i + 1]);
                    currentFrameRollsCount = FrameConstants.MAX_ROLLS_PER_NORMAL_FRAME;
                }

                if (IsLastFrame(currentFrameIndex) && IsBonusTypeFrame(currentFrame.FrameType))
                {
                    // Case when 10th frame is strike frame.
                    if (currentFrame.Roll2 == null)
                    {
                        currentFrame.Roll2 = rolls[i + 1];
                    }
                    currentFrame.Roll3     = rolls[i + 2];
                    currentFrameRollsCount = FrameConstants.MAX_ROLLS_FOR_LAST_FRAME;
                }

                i += currentFrameRollsCount;
                currentFrame.Id    = currentFrameIndex;
                currentFrame.Bonus = currentFrame.CalculateBonus(GetRollPointsAt(rolls, i), GetRollPointsAt(rolls, i + 1));
                frames.Add(currentFrame);
            }
            return(frames);
        }
        public void ShouldScoreGameWithSpare()
        {
            //arrange
            Game   game   = new Game();
            IFrame frame1 = new SpareFrame(new Ball(4), new Ball(6));
            IFrame frame2 = new RegularFrame(new Ball(3), new Ball(3));

            game.AddFrame(frame1);
            game.AddFrame(frame2);
            AddFramesWithScore(game, 8);
            Score expectedScore = new Score(19);

            //act
            Score actualScore = game.ScoreGame();

            //assert
            actualScore.Should().Be(expectedScore);
        }
Exemplo n.º 7
0
        internal static Frame Create(string singleFrameToParse,
                                     Roll[] frameRolls,
                                     Roll nextRoll,
                                     Roll secondNextRollForStrike)
        {
            var frame = new Frame(frameRolls);

            if (Parser.IsSpare(singleFrameToParse))
            {
                frame = new SpareFrame(nextRoll,
                                       frameRolls);
            }
            if (Parser.IsStrike(singleFrameToParse))
            {
                frame = new StrikeFrame(nextRoll,
                                        secondNextRollForStrike,
                                        frameRolls);
            }

            return(frame);
        }
Exemplo n.º 8
0
        public Frame CreateFrame(int pindowns)
        {
            Frame frame = null;

            // Handling last frame which would be spare or strike
            if (isSpecialFrame && frameCount == 10)
            {
                if (isSprareSpecial)
                {
                    frame = new SpecialFrame(prevRoll, currentRoll, pindowns);
                }
                else if (isStrikeSpecial && roll == 1)
                {
                    currentRoll = pindowns;
                    roll++;
                }
                else
                {
                    frame = new SpecialFrame(prevRoll, currentRoll, pindowns);
                }
            }
            else if (pindowns == 10)
            {
                frameCount++;
                if (frameCount == 10)
                {
                    // pindown is 10 and it is first roll of last frame
                    isSpecialFrame  = true;
                    prevRoll        = pindowns;
                    isStrikeSpecial = true;
                    roll++;
                }
                else
                {
                    frame = new StrikeFrame(10, 0); // if pindown is 10 and it is not last frame
                }
                if (frameCount != 10)
                {
                    prevRoll = 0;
                }
            }
            else if (firstRoll)
            {
                prevRoll  = pindowns;
                firstRoll = false;
            }
            else
            {
                if (prevRoll + pindowns == 10)
                {
                    firstRoll = true;
                    frameCount++;
                    if (frameCount == 10)
                    {
                        // last frame and it is second roll, eligble for special frame
                        isSpecialFrame  = true;
                        currentRoll     = pindowns;
                        isSprareSpecial = true;
                    }
                    else
                    {
                        frame = new SpareFrame(prevRoll, pindowns);
                    }
                }
                else
                {
                    firstRoll = true;
                    frameCount++;
                    frame = new NormalFrame(prevRoll, pindowns);
                }
                if (frameCount != 10)
                {
                    prevRoll = 0;
                }
            }

            return(frame);;
        }
Exemplo n.º 9
0
        public void GenerateFrame(int pins)
        {
            var currentFrame = _framesList.LastOrDefault();

            //initialise first frame
            if (currentFrame == null)
            {
                var frame = new Frame()
                {
                    FrameID = 1,
                    Rolls   = new List <int>()
                    {
                        pins
                    }
                };
                _framesList.Add(frame);
                return;
            }

            var isStrikeAchieved = (currentFrame.Rolls.Count == 1 && currentFrame.Rolls[0] == _maxPins);
            var isSpareAchieved  = (currentFrame.Rolls.Count == 2 && currentFrame.Rolls.Sum(x => x) == _maxPins);
            var isLastFrame      = currentFrame.FrameID == _maxFrames;

            //generate strike frame
            if (isStrikeAchieved && !isLastFrame)
            {
                var strikeFrame = new StrikeFrame()
                {
                    FrameID = currentFrame.FrameID,
                    Rolls   = new List <int>()
                    {
                        currentFrame.Rolls[0]
                    }
                };
                _framesList[currentFrame.FrameID - 1] = strikeFrame;
            }
            //generate spare frame
            else if (isSpareAchieved && !isLastFrame)
            {
                var spareFrame = new SpareFrame()
                {
                    FrameID = currentFrame.FrameID,
                    Rolls   = new List <int>()
                    {
                        currentFrame.Rolls[0], currentFrame.Rolls[1]
                    }
                };
                _framesList[currentFrame.FrameID - 1] = spareFrame;
            }
            //generate default frame
            else if (isLastFrame || currentFrame.Rolls.Count < 2)
            {
                if (currentFrame.Rolls.Count != 3)
                {
                    currentFrame.Rolls.Add(pins);
                }
                return;
            }

            //if nothing above matches, then for current roll make another frame
            var newFrame = new Frame()
            {
                FrameID = currentFrame.FrameID + 1,
                Rolls   = new List <int>()
                {
                    pins
                }
            };

            _framesList.Add(newFrame);

            return;
        }
Exemplo n.º 10
0
 public SpareFrameTests()
 {
     _frameUnderTest = new SpareFrame(new Roll(5));
 }