public static BowlingGameModel MapViewModelToGameLogicModel(IEnumerable <Frame> data)
        {
            var bowlingGameModel = new BowlingGameModel
            {
                Frames = data.Select(e => new BLL.Model.Frame {
                    Pins = MapPinArray(e),
                }).ToList()
            };

            return(bowlingGameModel);
        }
        public void BuildScore_All_Strikes_Test()
        {
            var bowlingGame = new BowlingGameModel
            {
                Frames = new List <Frame>
                {
                    new Frame
                    {
                        Pins = new [] { 10, 10 }
                    },
                    new Frame
                    {
                        Pins = new [] { 10, 10 }
                    },
                    new Frame
                    {
                        Pins = new [] { 10, 10 }
                    },
                    new Frame
                    {
                        Pins = new [] { 10, 10 }
                    },
                    new Frame
                    {
                        Pins = new [] { 10, 10 }
                    },
                    new Frame
                    {
                        Pins = new [] { 10, 10 }
                    },
                    new Frame
                    {
                        Pins = new [] { 10, 10 }
                    },
                    new Frame
                    {
                        Pins = new [] { 10, 10 }
                    },
                    new Frame
                    {
                        Pins = new [] { 10, 10 }
                    },
                    new Frame
                    {
                        Pins = new [] { 10, 10, 10 }
                    }
                }
            };

            var scoreResult = _gameCalculator.BuildScore(bowlingGame);

            Assert.AreEqual(300, scoreResult);
        }
        public void BuildScore_FirstValue_Test()
        {
            var bowlingGame = new BowlingGameModel
            {
                Frames = new List <Frame>
                {
                    new Frame
                    {
                        Pins = new [] { 1, 4 }
                    },
                    new Frame
                    {
                        Pins = new [] { 4, 5 }
                    },
                    new Frame
                    {
                        Pins = new [] { 6, 4 }
                    },
                    new Frame
                    {
                        Pins = new [] { 5, 5 }
                    },
                    new Frame
                    {
                        Pins = new [] { 10, 10 }
                    },
                    new Frame
                    {
                        Pins = new [] { 0, 1 }
                    },
                    new Frame
                    {
                        Pins = new [] { 7, 3 }
                    },
                    new Frame
                    {
                        Pins = new [] { 6, 4 }
                    },
                    new Frame
                    {
                        Pins = new [] { 10, 10 }
                    },
                    new Frame
                    {
                        Pins = new [] { 2, 8, 6 }
                    }
                }
            };

            var scoreResult = _gameCalculator.BuildScore(bowlingGame);

            Assert.AreEqual(5, bowlingGame.Frames[0].Score);
        }
        public static IEnumerable <Frame> MapGameLogicModelToViewModel(BowlingGameModel bowlingGameModel)
        {
            var resultGameView = bowlingGameModel.Frames.Select(r => new Frame
            {
                PinFirst  = r.Pins[0],
                PinSecond = r.Pins[1],
                PinExtra  = (r.Pins.Count() > 2 ? r.Pins[2] : (int?)null),
                Score     = r.Score
            });

            return(resultGameView);
        }
Exemple #5
0
        public int BuildScore(BowlingGameModel bowlingGameModel)
        {
            int scoreResult = 0;

            for (int index = 0; index < bowlingGameModel.Frames.Count; index++)
            {
                var frames       = bowlingGameModel.Frames;
                var currentFrame = frames[index];

                //Strike check
                if (IsStrike(currentFrame))
                {
                    scoreResult += CalculateScore(
                        //Calculation strategy of Strike case for on Frame 10
                        (currentFrame1) => (FrameMaxPins + currentFrame1.Pins[1] + currentFrame1.Pins[2]),
                        //Calculation strategy of Strike case for all Frames
                        (frames1, currIndex) => (FrameMaxPins + frames1[currIndex + 1].Pins[0] + frames1[currIndex + 1].Pins[1]),
                        currentFrame, frames, index);
                }
                //Spare check
                else if (IsSpare(currentFrame))
                {
                    scoreResult += CalculateScore(
                        //Calculation strategy of Spare case for  on Frame 10
                        (currentFrame1) => (10 + currentFrame.Pins[2]),
                        //Calculation strategy of Spare case for all Frames
                        (frames1, currIndex) => (10 + frames1[currIndex + 1].Pins[0]),
                        currentFrame, frames, index);
                }
                else
                {
                    scoreResult += currentFrame.Pins[0] + currentFrame.Pins[1];
                }

                currentFrame.Score = scoreResult;
            }

            return(scoreResult);
        }
        public void BuildScore_AllValues_Success_Test()
        {
            var bowlingGame = new BowlingGameModel
            {
                Frames = new List <Frame>
                {
                    new Frame
                    {
                        Pins = new[] { 1, 4 }
                    },
                    new Frame
                    {
                        Pins = new[] { 4, 5 }
                    },
                    new Frame
                    {
                        Pins = new[] { 6, 4 }
                    },
                    new Frame
                    {
                        Pins = new[] { 5, 5 }
                    },
                    new Frame
                    {
                        Pins = new[] { 10, 10 }
                    },
                    new Frame
                    {
                        Pins = new[] { 0, 1 }
                    },
                    new Frame
                    {
                        Pins = new[] { 7, 3 }
                    },
                    new Frame
                    {
                        Pins = new[] { 6, 4 }
                    },
                    new Frame
                    {
                        Pins = new[] { 10, 10 }
                    },
                    new Frame
                    {
                        Pins = new[] { 2, 8, 6 }
                    }
                }
            };

            var scoreResult = _gameCalculator.BuildScore(bowlingGame);

            Assert.AreEqual(5, bowlingGame.Frames[0].Score);

            Assert.AreEqual(14, bowlingGame.Frames[1].Score);

            Assert.AreEqual(29, bowlingGame.Frames[2].Score);

            Assert.AreEqual(49, bowlingGame.Frames[3].Score);

            Assert.AreEqual(60, bowlingGame.Frames[4].Score);

            Assert.AreEqual(61, bowlingGame.Frames[5].Score);

            Assert.AreEqual(77, bowlingGame.Frames[6].Score);

            Assert.AreEqual(97, bowlingGame.Frames[7].Score);

            Assert.AreEqual(117, bowlingGame.Frames[8].Score);

            Assert.AreEqual(133, bowlingGame.Frames[9].Score);

            Assert.AreEqual(133, scoreResult);
        }