Вот еще одна идея: используйте универсальный метод isStraight () и используйте его для проверки двух срезов массива.
$tests = array(
array(1,2,3,4,5),
array(1,1,2,3,4),
array(4,3,2,1,4),
array(3,4,5,6,1)
);
foreach($tests as $test) {
print_r($test);
echo "Straight: ";
var_dump(isStraight($test));
echo "Short Straight: ";
var_dump(isShortStraight($test));
echo "\n";
}
function isShortStraight($hand) {
return isStraight(array_slice($hand, 0, 4)) ||
isStraight(array_slice($hand, 1, 4));
}
function isStraight($hand) {
$unique = array_unique($hand);
if (count($hand) != count($unique)) {
/* Has Duplicates, not a straight */
return false;
}
sort($unique);
if ($unique != $hand) {
/* Sort order changed, not a straight */
return false;
}
return true;
}