Need to calculate number of white pegs so I can write fitness function. It works sometimes and it doesn't work other times.
I got the calculate number of black pegs to work.
A black peg is correct position and color.
A white peg is correct color wrong position.
Colors are numbers in my program.
Here is all my code so far.
Any help appreciated.
main.cpp
#include <iostream>
#include <ctime>
#include "Mastermind.h"
using namespace std;
int main()
{
int* answer=new int[4];
int populationsize=0;
srand((unsigned)time(NULL));
cout<<"please select 4 numbers out of the following options";
cout<<"\nred:0\n"
"black:1\n"
"yellow:2\n"
"white:3\n"
"blue:4\n"
"green:5\n";
for(int i=0; i<4; i++)
{
cin>>answer[i];
}
Mastermind a;
a.setanswer(answer);
cout<<"please enter a population size";
cin>>populationsize;
a.setpopulationsize(populationsize);
a.initializepopulation();
cout<<std::endl;
a.calculatenumberofblackpegs();
a.calculatenumberofwhitepegs();
//a.calculatenumberofwhitepegs();
//a.evaluatepopulation();
//while(populationfitness
return 0;
}
Mastermind.h
#include <iostream>
class Mastermind
{
public:
Mastermind();
void initializepopulation();
void evaluatepopulation();
void setpopulationsize(int populationsize);
void setanswer(int* answer);
void setgenerationnumber(int generationnumber);
int randomizenumber();
void calculatenumberofblackpegs();
void calculatenumberofwhitepegs();
void totalnumberofpegs();
void mutate();
void crossover();
int calculatepopulationfitness();
private:
int** population_;
int populationsize_;
int* answer_;
int generationnumber_;
int* totalnumberofblackpegs_;
int* totalnumberofwhitepegs_;
int* totalnumberofpegs_;
int** temp;
int* fitness_;
};
Mastermind.cpp
#include <cstdlib>
#include "Mastermind.h"
Mastermind::Mastermind()
{
populationsize_=0;
answer_=new int[4];
generationnumber_=0;
totalnumberofblackpegs_=new int[populationsize_];
totalnumberofwhitepegs_=new int[populationsize_];
totalnumberofpegs_=new int[populationsize_];
fitness_=new int[populationsize_];
}
void Mastermind::initializepopulation()
{
population_=(int**)(malloc(sizeof(int*)*populationsize_));
for(int i=0; i<populationsize_; ++i)
{
*(population_+i)=(int*)(malloc(sizeof(int)*4));
}
for(int i=0; i<populationsize_; i++)
{
std::cout<<i<<std::endl;
for(int j=0; j<4; j++)
{
population_[i][j]=randomizenumber();
std::cout<<population_[i][j];
}
std::cout<<"\n";
}
}
void Mastermind::setpopulationsize(int populationsize)
{
populationsize_=populationsize;
}
void Mastermind::setanswer(int* answer)
{
answer_=answer;
}
void Mastermind::setgenerationnumber(int generationnumber)
{
generationnumber_=generationnumber;
}
int Mastermind::randomizenumber()
{
int d=rand()% 6;
return d;
}
void Mastermind::calculatenumberofblackpegs()
{
for(int k=0; k<populationsize_; k++)
{
totalnumberofblackpegs_[k]=0;
totalnumberofwhitepegs_[k]=0;
totalnumberofpegs_[k]=0;
}
temp=(int**)(malloc(sizeof(int*)*populationsize_));
for(int i=0; i<populationsize_; ++i)
{
*(temp+i)=(int*)(malloc(sizeof(int)*4));
}
//initalize temp
for(int i=0; i<populationsize_; ++i)
{
for(int j=0; j<4; ++j)
{
temp[i][j]=6;
}
}
for(int i=0; i<populationsize_; i++)
{
for(int j=0; j<4; j++)
{
//position matches and correct color
if(population_[i][j]==answer_[j])
{
totalnumberofblackpegs_[i]++;
}
else
{
totalnumberofblackpegs_=totalnumberofblackpegs_;
temp[i][j]=population_[i][j];
}
}
std::cout<<i<<"::"<<totalnumberofblackpegs_[i]<<"\n";
}
}
void Mastermind::calculatenumberofwhitepegs()
{
//correct colored peg placed in wrong position
//start with first item in array and check against the temp array
for(int k=0; k<populationsize_; k++)
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(temp[k][j]==6)
{
//std::cout<<"do nothing\n";
}
else if(answer_[i]==temp[k][j])
{
totalnumberofwhitepegs_[i]++;
}
else
{
totalnumberofwhitepegs_[i]=totalnumberofwhitepegs_[i];
}
}
}
std::cout<<k<<"::"<<totalnumberofwhitepegs_[k]<<"\n";
}
}
void Mastermind::totalnumberofpegs()
{
for(int i=0; i<populationsize_; i++)
{
totalnumberofpegs_[i]=totalnumberofwhitepegs_[i]+totalnumberofblackpegs_[i];
std::cout<<"total number is"<<totalnumberofpegs_[i]<<std::endl;
}
}
void Mastermind::mutate()
{
}
void crossover()
{
}
/*int calculatepopulationfitness()
{
}*/