# Thread: Using the genetic algorithm

1. ## Using the genetic algorithm

Hi! I'm looking for some help on how to create a game in C++ (so I'm not sure if I should post in this board or the Game Programming board) so that a user inputs a six digit number composed entirely of 1s and 0s and the computer attempts to guess the number through the genetic algorithm.

I have some thoughts on how I might go about this, but my mind is complete mush at the moment from thinking about it far too much today.

What I have:
* Instructions for what the user has to do.
* An initial population of five randomly selected numbers. (Which is really a combination of 6 1s or 0s stringed together to make it a 6 digit number.)
* A rudimentary crossover, but I'm not sure if this is the correct way to go about it.

Please see the attached CPP file. I use the Bloodshed DEV-C++ compiler.

1. Learn how to indent code. Yours is mostly on the left hand side, with a few random lines (nothing to do with the flow of the program) indented one level.

2. Closing braces should be on a line by themselves, so
computer_sequence[4] = temp_sequence;}
Would be
Code:
```    computer_sequence[4] = temp_sequence;
}```
If you're ever in the unfortunate position of trying to deal with a mis-placed brace, then having them hiding at the ends of lines will just make it that much harder.

3. Don't be afraid to put in some blank lines, say between the variables and the first statement, or between separate functional blocks within a function.

4. All your _first, _second etc variables are screaming to be an array.

5. For something supposed to be implementing a GA, I would have expected to see a couple of functions called say 'evolve', where two parent 'genes' are combined to produce the next generation, and a 'mutate' which occasionally makes a random change to the gene. I would also like to have seen a 'survives' function which tests the fitness of any single gene to make it into the next generation.

Your code, c/o indent -kr -nut -ts2 foo.cpp
Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
srand(time(0));
int user_sequence;
int user_first;
int user_second;
int user_third;
int user_fourth;
int user_fifth;
int user_sixth;
int computer_sequence[5];
int computer_first[5];
int computer_second[5];
int computer_third[5];
int computer_fourth[5];
int computer_fifth[5];
int computer_sixth[5];
int fitness[5];
int iteration;
int temp_fitness;
int temp_sequence;
int random;
double split;
int post_split_beginning;
int post_split_end;
int post_split_fitness;
int post_split_first;
int post_split_second;
int post_split_third;
int post_split_fourth;
int post_split_fifth;
int post_split_sixth;
int post_split_sequence;

cout << "Instructions:" << endl <<
"The program will prompt for a sequence of six numbers (0 or 1) and then"
<< endl <<
"the program will use the genetic algorithm to determine the sequence."
<< endl << endl;
cout << "Enter a sequence: ";
cin >> user_sequence;

user_first = user_sequence / 100000;
user_second = user_sequence / 10000 &#37; 10;
user_third = user_sequence / 1000 % 10;
user_fourth = user_sequence / 100 % 10;
user_fifth = user_sequence / 10 % 10;
user_sixth = user_sequence % 10;

iteration = 0;
do {
fitness[iteration] = 0;
computer_first[iteration] = rand() % 2;
computer_second[iteration] = rand() % 2;
computer_third[iteration] = rand() % 2;
computer_fourth[iteration] = rand() % 2;
computer_fifth[iteration] = rand() % 2;
computer_sixth[iteration] = rand() % 2;
if (user_first == computer_first[iteration])
fitness[iteration] = fitness[iteration] + 1;
if (user_second == computer_second[iteration])
fitness[iteration] = fitness[iteration] + 1;
if (user_third == computer_third[iteration])
fitness[iteration] = fitness[iteration] + 1;
if (user_fourth == computer_fourth[iteration])
fitness[iteration] = fitness[iteration] + 1;
if (user_fifth == computer_fifth[iteration])
fitness[iteration] = fitness[iteration] + 1;
if (user_sixth == computer_sixth[iteration])
fitness[iteration] = fitness[iteration] + 1;
computer_sequence[iteration] =
computer_first[iteration] * 100000 +
computer_second[iteration] * 10000 +
computer_third[iteration] * 1000 +
computer_fourth[iteration] * 100 +
computer_fifth[iteration] * 10 + computer_sixth[iteration];
cout << "Computer sequence: " << setfill('0') << setw(6) <<
computer_sequence[iteration] << endl;
cout << "Fitness score: " << fitness[iteration] << endl;
if (fitness[iteration] == 6) {
system("pause");
return 0;
}
iteration = iteration + 1;
} while (iteration < 5);

