示例#1
0
        public void Breed(Cell[,] grid, List <LadyBird> ladyBirds)
        {
            //This method handle the breed part for ladybird, it has two main parts:

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

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

            int[] breedCell;

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

                // Checks if the cell exist within the grid (row and column)
                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)
            {
                int randBreedCell = rand.Next(0, possibleCells.Count); // Min = 0 ||| Maxpossible = 0 or 1 or 2 or 3.
                breedCell = possibleCells[randBreedCell];

                LadyBird nextLb = new LadyBird(breedCell[0], breedCell[1]);
                grid[nextLb.row, nextLb.column].content = nextLb.shape;
                ladyBirds.Add(nextLb);
            }
        }
示例#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);
                }
            }
        }
示例#3
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.
        }