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()
{
	
}*/

Recommended Answers

All 23 Replies

For example:
if i enter 1232
and population size is 5. randomly generate following population. the temp changes number to 6 which match in position and value. Please help.

population Black Temp White ExpectedWhite
5024| 0| 5024| 0| 1
5210| 1| 5610| 1| 1
5551| 0| 5551| 0| 1
5143| 0| 5143| 1| 2
2455| 0| 2455| 0| 1

1) Count the numbers that occur in both arrays -- correct pegs.
2) Count the numbers that occur in the correct position in the arrays -- black pegs.
3) Subtract the black pegs from the correct pegs -- white pegs

Answer: 2658
Guess: 6625

Correct: 3
Black: 1

Therefore-
White: 2

i am unsure of how to do the first part.

Here's what I have.
Not sure why it doesn't work. Any help appreciated.

void Mastermind::totalnumberofpegs()
{
	for(int k=0; k<populationsize_; k++)
	{
		for(int i=0; i<4; i++)
		{
			for(int j=0; j<4; j++)
			{
				if(answer_[i]==population_[k][j])
				{
					totalnumberofpegs_[i]++;
				}
				else
				{
					totalnumberofpegs_[i]=totalnumberofwhitepegs_[i];
				}
			}
		}
		std::cout<<k<<"::"<<totalnumberofpegs_[k]<<"\n";
	}
}

So i found one of my earlier codes which takes two files and find the elements in common and not in common so i was going to do the same thing but with arrays. It gives me some run time error and I am unsure why. Thanks for the help. The debug error is in the second getnextitem().

void Mastermind::totalnumberofpegs()
{
	for(int k=0; k<populationsize_; k++)
	{
		int i=0;
		int j=0;
		int moreitems;
		//answer array
		int list1=getnextitem(i);
		//population array
		int list2=getnextitem(k,j);
		moreitems=list1&&list2;
		while(moreitems)
		{
			if(list1<list2)
			{
				list1=getnextitem(i++);
				totalnumberofpegs_[k]=totalnumberofpegs_[k];
			}
			else if(list1==list2)
			{
				list1=getnextitem(i++);
				list2=getnextitem(k,j++);
				totalnumberofpegs_[k]++;
			}
			else
			{
				list2=getnextitem(k,j++);
				totalnumberofpegs_[k]=totalnumberofpegs_[k];
			}
		}
		std::cout<<k<<"::"<<totalnumberofpegs_[k]<<"\n";
	}
}

int Mastermind::getnextitem(int index)
{
	return answer_[index];
}

int Mastermind::getnextitem(int pop,int index)
{
	return population_[pop][index];
}

i am baiscally trying to find the intersection of two arrays but keeping duplicates? does that make sense?

I think i finally got it!!
I put both of them in one function.
Anyone check whether this is correct.
Thanks

void Mastermind::calculatenumberofwhiteandblackpegs()
{
	for(int k=0; k<populationsize_; k++)
	{
		totalnumberofblackpegs_[k]=0;
		totalnumberofwhitepegs_[k]=0;
		totalnumberofpegs_[k]=0;
	}
	int flag[4];
	//correct colored peg placed in wrong position
	//start with first item in array and check against the temp array
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			flag[j]=0;
			if(answer_[j]==population_[i][j])
			{
				totalnumberofblackpegs_[i]++;
			}
			else
			{
				for(int s=0; s<4; s++)
				{
					if(s!=j && population_[i][j]==answer_[s]&&!flag[s])
					{
						totalnumberofwhitepegs_[i]++;
						flag[s]=1;
						break;
					}
				}
			}
		}
		std::cout<<"total number of black:"<<totalnumberofblackpegs_[i]
		<<"total number of white:"<<totalnumberofwhitepegs_[i]<<std::endl;
	}
}

nope that doesn't work. For example the case 1221. gets you 2 and 2 when it should be 2 and 0.

