Now I know the basic logic behind finding a straight, and I assume that would include a pseudo of
function is_straight(array $cards) {
sort($cards);
if(($cards[4] - $cards[0]) == 5) {
//Code to make sure the cards in between are increment
//is straight.
}
}
would theoretically work for a 5 card check.
But how would one go for eliminating cards from the array of 7 cards to find a straight?
Would I have to individually check all 5 hand combinations within the 7 cards array?
so eliminate two cards from the $cards array and check that combination for a straight?
So I'm a little stuck on the logical side of this, rather than the code side.
In pseudo code
#filter doubles
cards = array_unique(cards)
sort(cards)
foreach cards as key, value:
if not key_exists(cards, key+4):
return false
if cards[key+4] == value + 4:
return true
longer potentially more explicit version
#filter doubles
cards = array_unique(cards)
sort(cards)
straight_counter = 1
foreach cards as key, value:
if not key_exists(cards, key+1):
return false
# is the following card an increment to the current one
if cards[key+1] == value + 1:
straight_counter++
else:
straight_counter = 1
if straight_counter == 5:
return true
Assuming that $cards
is an array containing cards values from 1 to 13, I think you need to evaluate with a difference of 4, not 5 :
5 - 1 = 4
6 - 2 = 4
7 - 3 = 4
etc.
You also need to add a specific logic for 10, J, Q, K, A
But for your specific question, what about :
function is_straight(array $cards) {
sort($cards);
if((($cards[4] - $cards[0]) == 4) ||
(($cards[5] - $cards[1]) == 4) ||
(($cards[6] - $cards[2]) == 4)) {
//Code to make sure the cards in between are increment
//is straight.
}
}
function is_straight(array $array) {
$alpha = array_keys(array_count_values($array));
sort($alpha);
if (count($alpha) > 4) {
if (($alpha[4] - $alpha[0]) == 4) {
$result = $alpha[4];
return $result;
}
if (count($alpha) > 5) {
if (($alpha[5] - $alpha[1]) == 4) {
$result = $alpha[5];
return $result;
}
}
if (count($alpha) > 6) {
if (($alpha[6] - $alpha[2]) == 4) {
$result = $alpha[6];
return $result;
}
}
}
}