starting to think that memory isn't big enough.
the totalnumberofpegs
i only set it to 0 in my code yet it changes it self somehow :(. please help
i have tried everything. It shows that it is 0 in all other functions.
main.cpp
#include <iostream>
#include <ctime>
#include <stdio.h>
#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();
a.calculatenumberofblackpegs();
a.calculatenumberofwhitepegs();
a.print();
return 0;
}
mastermind.h
#include <iostream>
#include <cstdlib>
#define RIGHT_COLOR_RIGHT_POS 1
#define RIGHT_COLOR_WRONG_POS 2
#define WRONG_COLOR_WRONG_POS 3
class Mastermind
{
public:
Mastermind();
void initializepopulation();
void setpopulationsize(int populationsize);
void setanswer(int* answer);
void setgenerationnumber(int generationnumber);
int randomizenumber();
void calculatenumberofblackpegs();
void calculatenumberofwhitepegs();
void mutate();
void crossover();
void calculatepopulationfitness();
int evaluatepopulation();
void print();
private:
int** population_;
int populationsize_;
int* answer_;
int generationnumber_;
int* totalnumberofblackpegs_;
int* totalnumberofwhitepegs_;
int* totalnumberofpegs_;
int* fitness_;
int sum(int index);
int pickrandomposition();
int** guess_pos;
int** code_pos;
};
mastermind.cpp
#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++)
{
for(int j=0; j<4; j++)
{
population_[i][j]=randomizenumber();
}
}
}
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()
{
guess_pos=(int**)(malloc(sizeof(int*)*populationsize_));
for(int i=0; i<populationsize_; ++i)
{
*(guess_pos+i)=(int*)(malloc(sizeof(int)*4));
}
code_pos=(int**)(malloc(sizeof(int*)*populationsize_));
for(int m=0; m<populationsize_; ++m)
{
*(code_pos+m)=(int*)(malloc(sizeof(int)*4));
}
//intialize arrays
for(int i=0; i<populationsize_; i++)
{
totalnumberofblackpegs_[i]=0;
totalnumberofwhitepegs_[i]=0;
totalnumberofpegs_[i]=0;
for(int p=0; p<4; p++)
{
guess_pos[i][p]=3;
code_pos[i][p]=3;
}
}
for(int i=0; i<populationsize_; i++)
{
for(int j=0; j<4; j++)
{
if(answer_[j]==population_[i][j])
{
totalnumberofblackpegs_[i]++;
guess_pos[i][j]=1;
code_pos[i][j]=1;
}
else
{
totalnumberofblackpegs_[i]=totalnumberofblackpegs_[i];
}
}
}
}
void Mastermind::calculatenumberofwhitepegs()
{
for(int k=0; k<populationsize_; k++)
{
for (int i=0; i <4; i++)
{
for (int j=0; j <4; j++)
{
if (population_[k][i] == answer_[j] &&
code_pos[k][j] == 3 &&
guess_pos[k][i] == 3)
{
guess_pos[k][i]=2;
code_pos[k][j]=2;
totalnumberofwhitepegs_[k]++;
}
else
{
totalnumberofwhitepegs_[k]=totalnumberofwhitepegs_[k];
}
}
}
}
}
void Mastermind::print()
{
for(int i=0; i<populationsize_; i++)
{
//print individual
for(int j=0; j<4; j++)
{
std::cout<<population_[i][j];
}
std::cout<<"|";
for(int k=0; k<4; k++)
{
std::cout<<guess_pos[i][k];
}
std::cout<<"|";
for(int m=0; m<4; m++)
{
std::cout<<code_pos[i][m];
}
std::cout<<"|";
std::cout<<totalnumberofblackpegs_[i];
std::cout<<"|";
std::cout<<totalnumberofwhitepegs_[i];
std::cout<<"|";
std::cout<<totalnumberofpegs_[i];
std::cout<<"|";
std::cout<<std::endl;
}
}