You don't need to complicate it so much.

Loop 1 through the answer array i=0-3
    Loop 2 through the input array j=0-3
        if answer[i]=user[j]
                negate user[j] (flag as seen)
                increment count
                exit loop 2
    next j
next i

i am really confused now.... so my answer would store 4 numbers and then input would store 4 numbers. But why am I comparing answer at index to user where user is 0 or 1. Should the condition be

if(answer[i]==input[j])?

Thanks.

What exactly is this calculating?? I am really confused now.

I don't think this is right.

for(int m=0; m<populationsize_; m++)
	{
		totalnumberofpegs_[m]=0;
	}
	int flag[4];
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			flag[j]=0;
			for(int k=0; k<4; k++)
			{
				if(answer_[j]==population_[i][k])
				{
					flag[j]=1;
					totalnumberofpegs_[i]++;
				}
			}
		}
			std::cout<<totalnumberofpegs_[i]<<std::endl;
	}

So I think I MADE SOME PROGRESS:
THIS IS MOST RECENT ONE THERE ARE STILL SOME GLICHES... Disregard earlier posts
PLEASE HELP.
Take a look at function calculatenumberofwhitepegs() and calculatenumberofblackpegs(). That's the part where I am stuck. :( I have spent 40 hours on this.... think its about to call it quits..

main

#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();
	a.calculatenumberofblackpegs();
	cout<<"white pegs\n";
	a.calculatenumberofwhitepegs();
	return 0;
}

Mastermind.h

Mastermind.cpp

#include <cstdlib>
#include "Mastermind.h"
#include <iostream>

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()
{
	temp_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(temp_+i)=(int*)(malloc(sizeof(int)*4));
	}
	indtemp_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(indtemp_+i)=(int*)(malloc(sizeof(int)*4));
	}
	for(int i=0; i<populationsize_; i++)
	{
		totalnumberofblackpegs_[i]=0;
		totalnumberofwhitepegs_[i]=0;
		totalnumberofpegs_[i]=0;
		for(int j=0; j<4; j++)
		{
			//set indtemp and temp to 0
			indtemp_[i][j]=0;
			temp_[i][j]=0;
			if(answer_[j]==population_[i][j])
			{
				//get rid of positions which match by putting 6 in those positions
				totalnumberofblackpegs_[i]++;
				temp_[i][j]=6;
				indtemp_[i][j]=6;
			}
			else
			{
				temp_[i][j]=answer_[j];
				indtemp_[i][j]=population_[i][j];
			}
		}
		std::cout<<totalnumberofblackpegs_[i]<<std::endl;
	}
	std::cout<<"temp+indtemp\n";
	for(int j=0; j<populationsize_; j++)
	{
		for(int k=0; k<4; k++)
		{
			std::cout<<temp_[j][k];
		}
		std::cout<<std::endl;
		for(int k=0; k<4; k++)
		{
			std::cout<<indtemp_[j][k];
		}
		std::cout<<std::endl;
	}
}

void Mastermind::calculatenumberofwhitepegs()
{
	//index through answer array which contains data that doesn't exist

	for(int pop=0; pop<populationsize_; pop++)
	{
		//index temp which is answer array replaced by 6 where positions match
		int i=0;
		//index indtemp which is each individual in population where 
		//each match is replaced by 6
		int j=0;
		//temp isn't at end
		do
		{
			//position in temp already accounted for
			if(temp_[pop][i]==6)
			{
				i++;
			}
			else
			{
				//indtemp isn't at end
				do
				{
					//values match
					if(indtemp_[pop][j]==temp_[pop][i])
					{
						if(indtemp_[pop][j]!=6)
						{
							totalnumberofwhitepegs_[pop]++;
						}
						j++;
					}
					else
					{
						j++;
					}
				}while(j<4);
				i++;
                                j=0;
			}
		}while(i<4);
		std::cout<<totalnumberofwhitepegs_[pop]<<std::endl;
	}
}

