示例#1
0
        /// <summary> Applies specified dice operation to the given roll values. </summary>
        /// <remarks> <see cref="DiceOperation.Plus"/> and <see cref="DiceOperation.Minus"/> are modifying roll totals,
        /// so there isn't any good way to shove them in. For these operations 
        /// <paramref name="opValueProvider"/>'s value is being added to the first roll value.</remarks>
        /// <param name="rolls">Roll values to modify.</param>
        /// <param name="operation"></param>
        /// <param name="opValueProvider">Rolls a value which will be used for modification.</param>
        /// <returns>Rolls modified with <paramref name="opValueProvider"/>'s value according to the given operation. 
        /// </returns>
        public static IEnumerable<double> ApplyOperation(IEnumerable<double> rolls, DiceOperation operation,
            IDie opValueProvider)
        {
            Contract.Requires(rolls != null);
            Contract.Requires(operation != DiceOperation.None);
            Contract.Requires(opValueProvider != null);
            Contract.Ensures(Contract.Result<IEnumerable<double>>() != null);

            // Plus and Minus are modifying roll totals, so there isn't any good way to shove them in.
            // Let's just add them to first value.
            if (operation == DiceOperation.Minus || operation == DiceOperation.Plus) {
                bool applied = false;
                foreach (var roll in rolls) {
                    if (!applied) {
                        yield return PerformRollCombination(roll, operation, opValueProvider.Roll());
                        applied = true;
                    }
                    else { yield return roll; }
                }
                // If rolls were empty, return the total modifier as is:
                if (!applied) {
                    var value = opValueProvider.Roll();
                    yield return (operation == DiceOperation.Minus) ? -value : value;
                }
            }
            else {  // For other operations just modify each roll value using the value provider.
                foreach (var roll in rolls) {
                    yield return PerformRollCombination(roll, operation, opValueProvider.Roll());
                }
            }
        }
示例#2
0
        public void GivenAPlayerLandsOnTheFinalSquare_ThenTheyWinTheGame()
        {
            GivenIHaveMovedToSquare(97);
            _die.Roll().Returns(3);

            _game.MakeMove();

            _player.Square.ShouldEqual(100);
            _game.Winner.Should().BeEquivalentTo(_player);
        }
示例#3
0
 public void ShouldRollAndGetResultBetweenRange()
 {
     // there's a random factor inside the implementation but it check the random result is between the range
     for (int i = 0; i < 10; i++)
     {
         // not a deterministic test, but this will show some stability or unintentional updates across different tests
         _die.Roll();
         Assert.IsTrue(_die.Result >= 1 && _die.Result <= 10);
     }
 }
示例#4
0
        public void Roll()
        {
            var positionBeforeRoll = Square;
            var numberRolled       = _die.Roll();

            Square += numberRolled;
            if (Square > 100)
            {
                Square = positionBeforeRoll;
            }
        }
示例#5
0
        /// <summary> Applies specified dice operation to the given roll values. </summary>
        /// <remarks> <see cref="DiceOperation.Plus"/> and <see cref="DiceOperation.Minus"/> are modifying roll totals,
        /// so there isn't any good way to shove them in. For these operations
        /// <paramref name="opValueProvider"/>'s value is being added to the first roll value.</remarks>
        /// <param name="rolls">Roll values to modify.</param>
        /// <param name="operation"></param>
        /// <param name="opValueProvider">Rolls a value which will be used for modification.</param>
        /// <returns>Rolls modified with <paramref name="opValueProvider"/>'s value according to the given operation.
        /// </returns>
        public static IEnumerable <double> ApplyOperation(IEnumerable <double> rolls, DiceOperation operation,
                                                          IDie opValueProvider)
        {
            Contract.Requires(rolls != null);
            Contract.Requires(operation != DiceOperation.None);
            Contract.Requires(opValueProvider != null);
            Contract.Ensures(Contract.Result <IEnumerable <double> >() != null);

            // Plus and Minus are modifying roll totals, so there isn't any good way to shove them in.
            // Let's just add them to first value.
            if (operation == DiceOperation.Minus || operation == DiceOperation.Plus)
            {
                bool applied = false;
                foreach (var roll in rolls)
                {
                    if (!applied)
                    {
                        yield return(PerformRollCombination(roll, operation, opValueProvider.Roll()));

                        applied = true;
                    }
                    else
                    {
                        yield return(roll);
                    }
                }
                // If rolls were empty, return the total modifier as is:
                if (!applied)
                {
                    var value = opValueProvider.Roll();
                    yield return((operation == DiceOperation.Minus) ? -value : value);
                }
            }
            else    // For other operations just modify each roll value using the value provider.
            {
                foreach (var roll in rolls)
                {
                    yield return(PerformRollCombination(roll, operation, opValueProvider.Roll()));
                }
            }
        }
示例#6
0
        public void LoadGame(string path)
        {
            GameState = GameState.LoadGame(path);

            if (!GameState.IsSavedGame)
            {
                GameState.Protagonist = new Protagonist
                {
                    Skill =
                    {
                        Value = 6 + _die.Roll()
                    },
                    Stamina =
                    {
                        Value = 12 + _die.Roll() + _die.Roll()
                    },
                    Luck =
                    {
                        Value = 6 + _die.Roll()
                    }
                };

                GameState.Location = 0;
            }
        }
示例#7
0
        public void Move()
        {
            var newPosition = CurrentPosition + _die.Roll();

            if (newPosition <= _lastPosition)
            {
                CurrentPosition = newPosition;
            }
            else
            {
                Console.WriteLine($"--> {Name} tried to go outside of the board, so will stop at the current Position: {CurrentPosition}");
            }
        }
        public int RateGuess(int guess)
        {
            var result = _die.Roll();

            if (result == guess)
            {
                return(2);
            }
            if (result - 1 == guess || result + 1 == guess)
            {
                return(1);
            }
            return(0);
        }
示例#9
0
        public void GivenAPlayerMakesAMove_WhenTheyRollFour_ThenTheyInteractWithTheSquareFourSpacesAhead()
        {
            _die.Roll().Returns(4);

            _player.Roll();

            _die.Received(1).Roll();
            _player.Square.ShouldEqual(5);
        }
示例#10
0
        public void Die_throws_between_one_and_six()
        {
            var rolls = new List <int>();

            for (var i = 0; i < 100; i++)
            {
                var result = _die.Roll();

                Assert.That(result, Is.InRange(1, 6));

                rolls.Add(result);
            }

            Assert.True(rolls.Any(r => r == 1));
            Assert.True(rolls.Any(r => r == 2));
            Assert.True(rolls.Any(r => r == 3));
            Assert.True(rolls.Any(r => r == 4));
            Assert.True(rolls.Any(r => r == 5));
            Assert.True(rolls.Any(r => r == 6));
        }
示例#11
0
 public void ThrowWhenRolled(IDie die) => Assert.Throws <NDiceException>(() => die.Roll());
 public void TakeTurn()
 {
     MoveToken(_die.Roll());
 }