Guest User

pkras3

a guest
Jan 4th, 2021
25
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package lib.sapphire.game.math {
  2.  
  3. //import lib.sapphire.data.Satellite;
  4. import lib.sapphire.iface.LanguageFilter;
  5.  
  6. public final class HandEvaluator {
  7.  
  8. private static const PAIR:String = "PAIR";
  9. private static const TWO_PAIR:String = "TWO PAIR";
  10. private static const TRIP:String = "TRIP";
  11. private static const THREE_OF_A_KIND:String = "THREE OF A KIND";
  12. private static const STRAIGHT:String = "STRAIGHT";
  13. private static const STRAIGHT_TO:String = "STRAIGHT TO";
  14. private static const FLUSH:String = "FLUSH";
  15. private static const FULL:String = "FULL";
  16. private static const FULL_HOUSE:String = "FULL HOUSE";
  17. private static const FOUR:String = "FOUR";
  18. private static const FOUR_OF_A_KIND:String = "FOUR OF A KIND";
  19. private static const STRAIGHT_FLUSH:String = "STRAIGHT FLUSH";
  20. private static const ROYAL_FLUSH:String = "ROYAL FLUSH";
  21. private static const WILD_ROYAL_FLUSH:String = "WILD ROYAL FLUSH";
  22. private static const FIVE_OF_A_KIND:String = "FIVE OF A KIND";
  23. private static const NATURAL_ROYAL_FLUSH:String = "NATURAL ROYAL FLUSH";
  24.  
  25. private static const BACK:String = "back";
  26.  
  27.  
  28.  
  29. public static function cardToNum(c:String):Number {
  30. var ac:Object = {"A":14,"K":13,"Q":12,"J":11};
  31. if (isNaN(Number(c))) {
  32. return (ac[c]);
  33. }
  34. return (Number(c));
  35. }
  36.  
  37. public static function cardFromNum(n:int):String {
  38. if (n<11) return (String(n));
  39. var ac:Array = ["J","Q","K","A"];
  40. return (ac[n-11]);
  41. }
  42.  
  43. public static function cardFromID(id:int):Object {
  44. var suits:Array = ["C","D","H","S"];
  45. return ({val:cardFromNum((id%13)+2),suit:suits[Math.floor(id/13)]});
  46. }
  47.  
  48. public static function HiCard(a:Object,b:Object):Number {
  49. return cardToNum(a["val"]) > cardToNum(b["val"]) ? -1 : 1;
  50. }
  51.  
  52.  
  53. private static function sr(c:Array):String {
  54. var s:String = "";
  55. for each (var a:Object in c) {
  56. s+=a.val+a.suit;
  57. }
  58. return (s);
  59. }
  60.  
  61. public static function Poker(playercards:Array,housecards:Array,byRank:Boolean = false,skipIncompleteHands:Boolean = true,
  62. hasSplit:Boolean = false,
  63. seat:int = -1):String {
  64. //clocks at about 0.000298 seconds per eval, or about 3348/sec. on a 1.83Ghz Core (1) Duo.
  65. //Note that 1 full Omaha table has to run this 540 times every 2 seconds. That's 0.16092 seconds of processing time each time,
  66. //not counting the Omaha overhead.
  67. if (!playercards || playercards.length<1) {
  68. return ("");
  69. }
  70.  
  71. playercards = playercards.slice();
  72. housecards = housecards.slice();
  73.  
  74. var cards:Array = new Array();
  75.  
  76. for each (var hc:Object in housecards) {
  77. if (!hc.dealt) {
  78. continue;
  79. }
  80. if (skipIncompleteHands && hc.val == BACK) {
  81. return ("");
  82. } else if (hc.val != BACK) {
  83. cards.push(hc);
  84. }
  85. }
  86.  
  87. for each (var tc:Object in playercards) {
  88. if (!tc.dealt) {
  89. continue;
  90. }
  91. if (skipIncompleteHands && tc.val == BACK) {
  92. return ("");
  93. } else if (tc.val != BACK) {
  94. cards.push(tc);
  95. }
  96. }
  97.  
  98.  
  99. if (cards.length<2) {
  100. return ("");
  101. }
  102.  
  103. var pairs:Array = new Array();
  104. var found:Boolean = false;
  105. var frontcount:Number = 0;
  106.  
  107. for each (var cp:Object in cards) {
  108.  
  109. //if (cp.val == "back") {
  110. // return (""); //return blank for any hand with even one back in it.
  111. //}
  112.  
  113. found = false;
  114. for (var pk:String in pairs) {
  115. if (pairs[pk].val == cp.val) {
  116. pairs[pk].cards.push(cp);
  117. found = true;
  118. }
  119. }
  120. if (!found) {
  121. pairs.push({val:cp.val,cards:[cp]});
  122. }
  123. }
  124.  
  125. for (var pk2:String in pairs) {
  126. pairs[pk2]["val"] = cardToNum(pairs[pk2].val);
  127. pairs[pk2]["num"] = pairs[pk2].cards.length;
  128. }
  129.  
  130. pairs.sortOn(["num", "val"],[Array.DESCENDING | Array.NUMERIC, Array.DESCENDING | Array.NUMERIC]);
  131. var pair:Boolean = false;
  132. var twopair:Boolean = false;
  133. var three:Boolean = false;
  134. var fullhouse:Boolean = false;
  135. var four:Boolean = false;
  136.  
  137. if (pairs[0].num>=4) {
  138. four = true;
  139. } else {
  140. switch (pairs[0].num) {
  141. case 3:
  142. three = true;
  143. if (pairs[1] && pairs[1].num >=2) {
  144. fullhouse = true;
  145. }
  146. break;
  147. case 2:
  148. pair = true;
  149. if (pairs[1] && pairs[1].num == 2) {
  150. twopair = true;
  151. }
  152. break;
  153. }
  154. }
  155.  
  156. //flush check//
  157. var flush:String;
  158. var suits:Object = {S:[],D:[],C:[],H:[]};
  159. cards.sort(HiCard);
  160. for each (var cf:Object in cards) {
  161. suits[cf.suit].push(cf);
  162. if (suits[cf.suit].length>4) {
  163. flush = cf.suit;
  164. break;
  165. }
  166. }
  167.  
  168. //straight check//
  169. var straight:Boolean = false;
  170. var cardCheck:Array;
  171. if (flush) {
  172. cardCheck = new Array();
  173. for each (var cs:Object in cards) {
  174. if (cs.suit == flush) {
  175. cardCheck.push(cs);
  176. }
  177. }
  178. } else {
  179. cardCheck = cards.slice();
  180. }
  181. cardCheck.sort(HiCard);
  182. var straightcards:Array = [cardCheck[0]];
  183. var lastC:Number = cardToNum(cardCheck[0].val);
  184. for each (var ccs:Object in cardCheck) {
  185. if (cardToNum(ccs.val) == lastC-1) {// || (cardToNum(ccs.val) == 5 && lastC == 14)) {
  186. straightcards.push(ccs);
  187. //new fix:
  188. if (cardToNum(ccs.val) == 2 && cardCheck[0].val == "A" && straightcards.length == 4) {
  189. straightcards.push({val:"A",suit:cardCheck[0].suit});
  190. } //#
  191. if (straightcards.length == 5) {
  192. straight = true;
  193. break;
  194. }
  195. } else if (cardToNum(ccs.val) != lastC) {
  196. straightcards = [ccs];
  197. }
  198. lastC = cardToNum(ccs.val);
  199. }
  200.  
  201. //resort the wheel.
  202. //DEPRECATED w/ the new fix... this was right for 5-card hands, but failed on 7-card hands...
  203. /*if (straight && straightcards[0].val == "A" && straightcards[1].val == 5) {
  204. straightcards.push(straightcards.shift());
  205. }*/
  206.  
  207. //output//
  208. var out:String;
  209. var finalCards:Array;
  210. if (straight && flush) {
  211. if (straightcards[0].val == "A") {
  212. out = byRank ? "0x9" : LanguageFilter.Translate(ROYAL_FLUSH);
  213. } else {
  214. out = byRank ? "0x9" : LanguageFilter.Translate(STRAIGHT_FLUSH);
  215. }
  216. } else if (four) {
  217. out = byRank ? "0x8" : LanguageFilter.Translate(FOUR);
  218. } else if (fullhouse) {
  219. out = byRank ? "0x7" : pairs[0].cards[0].val+" "+LanguageFilter.Translate(FULL)+"/"+pairs[1].cards[0].val;
  220. } else if (flush) {
  221. out = byRank ? "0x6" : cardCheck[0].val+" "+LanguageFilter.Translate(FLUSH);
  222. } else if (straight) {
  223. out = byRank ? "0x5" : LanguageFilter.Translate(STRAIGHT_TO)+" "+straightcards[0].val;
  224. } else if (three) {
  225. out = byRank ? "0x4" : LanguageFilter.Translate(TRIP)+" "+pairs[0].cards[0].val+"s";
  226. } else if (twopair) {
  227. out = byRank ? "0x3" : LanguageFilter.Translate(TWO_PAIR)+" "+pairs[0].cards[0].val+"/"+pairs[1].cards[0].val;
  228. } else if (pair) {
  229. out = byRank ? "0x2" : LanguageFilter.Translate(PAIR)+" "+pairs[0].cards[0].val+"s";
  230. } else {
  231. out = byRank ? "0x1" : cardCheck[0].val+"-"+cardCheck[1].val+" "+LanguageFilter.Translate("HIGH");
  232. }
  233.  
  234. if (byRank) {
  235. var c:Number = 0;
  236. for each (var bhc:Object in cards) {
  237. c++;
  238. if (c>5) {
  239. break;
  240. }
  241. var hx:uint = uint(cardToNum(bhc.val));
  242. out += hx.toString(16);
  243. }
  244. }
  245. return (out);
  246. }
  247.  
  248.  
  249. }
  250. }
RAW Paste Data