Jump to content

PHP Genetics


kinks

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.