void Mastermind::totalnumberofpegs()
{

}

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;
}

Here's the function:
still some errors with it :(. Please help.

void Mastermind::calculatenumberofblackpegs()
{
	temp_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(temp_+i)=(int*)(malloc(sizeof(int)*4));
	}
	indtemp_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(indtemp_+i)=(int*)(malloc(sizeof(int)*4));
	}
	for(int i=0; i<populationsize_; i++)
	{
		totalnumberofblackpegs_[i]=0;
		totalnumberofwhitepegs_[i]=0;
		totalnumberofpegs_[i]=0;
		for(int j=0; j<4; j++)
		{
			//set indtemp and temp to 0
			indtemp_[i][j]=0;
			temp_[i][j]=0;
			if(answer_[j]==population_[i][j])
			{
				//get rid of positions which match by putting 6 in those positions
				totalnumberofblackpegs_[i]++;
				temp_[i][j]=6;
				indtemp_[i][j]=6;
			}
			else
			{
				temp_[i][j]=answer_[j];
				indtemp_[i][j]=population_[i][j];
			}
		}
		std::cout<<totalnumberofblackpegs_[i]<<std::endl;
	}
	/*std::cout<<"temp+indtemp\n";
	for(int j=0; j<populationsize_; j++)
	{
		for(int k=0; k<4; k++)
		{
			std::cout<<temp_[j][k];
		}
		std::cout<<std::endl;
		for(int k=0; k<4; k++)
		{
			std::cout<<indtemp_[j][k];
		}
		std::cout<<std::endl;
	}*/
}

void Mastermind::calculatenumberofwhitepegs()
{
	
	for(int pop=0; pop<populationsize_; pop++)
	{
		//index temp which is answer array replaced by 6 where positions match
		int i=0;
		//index indtemp which is each individual in population where 
		//each match is replaced by 6
		int j=0;
		//temp isn't at end
		do
		{
			//position in temp already accounted for
			if(temp_[pop][i]==6)
			{
				i++;
			}
			else
			{
				//indtemp isn't at end
				do
				{
					//values match
					if(indtemp_[pop][j]==temp_[pop][i])
					{
						if(indtemp_[pop][j]!=6&&temp_[pop][i]!=6)
						{
							totalnumberofwhitepegs_[pop]++;
							//set count to 6 so that it skips 
							//those since it accounted for those
							indtemp_[pop][j]=6;
							temp_[pop][i]=6;
						}
						//move both indexes forward
						j++;
						i++;
					}
					else
					{
						j++;
					}
				}while(j<4);
				i++;
				j=0;
			}
		}while(i<4);
		std::cout<<totalnumberofwhitepegs_[pop]<<std::endl;
	}
}

so i think to fix this i need an array which stores number of each element? Based on above??????

AHHHHHHHHHHHHH

So I think i figure it out... But when my population is too big the answers are screwed up. :(.

Here's my code:
Main

#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();
	a.processGuess();
	a.print();
	return 0;
}

Mastermind.h

#include <iostream>

class Mastermind
{
public:
	Mastermind();

	void initializepopulation();
	void setpopulationsize(int populationsize);
	void setanswer(int* answer);
	void setgenerationnumber(int generationnumber);
	int randomizenumber();

	void processGuess();
	int getblackpegs(int* guess);
	int getwhitepegs(int* guess);

	/*void calculatenumberofblackpegs();
	void calculatenumberofwhitepegs();
	void totalnumberofpegs();*/

	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_;
	//store answer
	//int** temp_;
	//int** indtemp_;
	int* fitness_;
	int sum(int index);
	int pickrandomposition();
};

Mastermind.cpp

#include <cstdlib>
#include "Mastermind.h"
#include <iostream>

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::processGuess()
{
	for(int k=0; k<populationsize_; k++)
	{
		totalnumberofblackpegs_[k]=0;
		totalnumberofwhitepegs_[k]=0;
		totalnumberofpegs_[k]=0;
		int blackpegs=getblackpegs(population_[k]);
		int whitepegs=getwhitepegs(population_[k]);

		int outarray[]={-1,-1,-1,-1};
		for(int i=0; i<4; i++)
		{
			if(blackpegs>0)
			{
				outarray[i]=4;
				blackpegs--;
			}
		}
		for(int j=0; j<4; j++)
		{
			if(whitepegs>0 && outarray[j]<0)
			{
				outarray[j]=5;
				whitepegs--;
			}
		}
		for(int j=0; j<4; j++)
		{
			//std::cout<<outarray[j];
			if(outarray[j]==5)
			{
				totalnumberofwhitepegs_[k]++;
			}
			else if(outarray[j]==4)
			{
				totalnumberofblackpegs_[k]++;
			}
			else
			{
				totalnumberofwhitepegs_[k]=totalnumberofwhitepegs_[k];
				totalnumberofblackpegs_[k]=totalnumberofblackpegs_[k];
			}
		}
		totalnumberofpegs_[k]=totalnumberofwhitepegs_[k]+totalnumberofblackpegs_[k];
		//std::cout<<std::endl;
	}
}

int Mastermind::getblackpegs(int* guess)
{
	int blackpegs=0;
	for(int i=0; i<4; i++)
	{
		if(guess[i]==answer_[i])
		{
			blackpegs++;
		}
	}
	return blackpegs;
}

int Mastermind::getwhitepegs(int* guess)
{
	int whitepegs=0;
	bool guesscounted[]={false,false,false,false};
	bool keycounted[]={false,false,false,false};
	for(int i=0; i<4; i++)
	{
		for(int j=0; j<4; j++)
		{
			if(guess[i]==answer_[j]&&!keycounted[j]&&!guesscounted[i])
			{
				whitepegs++;
				guesscounted[i]=true;
				keycounted[j]=true;
			}
		}
	}
	whitepegs=whitepegs-getblackpegs(guess);
	return whitepegs;
}


/*void Mastermind::calculatenumberofblackpegs()
{
	temp_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(temp_+i)=(int*)(malloc(sizeof(int)*4));
	}
	indtemp_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(indtemp_+i)=(int*)(malloc(sizeof(int)*4));
	}
	//intialize
	for(int i=0; i<populationsize_; i++)
	{
		totalnumberofblackpegs_[i]=0;
		totalnumberofwhitepegs_[i]=0;
		totalnumberofpegs_[i]=0;
		for(int j=0; j<4; j++)
		{
			indtemp_[i][j]=0;
			temp_[i][j]=0;
		}
	}


	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			//set indtemp and temp to 0
			if(answer_[j]==population_[i][j])
			{
				//get rid of positions which match by putting 6 in those positions
				totalnumberofblackpegs_[i]++;
				temp_[i][j]=6;
				indtemp_[i][j]=6;
			}
			else
			{
				temp_[i][j]=answer_[j];
				indtemp_[i][j]=population_[i][j];
			}
		}
		//std::cout<<totalnumberofblackpegs_[i]<<std::endl;
	}
	std::cout<<"temp+indtemp\n";
	for(int j=0; j<populationsize_; j++)
	{
		for(int k=0; k<4; k++)
		{
			std::cout<<temp_[j][k];
		}
		std::cout<<std::endl;
		for(int k=0; k<4; k++)
		{
			std::cout<<indtemp_[j][k];
		}
		std::cout<<std::endl;
	}
}

void Mastermind::calculatenumberofwhitepegs()
{
	
	for(int pop=0; pop<populationsize_; pop++)
	{
		//index temp which is answer array replaced by 6 where positions match
		int i=0;
		//index indtemp which is each individual in population where 
		//each match is replaced by 6
		int j=0;
		//temp isn't at end
		while(i<4)
		{
			//position in temp already accounted for
			if(temp_[pop][i]==6)
			{
				i++;
			}
			else
			{
				//indtemp isn't at end
				while(j<4)
				{
					//values match
					if(indtemp_[pop][j]==temp_[pop][i])
					{
						if(indtemp_[pop][j]!=6&&temp_[pop][i]!=6)
						{
							totalnumberofwhitepegs_[pop]++;
							//set count to 6 so that it skips 
							//those since it accounted for those
							indtemp_[pop][j]=6;
							temp_[pop][i]=6;
						}
						//move both indexes forward
						j++;
						i++;
					}
					else
					{
						j++;
					}
				}
				i++;
				j=0;
			}
		}
		//std::cout<<totalnumberofwhitepegs_[pop]<<std::endl;
	}
}

void Mastermind::totalnumberofpegs()
{

}*/

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<<"|";
		std::cout<<totalnumberofblackpegs_[i];
		std::cout<<"|";
		std::cout<<totalnumberofwhitepegs_[i];
		std::cout<<"|";
		std::cout<<totalnumberofpegs_[i];
		std::cout<<"|";
		std::cout<<std::endl;



		//std::cout<<totalnumberofblackpegs_[i];
		//std::cout<<totalnumberofwhitepegs_[i];
		
	}
}

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;
}

