public void DiceFacesShouldAppearRandomlyAndStatisticallySimilarCounts()
        {
            var die1 = Substitute.For<Dice>();
            var die2 = Substitute.For<Dice>();
            var die3 = Substitute.For<Dice>();

            int bet = 5;

            int winCount = 0;
            int loseCount = 0;

            var pick = Dice.RandomValue;

            var game = new Game(die1, die2, die3);

            Program.Play100Games(bet, game, ref pick, ref winCount, ref loseCount);

            foreach(var face in Enum.GetValues(typeof(DiceValue)))
            {
                Assert.Contains(face.ToString(), game.PickCount.Keys.Select(k => k.ToString()));
                Assert.Contains(face.ToString(), game.RollCount.Keys.Select(k => k.ToString()));
            }

            // Make sure that each face has come up in 100 games.
            Assert.Equal(6, game.PickCount.Keys.Count);
            Assert.Equal(6, game.RollCount.Keys.Count);

            Assert.InRange(game.PickCount.Values.Min(), 100/6 - 10, 100/6 + 10);
            Assert.InRange(game.PickCount.Values.Max(), 100 / 6 - 10, 100 / 6 + 10);

            Assert.InRange(game.RollCount.Values.Min(), 300 / 6 - 30, 300 / 6 + 30);
            Assert.InRange(game.RollCount.Values.Max(), 300 / 6 - 30, 300 / 6 + 30);
        }
        public void BugFixForReturningStakeWhenWon()
        {
            var die1 = Substitute.For<Dice>();
            var die2 = Substitute.For<Dice>();
            var die3 = Substitute.For<Dice>();

            die1.currentValue = DiceValue.ANCHOR;
            die2.currentValue = DiceValue.HEART;
            die3.currentValue = DiceValue.HEART;

            DiceValue pick = DiceValue.ANCHOR;
            int bet = 10;
            int winnings = 0;
            int funds = 100;

            var player = Substitute.For<Player>("Test", funds);

            var game = new Game(die1, die2, die3);

            winnings = game.playRound(player, pick, bet);

            player.Received().returnBet(Arg.Is(bet));

            Assert.Equal(bet, winnings);
            Assert.Equal(funds + bet, player.Balance);
        }
        public void BroadTestForPlayerCanReachBettingLimit()
        {
            var die1 = Substitute.For<Dice>();
            var die2 = Substitute.For<Dice>();
            var die3 = Substitute.For<Dice>();

            die1.currentValue = DiceValue.ANCHOR;
            die2.currentValue = DiceValue.HEART;
            die3.currentValue = DiceValue.HEART;

            DiceValue pick = DiceValue.CLUB;
            int bet = 5;
            int winnings = 0;
            int funds = 100;

            int winCount = 0;
            int loseCount = 0;

            Player player;
            var game = new Game(die1, die2, die3);

            do
            {
                player = new Player("Test", funds) {Limit = 0};

                Program.PlayGame(bet, game, player, ref pick, 1, ref winCount, ref loseCount);

            } while (player.Balance >= 200); // Repeat test if it breaks bank at $200.

            Assert.Equal(0, player.Balance);
        }
        public void BroadTestForCorrectOdds()
        {
            var die1 = Substitute.For<Dice>();
            var die2 = Substitute.For<Dice>();
            var die3 = Substitute.For<Dice>();

            var gamesPlayed = 0;
            var gamesWon = 0;

            for (int i = 0; i < 100; i++)
            {
                int bet = 5;
                int winnings = 0;
                int funds = 100;

                int winCount = 0;
                int loseCount = 0;

                var player = new Player("Test", funds) {Limit = 0};
                var game = new Game(die1, die2, die3);

                var pick = Dice.RandomValue;

                Program.PlayGame(bet, game, player, ref pick, i, ref winCount, ref loseCount);

                gamesPlayed += (winCount + loseCount);
                gamesWon += winCount;
            }

            // Make sure the games come in close to 0.42.
            Assert.InRange(gamesWon/(double) gamesPlayed, 0.41, 0.43);
        }
        internal static void PlayGame(int bet, Game game, Player player, ref DiceValue pick, int currentGame, ref int winCount, ref int loseCount)
        {
            Console.Write("Start Game {0}: ", currentGame);
            Console.WriteLine("{0} starts with balance {1}", player.Name, player.Balance);

            int turn = 0;

            while (player.balanceExceedsLimitBy(bet) && player.Balance < 200)
            {
                try
                {
                    PlayRound(bet, game, player, pick, ref winCount, ref loseCount);
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine("{0}\n\n", e.Message);
                }

                pick = Dice.RandomValue;
                turn++;
            } //while

            Console.Write("{1} turns later.\nEnd Game {0}: ", turn, currentGame);
            Console.WriteLine("{0} now has balance {1}\n", player.Name, player.Balance);
        }
        internal static void PlayRound(int bet, Game game, Player player, DiceValue pick, ref int winCount, ref int loseCount)
        {
            var winnings = game.playRound(player, pick, bet);
            var currentDiceValues = game.CurrentDiceValues;

            Console.WriteLine("Rolled {0} {1} {2}", currentDiceValues[0], currentDiceValues[1], currentDiceValues[2]);
            if (winnings > 0)
            {
                Console.WriteLine("{0} won {1} balance now {2}", player.Name, winnings, player.Balance);
                winCount++;
            }
            else
            {
                Console.WriteLine("{0} lost {1} balance now {2}", player.Name, bet, player.Balance);
                loseCount++;
            }
        }
        internal static void Play100Games(int bet, Game game, ref DiceValue pick, ref int totalWins, ref int totalLosses)
        {
            int winCount = 0;
            int loseCount = 0;

            var player = new Player("Fred", 100);

            for (int i = 0; i < 100; i++)
            {
                PlayGame(bet, game, player, ref pick, i, ref winCount, ref loseCount);
            } //for

            Log.Information("Win count = {Wins}, Lose Count = {Losses}, {Rate}", winCount, loseCount,
                (float)winCount / (winCount + loseCount));
            Console.WriteLine("Win count = {0}, Lose Count = {1}, {2:0.00}", winCount, loseCount,
                (float) winCount/(winCount + loseCount));

            totalWins += winCount;
            totalLosses += loseCount;
        }
