```
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
/*
MasterMind Computer Player, By Aytek Ustundag (holyone@tahribat.com)
*/
using namespace std;
int probabilitycount=0;
string probabilities[120];
inline bool hasduplicate(string s)
{
int k;
int j;
for (j=0;j<4;j++)
for (k=0;k<4;k++)
{
if (k!=j)
if (s[k]==s[j]) { return true;}
}
return false;
}
inline bool checkstring(string s)
{
if (s.length()!=4) return false;
if (s[0]== 0x30) return false;
if (hasduplicate(s)) return false;
return true;
}
int HowManyAreCorrect(string a,string b)
{
int t=0;
for(int i=0;i<4;i++)
{
if (a[i]==b[i]) t++;
}
return t;
}
int HowManyAreMisplaced(string a, string b)
{
int l=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if((a[i]==b[j])&&(i!=j))
{
l++;
}
}
return l;
}
string generatestringfromnumbers(int i,int j,int k,int l)
{
string result="";
result+= char(0x30+i);
result+= char(0x30+j);
result+= char(0x30+k);
result+= char(0x30+l);
return result;
}
void eliminateprobability(int i)
{
int x;
for (x=i;x<probabilitycount;x++)
{
probabilities[x]=probabilities[x+1];
}
probabilitycount=probabilitycount-1;
}
int main()
{
srand(time(0));
cout<<endl;
cout<<" Wellcome to Mastermind - Computer Player\n";
cout<<"--------------------------------------------------------------------------------\n";
cout<<" hold a random 4 digit number with diffrent digits\n";
cout<<" I will tell you the number, just give me the information about \n";
cout<<" how many of the digits that i guessed right, and how many in wrong places \n";
cout<<" DONT TRY TO FOOL ME HUMAN!, BECAUSE I CAN FIND IT OUT!\n";
cout<<" Lets start the game if you are ready.\n\n";
short int correct,wrongplace,i,j,k,l;
string lastguess;
string Temp[120];
//Generating probability List
for (i=0;i<5;i++)
for (j=0;j<5;j++)
for (k=0;k<5;k++)
for (l=0;l<5;l++)
{
if (checkstring( generatestringfromnumbers(i,j,k,l) )==true)
{
probabilities[probabilitycount]= generatestringfromnumbers(i,j,k,l);
probabilitycount+=1;
}
}
//Done Generating probability List
int GuessCnt=0;
do {
lastguess=probabilities[rand()%probabilitycount];
if (probabilitycount==1)
cout<<endl<<" There is only one probability left. And it Has to be be Right!\n"<<endl;
else cout<<endl<<" There is "<<probabilitycount<<" probabilities.\n";
cout<<" My guess: "<<lastguess<<endl<<endl;
cout<<" Input number of correct digits and input number of misplaced digits:\n";
cout<<endl;
cout<<"How many are correct?\t:";
cin>>correct;
cout<<"How many are misplaced?\t:";
cin>>wrongplace;
GuessCnt++;
i=0;
do
{
if ((HowManyAreCorrect(probabilities[i],lastguess)!=correct) || (HowManyAreMisplaced(probabilities[i],lastguess)!=wrongplace))
eliminateprobability(i); else
i++;
} while ((i<probabilitycount) && (probabilitycount>=0));
//delete slashes to see the probabilities left at your each guess
//for (j=0;j<probabilitycount+1;j++) cout<<probabilities[j]<<endl; cout<<"probabilitycount:"<<probabilitycount<<endl;
} while (((correct!=4) || (wrongplace!=0) )&& (probabilitycount>0));
//do {cout<<"Guess again:\n"; cin>>b;} while(checkstring(b)==false);
if (probabilitycount<=0)
{
cout<<" You Can't Fool me human, The datas you have provided are invalid.\n";
cout<<" Dont Try it again... I can sense you cheating.\n";
return -1;
}
cout<<"\n I found it in "<<GuessCnt<<" Guesses Human."<<endl;
cout<<" See ya later..."<<endl;
return 0;
}
```