PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/GR.Gambling.Backgammon/MatchEquityTable.cs

https://github.com/alexhanh/Botting-Library
C# | 363 lines | 194 code | 45 blank | 124 comment | 11 complexity | 504d07921df02995699deb737642efce MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Globalization;
  7. namespace GR.Gambling.Backgammon.Tools
  8. {
  9. // http://lists.gnu.org/archive/html/bug-gnubg/2006-07/msg00108.html
  10. // http://page.mi.fu-berlin.de/tkoch/Backgammon/
  11. // MatchEquityTable.CreateMoneyGameMET("test.xml", 1.0, 1.0, true);
  12. public class MatchEquityTable
  13. {
  14. private static XmlElement CreateMETRow(XmlDocument xmldoc, double[] values)
  15. {
  16. NumberFormatInfo ni = (NumberFormatInfo)CultureInfo.InstalledUICulture.NumberFormat.Clone();
  17. ni.NumberDecimalSeparator = ".";
  18. XmlElement row = xmldoc.CreateElement("row");
  19. for (int i = 0; i < values.Length; i++)
  20. {
  21. XmlElement me = xmldoc.CreateElement("me");
  22. me.AppendChild(xmldoc.CreateTextNode(values[i].ToString("0.0#######", ni)));
  23. row.AppendChild(me);
  24. }
  25. return row;
  26. }
  27. private static double METValue(double stake, double limit, bool rake, int points)
  28. {
  29. double pot = stake * points;
  30. if (pot > limit) pot = limit;
  31. if (rake)
  32. return 0.5 + 0.5 * (pot - 0.05 * pot) / limit;
  33. else
  34. return 0.5 - 0.5 * pot / limit;
  35. }
  36. private static double MetValue(int points, int max_points)//(int stake, int limit, int points, int max_points)
  37. {
  38. return 0.5 + (double)points / (double)max_points / 2.0;
  39. //return 0.5 + (double)Math.Min(stake * points, limit) / (double)limit / 2.0;
  40. }
  41. private static double MetValue(int stake, int limit, int points, int max_points)
  42. {
  43. //return 0.5 + (double)points / (double)max_points / 2.0;
  44. return 0.5 + (double)Math.Min(stake * points, limit) / (double)limit / 2.0;
  45. }
  46. public static void Test(string filepath, int stake, int limit)
  47. {
  48. XmlDocument xmldoc = new XmlDocument();
  49. xmldoc.AppendChild(xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", ""));
  50. xmldoc.AppendChild(xmldoc.CreateDocumentType("met", "-//GNU Backgammon//DTD Match Equity Tables//EN", "met.dtd", null));
  51. XmlElement root = xmldoc.CreateElement("met");
  52. xmldoc.AppendChild(root);
  53. XmlElement info = xmldoc.CreateElement("info");
  54. info.AppendChild(xmldoc.CreateElement("name"));
  55. info.LastChild.AppendChild(xmldoc.CreateTextNode("GRLib"));
  56. info.AppendChild(xmldoc.CreateElement("description"));
  57. info.LastChild.AppendChild(xmldoc.CreateTextNode("Stake: " + stake + " Limit: " + limit + " (in cents) rakeless table stakes MET."));
  58. info.AppendChild(xmldoc.CreateElement("length"));
  59. info.LastChild.AppendChild(xmldoc.CreateTextNode(""));//max_points.ToString()));
  60. root.AppendChild(info);
  61. // pre-crawford
  62. XmlElement pre = xmldoc.CreateElement("pre-crawford-table");
  63. pre.SetAttribute("type", "explicit");
  64. root.AppendChild(pre);
  65. // post-crawford
  66. XmlElement post = xmldoc.CreateElement("post-crawford-table");
  67. post.SetAttribute("type", "explicit");
  68. post.SetAttribute("player", "both");
  69. //double[] values = new double[max_points];
  70. //post.AppendChild(CreateMETRow(xml, values));
  71. root.AppendChild(post);
  72. xmldoc.Save(filepath);
  73. }
  74. public static void CreateRakelessMet(string filename, int stake, int limit)
  75. {
  76. int max_points = (int)(limit / stake);
  77. if (max_points * stake < limit)
  78. max_points++;
  79. if (max_points < 1)
  80. max_points = 1;
  81. XmlDocument xml = new XmlDocument();
  82. xml.AppendChild(xml.CreateNode(XmlNodeType.XmlDeclaration, "", ""));
  83. xml.AppendChild(xml.CreateDocumentType("met", "-//GNU Backgammon//DTD Match Equity Tables//EN", "met.dtd", null));
  84. XmlElement root = xml.CreateElement("met");
  85. xml.AppendChild(root);
  86. XmlElement info_element = xml.CreateElement("info");
  87. info_element.AppendChild(xml.CreateElement("name"));
  88. info_element.LastChild.AppendChild(xml.CreateTextNode("GRLib"));
  89. info_element.AppendChild(xml.CreateElement("description"));
  90. info_element.LastChild.AppendChild(xml.CreateTextNode("Stake: " + stake + " Limit: " + limit + " (in cents) rakeless table stakes MET."));
  91. info_element.AppendChild(xml.CreateElement("length"));
  92. info_element.LastChild.AppendChild(xml.CreateTextNode(max_points.ToString()));
  93. root.AppendChild(info_element);
  94. // pre-crawford
  95. XmlElement pre = xml.CreateElement("pre-crawford-table");
  96. pre.SetAttribute("type", "explicit");
  97. // post-crawford
  98. XmlElement post = xml.CreateElement("post-crawford-table");
  99. post.SetAttribute("type", "explicit");
  100. post.SetAttribute("player", "both");
  101. double[] hero_values = new double[max_points];
  102. int start_points = 0;
  103. for (int max = max_points; max > 0; max--)
  104. {
  105. for (int points = start_points, counter = 0; counter < max_points; counter++, points++)
  106. {
  107. hero_values[counter] = MetValue(stake, limit, points, max_points);//MetValue(points, max_points);
  108. }
  109. if (max == max_points)
  110. post.AppendChild(CreateMETRow(xml, hero_values));
  111. pre.AppendChild(CreateMETRow(xml, hero_values));
  112. start_points--;
  113. }
  114. root.AppendChild(pre);
  115. root.AppendChild(post);
  116. xml.Save(filename);
  117. }
  118. public static int CreateMoneyGameMET(string filename, double stake, double limit, bool rake)
  119. {
  120. int max_points = (int)(limit / stake);//Game.MaxPoints(stake, limit);
  121. if (max_points * stake < limit) max_points++;
  122. if (max_points < 1) max_points = 1;
  123. try
  124. {
  125. XmlDocument xmldoc = new XmlDocument();
  126. xmldoc.AppendChild(xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", ""));
  127. xmldoc.AppendChild(xmldoc.CreateDocumentType("met", "-//GNU Backgammon//DTD Match Equity Tables//EN",
  128. "met\\" + "met.dtd", null));
  129. //root element
  130. XmlElement root = xmldoc.CreateElement("met");
  131. xmldoc.AppendChild(root);
  132. //info element
  133. XmlElement info = xmldoc.CreateElement("info");
  134. info.AppendChild(xmldoc.CreateElement("name"));
  135. info.LastChild.AppendChild(xmldoc.CreateTextNode("Munkittajat ry"));
  136. info.AppendChild(xmldoc.CreateElement("description"));
  137. info.LastChild.AppendChild(xmldoc.CreateTextNode("How to own the PartyGammon cash games"));
  138. info.AppendChild(xmldoc.CreateElement("length"));
  139. info.LastChild.AppendChild(xmldoc.CreateTextNode(max_points.ToString()));
  140. root.AppendChild(info);
  141. // pre-crawford
  142. XmlElement pre = xmldoc.CreateElement("pre-crawford-table");
  143. pre.SetAttribute("type", "explicit");
  144. // hero win
  145. double[] hero_values = new double[max_points];
  146. // for (int i = 0; i < hero_values.Length; i++) hero_values[i] = -1000;
  147. for (int i = max_points - 1; i >= 1; i--)
  148. {
  149. hero_values[hero_values.Length - 1] = METValue(stake, limit, true, i);
  150. pre.AppendChild(CreateMETRow(xmldoc, hero_values));
  151. }
  152. // opponent win
  153. double[] opponent_values = new double[max_points];
  154. // for (int i = 0; i < opponent_values.Length; i++) opponent_values[i] = -1000;
  155. for (int i = 0; i < max_points; i++)
  156. {
  157. opponent_values[i] = METValue(stake, limit, false, max_points - 1 - i);
  158. }
  159. pre.AppendChild(CreateMETRow(xmldoc, opponent_values));
  160. root.AppendChild(pre);
  161. // post-crawford
  162. XmlElement post = xmldoc.CreateElement("post-crawford-table");
  163. post.SetAttribute("type", "explicit");
  164. post.SetAttribute("player", "both");
  165. double[] values = new double[max_points];
  166. // for (int i=0; i<values.Length; i++) values[i]=-1000;
  167. post.AppendChild(CreateMETRow(xmldoc, values));
  168. root.AppendChild(post);
  169. xmldoc.Save("met//" + filename);
  170. }
  171. catch (Exception e)
  172. {
  173. Console.WriteLine(e.Message);
  174. }
  175. return max_points;
  176. }
  177. }
  178. /*class MatchEquityTable
  179. {
  180. private XmlElement CreateMETRow(XmlDocument xmldoc, double[] values)
  181. {
  182. NumberFormatInfo ni = (NumberFormatInfo)CultureInfo.InstalledUICulture.NumberFormat.Clone();
  183. ni.NumberDecimalSeparator = ".";
  184. XmlElement row = xmldoc.CreateElement("row");
  185. for (int i = 0; i < values.Length; i++)
  186. {
  187. XmlElement me = xmldoc.CreateElement("me");
  188. me.AppendChild(xmldoc.CreateTextNode(values[i].ToString("0.00#######", ni)));
  189. row.AppendChild(me);
  190. }
  191. return row;
  192. }
  193. private double METValue(double stake, double limit, bool rake, Player winner, int points)
  194. {
  195. return 0.5 + 0.5 * game.NetWin(stake, limit, rake, winner, points) / limit;
  196. }
  197. public static int CreateMoneyGameMET(string filename, double stake, double limit, bool rake)
  198. {
  199. int max_points = Game.MaxPoints(stake, limit);
  200. try
  201. {
  202. XmlDocument xmldoc = new XmlDocument();
  203. xmldoc.AppendChild(xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", ""));
  204. xmldoc.AppendChild(xmldoc.CreateDocumentType("met", "-//GNU Backgammon//DTD Match Equity Tables//EN",
  205. gnubg_dir + "met\\" + "met.dtd", null));
  206. //root element
  207. XmlElement root = xmldoc.CreateElement("met");
  208. xmldoc.AppendChild(root);
  209. //info element
  210. XmlElement info = xmldoc.CreateElement("info");
  211. info.AppendChild(xmldoc.CreateElement("name"));
  212. info.LastChild.AppendChild(xmldoc.CreateTextNode("Munkittajat ry"));
  213. info.AppendChild(xmldoc.CreateElement("description"));
  214. info.LastChild.AppendChild(xmldoc.CreateTextNode("How to own the PartyGammon cash games"));
  215. info.AppendChild(xmldoc.CreateElement("length"));
  216. info.LastChild.AppendChild(xmldoc.CreateTextNode(max_points.ToString()));
  217. root.AppendChild(info);
  218. // pre-crawford
  219. XmlElement pre = xmldoc.CreateElement("pre-crawford-table");
  220. pre.SetAttribute("type", "explicit");
  221. // hero win
  222. double[] hero_values = new double[max_points];
  223. // for (int i = 0; i < hero_values.Length; i++) hero_values[i] = -1000;
  224. for (int i = max_points - 1; i >= 1; i--)
  225. {
  226. hero_values[hero_values.Length - 1] = METValue(stake, limit, rake, Player.Hero, i);
  227. pre.AppendChild(CreateMETRow(xmldoc, hero_values));
  228. }
  229. // opponent win
  230. double[] opponent_values = new double[max_points];
  231. // for (int i = 0; i < opponent_values.Length; i++) opponent_values[i] = -1000;
  232. for (int i = 0; i < max_points; i++)
  233. {
  234. opponent_values[i] = METValue(stake, limit, rake, Player.Opponent, max_points - 1 - i);
  235. }
  236. pre.AppendChild(CreateMETRow(xmldoc, opponent_values));
  237. root.AppendChild(pre);
  238. // post-crawford
  239. XmlElement post = xmldoc.CreateElement("post-crawford-table");
  240. post.SetAttribute("type", "explicit");
  241. post.SetAttribute("player", "both");
  242. double[] values = new double[max_points];
  243. // for (int i=0; i<values.Length; i++) values[i]=-1000;
  244. post.AppendChild(CreateMETRow(xmldoc, values));
  245. root.AppendChild(post);
  246. xmldoc.Save(gnubg_dir + "met\\" + filename);
  247. }
  248. catch (Exception)
  249. {
  250. }
  251. return max_points;
  252. }
  253. }*/
  254. }