Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post

    consecutive numbers (with wildcards between) detection

    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??

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    this is what i have tried so far and it ALWAYS comes back with FAILED

    PHP Code:
    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)){
            
    //we're advancing!
            //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!";

    Last edited by boeing747fp; 07-19-2007 at 05:36 AM.

  • #3
    New to the CF scene
    Join Date
    Jan 2007
    Location
    Willich, NRW, Germany
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your function was a bit bloated. Here's my version (with a bit of help from a friend):

    PHP Code:
    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!";

    This is 10% luck, 20% percent skill, 15% concentrated power of will. 5% pleasure, 50% pain, And 100% reason to remember the name!

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •