Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   c++ mastermind game console app (http://www.velocityreviews.com/forums/t621628-c-mastermind-game-console-app.html)

ImmortalFire 06-22-2008 09:23 AM

c++ mastermind game console app
 
/*
* 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[i]='*';
tmpguess[i]='*';
}
}


// 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(),::toup per);
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;
}


All times are GMT. The time now is 03:11 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.