Example #1
0
 /// <summary>
 /// A method to breed this gene with another gene
 /// </summary>
 /// <param name="otherGene">The gene to breed this gene with</param>
 /// <returns>The result of breeding these two genes together</returns>
 public Gene breedWith(Gene otherGene)
 {
     Cell[][] newCells = new Cell[10][];
     for (int i = 0; i < cells.Length; i++)
     {
         newCells[i] = new Cell[10];
         for (int j = 0; j < cells.Length; j++)
         {
             int mutationChance = Simulation.getMutationChance();
             if (random.Next(mutationChance) == 1)
             {
                 newCells[i][j] = new Cell(random.Next(7), random.Next(7));
             }
             else
             {
                 int colour1 = this.getPart(i, j);
                 int colour2 = otherGene.getPart(i, j);
                 newCells[i][j] = new Cell(colour1, colour2);
             }
         }
     }
     return new Gene(newCells);
 }
Example #2
0
 /// <summary>
 /// An overloaded constructor for the gene that takes an array of cells. Used when breeding.
 /// </summary>
 /// <param name="cells">A 2D array of cells that represent this Genome</param>
 public Gene(Cell[][] cells)
 {
     this.cells = cells;
     this.init();
 }
Example #3
0
 /// <summary>
 /// An overloaded constructor to allow the specification of both the random number generator and the list of cells
 /// </summary>
 /// <param name="cells">The cells to use</param>
 /// <param name="rand">The random number generator</param>
 public Gene(Cell[][] cells, Random rand)
 {
     random = rand;
     this.cells = cells;
     this.init();
 }
Example #4
0
 private void generateCells()
 {
     cells = new Cell[cellsX][]; //Using jagged arrays here to improve extensibility
     for (int i = 0; i < cells.Length; i++)
     {
         cells[i] = new Cell[cellsY];
         for (int j = 0; j < cells[i].Length; j++)
         {
             int c = random.Next(7);
             cells[i][j] = new Cell(c, c);
         }
     }
 }
Example #5
0
        /// <summary>
        /// Checks if some shape matches at a given location. This is done by checking the next few rows and columns from the
        /// given location and checking they all match the shape.
        /// </summary>
        /// <param name="s">The shape to check</param>
        /// <param name="row">The row of the top left cell to look at</param>
        /// <param name="col">The column of the top left cell to look at</param>
        /// <returns>True if all cells match the given colours of the shape and false otherwise. The colour -1 is used to represent a wildcard</returns>
        private bool cellMatch(Shape s, int row, int col)
        {
            Cell[][] checkCells = new Cell[s.sizeRow()][];
            for (int i = 0; i < s.sizeRow(); i++)
            {
                checkCells[i] = new Cell[s.sizeCol()];
                for (int j = 0; j < s.sizeCol(); j++)
                {
                    checkCells[i][j] = cells[i + row][j + col];
                }
            }
            bool isMatch = true;
            int sizeCol = s.sizeCol();
            int sizeRow = s.sizeRow();

            for (int compCol = 0; compCol < sizeCol; compCol++)
            {
                for (int compRow = 0; compRow < sizeRow; compRow++)
                {
                    if(s.getColour(compRow, compCol) == -1)
                    {
                        //do nothing, -1 represents the wildcard
                    }
                    else if(s.getColour(compRow, compCol) == checkCells[compRow][compCol].getDomColour())
                    {
                        //do nothing, the cells match
                    }
                    else
                    {
                        isMatch = false; //Otherwise they don't match so the shape doesn't match at this location
                    }
                }
            }
            #if DEBUG
            if (isMatch)
            {
            }
            #endif
            return isMatch;
        }
