kinks Posted August 30, 2009 Share Posted August 30, 2009 OKay. I am stuck, and need help. I'm making a site where you raise and shoe 'virtual rabbits.' I'm trying to figure out the color genetics script. Let's give this scenario: I breed a black colored rabbit, his genetics are: aaB_C_D_E_ So, I breed the black with a Chocolate genetics: aabb_C_D_E_ The black gene, in this case, is the dominant gene (keep in mind both these have 'no' other genetics with them. Just for scenario reasons) So, let's say they breed, and 5 rabbits are born. The bulk of those rabbits are Black, but they carry the chocolate gene with them, and have a 1 in 3 chance of being chocolate. I was trying to write it using 'if' statements, but got stuck Help? Quote Link to comment https://forums.phpfreaks.com/topic/172500-php-genetics/ Share on other sites More sharing options...
kinks Posted August 31, 2009 Author Share Posted August 31, 2009 I would like to use if statements to create the code (not that I can think of any other way). because once someone selects their two rabbits and hits "breed" I want it to view this code quickly and determine the genetics. Even though it will be more complicated to make that possible. Quote Link to comment https://forums.phpfreaks.com/topic/172500-php-genetics/#findComment-909422 Share on other sites More sharing options...
lockdownd7 Posted August 31, 2009 Share Posted August 31, 2009 Is there a reason you formatted the genes like this: aaB_C_D_E_ If the user won't see the genes you're using then, you can format them in a lot easier way to process. I'm guessing capital letters are dominant genes? If that's the case you could put each rabbit's genes in an array; assign each gene an integer value depending on how dominant you wanted it. Then compare the parents values in whatever way you want to determine the offspring's array values. Quote Link to comment https://forums.phpfreaks.com/topic/172500-php-genetics/#findComment-909441 Share on other sites More sharing options...
kinks Posted August 31, 2009 Author Share Posted August 31, 2009 Is there a reason you formatted the genes like this: aaB_C_D_E_ If the user won't see the genes you're using then, you can format them in a lot easier way to process. I'm guessing capital letters are dominant genes? If that's the case you could put each rabbit's genes in an array; assign each gene an integer value depending on how dominant you wanted it. Then compare the parents values in whatever way you want to determine the offspring's array values. Well; I was hoping on allowing users to get 'genotype tests' so, users can view the genetics; but I don't think I will, so that way I can make it wasier on myself. Quote Link to comment https://forums.phpfreaks.com/topic/172500-php-genetics/#findComment-909517 Share on other sites More sharing options...
lockdownd7 Posted August 31, 2009 Share Posted August 31, 2009 You can do that and show it to them; but it would be easier if you ran the genetic calculations first and then converted them to a format you want users to see. My point was just that it will be easier if you don't try to compare string values like that to make the actual decisions. Quote Link to comment https://forums.phpfreaks.com/topic/172500-php-genetics/#findComment-909523 Share on other sites More sharing options...
Alex Posted August 31, 2009 Share Posted August 31, 2009 You're probably going to want to store each genetic trait in a separate element of an array. You can build in the string at the end. To do probability you can do something like this: if(rand(0,2) == 0) { //1/3 Chance } Quote Link to comment https://forums.phpfreaks.com/topic/172500-php-genetics/#findComment-909556 Share on other sites More sharing options...
RussellReal Posted August 31, 2009 Share Posted August 31, 2009 each gene usually comes in pairs of two.. like Bb or bb or BB.. so lets say you cross Bb with bb, but if you have like you said aaBb than you'd need to go into more detail.. this should cross any number of genetic traits across itself then you just randomly pick from $possabilities all you need to do is simply <?php $gene1 = 'aaBb'; $geneA = array(); $i = 0; while ($i < strlen($gene1)) $geneA[] = substr($gene2,$i++,1); $gene2 = 'Aabb'; $geneB = array(); $i = 0; while ($i < strlen($gene2)) $geneB[] = substr($gene2,$i++,1); $possabilities = array(); foreach ($geneA as $A) { foreach ($geneB as $B) { if ($A === strtoupper($A)) $possabilities[] = $A.$B; else { if ($B === strtoupper($B)) $possabilities[] = $B.$A; else $possabilities[] = $A.$B; } } } ?> and $possabilities SHOULD hold all 4 possabilities if I did that right.. the above is not tested.. Quote Link to comment https://forums.phpfreaks.com/topic/172500-php-genetics/#findComment-909579 Share on other sites More sharing options...
RussellReal Posted August 31, 2009 Share Posted August 31, 2009 sorry.. theres a mistake in my code above and I can't modify <?php $gene1 = 'aaBb'; $geneA = array(); $i = 0; while ($i < strlen($gene1)) $geneA[] = substr($gene1,$i++,1); $gene2 = 'Aabb'; $geneB = array(); $i = 0; while ($i < strlen($gene2)) $geneB[] = substr($gene2,$i++,1); $possabilities = array(); foreach ($geneA as $A) { foreach ($geneB as $B) { if ($A === strtoupper($A)) $possabilities[] = $A.$B; else { if ($B === strtoupper($B)) $possabilities[] = $B.$A; else $possabilities[] = $A.$B; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/172500-php-genetics/#findComment-909585 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.