so i think i know where the problem is...

it return garbage for code_pos even though i initalize and change values assigned to it. :( Please help.


but don't know how to fix it.

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 i=0; i<populationsize_; ++i)
	{
		*(code_pos+i)=(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;
			}
		}
		std::cout<<std::endl;
	}
}

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_[i]++;
					}
            }
        }
		std::cout<<std::endl;
	}
}
 

 
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<<std::endl;*/
		//std::cout<<"|";
		for(int m=0; m<populationsize_; m++)
		{
			std::cout<<code_pos[i][m];
		}
		std::cout<<std::endl;
		/*std::cout<<"|";
		std::cout<<totalnumberofblackpegs_[i];
		std::cout<<"|";
		std::cout<<totalnumberofwhitepegs_[i];
		std::cout<<"|";
		std::cout<<totalnumberofpegs_[i];
		std::cout<<"|";
		std::cout<<std::endl;*/
 
	}
}
 















/*void Mastermind::mutate()
{
 
}
 
void Mastermind::crossover()
{
 
}
 
void Mastermind::calculatepopulationfitness()
{
	//found fitness function online
	for(int k=0; k<populationsize_; k++)
	{
		fitness_[k]=((2*totalnumberofblackpegs_[k])+totalnumberofwhitepegs_[k])+sum(k);
	}
	for(int k=0; k<populationsize_; k++)
	{
		std::cout<<fitness_[k]<<std::endl;
	}
}
 
int Mastermind::evaluatepopulation()
{
	int mxm = fitness_[0];
	for (int i=0; i<populationsize_; i++) 
	{
		if (fitness_[i]>mxm) 
		{
			mxm = fitness_[i];
		}
	}
	return mxm;
}
 
int Mastermind::sum(int index)
{
	int sum=0;
	for(int i=1; i<totalnumberofpegs_[index]-1; i++)
	{
		sum=sum+i;
	}
	return sum;
}
 
int Mastermind::pickrandomposition()
{
	int d=rand()% 3;
	return d;
}*/

nvm figured it out. went to far in array index.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.