SHARE
TWEET

Untitled

a guest Feb 7th, 2019 115 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Dlang alt to the dice roller:
  2. import std.stdio;
  3. import std.random;
  4. import std.string;
  5. import std.conv;
  6. import core.exception;
  7. import std.algorithm.iteration : sum;
  8. import std.algorithm.comparison : equal;
  9. import std.algorithm.searching : canFind, startsWith;
  10.  
  11. void main(string[] args)
  12. {
  13.     string dice = "";
  14.     int modifier = 0;
  15.     try {
  16.         dice = args[1];
  17.     }
  18.     catch (RangeError){
  19.         writeln("Missing dice\nUsage: dice #d# [modifier]");
  20.         return;
  21.     }
  22.     try {
  23.         args[2] = replace(args[2], "+", "");
  24.         modifier = to!int(args[2]);
  25.     }
  26.     catch (RangeError){    } // Continue on range error
  27.     catch (ConvException) { } // Continue on conversion error
  28.  
  29.     if (!canFind(dice, "d")) {
  30.         writeln("Improper format\nUsage: dice #d# [modifier]");
  31.         return;
  32.     }
  33.     auto results = roll(dice);
  34.     string outp = "Rolled %s\nResults:\n";
  35.     if (modifier != 0) {
  36.         outp ~= "  Modifier: %s\n";
  37.     }
  38.     outp ~= "       Sum: %s\n";
  39.     if (modifier != 0) {
  40.         outp ~= "    No Mod: %s\n";
  41.     }
  42.     outp ~= "Individual: %s";
  43.  
  44.     int total = sum(results);
  45.     int modded = total + modifier;
  46.  
  47.     if (modifier != 0) {
  48.         writefln(outp, dice, modifier, total, modded, results);
  49.     }
  50.     else {
  51.         writefln(outp, dice, total, results);
  52.     }
  53.  
  54. }
  55.  
  56. int[] roll(string dice)
  57. {
  58.     int number;
  59.     if (startsWith(dice, "d")) {
  60.         number = 1;
  61.     }
  62.     else {
  63.         number = to!int(dice.split("d")[0]);
  64.     }
  65.     int die = to!int(dice.split("d")[1]);
  66.     int[] results = new int[number];
  67.     for (int i = 0; i < number; i++) {
  68.         results[i] = uniform(1, die+1);
  69.     }
  70.     return results;
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top