Example #1
0
        public void IsValidDouble_ValidScores_ReturnsTrue()
        {
            // Arrange
            List <int> validValues = new List <int>
            {
                2, 4, 16, 20, 30, 40, 50
            };

            foreach (int val in validValues)
            {
                // Act
                bool result = Dart.IsValidDouble(val);

                // Assert
                Assert.IsTrue(result, string.Format("{0} was expected to be valid", val));
            }
        }
Example #2
0
        public void IsValidDouble_InvalidScores_ReturnsFalse()
        {
            // Arrange
            List <int> validValues = new List <int>
            {
                0, 1, 17, 21, 31, 39, 41, 49, 51
            };

            foreach (int val in validValues)
            {
                // Act
                bool result = Dart.IsValidDouble(val);

                // Assert
                Assert.IsFalse(result, string.Format("{0} was expected to be invalid", val));
            }
        }
Example #3
0
        /// <summary>
        /// Takes the points scored and updates the current score. Also determines the state of the turn based on result of the calculation.
        /// </summary>
        /// <param name="points">The points scored by the player.</param>
        /// <returns>True if the points are can be applied to the turn, otherwise false.</returns>
        public bool RecordPointsScored(int points)
        {
            // Make sure the state of the turn is appropriate for recording a score.
            // Make sure the points provided are valid for one dart.
            if (TurnState.InProgress != State || !Dart.IsValidScore(points))
            {
                return(false);
            }

            // Update the current score.
            CurrentScore -= points;

            // Update last points scored
            LastPointsScored = points;

            // Update Dart's Points
            switch (DartsRemaining)
            {
            case 3:
            {
                // Is the first dart
                FirstDartPoints = points;
                break;
            }

            case 2:
            {
                // Is the second dart
                SecondDartPoints = points;
                break;
            }

            case 1:
            {
                // Is the third dart
                ThirdDartPoints = points;
                break;
            }
            }

            // Check for possible win.
            if (0 == CurrentScore)
            {
                DartsRemaining = 0;
                State          = Dart.IsValidDouble(points) ? TurnState.Win : TurnState.Bust;
                return(true);
            }
            // Check for Bust scenarios.
            else if (1 == CurrentScore || 0 > CurrentScore)
            {
                DartsRemaining = 0;
                State          = TurnState.Bust;
                return(true);
            }

            // Update the count of remaining darts.
            DartsRemaining--;

            // Check to see if the turn is done because all darts have been thrown.
            if (0 == DartsRemaining)
            {
                State = TurnState.Done;
            }

            return(true);
        }