Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Security.Cryptography;
- namespace CrapsSim
- {
- class Program
- {
- private static RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
- const int betsResolved = 1000000;
- const int numPlayers = 100;
- static void Main(string[] args)
- {
- List<PlayerResult> results = new List<PlayerResult>();
- for (int i = 1; i <= numPlayers; i++)
- {
- results.Add(CrapsGame(betsResolved));
- }
- DisplayData(results);
- rngCsp.Dispose();
- Console.ReadLine();
- }
- public static void DisplayData(List<PlayerResult> results)
- {
- decimal avgPlayerUnits = 0;
- decimal avgComeOutWins = 0;
- decimal avgCrapsLose = 0;
- decimal avgPointWins = 0;
- decimal avgSevenOuts = 0;
- decimal avgTotalRolls = 0;
- decimal mostUnits = results[0].playerUnits;
- decimal leastUnits = results[0].playerUnits;
- foreach (PlayerResult p in results)
- {
- if (p.playerUnits > mostUnits)
- mostUnits = p.playerUnits;
- if (p.playerUnits < leastUnits)
- leastUnits = p.playerUnits;
- avgPlayerUnits += p.playerUnits;
- avgComeOutWins += p.comeOutWins;
- avgCrapsLose += p.crapsLose;
- avgPointWins += p.pointWins;
- avgSevenOuts += p.sevenOuts;
- avgTotalRolls += p.totalRolls;
- }
- avgPlayerUnits = (avgPlayerUnits / numPlayers);
- avgComeOutWins = (avgComeOutWins / (betsResolved*numPlayers)) * 100;
- avgCrapsLose = (avgCrapsLose / (betsResolved * numPlayers)) * 100;
- avgPointWins = (avgPointWins / (betsResolved * numPlayers)) * 100;
- avgSevenOuts = (avgSevenOuts / (betsResolved * numPlayers)) * 100;
- avgTotalRolls = (avgTotalRolls / numPlayers);
- Console.WriteLine("------------------------------------------------------------------------------");
- Console.WriteLine("Average Results Among {0} Players Over {1} Bets Resolved", numPlayers, betsResolved);
- Console.WriteLine("------------------------------------------------------------------------------");
- Console.WriteLine("Total Rolls: {0}", avgTotalRolls);
- Console.WriteLine("Units: {0}", avgPlayerUnits);
- Console.WriteLine("7/11 Win: {0}%", avgComeOutWins);
- Console.WriteLine("Craps Lose: {0}%", avgCrapsLose);
- Console.WriteLine("Point Win: {0}%", avgPointWins);
- Console.WriteLine("7 Out: {0}%", avgSevenOuts);
- Console.WriteLine("------------------------------------------------------------------------------");
- Console.WriteLine("Most Units: {0}\nLeast Units: {1}", mostUnits, leastUnits);
- }
- public static PlayerResult CrapsGame(int resolutions)
- {
- PlayerResult outcome = new PlayerResult();
- outcome.playerUnits = 0;
- outcome.comeOutWins = 0;
- outcome.crapsLose = 0;
- outcome.pointWins = 0;
- outcome.sevenOuts = 0;
- outcome.totalRolls = 0;
- for (int x = 0; x < resolutions; x++)
- {
- int point = 0;
- int roll = RollTwoDice();
- outcome.totalRolls++;
- //Player wins come out roll
- if (roll == 7 || roll == 11)
- {
- outcome.playerUnits += 1;
- outcome.comeOutWins++;
- }
- //Player rolls craps on come out roll
- else if (roll == 2 || roll == 3 || roll == 12)
- {
- outcome.playerUnits -= 1;
- outcome.crapsLose++;
- }
- //Player establishes a point
- else
- {
- point = roll;
- //roll until bet is resolved
- do
- {
- roll = RollTwoDice();
- outcome.totalRolls++;
- //Player seven out
- if (roll == 7)
- {
- outcome.playerUnits -= 2; //player loses passline and odds bet
- outcome.sevenOuts++;
- point = 0;
- }
- //Player rolls point
- else if (roll == point)
- {
- outcome.playerUnits += 1; //pay even money on the passline
- outcome.playerUnits += GetOddsPayout(point); //pay the odds
- outcome.pointWins++;
- point = 0;
- }
- } while (point != 0);
- }
- }
- return outcome;
- }
- public static int RollTwoDice()
- {
- byte dice1 = RollDice(6);
- byte dice2 = RollDice(6);
- return dice1 + dice2;
- }
- public static decimal GetOddsPayout(int point)
- {
- decimal pay;
- switch (point)
- {
- case 4:
- pay = 2 / 1;
- break;
- case 10:
- pay = 2 / 1;
- break;
- case 5:
- pay = 3 / 2;
- break;
- case 9:
- pay = 3 / 2;
- break;
- case 6:
- pay = 6 / 5;
- break;
- case 8:
- pay = 6 / 5;
- break;
- default:
- pay = 0;
- break;
- }
- return pay;
- }
- // This method simulates a roll of the dice. The input parameter is the
- // number of sides of the dice.
- public static byte RollDice(byte numberSides)
- {
- if (numberSides <= 0)
- throw new ArgumentOutOfRangeException("numberSides");
- // Create a byte array to hold the random value.
- byte[] randomNumber = new byte[1];
- do
- {
- // Fill the array with a random value.
- rngCsp.GetBytes(randomNumber);
- }
- while (!IsFairRoll(randomNumber[0], numberSides));
- // Return the random number mod the number
- // of sides. The possible values are zero-
- // based, so we add one.
- return (byte)((randomNumber[0] % numberSides) + 1);
- }
- private static bool IsFairRoll(byte roll, byte numSides)
- {
- // There are MaxValue / numSides full sets of numbers that can come up
- // in a single byte. For instance, if we have a 6 sided die, there are
- // 42 full sets of 1-6 that come up. The 43rd set is incomplete.
- int fullSetsOfValues = Byte.MaxValue / numSides;
- // If the roll is within this range of fair values, then we let it continue.
- // In the 6 sided die case, a roll between 0 and 251 is allowed. (We use
- // < rather than <= since the = portion allows through an extra 0 value).
- // 252 through 255 would provide an extra 0, 1, 2, 3 so they are not fair
- // to use.
- return roll < numSides * fullSetsOfValues;
- }
- public struct PlayerResult
- {
- public decimal playerUnits;
- public decimal comeOutWins;
- public decimal crapsLose;
- public decimal pointWins;
- public decimal sevenOuts;
- public int totalRolls;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement