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);
        }
        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);
        }
        public void BalanceExceedsLimitByReturnsFalseOnEqual()
        {
            var player = new Player("Test", 5);

            var response = player.balanceExceedsLimitBy(5);

            Assert.True(response);
        }
        public void BalanceExceedsLimitByReturnsFalseWhenBalanceDoesNotExceedBetLimit()
        {
            var player = new Player("Test", 10) { Limit = 0 };

            var response = player.balanceExceedsLimitBy(15);

            Assert.False(response);
        }
        public void BalanceExceedsLimitByReturnsTrueWhenBalanceExceedsBetLimit()
        {
            var player = new Player("Test", 10) { Limit = 0 };

            var response = player.balanceExceedsLimitBy(5);

            Assert.True(response);
        }
Example #7
0
        public int playRound(Player player, DiceValue pick, int bet)
        {
            using (LogContext.PushProperties(new PropertyEnricher("Player", player, true)))
            {
                Log.Information("Player {Name} has bet {Bet} on {Pick}\tBalance: {Balance}", player.Name, bet, pick, player.Balance);
            }

            if (player == null) throw new ArgumentException("Player cannot be null");
            if (player == null) throw new ArgumentException("Pick cannot be null");
            if (bet < 0) throw new ArgumentException("Bet cannot be negative");

            if (!this.PickCount.ContainsKey(pick)) this.PickCount.Add(pick, 0);
            this.PickCount[pick]++; // Increment counter for pick count for DiceValue

            Log.Information("Deducting bet");
            player.takeBet(bet);
            Log.Information("Balance: {Balance}", player.Balance);

            int matches = 0;
            for (int i = 0; i < dice.Count; i++)
            {
                var value = dice[i].roll();
                Log.Information("Dice {Number} is a {Roll}", i, value);

                if (!this.RollCount.ContainsKey(value)) this.RollCount.Add(value, 0);
                this.RollCount[value]++; // Increment counter for roll count for DiceValue

                if (value.Equals(pick))
                {
                    matches += 1;
                    Log.Information("Match!");
                }
                else
                {
                    Log.Information("Not a Match!");
                }
            }

            int winnings = matches * bet;

            if (matches > 0)
            {
                player.receiveWinnings(winnings);
                player.returnBet(bet);
            }

            Log.Information("Winnings are {Winnings}", winnings);
            Log.Information("Player's Balance is now {Balance}", player.Balance);

            return winnings;
        }
        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 #10
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));
        }
Example #11
0
        public int playRound(Player player, DiceValue pick, int bet)
        {
            if (player == null) throw new ArgumentException("Player cannot be null");
            if (player == null) throw new ArgumentException("Pick cannot be null");
            if (bet < 0) throw new ArgumentException("Bet cannot be negative");

            player.takeBet(bet);

            int matches = 0;
            for (int i = 0; i < dice.Count; i++)
            {
                dice[i].roll();
                if (values[i].Equals(pick)) matches += 1;
            }

            int winnings = matches * bet;
            if (matches > 0)
            {
                player.receiveWinnings(winnings);
            }

            return winnings;
        }
Example #12
0
        public void GivenPlayerPlaysARound_WhenTheplayerWinsOrLosesAMatch_BalanceIncreases(
         DiceValue pick,
         DiceValue dieValue1,
         DiceValue dieValue2,
         DiceValue dieValue3,
         int balance,
         int bet,
         int winnings,
         int total,
         string name
            )
        {
            // Arrange.
            var player = new Player(name, balance);

            // Act : deduct bet and add winnings;
            player.takeBet(bet);
            player.receiveWinnings(winnings);

            // Assert
            player.Balance.Should().Be(total);
        }
 public void TakeBetThrowsExceptionWhenBetExceedsBalance()
 {
     var player = new Player("Test", 0) { Limit = 0 };
     Assert.Throws<ArgumentException>(() => player.takeBet(5));
 }
Example #14
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");
            }
        }
Example #15
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();
        }
        public void TakesBetWhenBalanceExceedsBet()
        {
            var player = new Player("Test", 10) { Limit = 0 };
            player.takeBet(5);

            // Check bet was taken and error wasn't thrown.
            Assert.Equal(5, player.Balance);
        }
Example #17
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 #18
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 #19
0
        public void BalanceExceedsLimitBy_With5DollarBetWhenOnly6DollarsRemaining_ReturnsFalse(string name)
        {
            var player = new Player(name, 6) { Limit = 0 };

            player.balanceExceedsLimitBy(5).Should().BeTrue();
        }
        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);
        }