Finding a sequence of numbers in an array
I am making a Yahtzee game and I need a way to calculate a small line (a sequence of 4 numbers in order). The following are valid: 1,2,3,4 | 2,3,4,5 | 3,4,5,6.
I have an array of 5 numbers and I need to find out if one of these 3 combinations is in this array.
For those unfamiliar with Yahtzee, there are 5 cubes (five numbers in an array) that can range from 1 to 6.
a source to share
function isStraight($hand) {
$straights = array(range(1, 4), range(2, 5), range(3, 6));
foreach($straights as $solution) {
if(array_intersect($solution, $hand) == $solution) {
return $solution;
}
}
return false;
}
$hand = array(1,5,2,4,3);
var_dump(isStraight($hand));
Not sure about the rules of the game, but this should do it.
The function will return the first solution it finds - in this case [1,2,3,4]
. It will return boolean false
if it doesn't find any lines in hand. Hope this helps.
a source to share
Perhaps something like this: --- warning --- air coding
function findSequenceLength(array $array)
{
// Filter duplicates out - and sort the array.
$sorted = array_unique($array, SORT_NUMERIC);
$lastValue = null;
$thisSeq = 0;
$longestSeq = 0;
foreach ($sorted as $value)
{
if ( ( $lastValue !== null ) && $value == $lastValue + 1)
{
// our value is exactly one above the last entry
// increase the counter
$thisSeq++;
} else {
// sequence ended - save the value
$longestSeq = max($longestSeq, $thisSeq);
$thisSeq = 1;
}
$lastValue = $value;
}
return max($longestSeq, $thisSeq);
}
$sequence = array(1,2,4,5,4,6);
echo findSequenceLength($sequence); // should return 3 [4,5,6]
Then you can check that the "sequence length"> = 4 to test the "little line"
a source to share
More precisely:
function array_match($array, $target_array) {
$offset = 0;
$maxoffset = sizeof($target_array) - sizeof($array);
for($y=0;$y<$maxoffset;$y++) {
$result = true;
for($x=0;$x<sizeof($array);$x++) {
if ($array[$x] != $target_array[$x+$y] ) {
$result = false;
continue;
}
}
if ($result)
return "true";
}
return "false";
}
echo array_match(array(1,2,3), array(1,2,3,4,5)); //true
echo array_match(array(1,2,3), array(4,1,2,3,5)); //true
echo array_match(array(1,2,3), array(4,2,2,1,2)); //false
a source to share
Here's another idea: use the generic isStraight () method and use it to check two slices of an array.
$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;
}