toms
toms

Reputation: 515

Poker hand generator and evaluator

I'm sure this topic has been covered to bits, but I've spent hours trying to work something out and I can't find enough resources that explain the process. Please note, I am new to JS and still relying on tutorials and code snippets to write code. I'm still not confident enough to write code from complete scratch.

THE GOAL:

  1. In JavaScript, 'draw' 5 random cards

  2. Evaluate the cards' rank among all possible hands

  3. Return a score from a Variable min/max, based on the rank of the card, unless it's less than a pair.

  4. Lastly, be able to draw a RANDOM hand based on rank (less than, more than, or exactly) EG. Return a hand that is of rank 100 or smaller. (could bring back 100 different hands)

Eg. While Min-Max score is 10-30. If a royal flush comes out, return 30 (best hand means best score). If low Two Pair (6H 6D 2C 2H 5S) comes out return 13. If high Two Pair (AH AD QC QH 5S), return 14. Etc. [Those are probably not accurate scores but you get the drift]

My Research results:

  1. Random Draw: Many applications have achieved this. My favorite so far has been this tutorial: http://www.informit.com/library/content.aspx?b=STY_JavaScript_24_hours&seqNum=229 it is quite simple and gets the result, however does not offer a full evaluation, only by category (pair, two pair, three of a kind etc). I need an evaluation that would be able to give a higher score to the superior of two hand that have two pairs.

  2. Evaluators: This got a bit confusing. I found a very basic evaluator, that uses javascript: http://jsfiddle.net/subskybox/r4mSF/ but it was too basic. Doesn't give me a complete rank. I found this one too: https://github.com/chenosaurus/poker-evaluator which uses the Two Plus Two algorithm and a lookup table. Now, it sounds really good, but I'm terribly confused as to how to install it on into my website, or how to use it. It says: to install: npm install poker-evaluator, which I never heard of before.

  3. Convert rating to score: Should be fairly easy maths. Perhaps: thisRank/maxRank*(MaxScore-MinScore)+MinScore

  4. Draw hand by rank: Haven't seen any way of doing this anywhere. Wouldn't mind seeing some examples or ideas. I'm not sure this can be done with the Two Plus Two poker-evaluator. It's more like the reverse process.

Now, it feels like I'm getting close with all this, but I'm not a 100% sure how to compile this completely. I feel like I could use the code I found in section 1, and the Two Plus Two poker-evaluator to achieve what I need. I would love to it if you could shed a light on the 'npm install', if I'm going in the right direction, or if you know other methods I could achieve the same thing.

Please don't tell me I have to try doing it myself first, because I really don't know how to do this from scratch without a little guidance.

Upvotes: 2

Views: 4582

Answers (1)

IazertyuiopI
IazertyuiopI

Reputation: 488

I will post another beginner's advice :

  • Write the algorithm of what you want to achieve in pseudo-code (e.g., words that are easy for you to read). If the algorithm is not clear in your head before you start coding it is not going to get clearer by itself.
    You are not able to write code : it is fine;
    you hope to write a program without having a detailed low-level vision of every one of its steps : it is not.
    At least that is how I see things.

    Example of pseudo-code that I would write for this case:

    1-
    create card deck 
      loop on number of cards to be drawn
          -generate random integer and remove corresponding card from card deck
          -add drawn card to hand
      end loop
    
    2-
    check if hand is highest figure and associate rating
         else check if hand is 2nd highest and associate rating
                    else...
    
    OR
    
    get data with all possible hands and search for this hand to retrieve score... 
    (see github repo)
    
    3-
    I did not get 3-
    
    4- If you have data with all hands and their value, you just have 
    to search this data by value instead of searching by hand like in 2-
    

  • Second, looking for code snippets on github is a good idea; read the javascript files in the projects which interest you and understand what they do. I think you will need to install node.js for that particular project, because it is used to import the lookup table. Just download the javascript files and include them in your project...do not forget to credit/thank the author.

  • Third, your question is not about a precise difficulty : it is a question about how to start programming stuff when you never did it before. I do not think stackoverflow is the right place for this, but I still answered your question because after all, this is also a help forum.
    My last advice would be to find a good book / tutorial; in every good book there is a sample project to follow in which you develop a complete program and will teach you the basics.

    P.S.: if you are really interested, don't give up because programming can be hard but it is also very rewarding to see stuff work...

    Upvotes: 2

  • Related Questions