if (fitness[0] > fitness[1])
{
temp_fitness = fitness[0];
fitness[0] = fitness[1];
fitness[1] = temp_fitness;
temp_sequence = computer_sequence[0];
computer_sequence[0] = computer_sequence[1];
computer_sequence[1] = temp_sequence;
}
if (fitness[1] > fitness[2])
{
temp_fitness = fitness[1];
fitness[1] = fitness[2];
fitness[2] = temp_fitness;
temp_sequence = computer_sequence[1];
computer_sequence[1] = computer_sequence[2];
computer_sequence[2] = temp_sequence;
}
if (fitness[2] > fitness[3])
{
temp_fitness = fitness[2];
fitness[2] = fitness[3];
fitness[3] = temp_fitness;
temp_sequence = computer_sequence[2];
computer_sequence[2] = computer_sequence[3];
computer_sequence[3] = temp_sequence;
}
if (fitness[3] > fitness[4])
{
temp_fitness = fitness[3];
fitness[3] = fitness[4];
fitness[4] = temp_fitness;
temp_sequence = computer_sequence[3];
computer_sequence[3] = computer_sequence[4];
computer_sequence[4] = temp_sequence;
}

cout << setw(6) << computer_sequence[3] << "  " << setw(6) <<
computer_sequence[4] << endl;
random = rand() % 5 + 1;
split = pow(10.0, random);
cout << random << "  " << split << endl;
post_split_beginning = 0;
post_split_end = 0;
post_split_first = 0;
post_split_second = 0;
post_split_third = 0;
post_split_fourth = 0;
post_split_fifth = 0;
post_split_sixth = 0;
post_split_fitness = 0;
post_split_beginning = computer_sequence[3] / int (split);
post_split_end = computer_sequence[4] % int (split);
post_split_sequence =
post_split_beginning * int (split) + post_split_end;
post_split_first = post_split_sequence / 100000 % 10;
post_split_second = post_split_sequence / 10000 % 10;
post_split_third = post_split_sequence / 1000 % 10;
post_split_fourth = post_split_sequence / 100 % 10;
post_split_fifth = post_split_sequence / 10 % 10;
post_split_sixth = post_split_sequence % 10;

if (user_first == post_split_first)
post_split_fitness = post_split_fitness + 1;
if (user_second == post_split_second)
post_split_fitness = post_split_fitness + 1;
if (user_third == post_split_third)
post_split_fitness = post_split_fitness + 1;
if (user_fourth == post_split_fourth)
post_split_fitness = post_split_fitness + 1;
if (user_fifth == post_split_fifth)
post_split_fitness = post_split_fitness + 1;
if (user_sixth == post_split_sixth)
post_split_fitness = post_split_fitness + 1;
cout << "Computer sequence: " << setw(6) << post_split_sequence <<
endl << "Fitness score: " << post_split_fitness << endl;

if (post_split_fitness == 6) {
system("pause");
return 0;
};
if (post_split_fitness != 6) {
cout << "Still work to do.";
};

system("pause");
return 0;
}```

3. Thanks for the suggestions, Salem. I worked with what I remembered from my old C++ class, but obviously I forgot some things along the way.

Are you saying arrays can have two indexes? Or subarrays? I think that's what I'm trying to say, you were alluding to the fact that
Code:
```        computer_first[iteration] = rand() &#37; 2;
computer_second[iteration] = rand() % 2;
computer_third[iteration] = rand() % 2;
computer_fourth[iteration] = rand() % 2;
computer_fifth[iteration] = rand() % 2;
computer_sixth[iteration] = rand() % 2;```
could be represented with an array I think?

4. Say
int computer[MAX_ITERATIONS][MAX_GENES];