using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ADN
{
    public int[] genes;
    public int geneLength;
    private int maxMoves;

    public ADN(int length, int maxMoves)
    {
        geneLength = length;
        this.maxMoves = maxMoves;
        genes = new int[geneLength];
        Randomize();
    }

    public ADN(ADN copyFrom)
    {
        geneLength = copyFrom.geneLength;
        maxMoves = copyFrom.maxMoves;
        genes = new int[geneLength];
        System.Array.Copy(copyFrom.genes, genes, geneLength);
    }

    public void Randomize()
    {
        for (int i = 0; i < geneLength; i++)
        {
            genes[i] = Random.Range(0, maxMoves);
        }
    }

    public ADN Crossover(ADN partner)
    {
        ADN child = new ADN(geneLength, maxMoves);

        // Two-point crossover
        int point1 = Random.Range(0, geneLength / 3);
        int point2 = Random.Range(2 * geneLength / 3, geneLength);

        for (int i = 0; i < geneLength; i++)
        {
            if (i < point1)
                child.genes[i] = genes[i];
            else if (i < point2)
                child.genes[i] = partner.genes[i];
            else
                child.genes[i] = genes[i];
        }

        return child;
    }

    public void Mutate(float mutationRate)
    {
        for (int i = 0; i < geneLength; i++)
        {
            if (Random.value < mutationRate)
            {
                genes[i] = Random.Range(0, maxMoves);
            }
        }
    }
}

