Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3277580
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:23:06+00:00 2026-05-17T19:23:06+00:00

I have an algorithm for calculating whether a player’s hand holds a straight in

  • 0

I have an algorithm for calculating whether a player’s hand holds a straight in Texas Hold’em. It works fine, but I wonder if there is a simpler way to do it that does not involve array/string conversions, etc.

Here’s a simplified version of what I have. Say the player is dealt a hand that is a 52-element array of card values:

var rawHand = [1,0,0,0,0,0,0,0,0,0,0,0,0, //clubs
               0,0,0,0,0,0,0,0,0,0,0,0,0, //diamonds
               0,1,1,0,1,0,0,0,0,0,0,0,0, //hearts
               0,0,0,1,0,0,0,0,1,0,0,0,0];//spades

A 1 represents a card in that value slot. The above hand has a 2-clubs, no diamonds, a 3-hearts, 4-hearts, and 6-hearts, a 5-spades and a 10-spades. Now I look at it to find a straight.

var suits = []; //array to hold representations of each suit

for (var i=0; i<4; i++) {
    var index = i*13;
    // commenting this line as I removed the rest of its use to simplifyy example
    //var hasAce = (rawHand[i+13]);

    //get a "suited" slice of the rawHand, convert it to a string representation
    //of a binary number, then parse the result as an integer and assign it to
    //an element of the "suits" array
    suits[i] = parseInt(rawHand.slice(index,index+13).join(""),2);
}

// OR the suits    
var result = suits[0] | suits[1] | suits[2] | suits[3];

// Store the result in a string for later iteration to determine
// whether straight exists and return the top value of that straight
// if it exists; we will need to determine if there is an ace in the hand
// for purposes of reporting a "low ace" straight (i.e., a "wheel"),
// but that is left out in this example
var resultString = result.toString(2);

//Show the result for the purposes of this example
alert("Result: " + resultString);

The trick here is to OR the various suits so there is just one 2-to-Ace representation. Am I wrong in thinking there must be a simpler way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:23:06+00:00Added an answer on May 17, 2026 at 7:23 pm

    Almost all of the work your code does is type conversion. If you just had the hand stored in bit format to begin with(needs > 32 bit type), you could do something like:

    var mask = 2^13 - 1; // this will zero out all but the low 13 bits
    var suits = (rawHand | rawHand>>13 | rawHand>>26 | rawHand>>39) & mask;
    

    The equivalent using a one line loop would be:

    var suits = [];
    for(var i=0; i < 13; i++) {
       suits[i] = rawHand[i] || rawHand[i+13] || rawHand[i+26] || rawHand[i+39];
    }
    

    This is much shorter and easier to understand.

    Converting to and from a bit-wise representation takes more code and CPU time than you save by using the bit-wise OR operator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have a decent algorithm for calculating axis minima and maxima? When creating
I have an algorithm that generates strings based on a list of input words.
I have a two part question Best-Practice I have an algorithm that performs some
I have a recursive algorithm which steps through a string, character by character, and
Does anyone have a good algorithm for taking an ordered list of integers, i.e.:
I have a language-agnostic question about an algorithm. This comes from a (probably simple)
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Does anyone have a trusted Proper Case or PCase algorithm (similar to a UCase
Does anybody have any reference material that details Cauchy-Reed algorithm? Googling for Cauchy-Reed Solomon
You have an ascending list of numbers, what is the most efficient algorithm you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.