Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

###dRoll: Beginner Dice Roller

dRoll: Beginner Dice Roller

###dRoll: Beginner Dice Roller

dRoll: Beginner Dice Roller

added 68 characters in body
Source Link

If the rolled value is equals to the die value "Ace". An other roll is add to the die result.
Exemple:

A Die 6 is also roll for every roll as Bonus die. resultB

If the rolled value is equals to the die value. An other roll is add to the die result.
Exemple:

If the rolled value is equals to the die value "Ace". An other roll is add to the die result.
Exemple:

A Die 6 is also roll for every roll as Bonus die. resultB

Source Link

Beginner Dice Roller

###dRoll: Beginner Dice Roller

Roll dice base on a input string.

1/. Input.

<number_of_dice> d <value> [/ <modifier>]
  • <number_of_dice>, int indicate the number of dice.
  • d, delimiter.
  • <value>, int dice value.
  • /, Optional delimiter for dice modifier.
  • <modifier>, int only with / delimiter

Each valid input can be separated by an ;.
Exemple of input:

"2d8" Valid
"1d8;1d8" Valid
"1d8/-2" Valid

Any not valid input is ignored.

2/. Roll rules:

If the rolled value is equals to the die value. An other roll is add to the die result.
Exemple:

  • "1d4" Roll n°1=4 n°2=3 TotalValue= 7
  • "1d4/-2" Roll n°1=4 n°2=3 TotalValue= 5

3/. Code

class dRoll
{
 private string _input;
 private List<dSet> _dSets = new List<dSet>();
 public dRoll(string input) {
 _input = input;
 ValidateInput();
 Run();
 }
 private void Run() { 
 foreach(var d in _dSets){
 d.Roll();
 d.PrintC();
 }
 }
 private void ValidateInput() {
 string[] argD = _input.Split(';');
 foreach (string arg in argD)
 {
 if (string.IsNullOrWhiteSpace(arg))
 {
 continue;
 }
 string[] dSplit = arg.ToLower().Split('d');
 if (dSplit.Length != 2)
 {
 continue;
 }
 int numT;
 if (!(int.TryParse(dSplit[0], out numT) && numT > 0))
 {
 continue;
 }
 int valT; int mod = 0;
 if (dSplit[1].Contains('/'))
 {
 string[] ModT = dSplit[1].Split('/');
 if (ModT.Length != 2)
 {
 continue;
 }
 if (!(int.TryParse(ModT[0], out valT) && valT > 1))
 {
 continue;
 }
 if (!(int.TryParse(ModT[1], out mod) && mod > 0))
 {
 continue;
 }
 }
 else
 {
 if (!(int.TryParse(dSplit[1], out valT) && valT > 1))
 {
 continue;
 }
 }
 for (int i = 1; i <= numT; i++)
 {
 if (mod == 0)
 {
 _dSets.Add(new dSet(1, valT));
 }
 else
 {
 _dSets.Add(new dSet(1, valT, mod)); 
 }
 }
 }
 }
}
class dSet 
{
 private static readonly Random random = new Random();
 private int _Dnum;
 private int _Dval;
 private int _Mval;
 dResult dResult = new dResult();
 public bool isMod
 {
 get
 {
 return _Mval!=0;
 }
 }
 public dSet() { 
 _Dnum=0;
 _Dval=0;
 _Mval=0;
 }
 public dSet(int Dnum, int Dval) {
 _Dnum = Dnum;
 _Dval = Dval;
 _Mval = 0;
 }
 public dSet(int Dnum, int Dval, int Mval) {
 _Dnum = Dnum;
 _Dval = Dval;
 _Mval = Mval;
 }
 public void Roll(){
 for (int i = 1; i <= _Dnum; i++)
 {
 bool NAce = true;
 bool BAce = true;
 for (int nNbr = 1; NAce; nNbr++)
 {
 int nRnd = random.Next(1, _Dval+1);
 dResult.resultN.Add(new Tuple<int, int>(nNbr, nRnd));
 if (nRnd != _Dval) 
 NAce = false;
 }
 for (int bNbr = 1; BAce; bNbr++)
 {
 int bRnd = random.Next(1, 6);
 dResult.resultB.Add(new Tuple<int, int>(bNbr, bRnd));
 if (bRnd != 6) 
 BAce = false;
 }
 }
 }
 public void PrintC(){
 Console.WriteLine("D{0}:{1}", _Dval, _Mval);
 Console.WriteLine("\tNormal : {0}",dResult.Ntot + _Mval);
 foreach (var n in dResult.resultN){
 Console.WriteLine("\t\t{0}: {1}", n.Item1, n.Item2);
 }
 Console.WriteLine("\tBonus : {0}", dResult.Btot + _Mval);
 foreach (var b in dResult.resultB) {
 Console.WriteLine("\t\t{0}: {1}", b.Item1, b.Item2);
 } 
 }
}
class dResult
{
 public List<Tuple<int, int>> resultN = new List<Tuple<int, int>>();
 public List<Tuple<int, int>> resultB = new List<Tuple<int, int>>();
 public int Ntot {
 get {
 return resultN.Sum(x => x.Item2);
 }
 }
 public int Btot {
 get
 {
 return resultB.Sum(x => x.Item2);
 }
 }
}
lang-cs

AltStyle によって変換されたページ (->オリジナル) /