public void Win1Randomness_IsApproximatelyConvergentToExpectation() { const DiceValue selectedFace = DiceValue.SPADE; const int numDice = 3; const int numThrows = 10000; var hitCount = 0d; var die = new Dice(); for (var i = 0; i <= numThrows; i++) { var hit = false; var rollCount = 0; for (var j = 0; j < numDice; j++) { die.roll(); if (die.CurrentValue == selectedFace) rollCount++; } if (rollCount == 1) hitCount++; } hitCount.Should() .BeGreaterThan(34) .And.BeLessThan(35, " P(X=1)=P (first dice match) +P(second dice match) +P(third dice match) =1/6 . 5/6 . 5/6 + 5/6 . 1/6 . 5/6 + 5/6 . 5/6 . 1/6 = 25/72 = 0.3472 And Should converge to:- 100 * 0.3472"); }
public void TestRoll() { Dictionary<DiceValue, int> countMap = new Dictionary<DiceValue, int>() { { DiceValue.CROWN,0}, { DiceValue.ANCHOR,0}, { DiceValue.HEART,0}, { DiceValue.DIAMOND,0}, { DiceValue.CLUB,0}, { DiceValue.SPADE,0}, }; Dice d1 = new Dice(); for (int i = 0; i < 100; i++) { d1.roll(); countMap[d1.CurrentValue] += 1; } foreach (DiceValue dv in Enum.GetValues(typeof(DiceValue))) { if (countMap[dv] == 0) { Assert.Fail("random didn't generate all values"); } } }
public void TestCurrentValueRepr() { Dice d1 = new Dice(); string expected = Dice.stringRepr(d1.CurrentValue); string actual = d1.CurrentValueRepr; Assert.AreEqual(expected, actual); }
public Game(Dice die1, Dice die2, Dice die3) { dice = new List<Dice>(); values = new List<DiceValue>(); dice.Add(die1); dice.Add(die2); dice.Add(die3); foreach (var die in dice) { values.Add(die.CurrentValue); } }
public void LossRandomness_IsApproximatelyConvergentToExpectation() { const int numThrows = 100000; const double tolerance = 30 / 100d; // +- 30% const double above = 1 + tolerance; const double below = 1 - 1 / 100d; var dice = new Dice[] { new Dice(), new Dice(), new Dice() }; foreach (DiceValue selectedFace in Enum.GetValues(typeof(DiceValue))) { //var selectedFace = DiceValue.CLUB; Console.WriteLine("Processing:" + selectedFace); double lossCount = 0; for (var i = 0; i <= numThrows; i++) { var win = false; foreach (var die in dice) { die.roll(); if (die.CurrentValue == selectedFace) win = true; } if (!win) lossCount++; } double houseBias = 125d / 216d - 1 / 2d; // Test house bias for losing should be 8% away from median. lossCount.Should().BeInRange((58d/100d * numThrows) * below, (58d/100d * numThrows ) * above, "House bias for " + selectedFace + ", should be :" + (houseBias * 100) + " numThrows/2:" + numThrows/2 + " "); // win + loss ratio: var winRatio = (numThrows - lossCount) / numThrows; winRatio.Should().BeInRange(0.42 * below, 0.42 * above, "win + loss ratio should approximate 0.42"); } }
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)); }
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(); }
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(); }
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(); }
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 TestGameDiceRandomness() { var dice = new Dice(); var values = new List<DiceValue>(); for (var i = 0; i < 100; i++) { values.Add(dice.roll()); } Console.WriteLine(string.Join(",", values.Distinct())); values.Distinct().Count().Should().Be(6); }
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); }