// dice.cs
// copyright (c) 2001 Mark Ayers n2sami@attbi.com
// compile with: csc /out:dice.exe /target:exe /debug- /optimize+ dice.cs

using System;

namespace TheDiscoveryProject {

  public class MainApp {

    public static void Usage() {

      string msg = "Dice Rolling Simulation\n\n";
      msg += "Usage: dice [-t <type of die>] [-n <number of dice>]\n\n";
      msg += "Examples:\n";
      msg += "dice \t\t\t==> result of 2D6\n";
      msg += "dice -t 20 \t\t==> result of 2D20\n";
      msg += "dice -n 3 \t\t==> result of 3D6\n";
      msg += "dice -t 10 -n 1 \t==> result of 1D10\n";
      msg += "dice -n 1 -t 10 \t==> result of 1D10\n\n";
      msg += "will print \"nDt ==> x\" to the screen\n";
      msg += "and return x for use by %errorlevel%";

      Console.WriteLine(msg);

    } // end of  public static void Usage()

    public static int Main(string[] args) {

      //setup defaults
      int typeDie = 6;
      int numDice = 2;

      if (args.Length > 0) { // if we have args

        for (int i = 0; i < args.Length; i++) { // process them

          if (args[i].StartsWith("-")) { // it is a switch

            switch (args[i]) {
              case "-t": // typeDie
                if (args.Length < i + 2) { 
		  Usage(); return -1; 
		}
                if (args[i + 1].StartsWith("-")) { 
		  Usage(); return -1; 
		}
                typeDie = Convert.ToInt32(args[i + 1]); 
		i++; 
		break;
              case "-n": // numDice
                if (args.Length < i + 2) { 
		  Usage(); return -1; 
		}
                if (args[i + 1].StartsWith("-")) { 
		  Usage(); return -1; 
		}
                numDice = Convert.ToInt32(args[i + 1]); 
		i++; 
		break;
              default: 
		Usage(); 
		return -1;
            } // it wasn't a switch

          } else { Usage(); return -1; }

        } // we are done processing arguments

      } // we can go on using defaults

      Random rnd = new Random((int)DateTime.Now.Ticks);
      int result = 0;

      //roll it
      for (int i = 1; i <= numDice; i++) {
	result += rnd.Next(1, typeDie + 1);
      }

      //display then return it
      Console.WriteLine("{0}D{1} ==> {2}", numDice, typeDie, result);
      return result;

    } // end of  public static int Main(string[] args)

  } // end of  public class MainApp

} // end of  namespace TheDiscoveryProject