Advertisement
Guest User

Dmitri

a guest
Oct 18th, 2009
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.46 KB | None | 0 0
  1. [Serializable]
  2. public class Mana
  3. {
  4.     // Fields
  5.     protected int black;
  6.     protected int blackwhite;
  7.     protected int blue;
  8.     protected int blueblack;
  9.     protected int bluewhite;
  10.     protected int colorless;
  11.     protected int green;
  12.     protected int greenblack;
  13.     protected int greenblue;
  14.     protected int greenred;
  15.     protected int greenwhite;
  16.     protected bool hasX;
  17.     protected int red;
  18.     protected int redblack;
  19.     protected int redblue;
  20.     protected int redwhite;
  21.     protected int white;
  22.  
  23.     // Methods
  24.     public static Mana Parse(string spec)
  25.     {
  26.         StringBuilder sb = new StringBuilder();
  27.         StringBuilder cb = new StringBuilder();
  28.         bool inHybrid = false;
  29.         Mana m = new Mana();
  30.         int $1 = 0;
  31.         int $2 = spec.Length;
  32.         if ($2 < 0)
  33.         {
  34.             throw new ArgumentOutOfRangeException("max");
  35.         }
  36.         while ($1 < $2)
  37.         {
  38.             int i = $1;
  39.             $1++;
  40.             if (inHybrid)
  41.             {
  42.                 cb.Append(spec[i]);
  43.             }
  44.             else
  45.             {
  46.                 if (char.IsDigit(spec[i]))
  47.                 {
  48.                     sb.Append(spec[i]);
  49.                     continue;
  50.                 }
  51.                 if (spec[i] == '{')
  52.                 {
  53.                     inHybrid = true;
  54.                     continue;
  55.                 }
  56.                 if (spec[i] == '}')
  57.                 {
  58.                     if (!inHybrid)
  59.                     {
  60.                         throw new ArgumentException("Closing } without opening");
  61.                     }
  62.                     inHybrid = false;
  63.                     string s = cb.ToString().ToUpper();
  64.                     if (s.Length != 2)
  65.                     {
  66.                         throw new ArgumentException("Only two-element hybrids supported");
  67.                     }
  68.                     if ((s == "GR") || (s == "RG"))
  69.                     {
  70.                         m.GreenRed++;
  71.                     }
  72.                     else if ((s == "GU") || (s == "UG"))
  73.                     {
  74.                         m.GreenBlue++;
  75.                     }
  76.                     else if ((s == "GB") || (s == "BG"))
  77.                     {
  78.                         m.GreenBlack++;
  79.                     }
  80.                     else if ((s == "GW") || (s == "WG"))
  81.                     {
  82.                         m.GreenWhite++;
  83.                     }
  84.                     else if ((s == "RU") || (s == "UR"))
  85.                     {
  86.                         m.RedBlue++;
  87.                     }
  88.                     else if ((s == "RB") || (s == "BR"))
  89.                     {
  90.                         m.RedBlack++;
  91.                     }
  92.                     else if ((s == "RW") || (s == "WR"))
  93.                     {
  94.                         m.RedWhite++;
  95.                     }
  96.                     else if ((s == "UB") || (s == "BU"))
  97.                     {
  98.                         m.BlueBlack++;
  99.                     }
  100.                     else if ((s == "UW") || (s == "WU"))
  101.                     {
  102.                         m.BlueWhite++;
  103.                     }
  104.                     else
  105.                     {
  106.                         if (!(s == "BW") && !(s == "WB"))
  107.                         {
  108.                             throw new ArgumentException(("Hybrid mana " + s) + " is not supported");
  109.                         }
  110.                         m.BlackWhite++;
  111.                     }
  112.                     continue;
  113.                 }
  114.                 if (char.ToUpper(spec[i]) == 'G')
  115.                 {
  116.                     m.Green++;
  117.                 }
  118.                 else
  119.                 {
  120.                     if (char.ToUpper(spec[i]) == 'R')
  121.                     {
  122.                         m.Red++;
  123.                         continue;
  124.                     }
  125.                     if (char.ToUpper(spec[i]) == 'U')
  126.                     {
  127.                         m.Blue++;
  128.                         continue;
  129.                     }
  130.                     if (char.ToUpper(spec[i]) == 'B')
  131.                     {
  132.                         m.Black++;
  133.                         continue;
  134.                     }
  135.                     if (char.ToUpper(spec[i]) == 'W')
  136.                     {
  137.                         m.White++;
  138.                         continue;
  139.                     }
  140.                     if (char.ToUpper(spec[i]) == 'X')
  141.                     {
  142.                         m.HasX = true;
  143.                     }
  144.                 }
  145.             }
  146.         }
  147.         return null;
  148.     }
  149.  
  150.     public override string ToString()
  151.     {
  152.         StringBuilder sb = new StringBuilder();
  153.         if (this.colorless > 0)
  154.         {
  155.             sb.Append(this.colorless);
  156.         }
  157.         if (this.green > 0)
  158.         {
  159.             sb.Append("G");
  160.         }
  161.         if (this.red > 0)
  162.         {
  163.             sb.Append("R");
  164.         }
  165.         if (this.blue > 0)
  166.         {
  167.             sb.Append("U");
  168.         }
  169.         if (this.black > 0)
  170.         {
  171.             sb.Append("B");
  172.         }
  173.         if (this.white > 0)
  174.         {
  175.             sb.Append("W");
  176.         }
  177.         if (this.greenred > 0)
  178.         {
  179.             sb.Append("{GR}");
  180.         }
  181.         if (this.greenblue > 0)
  182.         {
  183.             sb.Append("{GU}");
  184.         }
  185.         if (this.greenblack > 0)
  186.         {
  187.             sb.Append("{GB}");
  188.         }
  189.         if (this.greenwhite > 0)
  190.         {
  191.             sb.Append("{GW}");
  192.         }
  193.         if (this.redblue > 0)
  194.         {
  195.             sb.Append("{RU}");
  196.         }
  197.         if (this.redblack > 0)
  198.         {
  199.             sb.Append("{RB}");
  200.         }
  201.         if (this.redwhite > 0)
  202.         {
  203.             sb.Append("{RW}");
  204.         }
  205.         if (this.blueblack > 0)
  206.         {
  207.             sb.Append("{UB}");
  208.         }
  209.         if (this.bluewhite > 0)
  210.         {
  211.             sb.Append("{UW}");
  212.         }
  213.         if (this.blackwhite > 0)
  214.         {
  215.             sb.Append("{BW}");
  216.         }
  217.         if (this.hasX)
  218.         {
  219.             sb.Append("X");
  220.         }
  221.         return sb.ToString();
  222.     }
  223.  
  224.     // Properties
  225.     public int Black
  226.     {
  227.         get
  228.         {
  229.             return this.black;
  230.         }
  231.         set
  232.         {
  233.             this.black = value;
  234.         }
  235.     }
  236.  
  237.     public int BlackWhite
  238.     {
  239.         get
  240.         {
  241.             return this.blackwhite;
  242.         }
  243.         set
  244.         {
  245.             this.blackwhite = value;
  246.         }
  247.     }
  248.  
  249.     public int Blue
  250.     {
  251.         get
  252.         {
  253.             return this.blue;
  254.         }
  255.         set
  256.         {
  257.             this.blue = value;
  258.         }
  259.     }
  260.  
  261.     public int BlueBlack
  262.     {
  263.         get
  264.         {
  265.             return this.blueblack;
  266.         }
  267.         set
  268.         {
  269.             this.blueblack = value;
  270.         }
  271.     }
  272.  
  273.     public int BlueWhite
  274.     {
  275.         get
  276.         {
  277.             return this.bluewhite;
  278.         }
  279.         set
  280.         {
  281.             this.bluewhite = value;
  282.         }
  283.     }
  284.  
  285.     public int Colorless
  286.     {
  287.         get
  288.         {
  289.             return this.colorless;
  290.         }
  291.         set
  292.         {
  293.             this.colorless = value;
  294.         }
  295.     }
  296.  
  297.     public int ConvertedManaCost
  298.     {
  299.         get
  300.         {
  301.             return (((((this.Colorless + this.Green) + this.Red) + this.Blue) + this.Black) + this.White);
  302.         }
  303.     }
  304.  
  305.     public int Green
  306.     {
  307.         get
  308.         {
  309.             return this.green;
  310.         }
  311.         set
  312.         {
  313.             this.green = value;
  314.         }
  315.     }
  316.  
  317.     public int GreenBlack
  318.     {
  319.         get
  320.         {
  321.             return this.greenblack;
  322.         }
  323.         set
  324.         {
  325.             this.greenblack = value;
  326.         }
  327.     }
  328.  
  329.     public int GreenBlue
  330.     {
  331.         get
  332.         {
  333.             return this.greenblue;
  334.         }
  335.         set
  336.         {
  337.             this.greenblue = value;
  338.         }
  339.     }
  340.  
  341.     public int GreenRed
  342.     {
  343.         get
  344.         {
  345.             return this.greenred;
  346.         }
  347.         set
  348.         {
  349.             this.greenred = value;
  350.         }
  351.     }
  352.  
  353.     public int GreenWhite
  354.     {
  355.         get
  356.         {
  357.             return this.greenwhite;
  358.         }
  359.         set
  360.         {
  361.             this.greenwhite = value;
  362.         }
  363.     }
  364.  
  365.     public bool HasX
  366.     {
  367.         get
  368.         {
  369.             return this.hasX;
  370.         }
  371.         set
  372.         {
  373.             this.hasX = value;
  374.         }
  375.     }
  376.  
  377.     public bool IsBlack
  378.     {
  379.         get
  380.         {
  381.             return ((this.black <= 0) ? ((this.greenblack <= 0) ? ((this.redblack <= 0) ? ((this.blueblack <= 0) ? (this.blackwhite > 0) : true) : true) : true) : true);
  382.         }
  383.     }
  384.  
  385.     public bool IsBlue
  386.     {
  387.         get
  388.         {
  389.             return ((this.blue <= 0) ? ((this.greenblue <= 0) ? ((this.redblue <= 0) ? ((this.blueblack <= 0) ? (this.bluewhite > 0) : true) : true) : true) : true);
  390.         }
  391.     }
  392.  
  393.     public bool IsGreen
  394.     {
  395.         get
  396.         {
  397.             return ((this.green <= 0) ? ((this.greenred <= 0) ? ((this.greenblue <= 0) ? ((this.greenblack <= 0) ? (this.greenwhite > 0) : true) : true) : true) : true);
  398.         }
  399.     }
  400.  
  401.     public bool IsRed
  402.     {
  403.         get
  404.         {
  405.             return ((this.red <= 0) ? ((this.greenred <= 0) ? ((this.redblue <= 0) ? ((this.redblack <= 0) ? (this.redwhite > 0) : true) : true) : true) : true);
  406.         }
  407.     }
  408.  
  409.     public bool IsWhite
  410.     {
  411.         get
  412.         {
  413.             return ((this.white <= 0) ? ((this.greenwhite <= 0) ? ((this.redwhite <= 0) ? ((this.bluewhite <= 0) ? (this.blackwhite > 0) : true) : true) : true) : true);
  414.         }
  415.     }
  416.  
  417.     public int Red
  418.     {
  419.         get
  420.         {
  421.             return this.red;
  422.         }
  423.         set
  424.         {
  425.             this.red = value;
  426.         }
  427.     }
  428.  
  429.     public int RedBlack
  430.     {
  431.         get
  432.         {
  433.             return this.redblack;
  434.         }
  435.         set
  436.         {
  437.             this.redblack = value;
  438.         }
  439.     }
  440.  
  441.     public int RedBlue
  442.     {
  443.         get
  444.         {
  445.             return this.redblue;
  446.         }
  447.         set
  448.         {
  449.             this.redblue = value;
  450.         }
  451.     }
  452.  
  453.     public int RedWhite
  454.     {
  455.         get
  456.         {
  457.             return this.redwhite;
  458.         }
  459.         set
  460.         {
  461.             this.redwhite = value;
  462.         }
  463.     }
  464.  
  465.     public int White
  466.     {
  467.         get
  468.         {
  469.             return this.white;
  470.         }
  471.         set
  472.         {
  473.             this.white = value;
  474.         }
  475.     }
  476. }
  477.  
  478.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement