Page 1 of 1

[SOLVED]comparrisons

Posted: Sat Nov 20, 2004 9:44 pm
by dull1554
ok i have some combinations that i want to compare three random nubmbers against to see if the 3 random #s match any of the combos, i was wondering if anyone had any suggestions as to how to do this

the combinations are:
412
456
123
666
555
444
333
222
111
66
55
44
33
22
11

if the three random numbers say 124, (order does not matter)
i would want it to match the 412 combo then stop

if my question needs to be cleared up some just ask,
i honestly have no idea where to start so any help would be appreciated
thanks
~~Dull1554~~

Posted: Sun Nov 21, 2004 12:30 am
by rehfeld
i didnt test it extensively, but it seems to work

this will only match a complete match, it wont give you the "closest" match

ex: if the combo is 123
and the rnd nums are 1 and 2
this wont work.


also if you have 2 combos
123
321
given rand num 213

it will match the first combo it checks


the big problem i see w/ this is that
given the combo 123
and the rand number 111
this WILL match it, which, might not be ok for you purpose.
if you can explain more what is, and is not ok, i can work out a solution.


theres gotta be a better way to do this....prob something like splitting all nums into an array and using array_intersect or something like that

Code: Select all

<?php


$combos = array(
    '412',
    '456',
    '123',
    '666',
    '555',
);

$rand_nums = array(
    '5',
    '4',
    '6',
);


foreach ($combos as $combo) {
    $found = true;

    foreach ($rand_nums as $num) {
        if (false === strpos($combo, $num)) {
            $found = false;
            break;
        }
    }

    if ($found) {
        break;
    }

}


if ($found) {
    echo $combo;
}

// im guessing the rand nums prob come from user input through $_POST or somthing,
// so if you need a way to turn the string 214 into an array, 
// so you can use it for the $rand_nums array, 
// and dont have str_split() (php5), use this

function str_2array($string) {
    $array = array();
    $num_chars = strlen($string);

    for ($i=0; $i < $num_chars; $i++) {
        $array[] = $string[$i];
    }

    return $array;
}



// then

$rand_nums = str_2array($_POST['guess']);
?>

Posted: Sun Nov 21, 2004 8:12 am
by peni
rehfeld: it's a good solution, and in order to prevent matching "111" in "123" you can throw out the found element. then the code would have to look like:

- is there an "1" in "123"?
- no -> next combo
- yes -> throw out the "1" out of the "123" and look for the next figure (then the next "1" would not match with the rest "23")

Posted: Sun Nov 21, 2004 2:53 pm
by rehfeld
good idea

here it is

Code: Select all

&lt;?php




function str_replace_once($search, $replace, $haystack) {
    if (false !== ($start_pos = strpos($haystack, $search))) {
        $haystack = substr_replace($haystack, $replace, $start_pos, strlen($search));
    }
    return $haystack;
}


function str_2array($string) {
    $array = array();
    $num_chars = strlen($string);

    for ($i=0; $i &lt; $num_chars; $i++) {
        $array&#1111;] = $string&#1111;$i];
    }

    return $array;
}






$rand_nums = str_2array('645');


$combos = array(
    '412',
    '456',
    '123',
    '666',
    '555',
);




foreach ($combos as $combo) {
    $found = true;
    $tmp_combo = $combo; // make a copy to modify

    foreach ($rand_nums as $num) {
        if (false === strpos($tmp_combo, $num)) {
            $found = false;
            break;
        } else {
            $tmp_combo = str_replace_once($num, '', $tmp_combo); // remove 1 occurance of the $num we just found
        }
    }

    if ($found) {
        break;
    }

}


if ($found) {
    echo $combo;
}









?&gt;

Posted: Sun Nov 21, 2004 8:33 pm
by dull1554
thanks for the help, and im sorry i did not reply sooner, im working on the script and ill give it a shot as soon as i get a chance, and yet again thanks

Posted: Sun Nov 21, 2004 8:42 pm
by dull1554
ok, there is only one thing, if i make the array containing the possiable combos to this

Code: Select all

$combos = array(
    '412',
    '456',
    '123',
    '666',
    '555',
    '444',
    '333',
    '222',
    '111',
    '66',
    '55',
    '44',
    '33',
    '22',
    '11',
);
and if the random numbers are 4 4 6

it wont match the combo 44

i looked at it and tried to find a soultion but i dont really understand all of it

so if there is a quick fix i would be so greatful

Posted: Mon Nov 22, 2004 6:26 pm
by dull1554
ok so ive tried tweaking the code so that it can match 2 or 3 digit combos because before if the random number array consisted of 3 elements it would only match a three lenght combo so heres the new code that i tried and failed horriably

note - there are no errors but that it does not match anything

Code: Select all

<?php
function str_replace_once($search, $replace, $haystack) {
    if (false !== ($start_pos = strpos($haystack, $search))) {
        $haystack = substr_replace($haystack, $replace, $start_pos, strlen($search));
    }
    return $haystack;
}


function str_2array($string) {
    $array = array();
    $num_chars = strlen($string);

    for ($i=0; $i < $num_chars; $i++) {
        $array[] = $string[$i];
    }

    return $array;
}


