Guest User

Untitled

a guest
Aug 11th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Security.Cryptography;
  7.  
  8. namespace CrapsSim
  9. {
  10.     class Program
  11.     {
  12.         private static RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
  13.         const int betsResolved = 1000000;
  14.         const int numPlayers = 100;
  15.  
  16.         static void Main(string[] args)
  17.         {
  18.             List<PlayerResult> results = new List<PlayerResult>();
  19.  
  20.             for (int i = 1; i <= numPlayers; i++)
  21.             {
  22.                 results.Add(CrapsGame(betsResolved));
  23.             }
  24.  
  25.             DisplayData(results);
  26.  
  27.             rngCsp.Dispose();
  28.             Console.ReadLine();
  29.         }
  30.  
  31.         public static void DisplayData(List<PlayerResult> results)
  32.         {
  33.             decimal avgPlayerUnits = 0;
  34.             decimal avgComeOutWins = 0;
  35.             decimal avgCrapsLose = 0;
  36.             decimal avgPointWins = 0;
  37.             decimal avgSevenOuts = 0;
  38.             decimal avgTotalRolls = 0;
  39.             decimal mostUnits = results[0].playerUnits;
  40.             decimal leastUnits = results[0].playerUnits;
  41.             int avgUnitsWagered = 0;
  42.             foreach (PlayerResult p in results)
  43.             {
  44.                 if (p.playerUnits > mostUnits)
  45.                     mostUnits = p.playerUnits;
  46.  
  47.                 if (p.playerUnits < leastUnits)
  48.                     leastUnits = p.playerUnits;
  49.  
  50.                 avgPlayerUnits += p.playerUnits;
  51.                 avgComeOutWins += p.comeOutWins;
  52.                 avgCrapsLose += p.crapsLose;
  53.                 avgPointWins += p.pointWins;
  54.                 avgSevenOuts += p.sevenOuts;
  55.                 avgTotalRolls += p.totalRolls;
  56.                 avgUnitsWagered += p.unitsWagered;
  57.             }
  58.             avgPlayerUnits = (avgPlayerUnits / numPlayers);
  59.             avgComeOutWins = (avgComeOutWins / (betsResolved*numPlayers)) * 100;
  60.             avgCrapsLose = (avgCrapsLose / (betsResolved * numPlayers)) * 100;
  61.             avgPointWins = (avgPointWins / (betsResolved * numPlayers)) * 100;
  62.             avgSevenOuts = (avgSevenOuts / (betsResolved * numPlayers)) * 100;
  63.             avgTotalRolls = (avgTotalRolls / numPlayers);
  64.             avgUnitsWagered = (avgUnitsWagered / numPlayers);
  65.  
  66.             Console.WriteLine("------------------------------------------------------------------------------");
  67.             Console.WriteLine("Average Results Among {0} Players Over {1} Bets Resolved", numPlayers, betsResolved);
  68.             Console.WriteLine("------------------------------------------------------------------------------");
  69.             Console.WriteLine("Total Rolls: {0}", avgTotalRolls);
  70.             Console.WriteLine("Units Wagered: {0}", avgUnitsWagered);
  71.             Console.WriteLine("Units: {0}", avgPlayerUnits);
  72.             Console.WriteLine("7/11 Win: {0}%", avgComeOutWins);
  73.             Console.WriteLine("Craps Lose: {0}%", avgCrapsLose);
  74.             Console.WriteLine("Point Win: {0}%", avgPointWins);
  75.             Console.WriteLine("7 Out: {0}%", avgSevenOuts);
  76.             Console.WriteLine("------------------------------------------------------------------------------");
  77.             Console.WriteLine("Most Units: {0}\nLeast Units: {1}", mostUnits, leastUnits);
  78.            
  79.            
  80.  
  81.         }
  82.         public static PlayerResult CrapsGame(int resolutions)
  83.         {
  84.             rngCsp = new RNGCryptoServiceProvider();
  85.             PlayerResult outcome = new PlayerResult();
  86.  
  87.             outcome.playerUnits = 0;
  88.             outcome.comeOutWins = 0;
  89.             outcome.crapsLose = 0;
  90.             outcome.pointWins = 0;
  91.             outcome.sevenOuts = 0;
  92.             outcome.totalRolls = 0;
  93.             outcome.unitsWagered = 0;
  94.  
  95.             for (int x = 0; x < resolutions; x++)
  96.             {
  97.                 int point = 0;
  98.                 int roll = RollTwoDice();
  99.                 outcome.totalRolls++;
  100.                 outcome.unitsWagered++;
  101.                 //Player wins come out roll
  102.                 if (roll == 7 || roll == 11)
  103.                 {
  104.                     outcome.playerUnits += 1;
  105.                     outcome.comeOutWins++;
  106.                 }
  107.                 //Player rolls craps on come out roll
  108.                 else if (roll == 2 || roll == 3 || roll == 12)
  109.                 {
  110.                     outcome.playerUnits -= 1;
  111.                     outcome.crapsLose++;
  112.                 }
  113.                 //Player establishes a point
  114.                 else
  115.                 {
  116.                     outcome.unitsWagered++;
  117.                     point = roll;
  118.                     //roll until bet is resolved
  119.                     do
  120.                     {
  121.                         roll = RollTwoDice();
  122.                         outcome.totalRolls++;
  123.                         //Player seven out
  124.                         if (roll == 7)
  125.                         {
  126.                             outcome.playerUnits -= 2; //player loses passline and odds bet
  127.                             outcome.sevenOuts++;
  128.                             point = 0;
  129.                         }
  130.                         //Player rolls point
  131.                         else if (roll == point)
  132.                         {
  133.                             outcome.playerUnits += 1; //pay even money on the passline
  134.                             outcome.playerUnits += GetOddsPayout(point); //pay the odds
  135.                             outcome.pointWins++;
  136.                             point = 0;
  137.                         }
  138.  
  139.                     } while (point != 0);
  140.  
  141.                 }
  142.             }
  143.             return outcome;
  144.         }
  145.  
  146.  
  147.         public static int RollTwoDice()
  148.         {
  149.             byte dice1 = RollDice(6);
  150.             byte dice2 = RollDice(6);
  151.             return dice1 + dice2;
  152.         }
  153.  
  154.         public static decimal GetOddsPayout(int point)
  155.         {
  156.             decimal pay;
  157.             switch (point)
  158.             {
  159.                 case 4:
  160.                     pay = 2;
  161.                     break;
  162.                 case 10:
  163.                     pay = 2;
  164.                     break;
  165.                 case 5:
  166.                     pay = 1.5m;
  167.                     break;
  168.                 case 9:
  169.                     pay = 1.5m;
  170.                     break;
  171.                 case 6:
  172.                     pay = 1.2m;
  173.                     break;
  174.                 case 8:
  175.                     pay = 1.2m;
  176.                     break;
  177.                 default:
  178.                     pay = 0;
  179.                     break;
  180.             }
  181.             return pay;
  182.         }
  183.  
  184.         // This method simulates a roll of the dice. The input parameter is the
  185.         // number of sides of the dice.
  186.         public static byte RollDice(byte numberSides)
  187.         {
  188.             if (numberSides <= 0)
  189.                 throw new ArgumentOutOfRangeException("numberSides");
  190.  
  191.             // Create a byte array to hold the random value.
  192.             byte[] randomNumber = new byte[1];
  193.             do
  194.             {
  195.                 // Fill the array with a random value.
  196.                 rngCsp.GetBytes(randomNumber);
  197.             }
  198.             while (!IsFairRoll(randomNumber[0], numberSides));
  199.             // Return the random number mod the number
  200.             // of sides.  The possible values are zero-
  201.             // based, so we add one.
  202.             return (byte)((randomNumber[0] % numberSides) + 1);
  203.         }
  204.  
  205.         private static bool IsFairRoll(byte roll, byte numSides)
  206.         {
  207.             // There are MaxValue / numSides full sets of numbers that can come up
  208.             // in a single byte.  For instance, if we have a 6 sided die, there are
  209.             // 42 full sets of 1-6 that come up.  The 43rd set is incomplete.
  210.             int fullSetsOfValues = Byte.MaxValue / numSides;
  211.  
  212.             // If the roll is within this range of fair values, then we let it continue.
  213.             // In the 6 sided die case, a roll between 0 and 251 is allowed.  (We use
  214.             // < rather than <= since the = portion allows through an extra 0 value).
  215.             // 252 through 255 would provide an extra 0, 1, 2, 3 so they are not fair
  216.             // to use.
  217.             return roll < numSides * fullSetsOfValues;
  218.         }
  219.  
  220.         public struct PlayerResult
  221.         {
  222.             public decimal playerUnits;
  223.             public decimal comeOutWins;
  224.             public decimal crapsLose;
  225.             public decimal pointWins;
  226.             public decimal sevenOuts;
  227.             public int totalRolls;
  228.             public int unitsWagered;
  229.         }
  230.     }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment