# The Genetic Algorithm Game Bug Solved!

Simon’s version of the Citius Invaders game in Processing is finally working! The big bug that seemed so unsolvable (enemies endlessly crossing over) has been defeated!

Siraj Raval presented this game in Python during Week 9 of his “Math of Intelligence” course. Simon’s is a simplified version that still incorporates a genetic algorithm.

Link to our previous post on this project: https://antwerpenhomeschooling.wordpress.com/2017/11/23/problem-with-a-genetic-algorithm-game/

# Live Stream 30 November. Speechjs and Citius Invaders.

Below are the archived versions of Simon’s live streams today. The second part (starting with a genetic algorithm game in Processing/ p5.js) was an emotional roller-coaster, with the OBS software failing to capture sound and giving Simon a hard time jumping between scenes, and yet he didn’t give up.

Simon writes: In the first part of this live session, I redid my speechsynth.js tutorial (so it has some coding in it). In the second part (which will likely be WAY longer), I will do a Processing/p5 challenge about Citius Invaders.

# Problem with a Genetic Algorithm Game

Simon has been working very hard at making his version of the Citius Invaders game that Siraj Raval presented in Python during Week 9 of his “Math of Intelligence” course. It’s a simplified version that still incorporates a genetic algorithm. Simon’s plan is to code the same game in P5 during his next live tutorial (at 17:00 CET on 30 November). Unfortunately, after many lines of code in several files, when the game was nearly ready, he faced a problem he doesn’t know how to resolve. He tried getting some online help via the Coding Train Slack channel but didn’t get much feedback. The code is now on github at: https://github.com/simon-tiger/citius-invaders/

Here is how Simon describes the problem:

Hi! I’m working on a “Citius Invaders” game, and I have a problem.
In the game, there are enemies, a spaceship, and bullets that it shoots.
The parameters of the game are:
* The game starts with 6 enemies.
* The minimal amount of enemies is 4.
* The maximal amount of enemies is 100.
The rules of the game are:
* When a bullet collides with an enemy, the bullet and the enemy get deleted.
* If there are 4 enemies on the screen, you’re not allowed to shoot any bullets any more.
* If there are 100 enemies on the screen, you’ve lost the game.
* The enemies reproduce every 5 seconds (this amount of time decreases every generation).
The goal of the game is to stay as close to 4 enemies as possible.
The problem is that: After the 5 seconds, the enemies don’t stop crossing over! I think the order that I’m crossing them over is wrong. They breed (cross over) forever and that makes the game pause forever. Maybe the algorithm in which I let them cross over is wrong. I have a `while` loop somewhere, maybe it became a forever loop. The `while` loop is at line 52 in `Population.pde`:

``` while (!selectedEnemies1.isEmpty() || !selectedEnemies2.isEmpty()) {
int index1 = int(random(selectedEnemies1.size()));
int index2 = int(random(selectedEnemies2.size()));
if (index1 == index2) {
index2 = (index2 + 1) % selectedEnemies2.size();
}```

Below are a some videos showing Simon at different stages of the project. In the top video, he speaks of the difficulties he encountered with the frame rate and the genetic algorithm.