So I think i figure it out... But when my population is too big the answers are screwed up. :(.

Here's my code:
Main

int Mastermind::getblackpegs(int* guess)
{
	int blackpegs=0;
	for(int i=0; i<4; i++)
	{
		if(guess[i]==answer_[i])
		{
			blackpegs++;
		}
	}
	return blackpegs;
}

int Mastermind::getwhitepegs(int* guess)
{
	int whitepegs=0;
	bool guesscounted[]={false,false,false,false};
	bool keycounted[]={false,false,false,false};
	for(int i=0; i<4; i++)
	{
		for(int j=0; j<4; j++)
		{
			if(guess[i]==answer_[j]&&!keycounted[j]&&!guesscounted[i])
			{
				whitepegs++;
				guesscounted[i]=true;
				keycounted[j]=true;
			}
		}
	}
	whitepegs=whitepegs-getblackpegs(guess);
	return whitepegs;
}

That's soooooo much better. Short and sweet.

White Pegs is a little odd.
1) You only need one boolean array, not 2.
2) Outer loop should go through the answer pegs.
3) Inner loop through the guess pegs.
4) The boolean should match the guess peg/inner loop.
5) Return the number of pegs that match (change the name of the function)

This way you have two function calls in the calling function:

npeg = getMatchingPegs()  // get the number of pegs that match
bpeg = getblackpegs()     // get the number of pegs that match in position
wpeg = npeg - bpeg        // get the number of white pegs

This keeps the functions simpler and you don't have to call the black peg routine twice.

By the way, what's with all the underscores?

I have underscores since I can't have a function name be the same as a datatype name in my .h file. Thanks I will try that.

I'm not sure what you mean by 4) The boolean should match the guess peg/inner loop.

Here's what I did....

void Mastermind::getblackpegs()
{
	for(int k=0; k<populationsize_; k++)
	{
		for(int i=0; i<4; i++)
		{
			if(population_[k][i]==answer_[i])
			{
				totalnumberofblackpegs_[k]++;
			}
		}
	}
}

void Mastermind::gettotalnumberofpegs()
{
	bool** guesscounted=(bool**)(malloc(sizeof(bool*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(guesscounted+i)=(bool*)(malloc(sizeof(int)*4));
	}
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			guesscounted[i][j]=false;
		}
	}
	//through population
	for(int k=0; k<populationsize_; k++)
	{
		//outer loop go through answer peg
		for(int i=0; i<4; i++)
		{
			//through guess pegs
			for(int j=0; j<4; j++)
			{
				if((population_[k][j]==answer_[i])&&!guesscounted[k][j])
				{
					totalnumberofpegs_[k]++;
					guesscounted[i][j]=true;
				}
			}
		}
	}
}

