c++ mastermind game console app

I

ImmortalFire

/*
* Mastermind
*
* Author: Gerald Cruz
*
* Date: June 21, 2008
*
*/

#include<iostream>
#include<string>
#include<ctime>
#include<algorithm>

#define CODELEN 4 // length of code
#define NUMSYM 6 // number of symbols
#define MAXTRIES 10

using namespace std;

void clrscr()
{
system("cls");
}

int main()
{
srand(time(NULL));

bool playagain=false;

do
{
//create code
string code = "";
for(int i=0; i<CODELEN; i++)
{
code.append(1,rand() % NUMSYM + 'A');
}

clrscr();
cout << "Mastermind" << endl << endl;
cout << "Guess the " << CODELEN << " digit code using letters A to "
<< (char)('A'+NUMSYM-1) << "." << endl << endl;
cout << "The computer will give a clue:" << endl;
cout << "\t0 for a correct letter at a wrong position." << endl;
cout << "\t1 for a correct letter at a correct position." << endl <<
endl;
cout << "Enter a period (.) to exit the program." << endl << endl;

int numtries = 0;
bool gameover = false;

do
{
string guess("");

cout << "Enter you guess? ";
getline(cin,guess);

// capitalize guess
transform(guess.begin(),guess.end(),guess.begin(),::toupper);

// check for invalid characters
bool invalid = false;
for(unsigned int i=0; i<guess.length(); i++)
{
if(guess.at(i)<'A' || guess.at(i)>(char)('A'+NUMSYM-1))
{
invalid=true;
break;
}
}

if(guess.compare(".")==0)
{
goto exitprogram;
}
else if(invalid)
{
cout << "Your input has invalid characters." << endl;
}
else if(guess.length()!=CODELEN)
{
cout << "Your guess must be " << CODELEN << " characters long."
<< endl;
}
else
{
string tmpcode=code;
string tmpguess=guess;
string clue="";
int correct=0;
int misplaced=0;

numtries++;


// check for a correct letter at a correct position
for(int i=0; i<CODELEN; i++)
{
if(tmpcode.at(i)==tmpguess.at(i))
{
correct++;
clue.append("1");
tmpcode='*';
tmpguess='*';
}
}


// check for a correct letter at a wrong position
for(int i1=0; i1<CODELEN; i1++)
{
if(tmpguess.at(i1)=='*') continue;
for(int i2=0; i2<CODELEN; i2++)
{
if(tmpcode.at(i2)!='*' && tmpcode.at(i2)==tmpguess.at(i1))
{
misplaced++;
clue.append("0");
tmpcode[i2]='*';
tmpguess[i1]='*';
}
}
}

// shuffle clue
random_shuffle(clue.begin(),clue.end());

cout << "Clue: " << clue << endl;

if(correct==CODELEN)
{
cout << "Correct! The code is " << code << "." << endl;

if(numtries<2)

cout << "You got it in your first try." << endl;

else

cout << "You got it in " << numtries << " tries." << endl;

gameover=true;
}
else if(numtries>=MAXTRIES)
{
cout << "Wrong! The code is " << code << "." << endl;
gameover=true;
}

}

}
while(!gameover);

cout << endl << "Game Over" << endl << endl;


// ask to play again
bool askagain = true;
do
{
string ans = "";
cout << "Play again? ";
getline(cin,ans);
transform(ans.begin(),ans.end(),ans.begin(),::toupper);
if(ans.compare("Y")==0 || ans.compare("YES")==0)
{
askagain=false;
playagain=true;
}
else if(ans.compare("N")==0 || ans.compare("NO")==0)
{
askagain=false;
playagain=false;
}
}
while(askagain);



}
while(playagain);

exitprogram:

cout << endl << "Bye." << endl;

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top