Example #8
0
        public void EnsurePlayerIsNotWinningTooFrequently()
        {
            var die1 = new Dice();
            var die2 = new Dice();
            var die3 = new Dice();

            // Introduce a player with $100
            var player = new Player("SomeGuy", 100);

            // Choose a symbol from the playmat.
            var pick = Dice.RandomValue;

            // Place a bet of $5
            const int bet = 5;

            // Start a game
            var game = new Game(die1, die2, die3);
            var numMatches = 0;
            var numturns = 0;

            while (player.balanceExceedsLimitBy(bet) && player.Balance < 150)
            {
                numturns++;
                IList<DiceValue> currentValues1 = new List<DiceValue>(game.CurrentDiceValues);
                game.PlayRound(player, pick, bet);
                var currentValues2 = game.CurrentDiceValues;

                try
                {
                    currentValues1.Should().NotBeEquivalentTo(currentValues2);
                }
                catch (AssertException e)
                {
                    numMatches++;
                }
            }

            numMatches.Should().BeLessThan((int)Math.Round(0.125 * numturns));
        }
        public void BroadTestForUserPayoutOnSingleMatch()
        {
            var die1 = Substitute.For<IDice>();
            var die2 = Substitute.For<IDice>();
            var die3 = Substitute.For<IDice>();

            die1.roll().Returns(DiceValue.ANCHOR);
            die2.roll().Returns(DiceValue.HEART);
            die3.roll().Returns(DiceValue.HEART);

            DiceValue pick = DiceValue.ANCHOR;
            int bet = 10;
            int winnings = 0;
            int funds = 100;

            var player = Substitute.For<Player>("Test", funds);

            var game = new Game(die1, die2, die3);

            winnings = game.playRound(player, pick, bet);

            Assert.Equal(bet, winnings);
            Assert.Equal(funds + bet, player.Balance);
        }
