Example #1
0
        //function to create the greenfly randomly and store it in green fly list
        public void generateGreenFly()
        {
            numberOfInsects = 100;
            for (int i = 0; i < numberOfInsects; i++)
            {
                int randomX = generator.Next(0, gridWidth - 1);
                int randomY = generator.Next(0, gridHeight - 1);

                if (gameGrid[randomX, randomY] == null)
                {
                    GreenFly fly = new GreenFly(randomX, randomY);
                    gameGrid.SetValue("o", randomX, randomY);
                    greenFly.Add(fly);
                }
                else
                {
                    while (gameGrid[randomX, randomY] != null)
                    {
                        randomX = generator.Next(0, gridWidth - 1);
                        randomY = generator.Next(0, gridHeight - 1);
                    }
                    GreenFly fly = new GreenFly(randomX, randomY);
                    gameGrid.SetValue("o", randomX, randomY);
                    greenFly.Add(fly);
                }
            }
        }
Example #2
0
        //Breed green fly if it stayed alive for three moves
        public GreenFly BreedGreenFly(string[,] gameGrid)
        {
            GreenFly newGreenFly;

            if (Row + 1 >= 0 && Row + 1 < 20 && gameGrid[Row + 1, Column] == " ")
            {
                gameGrid[Row + 1, Column] = "o";
                newGreenFly = new GreenFly(Row + 1, Column);
                return(newGreenFly);
            }
            else if (Row - 1 >= 0 && Row - 1 < 20 && gameGrid[Row - 1, Column] == " ")
            {
                gameGrid[Row - 1, Column] = "o";
                newGreenFly = new GreenFly(Row - 1, Column);
                return(newGreenFly);
            }
            else if (Column + 1 >= 0 && Column + 1 < 20 && gameGrid[Row, Column + 1] == " ")
            {
                gameGrid[Row, Column + 1] = "o";
                newGreenFly = new GreenFly(Row, Column + 1);
                return(newGreenFly);
            }
            else if (Column - 1 >= 0 && Column - 1 < 20 && gameGrid[Row, Column - 1] == " ")
            {
                gameGrid[Row, Column - 1] = "o";
                newGreenFly = new GreenFly(Row, Column - 1);
                return(newGreenFly);
            }
            else
            {
                return(null);
            }
        }
Example #3
0
        //redraw the game and breed
        public void RedrawGame()
        {
            //kill lady bird if it didn't eat after the third move
            for (int i = 0; i < ladyBird.Count; i++)
            {
                LadyBird bird = ladyBird[i];
                if (bird.noEating == 3)
                {
                    ladyBird.Remove(bird);
                    gameGrid[bird.Row, bird.Column] = " ";
                }
            }

            //counter to get the lady bird count
            int ladybirdcounter = ladyBird.Count;

            for (int i = 0; i < ladybirdcounter; i++)
            {
                //move lady bird randomly
                LadyBird bird = ladyBird[i];
                bird.MoveLadyBird(gameGrid, greenFly);

                //call the breed function if the lady bird alive after 8 moves
                if (bird.lives == 8)
                {
                    LadyBird newLadyBird = bird.BreedLadyBird(gameGrid);
                    if (newLadyBird != null)
                    {
                        ladyBird.Add(newLadyBird);
                    }
                    bird.lives = 0;
                }
            }

            //move the green fly randomly based on the surrounded empty cells
            int flyCounter = greenFly.Count;

            for (int i = 0; i < flyCounter; i++)
            {
                //move green fly randomly
                GreenFly fly = greenFly[i];
                fly.MoveGreenFly(gameGrid);

                //breed green fly it its a live for three moves
                if (fly.lives == 3)
                {
                    GreenFly newfly = fly.BreedGreenFly(gameGrid);
                    if (newfly != null)
                    {
                        greenFly.Add(newfly);
                    }
                    fly.lives = 0;
                }
            }

            //redraw the cells
            for (int i = 0; i < gridHeight; i++)
            {
                for (int j = 0; j < gridWidth; j++)
                {
                    Console.Write(gameGrid[i, j]);
                    if (j == gridWidth - 1)
                    {
                        Console.WriteLine("\r");
                    }
                }
            }

            Console.WriteLine("Press Enter to move the cells or Escape to close the game");
            Console.WriteLine("Green Fly = " + greenFly.Count + " .");
            Console.WriteLine("Lady Birds = " + ladyBird.Count + " .");
            Console.WriteLine("Steps = " + countSteps++);
        }