//function to generateb lady bird randomly and store it in it's list public void generateLadyBird() { numberOfInsects = 5; 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) { LadyBird bird = new LadyBird(randomX, randomY); gameGrid.SetValue("x", randomX, randomY); ladyBird.Add(bird); } else { while (gameGrid[randomX, randomY] != null) { randomX = generator.Next(0, gridWidth - 1); randomY = generator.Next(0, gridHeight - 1); } LadyBird bird = new LadyBird(randomX, randomY); gameGrid.SetValue("x", randomX, randomY); ladyBird.Add(bird); } } }
//breed the lady bird if it is alive for 8 moves public LadyBird BreedLadyBird(string[,] gameGrid) { LadyBird newLadyBird; //check the cells around lady bird and create a new lady bird in the empty one if (Row + 1 >= 0 && Row + 1 < 20 && gameGrid[Row + 1, Column] == " ") { gameGrid[Row + 1, Column] = "x"; newLadyBird = new LadyBird(Row + 1, Column); return(newLadyBird); } else if (Row - 1 >= 0 && Row - 1 < 20 && gameGrid[Row - 1, Column] == " ") { gameGrid[Row - 1, Column] = "x"; newLadyBird = new LadyBird(Row - 1, Column); return(newLadyBird); } else if (Column + 1 >= 0 && Column + 1 < 20 && gameGrid[Row, Column + 1] == " ") { gameGrid[Row, Column + 1] = "x"; newLadyBird = new LadyBird(Row, Column + 1); return(newLadyBird); } else if (Column - 1 >= 0 && Column - 1 < 20 && gameGrid[Row, Column - 1] == " ") { gameGrid[Row, Column - 1] = "x"; newLadyBird = new LadyBird(Row, Column - 1); return(newLadyBird); } else { return(null); } }
//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++); }