;******************************************************** ; pokerlogic.au3 ; Poker Evaluation and point assignment ; Evalaute any poker $hand to determine score and get ; text description of $hand. ; By: JFish ; 2015 ;********************************************************/ #include func _initVars() ; initializes variables at the start - also called in the eval function below to clear them global $cardcount[13] = [0,0,0,0,0,0,0,0,0,0,0,0,0];used to test everything but flushes global $hearts[13]= [0,0,0,0,0,0,0,0,0,0,0,0,0];used flush tests global $clubs[13]= [0,0,0,0,0,0,0,0,0,0,0,0,0] global $spades[13]= [0,0,0,0,0,0,0,0,0,0,0,0,0] global $diamonds[13]= [0,0,0,0,0,0,0,0,0,0,0,0,0] global $heartscount=0; used for flush test global $clubscount=0 global $spadescount=0 global $diamondscount=0 global $hand,$newhand; used for testing - to create actual hands global $text=""; used to describe the $hand "pair", "3 of a kind" etc global $points=0; will hold the unique $points for that $hand global $ccPosition =12; "card count position" for loops that use the data to evaluate hands global $kickerScore=0; will hold the points for kickers where appropriate EndFunc _initVars() func _flushScore($suitArray); will score a flush - called by all three flushes but royal and straight zero out the points if false b/c they have their own point systems local $decimal = .01 for $pointPos=12 to 0 step -1 if($suitArray[$pointPos]==1) then $points+=($pointPos*$decimal); $decimal *= .01; EndIf Next $points+=50000 EndFunc func _suitcount($hand);populates four arrays with a $count of each suit used for flush, straight flush and royal flush for $startLoop=0 to UBound($hand)-1 local $card=$hand[$startLoop] local $position=stringtrimright($card,1) if $position =="A" then $position=12 elseif $position =="K" then $position=11 elseif $position=="Q" then $position=10 elseif $position =="J" then $position=9 elseif $position ==1 then $position=8; else $position=$position-2 EndIf $card = StringTrimLeft($hand[$startLoop],1) if ($card=="H") OR ($card=="0H") then $hearts[$position]+=1 $heartscount+=1; elseif(($card=="C") OR ($card=="0C")) then $clubs[$position]+=1 $clubscount+=1 elseif(($card=="S") OR ($card=="0S")) then $spades[$position]+=1 $spadescount+=1 elseif(($card=="D") OR ($card=="0D")) then $diamonds[$position]+=1 $diamondscount+=1 EndIf Next EndFunc func _createhand($hand); $counts the number of cards by type (i.e. 2,3,4,J,Q,K, etc.) in a $hand for $count = 0 to ubound($hand)-1 if (($hand[$count] == "AH") OR ($hand[$count] == "AS") OR ($hand[$count] == "AD") OR ($hand[$count] == "AC")) then $cardcount[12] += 1 elseif (($hand[$count] == "KH") OR ($hand[$count] == "KS") OR ($hand[$count] == "KD") OR ($hand[$count] == "KC")) then $cardcount[11] += 1 elseif (($hand[$count] == "QH") OR ($hand[$count] == "QS") OR ($hand[$count] == "QD") OR ($hand[$count] == "QC")) then $cardcount[10] += 1 elseif (($hand[$count] == "JH") OR ($hand[$count] == "JS") OR ($hand[$count] == "JD") OR ($hand[$count] == "JC")) then $cardcount[9] += 1 elseif (($hand[$count] == "10H") OR ($hand[$count] == "10S") OR ($hand[$count] == "10D") OR ($hand[$count] == "10C")) then $cardcount[8] += 1 elseif (($hand[$count] == "9H") OR ($hand[$count] == "9S") OR ($hand[$count] == "9D") OR ($hand[$count] == "9C")) then $cardcount[7] += 1 elseif (($hand[$count] == "8H") OR ($hand[$count] == "8S") OR ($hand[$count] == "8D") OR ($hand[$count] == "8C")) then $cardcount[6] += 1 elseif (($hand[$count] == "7H") OR ($hand[$count] == "7S") OR ($hand[$count] == "7D") OR ($hand[$count] == "7C")) then $cardcount[5] += 1 elseif (($hand[$count] == "6H") OR ($hand[$count] == "6S") OR ($hand[$count] == "6D") OR ($hand[$count] == "6C")) then $cardcount[4] += 1 elseif (($hand[$count] == "5H") OR ($hand[$count] == "5S") OR ($hand[$count] == "5D") OR ($hand[$count] == "5C")) then $cardcount[3] += 1 elseif (($hand[$count] == "4H") OR ($hand[$count] == "4S") OR ($hand[$count] == "4D") OR ($hand[$count] == "4C")) then $cardcount[2] += 1 elseif (($hand[$count] == "3H") OR ($hand[$count] == "3S") OR ($hand[$count] == "3D") OR ($hand[$count] == "3C")) then $cardcount[1] += 1 elseif (($hand[$count] == "2H") OR ($hand[$count] == "2S") OR ($hand[$count] == "2D") OR ($hand[$count] == "2C")) then $cardcount[0] += 1 EndIf next return $cardcount EndFunc ;***************** ; BEGIN TESTS FOR VARIOUS HANDS ;**************** func _findRoyalFlush() local $flushResult=_findFlush() if $flushResult then local $flushArray="" if $text=="Heart Flush" then $flushArray=$hearts elseif $text=="Club Flush" then $flushArray=$clubs elseif $text=="Spade Flush" then $flushArray=$spades elseif $text=="Diamond Flush" then $flushArray=$diamonds endif ;need to say that if the FLUSH ARRAY is not null then look in the array, else ignore if (($text == "Heart Flush") OR ($text == "Club Flush") OR ($text == "Spade Flush") OR ($text == "Diamond Flush")) then if (($flushArray[12]==1) AND ($flushArray[11] == 1) AND ($flushArray[10] == 1) AND ($flushArray[9] ==1) AND ($flushArray[8] == 1)) then $text = "Royal Flush" $points = 1000000 return true else $points=0 Return false EndIf else return false EndIf EndIf EndFunc func _findStraightFlush() local $flushResult=_findFlush(); if $flushResult then ; this is meant to tie to the else return false end if - need reconcile endif statements ;MsgBox("","","Triggered") local $flushArray if $text=="Heart Flush" then $flushArray=$hearts elseif $text=="Club Flush" then $flushArray=$clubs elseif $text=="Spade Flush" then $flushArray=$spades; elseif $text=="Diamond Flush" then $flushArray=$diamonds EndIf if (($text == "Heart Flush") OR ($text == "Club Flush") OR ($text == "Spade Flush") OR ($text == "Diamond Flush")) then for $i=12 to 3 step -1 local $j=($i-1) local $k=($i-2) local $l=($i-3) local $m=($i-4) if (($flushArray[$i] >= 1) AND ($flushArray[$j] >=1) AND ($flushArray[$k] >=1) AND ($flushArray[$l] >=1) AND ($flushArray[$m] >=1)) then $text = "Straight Flush" $points = $i + 250000 $kickerScore = ((.01*$j)+(.0001*$k)+(.000001*$l)+(.00000001*$m)) return true elseif (($flushArray[12]==1) AND ($flushArray[0] == 1) AND ($flushArray[1] == 1) AND ($flushArray[2] ==1) AND ($flushArray[3] == 1)) then $text = "Straight Flush" $points = 3 + 250000 $kickerScore = ((.01*0)+(.0001*1)+(.000001*2)+(.00000001*3)) return true; EndIf next $points=0 return false EndIf else return false; EndIf EndFunc func _findFour() local $innerWhilecounter2 =1;// used in the while loop local $decimalPlace2 = .01; //used for kicker score for $findcounter = 12 to 0 step -1 if $cardcount[$findcounter] == 4 then $text = "Four of a kind" $points = $findcounter+100000 local $startPos4 = 12 while ($innerWhilecounter2 < 2) if ($newhand[$startPos4] == 1) then $kickerScore += ($startPos4 * $decimalPlace2) $innerWhilecounter2 += 1 $decimalPlace2 *= .01 EndIf $startPos4 -= 1 WEnd return True endif next return false; EndFunc func _fh3() local $threeWhilecount = 0 for $ccPosition = 12 to 0 step -1 if ($cardcount[$ccPosition] == 3) then $threeWhilecount +=1 if ($threeWhilecount >=1) then $points = $ccPosition return true; EndIf EndIf next return false; EndFunc func _fh2() Local $twoWhilecount=0; for $ccPosition = 12 to 0 step -1 if ($cardcount[$ccPosition] == 2) then $twoWhilecount +=1 if ($twoWhilecount >=1) then $kickerScore = ($ccPosition*.01); in case 2nd pair matters return true EndIf EndIf Next return false EndFunc func _findThreeAndThree() Local $doubleTrips=0 for $ccPosition = 12 to 0 step -1 if ($cardcount[$ccPosition] == 3) then $doubleTrips +=1 if ($doubleTrips >=2) then $kickerScore = ($ccPosition*.01) return true EndIf EndIf Next return false EndFunc func _findFullHouse() if ((_fh3()) AND (_fh2())) OR (_findThreeAndThree()) then $text = "Full House" $points += 75000 return true else return false EndIf EndFunc func _findFlush() if ($heartscount>=5) then $text = "Heart Flush" _flushScore($hearts) return true elseif ($clubscount>=5) then $text = "Club Flush" _flushScore($clubs) return true elseif($spadescount>=5) then $text = "Spade Flush" _flushScore($spades) return true elseif ($diamondscount>=5) then $text = "Diamond Flush" _flushScore($diamonds) return true else return false EndIf EndFunc func _findStraight() for $i= 12 to 4 step -1 local $j=($i-1) local $k=($i-2) local $l=($i-3) local $m=($i-4) if (($cardcount[$i] >= 1) AND ($cardcount[$j] >=1) AND ($cardcount[$k] >=1) AND ($cardcount[$l] >=1) AND ($cardcount[$m] >=1)) then $text = "Straight" $points = $i + 10000 $kickerScore = ((.01*$j)+(.0001*$k)+(.000001*$l)+(.00000001*$m)) return true elseif (($cardcount[12]==1) AND ($cardcount[0] >= 1) AND ($cardcount[1] >= 1) AND ($cardcount[2] >=1) AND ($cardcount[3] >= 1)) then $text = "Straight" $points = 3 + 10000 $kickerScore = ((.01*0)+(.0001*1)+(.000001*2)+(.00000001*3)) return true EndIf Next return false EndFunc func _findThree() local $innerWhilecounter2 =1; used in the while loop local $decimalPlace2 = .01; used for kicker score for $findcounter = 12 to 0 step -1 if ($cardcount[$findcounter] == 3) then $text = "Three of a kind" $points = $findcounter+2000; local $startPos3 = 12 while ($innerWhilecounter2 < 3) ;ConsoleWrite("debug: this is $sartPos3: " &$startPos3) if $cardcount[$startPos3] == 1 then $kickerScore += ($startPos3 * $decimalPlace2) $innerWhilecounter2 += 1 $decimalPlace2 *= .01 EndIf $startPos3 -= 1 wend return true EndIf Next return false EndFunc func _findTwoPair() Local $doubles=0 local $kicker local $kickerMultiple=.01 local $twopaircounts[2]=[0,0] for $ccPosition = 12 to 0 step -1 if ($cardcount[$ccPosition] == 2) then $twopaircounts[$doubles]=$ccPosition $points+=($ccPosition*$kickerMultiple) $doubles +=1 $kickerMultiple*=.1 if ($doubles >=2) then $kickerScore = $kicker $text="Two Pair" $points=($points*1000)+1000 $kickerMultiple*=.1 ; get the kicker local $findKicker for $findKicker=12 to 0 step -1 if $findKicker=$twopaircounts[0] OR $findKicker=$twopaircounts[1] Then ContinueLoop elseif $cardcount[$findKicker]<>0 then $kickerScore=$findKicker*.01 ExitLoop EndIf next return true EndIf EndIf Next return false EndFunc func _findPair(); this is to find a pair local $totalkickers=0 local $decimalPlace = .01; //used for kicker score for $findCounter = 12 to 0 step -1 if ($cardcount[$findcounter] == 2) then $text = "Pair" $points = $findcounter +100 ; find the kicker for $a=12 to 0 step -1 if ($cardcount[$a] == 1) then $kickerScore+=($a * $decimalPlace) $totalkickers+=1 $decimalPlace*=.01 if $totalkickers==3 Then ExitLoop EndIf endif next return true EndIf next return false EndFunc func _findOne() ; find the high card local $innerWhilecounter =1;// used in the while loop local $decimalPlace = .01; //used for kicker score for $findcounter = 12 to 0 step -1 if ($cardcount[$findcounter] == 1) then $text = "High Card" $points = $findcounter local $startPos1 = $findcounter-1 while ($innerWhilecounter < 5) if ($cardcount[$startPos1] == 1) then $kickerScore += $startPos1 * $decimalPlace $innerWhilecounter += 1 $decimalPlace *= .01 EndIf $startPos1 -= 1 WEnd return true EndIf next EndFunc ;************************************************** ; This is called to evaulate any hand ;*************************************************/ func _evaluatehand($hand) _initVars() ; clear global vars $newhand=_createhand($hand) ;creates card count _suitcount($hand);used for flush tests if _findRoyalFlush() then return elseif _findStraightFlush() then return elseif _findFour() then return elseif _findFullHouse() then Return elseif _findFlush() then Return elseif _findStraight() then Return elseif _findThree() then Return elseif _findTwoPair() then Return elseif _findPair() then Return elseif _findOne() then Return EndIf EndFunc