Example #10
0
        static void Main(string[] args)
        {
            Dice d1 = new Dice();
            Dice d2 = new Dice();
            Dice d3 = new Dice();

            Player p = new Player("Fred", 100);
            Console.WriteLine(p);
            Console.WriteLine();

            Console.WriteLine("New game for {0}", p.Name);
            Game g = new Game(d1, d2, d3);
            IList<DiceValue> cdv = g.CurrentDiceValues;
            Console.WriteLine("Current dice values : {0} {1} {2}", cdv[0], cdv[1], cdv[2]);

            DiceValue rv = Dice.RandomValue;

            Random random = new Random();
            int bet = 5;
            p.Limit = 0;
            int winnings = 0;
            DiceValue pick = Dice.RandomValue;

            int totalWins = 0;
            int totalLosses = 0;

            while (true)
            {
                int winCount = 0;
                int loseCount = 0;
                for (int i = 0; i < 100; i++)
                {
                    p = new Player("Fred", 100);
                    Console.Write("Start Game {0}: ", i);
                    Console.WriteLine("{0} starts with balance {1}", p.Name, p.Balance);
                    int turn = 0;
                    while (p.balanceExceedsLimitBy(bet) && p.Balance < 200)
                    {
                        //Console.Write("Turn {0}: ", turn+1);
                        //Console.WriteLine("Player {0} betting {1} on {2}. Balance:{3}, Limit:{4}",
                        //                   p.Name, bet, Dice.stringRepr(pick), p.Balance, p.Limit);

                        try
                        {
                            p.placeBet(bet);
                            winnings = g.playRound(bet, pick);
                            cdv = g.CurrentDiceValues;

                            //Console.WriteLine("Rolled {0} {1} {2}", cdv[0], cdv[1], cdv[2]);
                            if (winnings > 0)
                            {
                                p.receiveWinnings(winnings);
                                //Console.WriteLine("{0} won {1}", p.Name, winnings);
                            }
                            else
                            {
                                //Console.WriteLine("{0} lost {1}", p.Name, bet);
                            }
                        }
                        catch (BelowLimitException e)
                        {
                            Console.WriteLine("Error: {0}", e.Message);
                            throw new Exception("Really don't expect to see this");
                        }
                        catch (ArgumentException e)
                        {
                            Console.WriteLine("{0}\n\n", e.Message);
                        }
                        pick = Dice.RandomValue;
                        winnings = 0;
                        turn++;
                    } //while

                    if (p.Balance >= 200) winCount++;
                    else loseCount++;
                    Console.Write("{1} turns later.\nEnd Game {0}: ", i, turn);
                    Console.WriteLine("{0} now has balance {1}\n", p.Name, p.Balance);
                } //for
                Console.WriteLine("Win count = {0}, Lose Count = {1}", winCount, loseCount);
                totalWins += winCount;
                totalLosses += loseCount;

                string ans = Console.ReadLine();
                if (ans.Equals("q")) break;
            } //while true
            Console.WriteLine("Overall loss rate = {0}%", (float)(totalLosses * 100) / (totalWins + totalLosses));
            Console.ReadLine();
        }
Example #11
0
        static void Main(string[] args)
        {
            // Create Dice.
            IDice d1 = new Dice();
            IDice d2 = new Dice();
            IDice d3 = new Dice();

            // Create player.
            IPlayer p = new Player("Fred", 100);
            Console.WriteLine(p);
            Console.WriteLine();

            // Create Game.
            Console.WriteLine("New game for {0}", p.Name);
            Game g = new Game(d1, d2, d3);

            // Initial dice values? Why does this matter?
            IList<DiceValue> cdv = g.CurrentDiceValues;
            Console.WriteLine("Current dice values : {0} {1} {2}", cdv[0], cdv[1], cdv[2]);

            // Unused random dice value? Why is this done?
            DiceValue rv = Dice.RandomValue;

            // Unused random
            Random random = new Random();

            // Setup initial conditions.
            int bet = 5;
            p.Limit = 0;
            int winnings = 0;
            DiceValue pick = Dice.RandomValue;
            Console.WriteLine("Chosen Dice Value:" + pick);

            int totalWins = 0;
            int totalLosses = 0;

            while (true)
            {
                int winCount = 0;
                int loseCount = 0;

                // Play 100 times
                for (int i = 0; i < 100; i++)
                {
                    // Re-creating player with the same balance each time.
                    p = new Player("Fred", 100);
                    Console.Write("Start Game {0}: ", i);
                    Console.WriteLine("{0} starts with balance {1}", p.Name, p.Balance);
                    int turn = 0;

                    // Balance lower limit 0 upper limit 200.
                    while (p.balanceExceedsLimitBy(bet) && p.Balance < 200)
                    {
                        try
                        {
                            winnings = g.PlayRound(p, pick, bet);
                            cdv = g.CurrentDiceValues;

                            Console.WriteLine("Picked {3} and Rolled {0} {1} {2}", cdv[0], cdv[1], cdv[2], pick);
                            if (winnings > 0)
                            {
                                Console.WriteLine("{0} bet {3} and won {1} balance now {2}", p.Name, winnings, p.Balance, bet);
                                winCount++;
                            }
                            else
                            {
                                Console.WriteLine("{0} bet {3} and lost {1} balance now {2}", p.Name, bet, p.Balance, bet);
                                loseCount++;
                            }
                        }
                        catch (ArgumentException e)
                        {
                            Console.WriteLine("{0}\n\n", e.Message);
                        }
                        pick = Dice.RandomValue;
                        winnings = 0;
                        turn++;
                    } //while

                    Console.Write("{1} turns later.\nEnd Game {0}: ", turn, i);
                    Console.WriteLine("{0} now has balance {1}\n", p.Name, p.Balance);
                } //for

                Console.WriteLine("Win count = {0}, Lose Count = {1}, {2:0.00}", winCount, loseCount, (float)winCount / (winCount + loseCount));
                totalWins += winCount;
                totalLosses += loseCount;

                string ans = Console.ReadLine();
                if (ans.Equals("q")) break;
            } //while true
            Console.WriteLine("Overall win rate = {0}%", (float)(totalWins * 100) / (totalWins + totalLosses));
            Console.ReadLine();
        }
