Exemplo n.º 1
0
        public int Calculate(PlayerRolls playerRolls)
        {
            int score      = 0;
            var frameIndex = 0;
            var rolls      = playerRolls.Safe;

            for (var frame = 0; frame < 10; frame++)
            {
                if (IsStrike(rolls, frameIndex))
                {
                    score += 10 + rolls[frameIndex + 1] + rolls[frameIndex + 2];
                    frameIndex++;
                }
                else if (IsSpare(rolls, frameIndex))
                {
                    score      += 10 + rolls.Safe[frameIndex + 2];
                    frameIndex += 2;
                }
                else
                {
                    score      += rolls[frameIndex] + rolls[frameIndex + 1];
                    frameIndex += 2;
                }
            }

            return(score);
        }
Exemplo n.º 2
0
        public bool HasFinishedFrame(int frame, PlayerRolls playerRolls)
        {
            var frameIndex = FrameIndex(playerRolls, frame);

            if (_scoringStrategy.IsStrike(playerRolls, frameIndex))
            {
                frameIndex++;
                if (frame == MaxFramesCount)
                {
                    frameIndex += 2;
                }
            }
            else if (_scoringStrategy.IsSpare(playerRolls, frameIndex))
            {
                frameIndex += 2;
                if (frame == MaxFramesCount)
                {
                    frameIndex++;
                }
            }
            else
            {
                frameIndex += 2;
            }

            var playerFinished = playerRolls.Count >= frameIndex;

            return(playerFinished);
        }
Exemplo n.º 3
0
        private int FrameIndex(PlayerRolls playerRolls, int frame)
        {
            int frameIndex = 0;

            while (frame-- > 0)
            {
                if (_scoringStrategy.IsStrike(playerRolls, frameIndex))
                {
                    frameIndex++;
                }
                else
                {
                    frameIndex += 2;
                }
            }

            return(frameIndex);
        }
Exemplo n.º 4
0
 public bool IsSpare(PlayerRolls playerRolls, int frameIndex)
 {
     return(playerRolls.Safe[frameIndex] + playerRolls.Safe[frameIndex + 1] == 10);
 }
Exemplo n.º 5
0
 public bool IsStrike(PlayerRolls playerRolls, int frameIndex)
 {
     return(playerRolls.Safe[frameIndex] == 10);
 }