Google Search

Saturday, October 24, 2015

Mastermind for beginners

// Note : Results
// 2 = correct position, correct number
// 1 = wrong position, correct number
// 0 = wrong position, wrong number

‪#‎include‬ <iostream.h>
int guess[4], given[4];
int x,y,n,lup,lup1;
int checking[4];
int main()
{
x = 0;
// user will give the correct combination
for (lup=1;lup<5;lup++)
{
cout<<"Enter given for " <<lup<< " place \t";
cin>>given[x];
x++;
}
lup1 = 1;
// user will guess the correct combination
for (y=0;y<4;y++)
{
cout<< "Enter guess for "<< lup1<<"\t";
cin>> guess[y];
// program will compare the "guess" with all the combination
if (guess[y] == given[y])
{
// guess is both correct in position and number
checking[y] = 2;
}
else
{
for (x=0;x<4;x++)
{
if(guess[y]== given[x])
{
//guess is just correct in number but position is wrong
checking[y] = 1;
}

}
}

lup1++;
// display the results
}
cout << "results:" <<"\n";
for (n=0;n<4;n++)
{
cout <<checking[n]<<"\t";
}
cout<<endl;
return 0;
}

No comments:

Post a Comment