Jump to content

i need a little help with a custom function combine 2 stirngs ?


Recommended Posts

okay so im making a ball python breeding game and im currently working on it right now and now im at the most important part fo the game the breed function!!!! :shrug:

 

now i l explain how it should work beacuse i dnt have any code yet for the function, but the rest of the site is done

 

okay in a ball python breeding action in real life would go something like this :

 

male + female = child

 

now the male and female snake will have traits that will determine what the child is example

there are three different king of traits for ball pythons co-dom , dom , and Rec

codom - half domanant traits

dom - compleatly dominant traits

and rec - where both snakes are required to carrie the trait in order to produce it ....

 

now i have some test snakes in my database like this

id - morph(base morph)  - het (enum yes or no) - and gen (.... whatever the snake traits are)

so what i did was start by placing the traits with slashes that i will explode with ex. spider/pastel/enhi

 

now i will use the base morph name  ex. cinnimon ball.

 

now here is where i need help

after explod() i have any array i wanna run the if stament if they carry the same traits but it will be dynamic so i wont no where to implode ?? :confused:

and i wanna kno is there a way to place dynamic varrable into an array say after they have bred i wanna give a random number of eggs then randomize what will hatch out them eggs :confused:

 

any help would be greratly appreciated and admin if this is considred to be spam i apologize

i love this forum and i dnt mean to spam

if(this is considered to be spam){

echo "Sorry PHPFREAKS.COM";

}ELSE{

echo "THANKS FOR THE HELP";

}

Link to comment
Share on other sites

I don't see how you can accomplish what you want with the way you have set up your data.

 

You state that you have a single field with the traits for a particular snake that are separated by a forward slash. But, where are you capturing what type of trait each one is? Do you have all the same trait descriptions for each same and in the same order: e.g. length, skin color, etc.

 

From my memory of Biology this all works something like this: there is a trait for a particular feature such as eye color. That trait has specific values (blue, brown, green, etc.) and each of those values is either dominant or recessive (I don't recall co-dominant, but I'll take your word for it).

 

So, if I was building something like what you explained, I would have one table for the traits that just describes the trait name (e.g. "eye color"). Then I would need another table for the trait values. That table would include a foreign key for the trait, the value (blue, green, etc) and whether that value was dominant, recessive or co-dominant. Then, lastly, I would have a table to describe the traits for each snake. It would include columns for snakeID, traitID, traitValueID.

 

With that (properly configured) database I could get the information needed for two snakes and then run a process to determine the traits for various offspring. Here is a rough example, I made some assumptions on how the trait determination should be made.

<?php

//Config values
$minOffSpring = 2;
$maxOffspring = 10;

$maleSnake = 2;   //You would determine this randomly
$femaleSnake = 8; //You would determine this randomly

//Run query to get the traits for both snakes
$query = "SELECT traitID, traitValueID, traitDominance
          FROM snakes
          JOIN snake_traits USING (snakeID)
          JOIN traits USING (traitID)
          JOIN traitValues USING (traitValueID)
          WHERE snakeID IN ($maleSnake, $femaleSnake)";
$result = mysql_query($qyery) or die(mysql_error());
//Create an associate array of all traits from the parents
//The key will be the trait ID and the value will be sub arrays 
//of the trait values and dominance for each parent
$parentTraits = array();
while($row = mysql_fetch_assoc($result))
{
    $parentTraits[$row['traitID']][] = array('valueID'=>$row['traitValueID'], 'dominance'=>$row['traitDominance']);
}

//Detemine number of offspring
$offspringCount = rand($minOffSpring, $maxOffsprin);

