Assignment - DNA Mixing

Create breed function by using the _mixDna function that mixes DNA of cat’s parents.

1 Like

DNA Mixing Assignment Code

breed
function breed(uint256 _dadId, uint256 _momId) public returns(uint256){
        require(_owns(msg.sender, _dadId) && _owns(msg.sender, _momId), "You must own both bears to breed them!");
        
        //get newDna from _mixDna
        uint256 newGenes = _mixDna(bears[_dadId].genes, bears[_momId].genes); 

        //Generation determination
        uint256 newGen;
        if(bears[_momId].generation == bears[_dadId].generation){
            newGen = bears[_momId].generation++;
        }
        else if (bears[_momId].generation < bears[_dadId].generation)
        {
            newGen = bears[_momId].generation++;
        }
        else{
            newGen = (bears[_momId].generation + bears[_dadId].generation) / 2;
        }

        //Create new bear and transfer to msg.sender
        _createBear(_momId, _dadId, newGen, newGenes, msg.sender);


    }
mixDNA

    function _mixDna(uint256 _dadDna, uint256 _momDna) internal pure returns(uint256){
        uint256 dadHalf = _dadDna / 100000000;
        uint256 momHalf = _momDna % 100000000;
        uint256 newDna = (dadHalf * 100000000) + momHalf;
        return newDna;
    }
1 Like