Program.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace CricketDuck_Standard_CLI
  5. {
  6. class MainClass
  7. {
  8. public static string traceback = "";
  9. public static int Main (string[] args)
  10. {
  11. try
  12. {
  13. Console.Write("What is the G50 value? (Enter 245 if not sure.) ");
  14. int G50 = int.Parse(Console.ReadLine());
  15. Console.Write ("Number of overs in the first innings (before interruptions): ");
  16. string N = ReadOvers();
  17. if (!IsValidOverCount (N))
  18. throw new Exception ("Number of overs is invalid or too high.");
  19. double R1 = GetResources (N, 0);
  20. Console.Write("What was the first innings score? ");
  21. int S = int.Parse(Console.ReadLine());
  22. Console.Write ("Was the first innings interrupted? (y/N) ");
  23. if (Console.ReadLine ().ToLower () == "y") {
  24. Console.Write("When did this interruption occur? ");
  25. string interruption_time = ReadOvers();
  26. Console.Write("How many wickets had been lost at the time? ");
  27. int wickets_lost = int.Parse(Console.ReadLine());
  28. Console.Write("What was the new over count? ");
  29. string new_over_count = ReadOvers();
  30. double resources_lost = GetResources(SubtractOvers(N, interruption_time), wickets_lost) - GetResources(SubtractOvers(new_over_count, interruption_time), wickets_lost);
  31. R1 -= resources_lost;
  32. }
  33. Console.Write("How many overs did the team batting second have (prior to interruptions)? ");
  34. string N2 = ReadOvers();
  35. Console.WriteLine("Pick an option:");
  36. Console.WriteLine("[1] Calculate Par score.");
  37. Console.WriteLine("[2] Calculate second innings target.");
  38. switch(Console.ReadLine())
  39. {
  40. case "1":
  41. {
  42. Console.Write("When was the second innings interrupted? ");
  43. string second_interruption = ReadOvers();
  44. if(SubtractOvers(N2, second_interruption).Contains("-"))
  45. {
  46. throw new Exception("Revised over count cannot be above original over count.");
  47. }
  48. Console.Write("How many wickets had the team batting second lost at the time? ");
  49. int wickets_lost = int.Parse(Console.ReadLine());
  50. double R2 = GetResources(N2, 0) - GetResources(SubtractOvers(N2, second_interruption), wickets_lost);
  51. int newTarget = CalculateNewTarget(S, R1, R2, G50);
  52. Console.WriteLine("Team 2 needed " + newTarget + " to win or " + (newTarget - 1) + " to tie from " + second_interruption + " overs.");
  53. return 0;
  54. }
  55. case "2":
  56. {
  57. double R2 = GetResources(N2, 0);
  58. Console.Write("Was the second innings interrupted? (y/N) ");
  59. if(Console.ReadLine().ToLower() == "y")
  60. {
  61. Console.Write("How many overs had passed at the time of the interruption? ");
  62. string overs_remaining1 = SubtractOvers(N2, ReadOvers());
  63. Console.Write("How many wickets had been lost at the time? ");
  64. int wickets_lost = int.Parse(Console.ReadLine());
  65. Console.Write("What was the new over count after the interruption? ");
  66. string overs_remaining2 = ReadOvers();
  67. if(SubtractOvers(N2, overs_remaining2).Contains("-"))
  68. {
  69. throw new Exception("Revised over count cannot be above original over count.");
  70. }
  71. overs_remaining2 = SubtractOvers(overs_remaining2, SubtractOvers(N2, overs_remaining1));
  72. R2 -= GetResources(overs_remaining1, wickets_lost) - GetResources(overs_remaining2, wickets_lost);
  73. }
  74. int newTarget = CalculateNewTarget(S, R1, R2, G50);
  75. Console.WriteLine("Team 2 need " + newTarget + " to win or " + (newTarget - 1) + " to tie.");
  76. return 0;
  77. }
  78. default:
  79. {
  80. throw new Exception("Invalid option.");
  81. }
  82. }
  83. }
  84. catch(Exception e) {
  85. writeTraceback("At Main(string[]):");
  86. Console.Error.WriteLine ("Traceback (most recent call last):");
  87. Console.Error.Write (traceback.ToString());
  88. Console.Error.WriteLine ("Error: " + e.GetType () + " occurred.");
  89. Console.Error.WriteLine ("Description: " + e.Message);
  90. return -1;
  91. }
  92. }
  93. protected static double GetResources(string overs_remaining, int wickets_lost)
  94. {
  95. try
  96. {
  97. // Gets the resource percentage for a given number of overs remaining and wickets lost.
  98. using (Stream database = File.OpenRead(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly ().Location) + "/table_of_resources.csv"))
  99. using (StreamReader databaseReader = new StreamReader(database)) {
  100. return Parser.parseDouble (Grep (overs_remaining + "," + wickets_lost + ",", databaseReader).Replace (overs_remaining + "," + wickets_lost + ",", ""));
  101. }
  102. }
  103. catch(Exception e) {
  104. writeTraceback ("At GetResources(string, int):");
  105. throw e;
  106. }
  107. }
  108. private static string Grep(string s, StreamReader reader)
  109. {
  110. try
  111. {
  112. // Note: This is not suitable as a C# version of the Unix 'grep'. It is more of an implementation of 'grep -c 1'.
  113. while (!reader.EndOfStream) {
  114. string s2 = reader.ReadLine ();
  115. if (s2.StartsWith(s))
  116. return s2;
  117. }
  118. throw new EndOfStreamException ();
  119. }
  120. catch(Exception e) {
  121. writeTraceback ("At Grep(string, StreamReader):");
  122. throw e;
  123. }
  124. }
  125. protected static int CalculateNewTarget(int FirstInningsTotal, double R1, double R2, int G50)
  126. {
  127. try
  128. {
  129. // R1 is the resource percentage available to the team batting first.
  130. // R2 is the resource percentage available to the team batting second.
  131. if (R2 < R1) {
  132. return (int)((FirstInningsTotal * (R2 / R1)) + 1);
  133. } else if (R2 == R1) {
  134. return FirstInningsTotal + 1;
  135. } else if (R2 > R1) {
  136. return (int)(FirstInningsTotal + ((R2 - R1) * (G50/100d)) + 1);
  137. }
  138. throw new Exception ("An unknown error occurred while calculating the new target.");
  139. }
  140. catch(Exception e) {
  141. writeTraceback ("At CalculateNewTarget(int, double, double):");
  142. throw e;
  143. }
  144. }
  145. private static bool IsValidOverCount (string n)
  146. {
  147. try
  148. {
  149. double oversAsDouble = Parser.parseDouble(n);
  150. int oversAsInteger = (int)oversAsDouble;
  151. if(oversAsDouble != Math.Abs(oversAsDouble) || Math.Abs(oversAsDouble - oversAsInteger) > 0.6 || (oversAsDouble.ToString().Split('.').Length > 1 && oversAsDouble.ToString().Split('.')[1].Length > 1) || oversAsDouble > 50)
  152. {
  153. return false;
  154. }
  155. return true;
  156. }
  157. catch {
  158. return false;
  159. }
  160. }
  161. protected static string SubtractOvers(string minuend, string subtrahend)
  162. {
  163. try
  164. {
  165. if (!IsValidOverCount (minuend) || !IsValidOverCount (subtrahend))
  166. throw new FormatException ();
  167. int m_minuend = (int.Parse (minuend.Split ('.') [0]) * 6) + ((int)minuend.Split ('.') [1] [0] - 48);
  168. int m_subtrahend = (int.Parse (subtrahend.Split ('.') [0]) * 6) + ((int)subtrahend.Split ('.') [1] [0] - 48);
  169. return BallsToOvers (m_minuend - m_subtrahend);
  170. }
  171. catch(Exception e) {
  172. writeTraceback ("At SubtractOvers(string, string):");
  173. throw e;
  174. }
  175. }
  176. private static string BallsToOvers (int i)
  177. {
  178. try
  179. {
  180. return (int)(i / 6d) + "." + (i % 6);
  181. }
  182. catch(Exception e) {
  183. writeTraceback ("At BallsToOvers(int):");
  184. throw e;
  185. }
  186. }
  187. static void writeTraceback (string data)
  188. {
  189. traceback += "\t" + data + "\n";
  190. }
  191. public static string ReadOvers()
  192. {
  193. string s = Console.ReadLine ();
  194. return s.Contains (".") ? s : s + ".0";
  195. }
  196. }
  197. }