//Detemine offspring traits
$offspring = array();
for($baby=0; $baby<$offspringCount; $baby++)
{
    //Run loop of parent traits toget the parent traits
    foreach($parentTraits as $traitID => $traitValuesAry)
    {
        $parent1 = $traitValuesAry[0];
        $parent2 = $traitValuesAry[1];

        if($parent1['traitDominance']=='dominant' && $parent2['traitDominance']!='dominant')
        {
            //Parent 1 trait is dominant and parent 2 is not
            $traitID = $parent1['traitValueID'];
        }
        elseif($parent1['traitDominance']!='dominant' && $parent2['traitDominance']=='dominant')
        {
            //Parent 2 trait is dominant and parent 1 is not
            $traitID = $parent2['traitValueID'];
        }
        elseif($parent1['traitDominance']=='dominant' && $parent2['traitDominance']=='dominant')
        {
            //Both parent have a dominant trait (use random?)
            $traitID = (rand(1, 2)==1) ? $parent1['traitValueID'] : $parent2['traitValueID'];
        }
        elseif($parent1['traitDominance']=='codominant' && $parent2['traitDominance']=='recessive')
        {
            //Parent 1 is codominant, parent 2 is recessive
            $traitID = $parent1['traitValueID'];
        }
        elseif($parent1['traitDominance']=='recessive' && $parent2['traitDominance']=='codominant')
        {
            //Parent 1 is codominant, parent 2 is recessive
            $traitID = $parent2['traitValueID'];
        }
        elseif($parent1['traitDominance']=='codominant' && $parent2['traitDominance']=='codominant')
        {
            //Both parent have a codominant trait (use random?)
            $traitID = (rand(1, 2)==1) ? $parent1['traitValueID'] : $parent2['traitValueID'];
        }
        elseif($parent1['traitDominance']=='recessive' && $parent2['traitDominance']=='codominant')
        {
            //Both parent are recessive (use random?)
            $traitID = (rand(1, 2)==1) ? $parent1['traitValueID'] : $parent2['traitValueID'];
        }

        $offspring[$baby][$traitID] = $traitValueID;
    }
}

//You should now have an array of offspring along withtheir traits
?>

Link to comment
Share on other sites

thanks for the help buddie your code was most helpful but i think i could have been a little clearer.....

 

the main trait of the snake would be its base morph ex. albino (witch is aN rec Trait)

 

and every thing its het for  sits on a another row like such pastel /spaider/...../....

 

so that when i query and pass the info through a an option of an select menu the value togther would look like this:

<option value=\"$base/$het\">$.....</option>

so when the useer decides to breed the post data will look like this...

( base/het ) ex. or equily  albino/pastel/spider

then i explode by the forward slash

(/)albino

(/)pastel

(/)spider

:D

now i have them in an array how do i get them alone like a loop?

$trait[] = albino

$trait[] = pastel

$trait[] = spider

$trait[] = .....

$trait[] = .....

then count ?????? im stuck but u did answer a very big question and for that i thank you sincerly!!!! :D

i was wonder did i have to right out all the possible difernt combonations amongset the three traits or was there a faster way and u just showed me that that is the only way ..... lol! :D

how would i go about seperating that array after the explosin?????

 

Link to comment
Share on other sites

i explode by the forward slash

(/)albino

(/)pastel

(/)spider

:D

now i have them in an array how do i get them alone like a loop?

 

Maybe I am not understanding you, but foreach() will loop through results of an explode(). Really not understanding you. It may help to show the input and the expected output.

Link to comment
Share on other sites

It's possible. We have no idea what data you're dealing with, or the dimensions of the array, so we can't really help.

 

We can give you theoretical solutions, but my guess is you mostly want it done for you.

Link to comment
Share on other sites

let me give a better mark up

after query and query after user post the form this is what im left with

 

ex. albino/pastel/spider/

ex. pied/pastel/

ex. dram/bee

 

those are three different random examples

 

so say that first one is a male and that second is a female

they are passed into the function like so

 

$male = .......

$female = ......

 

i want to explode by the /

ex.

$traits =  explode('/',$male);

giving me an array ....

now that i have this array  with just the genetic background in it (ex. pied  albino pastel)

any pointers on a breed function from there with an array like that?

 

 

Link to comment
Share on other sites

That explanation is only partially helpful. My understanding (from that example) is that you want to pass two variables to a function, such as:

$male = "albino/pastel/spider/";
$female = "pied/pastel/";

getOffspring($male, $female);


function getOffspring($maleData, $femaleData)
{
    $maleDataAry = explode("/", $maleData);
    $femaleDataAry = explode("/", $femaleData);

    // ? ? ? ? ? ?
}

 

After that I have no idea what you want to do. One set of data has three traits and the other has only two. You have no way of correlating which traits from the male correspond to the traits of the female to determine recessive/dominant traits. You stated previously you wanted the data exploded with "keys". Assuming you want named keys and not numerically indexed keys (0, 1, 2, ...) where do you plan on getting those keys. You made a poor decision on storing the traits as you did and I see no solution based upon my understanding.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.