PDA

View Full Version : consecutive numbers (with wildcards between) detection

boeing747fp
07-19-2007, 03:54 AM
I'm coding an online game and the cards have values from 1-12 and there can be wild cards for any given number... how can i make a function that will detect a "Run of #" including wilds without manually making an if/else statement for each combination that is possible??

boeing747fp
07-19-2007, 04:56 AM
this is what i have tried so far and it ALWAYS comes back with FAILED :(

function isRun(\$numbervalues,\$num_wilds){
sort(\$numbervalues,SORT_NUMERIC);
\$nextnumber = \$numbervalues[0]+1;
\$newnumberset=array();
for(\$i=1;\$i<count(\$numbervalues);\$i++){
array_push(\$newnumberset,\$numbervalues[\$i]);
}
if(in_array(\$nextnumber,\$numbervalues)){
//start the function again with "next number" and same number of wild cards
if(isRun(\$newnumberset,\$num_wilds)){
return true;
}else{
return false;
}
}elseif(\$num_wilds >0){
\$new_wildcount=\$num_wilds-1;
//put a wild in for "next number"
//start the function again with "next number" and one less wild card
if(isRun(\$newnumberset,\$new_wildcount)){
return true;
}else{
return false;
}
}else{
return false;
}
}

\$thenumbers = array(2,3,6); //this array is missing 4 and 5 to form the complete run
\$wilds=2; //these 2 wilds should take the place of 4 and 5 in this run

if(!isRun(\$thenumbers,\$wilds)){
echo "Failed";
}else{
echo "Success!";
}

c010depunkk
07-19-2007, 02:15 PM
Your function was a bit bloated. Here's my version (with a bit of help from a friend):

function isRun2(\$number_values,\$num_wilds) {
sort(\$number_values,SORT_NUMERIC);
for(\$i=\$number_values[0];\$i<end(\$number_values);\$i++) {
if(!in_array(\$i,\$number_values)) {
if((\$num_wilds--)==0) {
return false;
}
}
}
return true;
}

\$numbers=array(2,4,5,7);
\$wilds=2;

if(isRun2(\$numbers,\$wilds)) {
echo "Success!";
} else {
echo "Failed!";
}

boeing747fp
07-19-2007, 06:05 PM
sweet! thanks. (and yea, i figured mine was bloated because i just was adding/removing stuff from it trying to get it to work). yours worked. thanks again!