Posted By

kyrathaba on 01/24/11


Tagged

dice rolling craps


Versions (?)

Dice


 / Published in: C#
 

  1. namespace Kyrathasoft.Games.Dice
  2. {
  3. using System;
  4.  
  5. class clsDice
  6. {
  7. //member variables
  8.  
  9.  
  10. private int die_type = 0; //how many sides?
  11.  
  12. private int num_dice = 0; //roll how many of these x-sided dice?
  13.  
  14. private int plus_amt = 0; //amount to add to roll result
  15.  
  16. private string operation = "add";
  17.  
  18. private readonly int result = 0; //result of die roll
  19.  
  20. private bool parse_error = true;
  21.  
  22. private readonly int d_pos = 0;
  23.  
  24. private readonly int plus_pos = 0;
  25.  
  26. private readonly int minus_pos = 0;
  27.  
  28. private readonly string dieroll = "";
  29.  
  30. static Random myRand = new Random(); //creates instance of Random object
  31.  
  32. //============= constructor code ================================
  33.  
  34.  
  35. public clsDice(string passed_dieroll)
  36. {
  37.  
  38. string s = "";
  39.  
  40. dieroll = passed_dieroll;
  41.  
  42. d_pos = dieroll.IndexOf("d");
  43.  
  44. plus_pos = dieroll.IndexOf("+");
  45. if (plus_pos == -1)
  46. {
  47. minus_pos = dieroll.IndexOf("-");
  48. if (minus_pos != -1)
  49. {
  50. //Console.WriteLine("Minus sign detected!!!");
  51. //Console.ReadLine();
  52. operation = "subtract";
  53. plus_pos = minus_pos;
  54. }
  55. }
  56. else
  57. {
  58. operation = "add";
  59. }
  60.  
  61. //assign value to member die_type
  62.  
  63. try
  64. {
  65. die_type = Convert.ToInt32(ParseDieType());
  66. parse_error = false;
  67. }
  68. catch
  69. {
  70. parse_error = true;
  71. s = "Failure to extract die_type from 'dieroll' string ";
  72.  
  73. Console.WriteLine(s);
  74.  
  75. }//end try-catch block
  76.  
  77.  
  78.  
  79. //assign value to member num_dice
  80.  
  81. try
  82. {
  83. num_dice = Convert.ToInt32(ParseNumDice());
  84. parse_error = false;
  85. }
  86. catch
  87. {
  88. parse_error = true;
  89. s = "Failure to extract num_dice from 'dieroll' string ";
  90.  
  91. Console.WriteLine(s);
  92.  
  93. }//end try-catch block
  94.  
  95.  
  96.  
  97. //assign value to member plus_amt
  98.  
  99. try
  100. {
  101.  
  102. plus_amt = Convert.ToInt32(ParsePlusAmt());
  103. if (operation == "subtract")
  104. {
  105. plus_amt = -(plus_amt);
  106. }
  107. parse_error = false;
  108. }
  109. catch
  110. {
  111. parse_error = true;
  112. s = "Failure to extract plus_amt from 'dieroll' string ";
  113. Console.WriteLine(s);
  114.  
  115. }//end try-catch block
  116.  
  117. if (parse_error == false)
  118. {
  119. int single_roll = 0;
  120. int local_result = 0;
  121.  
  122.  
  123. for (int i = 1; i <= num_dice; i++)
  124. {
  125. single_roll = (int)(myRand.NextDouble() * die_type) + 1;
  126. Console.Write("Result before modifier: " + single_roll.ToString() + "\n");
  127.  
  128.  
  129. local_result += single_roll;
  130.  
  131. }//end for loop
  132. local_result += plus_amt;
  133. result = local_result;
  134. }
  135. else
  136. {
  137. result = 0;
  138. }//endif
  139.  
  140.  
  141. }//end initial constructor
  142.  
  143.  
  144.  
  145. public clsDice(int type_of_die)
  146. {
  147.  
  148. result = (int)(myRand.NextDouble() * type_of_die) + 1;
  149. Console.WriteLine("1d" + type_of_die.ToString() + " = " + result.ToString());
  150.  
  151. } //end overloaded constructor
  152.  
  153.  
  154.  
  155.  
  156. // ============== end constructor code ==============================
  157.  
  158.  
  159.  
  160.  
  161. // ======================= Class Methods =============================
  162.  
  163.  
  164.  
  165.  
  166. //------------------- Method ParseNumDice() -------------------------
  167.  
  168.  
  169. private string ParseNumDice()
  170. {
  171.  
  172.  
  173. string mySubStr = "";
  174.  
  175. mySubStr = dieroll.Substring(0, d_pos);
  176.  
  177. Console.WriteLine("Number of dice being rolled: " + mySubStr);
  178.  
  179. return mySubStr;
  180.  
  181.  
  182. } //end method ParseNumDice()
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189. //----------------- Method ParseDieType() ---------------------------
  190.  
  191.  
  192. private string ParseDieType()
  193. {
  194.  
  195. //I used the following commented-out lines to help me figure
  196. //out the formula to use...
  197.  
  198. //example die roll: "800d60+3"
  199. //d_pos = 3
  200. //plus_pos = 6
  201.  
  202. string mySubStr = "";
  203.  
  204. mySubStr = dieroll.Substring(d_pos + 1, (plus_pos - d_pos) - 1);
  205.  
  206. Console.WriteLine("Type of die being rolled: " + mySubStr + "-sider");
  207.  
  208. return mySubStr;
  209.  
  210. } //end method ParseDieType()
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217. //------------------ Method ParsePlusAmt() --------------------------
  218.  
  219.  
  220. private string ParsePlusAmt()
  221. {
  222.  
  223. //I used the following commented-out lines to help me figure
  224. //out the formula to use...
  225.  
  226. //example die roll: "800d6000+125"
  227. //plus_pos = 8
  228. //after_plus = 12 - 8 = 4
  229. //mySubStr = dieroll.Substring(8+1, 3)
  230.  
  231.  
  232. string mySubStr = "";
  233.  
  234. int after_plus = dieroll.Length - plus_pos;
  235.  
  236.  
  237. mySubStr = dieroll.Substring(plus_pos + 1, after_plus - 1);
  238.  
  239. Console.Write("Modifier to die roll: ");
  240. if (operation == "subtract")
  241. {
  242. Console.Write("-");
  243. }
  244. else
  245. {
  246. Console.Write("+");
  247. }
  248. Console.WriteLine(mySubStr);
  249. return mySubStr;
  250.  
  251. } //end method ParsePlusAmt()
  252.  
  253. // =================== end Class Methods =============================
  254.  
  255.  
  256.  
  257.  
  258. //=============== Dice class properties ===========================
  259.  
  260.  
  261. public int Die_type
  262. {
  263. get
  264. {
  265. return die_type;
  266. }
  267. set
  268. {
  269. die_type = value;
  270. }
  271. }//end property Die_type
  272.  
  273.  
  274. public int Num_dice
  275. {
  276. get
  277. {
  278. return num_dice;
  279. }
  280. set
  281. {
  282. num_dice = value;
  283. }
  284. }//end property Num_dice
  285.  
  286.  
  287. public int Plus_amt
  288. {
  289. get
  290. {
  291. return plus_amt;
  292. }
  293. set
  294. {
  295. plus_amt = value;
  296. }
  297. }//end property Plus_amt
  298.  
  299.  
  300. public int Result
  301. {
  302. get
  303. {
  304. return result;
  305. }
  306. }//end property Result
  307.  
  308.  
  309. public string Dieroll
  310. {
  311. get
  312. {
  313. return dieroll;
  314. }
  315. }
  316.  
  317.  
  318. //=================== end Dice class properties =====================
  319.  
  320. }
  321. }

Report this snippet  

You need to login to post a comment.