Esempio n. 1
0
        public void Breed(Cell[,] grid, List <GreenFly> greenFlies)
        {
            //This method handle the breed part for greenfly, it has two main parts:

            //[1] (for loop) it checks the four cells (up, down, right, left) close to the greenfly and add empty cells to the list.
            //[2] (if part) if empty cells were found, a random cell is chosen to create a new greenfly in it.

            List <int[]> possibleCells = new List <int[]>();

            int[] breedCell;

            for (int i = 0; i < 4; i++) //[1]
            {
                breedCell = GetNeighbourCoor(i, row, column);

                // Checks if the cell exist within the grid.
                if ((breedCell[0] < grid.GetLength(0) && breedCell[0] >= 0) && (breedCell[1] < grid.GetLength(1) && breedCell[1] >= 0))
                {
                    if (grid[breedCell[0], breedCell[1]].content == ' ')
                    {
                        possibleCells.Add(breedCell);
                    }
                }
            }

            if (possibleCells.Count > 0)                               // [2]
            {
                int randBreedCell = rand.Next(0, possibleCells.Count); // Min = 0 || Maxpossible = 0 or 1 or 2 or 3.
                breedCell = possibleCells[randBreedCell];

                GreenFly nextGf = new GreenFly(breedCell[0], breedCell[1]);
                grid[nextGf.row, nextGf.column].content = nextGf.shape;
                greenFlies.Add(nextGf);
            }
        }
Esempio n. 2
0
        private void ChangeLadybirds() // This method Move and Breed Ladybirds.
        {
            // We iterate starting at the end of the list for two reasons:
            // 1.If a ladybird is removed, we don't want to try an access an element that doesn't exist.
            // 2.When new ladybirds are added we won't loop over them until next turn.

            for (int L = ladybirds.Count - 1; L >= 0; L--)
            {
                LadyBird currentLb = ladybirds[L];
                currentLb.lifeTime++;

                if (currentLb.notEating == 3) // Checking if the ladybird has starved.
                {
                    cells[currentLb.row, currentLb.column].content = ' ';
                    ladybirds.Remove(currentLb);
                    continue;
                }

                // MOVE Part for LadyBirds.
                currentLb.Move(cells);

                if (currentLb.eat)
                {
                    GreenFly.KillGreenFly(greenflies, currentLb.row, currentLb.column);
                }

                // BREED Part For LadyBirds.
                if (currentLb.lifeTime != 0 && currentLb.lifeTime % 8 == 0)
                {
                    currentLb.Breed(cells, ladybirds);
                }
            }
        }
Esempio n. 3
0
        private void ChangeGreenflies() // This method Move and Breed GreenFlies.
        {
            // Storing the current number of greenflies so we don't loop over new greenflies until next turn.
            int gfNums = greenflies.Count;

            for (int G = 0; G < gfNums; G++)
            {
                GreenFly currentGf = greenflies[G];
                currentGf.lifeTime++;

                // MOVE PART For GreenFlies.
                currentGf.Move(cells);

                // BREED PART For GreenFlies.
                if (currentGf.lifeTime != 0 && currentGf.lifeTime % 3 == 0)
                {
                    currentGf.Breed(cells, greenflies);
                }
            }
        }
Esempio n. 4
0
        public void Generate() // This method Create the world each turn.
        {
            /// The method generate the world, it has two parts:
            /// 1. When the world is empty (else part).
            /// 2. When the world has been created (if part) the method will call other methods to make the necessary changes.

            if (cells != null) // [2]
            {
                ChangeLadybirds();
                ChangeGreenflies();
                graphValues.Add(timeStep, new int[] { greenflies.Count, ladybirds.Count });

                continu &= (ladybirds.Count != 0 && greenflies.Count != 0); // Checking if the number of greenflies or ladybirds has reached 0.
            }

            else //[1]
            {
                timeStep = 0;

                graphValues.Add(timeStep, new int[] { startGreenflyNums, startLadybirdNums });
                cells = new Cell[length, width];

                for (int i = 0; i < cells.GetLength(0); i++)
                {
                    for (int j = 0; j < cells.GetLength(1); j++)
                    {
                        cells[i, j] = new Cell(i, j);
                    }
                }

                // GreenFlies and Ladybirds are created randomly throughout the grid.

                Random rand = new Random();
                for (int L = 0; L < startLadybirdNums; L++) // Creating Ladybirds.
                {
                    int randX = rand.Next(0, length);
                    int randY = rand.Next(0, width);

                    if (CellIsEmpty(randX, randY))
                    {
                        LadyBird ladyBird = new LadyBird(randX, randY);
                        ladybirds.Add(ladyBird);
                        cells[randX, randY].content = ladyBird.shape;
                    }
                    else
                    {
                        while (!CellIsEmpty(randX, randY)) // keep looping until en empty cell is found.
                        {
                            randX = rand.Next(0, length);
                            randY = rand.Next(0, width);
                        }
                        LadyBird ladyBird = new LadyBird(randX, randY);
                        ladybirds.Add(ladyBird);
                        cells[randX, randY].content = ladyBird.shape;
                    }
                }

                for (int G = 0; G < startGreenflyNums; G++) //Creating Greenflies.
                {
                    int randX = rand.Next(0, length);
                    int randY = rand.Next(0, width);

                    if (CellIsEmpty(randX, randY))
                    {
                        GreenFly greenfly = new GreenFly(randX, randY);
                        greenflies.Add(greenfly);
                        cells[randX, randY].content = greenfly.shape;
                    }
                    else
                    {
                        while (!CellIsEmpty(randX, randY))
                        {
                            randX = rand.Next(0, length);
                            randY = rand.Next(0, width);
                        }
                        GreenFly greenfly = new GreenFly(randX, randY);
                        greenflies.Add(greenfly);
                        cells[randX, randY].content = greenfly.shape;
                    }
                }
            }

            Information(); // Update the information.
        }