Jump to content

Smaller poker hand evaluator (autoit game not game automation)


Jfish
 Share

Recommended Posts

Note: this is code to create a card game in AutoIt - not anything that would violate forum rules. Just posting that upfront as it is a game but not of the offending variety.

That said, hello all!  Back in 2015 I wrote this poker hand evaluator that is 423 lines.  For giggles I played around with the array structure for the cards and created a lighter version that evaluates any five cards and returns the text for the hand that is under 50 lines. This was done mostly by relying on the array functions in the built in AutoIt UDF.  In fairness, the bigger  initial version can accommodate additional cards and scoring.  However, I still thought this version with array functions for the tests to reduce code was neat and worth posting as a study in contrast. Enjoy.

;*******************************************************************************
; Lightweight poker hand tester - five cards no scoring simply identifies hands
; by: Jfish July 2022
;*******************************************************************************
#include <Array.au3>
Global $straight=false, $flush=false, $cardCount[13], $handText=""
Global $hand[5][4]=[["10H","10","H",10],["9H","9","H",9],["8H","8","H",8],["7H","7","H",7],["6H","6","H",6]]; CHANGE ME TO ANY HAND [[card/suit][card w/o suit][suit without card][value 2-10 then 11-14 for J-A]] ||COL 2 for future use|| FULL DECK (use _arrayShuffle) to shuffle:|| Global $dk[52][4]=[["AH","A","H",14],["KH","K","H",13],["QH","Q","H",12],["JH","J","H",11],["10H","10","H",10],["9H","9","H",9],["8H","8","H",8],["7H","7","H",7],["6H","6","H",6],["5H","5","H",5],["4H","4","H",4],["3H","3","H",3],["2H","2","H",2],["AS","A","S",14],["KS","K","S",13],["QS","Q","S",12],["JS","J","S",11],["10S","10","S",10],["9S","9","S",9],["8S","8","S",8],["7S","7","S",7],["6S","6","S",6],["5S","5","S",5],["4S","4","S",4],["3S","3","S",3],["2S","2","S",2],["AD","A","D",14],["KD","K","D",13],["QD","Q","D",12],["JD","J","D",11],["10D","10","D",10],["9D","9","D",9],["8D","8","D",8],["7D","7","D",7],["6D","6","D",6],["5D","5","D",5],["4D","4","D",4],["3D","3","D",3],["2D","2","D",2],["AC","A","C",14],["KC","K","C",13],["QC","Q","C",12],["JC","J","C",11],["10C","10","C",10],["9C","9","C",9],["8C","8","C",8],["7C","7","C",7],["6C","6","C",6],["5C","5","C",5],["4C","4","C",4],["3C","3","C",3],["2C","2","C",2]]

func _handTester($hand)
    for $a=0 to ubound($hand)-1 ; create array to be used by subsquent hand tests - counting number of card values (i.e. Aces, Kings, etc.) in a hand
        $row=$hand[$a][3]-2
        $cardCount[$row]+=1
    Next

    For $a=ubound($cardCount)-1 to 4 step -1 ; test for a straight
        If ($cardCount[$a]=1) AND ($cardCount[$a-1]=1) AND ($cardCount[$a-2]=1) AND ($cardCount[$a-3]=1) AND ($cardCount[$a-4]=1) Then
            $straight=true
        EndIf
    Next

    $uniqueSuits=_arrayUnique($hand,2) ; test for a flush
    if $uniqueSuits[0]=1 then
        $flush=True
    EndIf

    If ($cardCount[12]=1) AND ($cardCount[11]=1) AND ($cardCount[10]=1) AND ($cardCount[9]=1) AND ($cardCount[8]=1) and $flush=true Then
        return 'royal flush'
    elseif $flush= true and $straight=true then
        return 'straight flush'
    elseif IsArray(_ArrayFindAll($cardCount,4)) Then
        return 'four of a kind'
    elseif IsArray(_ArrayFindAll($cardCount,3)) AND IsArray(_ArrayFindAll($cardCount,2)) Then
        return 'full house'
    elseif $flush=true then
        return 'flush'
    elseif $straight=true then
        return 'straight'
    elseif IsArray(_ArrayFindAll($cardCount,3)) then
        return 'trips'
    elseif IsArray(_ArrayFindAll($cardCount,2)) AND ubound(_ArrayFindAll($cardCount,2))=2 Then
        return 'two pair'
    elseif IsArray(_ArrayFindAll($cardCount,2)) then
        return 'pair'
    else
        return 'high card'
    EndIf
EndFunc
MsgBox(0,'',_handTester($hand))

EDIT: changed the straight loop to stop at 4 so it doesn't bust the array dimensions when looking for a straight 

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...