Advertisement
Guest User

Untitled

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