Example #12
0
        static void Main(string[] args)
        {
            Dice d1 = new Dice();
            Dice d2 = new Dice();
            Dice d3 = new Dice();

            Player p = new Player("Fred", 100);
            Console.WriteLine(p);
            Console.WriteLine();

            Console.WriteLine("New game for {0}", p.Name);
            Game g = new Game(d1, d2, d3);
            IList<DiceValue> cdv = g.CurrentDiceValues;
            Console.WriteLine("Current dice values : {0} {1} {2}", cdv[0], cdv[1], cdv[2]);

            DiceValue rv = Dice.RandomValue;

            Random random = new Random();
            int bet = 5;
            p.Limit = 0;
            int winnings = 0;
            DiceValue pick = Dice.RandomValue;

            int totalWins = 0;
            int totalLosses = 0;

            while (true)
            {
                int winCount = 0;
                int loseCount = 0;
                for (int i = 0; i < 100; i++)
                {
                    p = new Player("Fred", 100);
                    Console.Write("Start Game {0}: ", i);
                    Console.WriteLine("{0} starts with balance {1}", p.Name, p.Balance);
                    int turn = 0;
                    while (p.balanceExceedsLimitBy(bet) && p.Balance < 200)
                    {
                        try
                        {
                            winnings = g.playRound(p, pick, bet);
                            cdv = g.CurrentDiceValues;

                            Console.WriteLine("Rolled {0} {1} {2}", cdv[0], cdv[1], cdv[2]);
                            if (winnings > 0)
                            {
                                Console.WriteLine("{0} won {1} balance now {2}", p.Name, winnings, p.Balance);
                                winCount++;
                            }
                            else
                            {
                                Console.WriteLine("{0} lost {1} balance now {2}", p.Name, bet, p.Balance);
                                loseCount++;
                            }
                        }
                        catch (ArgumentException e)
                        {
                            Console.WriteLine("{0}\n\n", e.Message);
                        }
                        pick = Dice.RandomValue;
                        winnings = 0;
                        turn++;
                    } //while

                    Console.Write("{1} turns later.\nEnd Game {0}: ", turn, i);
                    Console.WriteLine("{0} now has balance {1}\n", p.Name, p.Balance);
                } //for

                Console.WriteLine("Win count = {0}, Lose Count = {1}, {2:0.00}", winCount, loseCount, (float) winCount/(winCount+loseCount));
                totalWins += winCount;
                totalLosses += loseCount;

                string ans = Console.ReadLine();
                if (ans.Equals("q")) break;
            } //while true
            Console.WriteLine("Overall win rate = {0}%", (float)(totalWins * 100) / (totalWins + totalLosses));
            Console.ReadLine();
        }
Example #13
0
        public void GivenGameRoundIsPlayed_WhenTheplayerWinsOrLosesAMatch_PaysOutAtTheCorrectRate(
            DiceValue pick,
            DiceValue dieValue1,
            DiceValue dieValue2,
            DiceValue dieValue3,
            int balance,
            int bet,
            int winnings,
            int total,
            IDice die1,
            IDice die2,
            IDice die3,
            IPlayer player)
        {
            // Arrange.
            // Set the value of the dice rolled.
            die1.CurrentValue.Returns(dieValue1);
            die2.CurrentValue.Returns(dieValue2);
            die3.CurrentValue.Returns(dieValue3);

            die1.roll().Returns(dieValue1);
            die2.roll().Returns(dieValue2);
            die3.roll().Returns(dieValue3);

            // Taking a bet deducts money from the player.
            player.When(x => x.takeBet(Arg.Any<int>()));//.Do(x => total -= bet);

            var game = new Game(die1, die2, die3);

            // Act
            var sut = game.PlayRound(player, pick, bet);

            // Assert
            sut.Should().Be(winnings);
        }
        public void SideEffectBetIsDeductedDuringGame()
        {
            var die1 = Substitute.For<Dice>();
            var die2 = Substitute.For<Dice>();
            var die3 = Substitute.For<Dice>();

            DiceValue pick = DiceValue.ANCHOR;
            int bet = 10;
            int winnings = 0;
            int funds = 100;

            var player = Substitute.For<Player>("Test", funds);

            var game = new Game(die1, die2, die3);

            winnings = game.playRound(player, pick, bet);

            // Make sure at some point that the bet is deducted by the funds.
            player.Received().takeBet(bet);
        }
Example #15
0
        private static void Main(string[] args)
        {
            // Init a new instance of logger for logging to text file and console.
            Log.Logger = new LoggerConfiguration()
                .Enrich.FromLogContext()
            #if DEBUG
                .WriteTo.ColoredConsole()
            #endif
                .WriteTo.RollingFile(@"Log-{Date}.txt")
                .CreateLogger();

            Log.Information(
                $"----------------------------------------------\nStarting new instance at {DateTime.Now.ToString()}\n----------------------------------------------\n\n");

            try
            {

                Dice d1 = new Dice();
                Dice d2 = new Dice();
                Dice d3 = new Dice();

                Player player = new Player("Fred", 100);
                Console.WriteLine(player);
                Console.WriteLine();

                Console.WriteLine("New game for {0}", player.Name);
                Game game = new Game(d1, d2, d3);
                IList<DiceValue> currentDiceValues = game.CurrentDiceValues;
                Console.WriteLine("Current dice values : {0} {1} {2}", currentDiceValues[0], currentDiceValues[1], currentDiceValues[2]);

                int bet = 5;
                player.Limit = 0;
                int winnings = 0;
                DiceValue pick = Dice.RandomValue;

                int totalWins = 0;
                int totalLosses = 0;

                while (true)
                {
                    Play100Games(bet, game, ref pick, ref totalWins, ref totalLosses);

                    string ans = Console.ReadLine();
                    if (ans.Equals("q")) break;
                } //while true

                Log.Information("Overall win rate = {Rate}%", (float)(totalWins * 100) / (totalWins + totalLosses));
                Console.WriteLine("Overall win rate = {0}%", (float)(totalWins * 100) / (totalWins + totalLosses));
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Quit Application by force\n\n\n\n");
            }
        }
        public void SideEffectFundsAreIncrementedByWinningsDuringGame()
        {
            var die1 = Substitute.For<Dice>();
            var die2 = Substitute.For<Dice>();
            var die3 = Substitute.For<Dice>();

            DiceValue pick = DiceValue.ANCHOR;
            int bet = 10;
            int winnings = 0;
            int funds = 100;

            var player = Substitute.For<Player>("Test", funds);

            var game = new Game(die1, die2, die3);

            winnings = game.playRound(player, pick, bet);

            // Make sure at some point that the winnings are added to the funds during the game.
            player.Received().receiveWinnings(winnings);
        }
        public void WhenDiceRolledValueShouldReflectNewRoll()
        {
            var die1 = new Dice();
            var die2 = new Dice();
            var die3 = new Dice();

            int bet = 5;

            int winCount = 0;
            int loseCount = 0;

            var pick = Dice.RandomValue;
            var player = new Player("Test", 100);

            var game = new Game(die1, die2, die3);

            game.playRound(player, pick, bet);

            var newDie1Value = die1.roll();
            var newDie2Value = die2.roll();
            var newDie3Value = die3.roll();

            Assert.Equal(die1.CurrentValue, game.CurrentDiceValues[0]);
            Assert.Equal(die2.CurrentValue, game.CurrentDiceValues[1]);
            Assert.Equal(die3.CurrentValue, game.CurrentDiceValues[2]);

            Assert.Equal(newDie1Value, die1.CurrentValue);
            Assert.Equal(newDie2Value, die2.CurrentValue);
            Assert.Equal(newDie3Value, die3.CurrentValue);
        }