Example #6
0
        /// <summary>
        /// The update method of the Simulation, calls the update method of the current state of the simulation
        /// </summary>
        /// <param name="gameTime">The time since update was last called</param>
        protected override void Update(GameTime gameTime)
        {
            #region testing
            #if CELLTEST
            int[][] colours = new int[6][];
            colours[0] = new int[] { 0, 0 }; //Check the same
            colours[1] = new int[] { 7, 7 }; //Check the same
            colours[2] = new int[] { 1, 3 }; //Check different
            colours[3] = new int[] { 3, 1 }; //Check different but the other way around
            colours[4] = new int[] { 7, 1 }; //Check different and wrapping
            colours[5] = new int[] { 1, 7 }; //Check different and wrapping but the other way around
            foreach(int[] i in colours)
            {
                Cell c = new Cell(i[0], i[1]);
                Console.WriteLine("C1: " + i[0] + " C2: " + i[1] + " DOM: " + c.getDomColour());
            }
            Environment.Exit(0);
            #endif
            #if PATTERNTEST
            //Code for checking various things
            Cell[][] c = new Cell[3][];
            c[0] = new Cell[] { new Cell(1, 1), new Cell(0, 0), new Cell(0, 0) };
            c[1] = new Cell[] { new Cell(0, 0), new Cell(1, 1), new Cell(0, 0) };
            c[2] = new Cell[] { new Cell(0, 0), new Cell(0, 0), new Cell(1, 1) };
            geneTest(new Gene(c));

            c[0] = new Cell[] { new Cell(1, 1), new Cell(0, 0), new Cell(0, 0) };
            c[1] = new Cell[] { new Cell(0, 0), new Cell(1, 1), new Cell(0, 0) };
            c[2] = new Cell[] { new Cell(0, 0), new Cell(0, 0), new Cell(0, 0) };
            geneTest(new Gene(c));

            c[0] = new Cell[] { new Cell(0, 0), new Cell(0, 0), new Cell(1, 1) };
            c[1] = new Cell[] { new Cell(0, 0), new Cell(1, 1), new Cell(0, 0) };
            c[2] = new Cell[] { new Cell(1, 1), new Cell(0, 0), new Cell(0, 0) };
            geneTest(new Gene(c));

            c[0] = new Cell[] { new Cell(1, 1), new Cell(0, 0), new Cell(1, 1) };
            c[1] = new Cell[] { new Cell(0, 0), new Cell(1, 1), new Cell(0, 0) };
            c[2] = new Cell[] { new Cell(1, 1), new Cell(0, 0), new Cell(1, 1) };
            geneTest(new Gene(c));

            c[0] = new Cell[] { new Cell(0, 0), new Cell(6, 6), new Cell(1, 1), new Cell(1, 1) };
            c[1] = new Cell[] { new Cell(1, 1), new Cell(0, 0), new Cell(6, 6), new Cell(1, 1) };
            c[2] = new Cell[] { new Cell(1, 1), new Cell(1, 1), new Cell(0, 0), new Cell(6, 6) };
            geneTest(new Gene(c));

            c[0] = new Cell[] { new Cell(0, 0), new Cell(0, 0), new Cell(0, 0) };
            c[1] = new Cell[] { new Cell(0, 0), new Cell(0, 0), new Cell(0, 0) };
            c[2] = new Cell[] { new Cell(0, 0), new Cell(0, 0), new Cell(1, 1) };

            Cell[][] rCells = new Cell[1][];
            rCells[0] = new Cell[] { new Cell(1, 1) };
            List<ParamToken> rMods = new List<ParamToken>();
            rMods.Add(ParamToken.STRENGTH);
            recognisedShapes = new List<Shape>();
            recognisedShapes.Add(new Shape(rCells, rMods, new List<ParamToken>()));

            geneTest(new Gene(c));

            Environment.Exit(0);
            #endif
            #if POLLTEST
            Cell[][] newGene = new Cell[10][];
            for (int colour = 0; colour < 7; colour++)
            {
                for (int i = 0; i < newGene.Length; i++)
                {
                    newGene[i] = new Cell[10];
                    for (int j = 0; j < newGene[i].Length; j++)
                    {
                        newGene[i][j] = new Cell(colour, colour);
                    }
                }
                creatureTest(newGene);
            }

            for (int i = 0; i < newGene.Length; i++)
            {
                newGene[i] = new Cell[10];
                for (int j = 0; j < newGene[i].Length; j++)
                {
                    newGene[i][j] = new Cell(0, 0);
                }
            }
            newGene[0][0] = new Cell(0, 0); newGene[0][1] = new Cell(0, 0); newGene[0][2] = new Cell(0, 0);
            newGene[1][0] = new Cell(0, 0); newGene[1][1] = new Cell(0, 0); newGene[1][2] = new Cell(0, 0);
            newGene[2][0] = new Cell(0, 0); newGene[2][1] = new Cell(0, 0); newGene[2][2] = new Cell(1, 1);
            creatureTest(newGene);

            newGene[0][0] = new Cell(0, 0); newGene[0][1] = new Cell(0, 0); newGene[0][2] = new Cell(0, 0);
            newGene[1][0] = new Cell(0, 0); newGene[1][1] = new Cell(2, 2); newGene[1][2] = new Cell(1, 1);
            newGene[2][0] = new Cell(1, 1); newGene[2][1] = new Cell(1, 1); newGene[2][2] = new Cell(1, 1);
            creatureTest(newGene);

            newGene[0][0] = new Cell(1, 1); newGene[0][1] = new Cell(0, 0); newGene[0][2] = new Cell(0, 0);
            newGene[1][0] = new Cell(0, 0); newGene[1][1] = new Cell(2, 2); newGene[1][2] = new Cell(1, 1);
            newGene[2][0] = new Cell(1, 1); newGene[2][1] = new Cell(1, 1); newGene[2][2] = new Cell(0, 0);
            creatureTest(newGene);

            Environment.Exit(0);
            #endif
            #if DIETTEST
            Cell[][] cc = new Cell[10][];
            Cell[][] ch = new Cell[10][];
            for (int i = 0; i < 10; i++)
            {
                cc[i] = new Cell[10];
                ch[i] = new Cell[10];
                for (int j = 0; j < 10; j++)
                {
                    cc[i][j] = new Cell(0, 0);
                    ch[i][j] = new Cell(6, 6);
                }
            }
            Creature carn = new Creature(new Gene(cc));
            Creature herb = new Creature(new Gene(ch));
            List<FoodSource> foodToSort = new List<FoodSource>();
            plantFoodValue = 1000;
            remainsFoodValue = 1000;
            foodToSort.Add(new Plant(new Random()));
            foodToSort.Add(new Remains(new Random()));

            FoodSource carnChoice = carn.pubGetMostNourishing(foodToSort);
            FoodSource herbChoice = herb.pubGetMostNourishing(foodToSort);

            Console.WriteLine("Diet: Carnivore : " + carn.getDiet() + " Herbivore: " + herb.getDiet());
            Console.WriteLine("Actual values: Plant: " + foodToSort[0].getFoodValue() + " Remains: " + foodToSort[1].getFoodValue());
            Console.WriteLine("Carnivore sees plant as: " + carn.pubGetNourishmentAmt(foodToSort[0]) + " and remains as " + carn.pubGetNourishmentAmt(foodToSort[1]));
            Console.WriteLine("Herbivore sees plant as: " + herb.pubGetNourishmentAmt(foodToSort[0]) + " and remains as " + herb.pubGetNourishmentAmt(foodToSort[1]));
            Console.WriteLine("Carnivore chooses meat: " + !carnChoice.isPlant());
            Console.WriteLine("Herbivore chooses plant: " + herbChoice.isPlant());
            Environment.Exit(0);
            #endif
            #if BREEDTEST
            Cell[][] newGene = new Cell[10][];
            for (int i = 0; i < newGene.Length; i++)
            {
                newGene[i] = new Cell[10];
                for (int j = 0; j < newGene[i].Length; j++)
                {
                    newGene[i][j] = new Cell(0, 0);
                }
            }
            Gene g = new Gene(newGene, new Random());
            for (int i = 0; i < 100; i++)
            {
                g.breedWith(g);
            }
            Environment.Exit(0);
            #endif
            #endregion
            base.Update(gameTime);
            state.update(gameTime);
        }
Example #7
0
 private void creatureTest(Cell[][] newGene)
 {
     Creature cret = new Creature(new Gene(newGene));
     Dictionary<Scenario, Response> beh = cret.getBehaviour();
     foreach (Scenario s in beh.Keys)
     {
         Console.WriteLine("Scen: " + s + " Resp: " + beh[s]);
     }
 }
Example #8
0
 public Shape(Cell[][] cells, List<ParamToken> posMods, List<ParamToken> negMods)
 {
     this.cells = cells;
     this.posMods = posMods;
     this.negMods = negMods;
 }