void Mastermind::getwhitepegs()
{
	for(int i=0; i<populationsize_; i++)
	{
		totalnumberofwhitepegs_[i]=totalnumberofpegs_[i]-totalnumberofblackpegs_[i];
	}
}

WaltP your way doesn't even work... you need the 2nd array to say where you already guessed that position or not.

WaltP your way doesn't even work... you need the 2nd array to say where you already guessed that position or not.

Sure it does. You only need to test the current values entered against the answer key. Based on what I see in your code, it's 10 orders of magnitude more complex than it needs to be.
bool**, malloc'ed bool arrays, triple nested loops -- all unnecessary. You need to think simple.

And in MM, you don't return the positions correct, only the count. I don't think the 2nd array is necessary.

that's exactly what i am doing here...

if(population_[k][j]==answer_[i])

you need the && condition so that you don't double count.

The second array is necessary to say that already accounted in black so don't account it again.... since you don't want the total of black and white to be greater than 4.

in regards to the second comment, i don't care about positions, i care about number of white and blacks.

If you say so. All I know is my version uses a total - that's total - of 3 arrays, and 5 variables.

char  pattern[NUMVAL];     // The answer key
    char  patterntst[NUMVAL];  // Temp version of the answer key
    char  guess[NUMGUESS];     // The user's guess

the temp version of array is replaced by bool one in my code. So the temp version of answer will store which ever ones don't contain black?? Anyways I am off to sleep. Have a week to finish up this code.

No. Copy the answer code to tmpAns. Then test for black in a loop with:

If guess(i) == tmpAns(i)
    black = black + 1
    tmpAns(i) = -1      // answer code is found, don't reuse
    guess(i) = -2       // guess has been used, don't reuse

That's the entire black test.

White test takes a nested loop, but it's identical..

So I tried your way....
It works for 90% of them

It doesn't work for some of them. answer i enter
is=1232 it gives.... W|B|Total
3131 1 |2|3
2131 1 |1|2

void Mastermind::getblackpegs()
{
	//allocate array
	tempanswer_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(tempanswer_+i)=(int*)(malloc(sizeof(int)*4));
	}

	//copy data
	for(int k=0; k<populationsize_; k++)
	{
		for(int j=0; j<4; j++)
		{
			tempanswer_[k][j]=answer_[j];
		}
	}

	//test for black
	for(int k=0; k<populationsize_; k++)
	{
		for(int j=0; j<4; j++)
		{
			if(population_[k][j]==tempanswer_[k][j])
			{
				totalnumberofblackpegs_[k]++;
				tempanswer_[k][j]=-1;  //answer code is found don't reuse
				population_[k][j]=-2; //guess has been used, don't reuse
			}
		}
	}
}

void Mastermind::getwhitepegs()
{
	for(int k=0; k<populationsize_; k++)
	{
		//go through tempanswer array
		for(int j=0; j<4; j++)
		{
			//go through guesses
			for(int i=0; i<4; i++)
			{	
				//not already accounted and 
				if((population_[k][j]==tempanswer_[k][i]))//&&population_[k][j]!=-2&&tempanswer_[k][j]!=-1)
				{
					tempanswer_[k][j]=-1;  //answer is found don't reuse
					population_[k][j]=-2; //guess has been used, don't reuse
					totalnumberofwhitepegs_[k]++;
				}
				else
				{
					totalnumberofwhitepegs_[k]=totalnumberofwhitepegs_[k];
				}
			}
		}
	}
}

void Mastermind::gettotalnumberofpegs()
{
	for(int k=0; k<populationsize_; k++)
	{
		totalnumberofpegs_[k]=totalnumberofblackpegs_[k]+totalnumberofwhitepegs_[k];
	}
}
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.