$rand_nums = str_2array('232');
print'<div>';
print_r($rand_nums);
print'</div>';

$combos = array(
    '412',
    '456',
    '123',
    '666',
    '555',
    '444',
    '333',
    '222',
    '111',
    '66',
    '55',
    '44',
    '33',
    '22',//note - 232 should match this combo
    '11',
);




foreach ($combos as $combo) {
    $found = true;
    $tmp_combo = $combo; // make a copy to modify
    for($i = strlen($tmp_combo); $i > 0; $i--) {//this should make it check for a match of 3 numbers if the combo is 3 in length
//and it should check for a match of 2 if the combo is 2 in lenght...maybe my logic is flawed.....
    $num = $rand_nums;
    for($x = count($num); $i > 0; $i--) {//checks each of the random numbers against the combo
        if (false === strpos($tmp_combo, $num)) {
            $found = false;
            break;
        } else {
            $tmp_combo = str_replace_once($num, '', $tmp_combo); // remove 1 occurance of the $num we just found
        }
    }
    }
    if ($found) {
        break;
    }

}


if ($found) {
    echo $combo;
}

?>
anythoughts as to why it fails

Posted: Mon Nov 22, 2004 6:28 pm
by dull1554
after looking at it after posting i think the occurance of break; should be removed, i tried it but it still does not work

Posted: Mon Nov 22, 2004 8:26 pm
by rehfeld
will the combos always be 2 or 3 digits? or could they be 4, 5, or 1 etc....

Posted: Mon Nov 22, 2004 8:33 pm
by dull1554
they will be three or 2 digits, its for a game, i may make a harder lever with combos with 4 and 3 digit combos but for now id like it to work with 3 digits or 2 digits

Posted: Mon Nov 22, 2004 8:51 pm
by dull1554
ok heres another shot that i apperantly missed

Code: Select all

<?php
function str_replace_once($search, $replace, $haystack) {
    if (false !== ($start_pos = strpos($haystack, $search))) {
        $haystack = substr_replace($haystack, $replace, $start_pos, strlen($search));
    }
    return $haystack;
}
$combos = array(
'412',
'456',
'123',
'666',
'555',
'444',
'333',
'222',
'111',
'66',
'55',
'44',
'33',
'22',
'11',);
$num = array();
$num[1] = 2;//mt_rand(1,6);
$num[2] = 3;//mt_rand(1,6);
$num[3] = 2;//mt_rand(1,6);

foreach($combos as $combo) {
    $found = true;
    $combo1 = $combo;
    for($i = strlen($combo1); $i > 0; $i--) {
        if(strpos($combo1,$num[$i]) == false){$found = false;}
        else{str_replace_once($num[$i], '', $combo1);}

    }
    if($found == true){$match = $combo; break;}
}
if(isset($match)){echo $match;}
?>
see anything wrong, i yet again with this test get no errors but it does not return what it should

thanks in advance....

Posted: Mon Nov 22, 2004 9:05 pm
by rehfeld
think i got it

this scales to larger combos as well.

you will need to make sure that the $combos is sorted descending from longest to shortest (just like you have been). otherwise it may fail


also, if you use a rand_num like '11'
this will still match combo '11',
(but if you want it to rather match combo '111', or '21412', let me know i think i have an easy fix for it, but it seems like that would be contrary to what your doing). im just not sure the rand_num will always be at least as long as the longest combo


Code: Select all

<?php




function str_replace_once($search, $replace, $haystack) {
    if (false !== ($start_pos = strpos($haystack, $search))) {
        $haystack = substr_replace($haystack, $replace, $start_pos, strlen($search));
    }
    return $haystack;
}


function str_2array($string) {
    $array = array();
    $num_chars = strlen($string);

    for ($i=0; $i < $num_chars; $i++) {
        $array[] = $string[$i];
    }

    return $array;
}


$rand_nums = str_2array('4211');


$combos = array(
    '21412',
    '14456',
    '15123',
    '1412',
    '1456',
    '1123',
    '412',
    '456',
    '123',
    '666',
    '555',
    '444',
    '333',
    '222',
    '111',
    '66',
    '55',
    '44',
    '33',
    '22',//note - 232 should match this combo
    '11',
);




foreach ($combos as $combo) {

    $tmp_combo = $combo; // make a copy to modify
    $unique_occurances = 0;

    foreach ($rand_nums as $num) {
        if (false !== strpos($tmp_combo, $num)) {
            $tmp_combo = str_replace_once($num, '', $tmp_combo);
            $unique_occurances++;
        }
    }

    if ($unique_occurances === strlen($combo)) {
        $found = true;
        break;
    }

}


if (isSet($found)) {
    echo $combo;
}


?>

so do i win the game? i want my prize. lol j/k

Posted: Mon Nov 22, 2004 9:10 pm
by dull1554
thanks man
works like a charm
i really appreciat all the help

Posted: Mon Nov 22, 2004 9:23 pm
by rehfeld
glad it worked

Posted: Mon Nov 22, 2004 9:25 pm
by dull1554
:)