Skip to content

Instantly share code, notes, and snippets.

@pavellishin
Created May 10, 2012 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavellishin/2654187 to your computer and use it in GitHub Desktop.
Save pavellishin/2654187 to your computer and use it in GitHub Desktop.
function check_straight($hand) {
$h=$hand;natsort($h);$h=str_replace(str_split('hdcs'),'',implode($h));return strpos('2345a2345678910jq910jkq10ajkq', $h)>-1;
}
@pavellishin
Copy link
Author

function check_straight($hand) {
    // Rename variable to save a few characters here and there
    $h=$hand;

    // Sort in 2..10ajkq order
    natsort($h);

    // Replace the suit characters with a blank space
    $h=str_replace(str_split('hdcs'),'',implode($h));

    // Check against all possible values of a straight,
    // taking into consideration the sorting trickiness

    // a2345 == 2345a
    // 23456 == 23456
    // ...
    // 678910 == 678910
    // 78910J == 78910J
    // 8910JQ == 8910JQ
    // 910JQK == 910JKQ
    // 10JQKA == 10AJKQ

    // I could shorten this by a character by returning strpos('...', $h) + 1
    // since that would turn the "false" reply of -1 into a 0 (which evaluates to False)
    // and anything else into a positive number greater than zero (which evaluates to True)

    return strpos('2345a2345678910jq910jkq10ajkq', $h)>-1;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment