c++ mastermind game console app

Discussion in 'C++' started by ImmortalFire, Jun 22, 2008.

  1. ImmortalFire

    ImmortalFire Guest

    /*
    * 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;
    }
     
    ImmortalFire, Jun 22, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kraig
    Replies:
    6
    Views:
    1,470
    Will Twentyman
    Dec 10, 2004
  2. pkellner
    Replies:
    0
    Views:
    524
    pkellner
    Apr 27, 2007
  3. Mastermind

    , Jun 13, 2008, in forum: C++
    Replies:
    2
    Views:
    622
  4. Kris Rudin

    console app w/o seeing console?

    Kris Rudin, Nov 11, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    144
    Randy Charles Morin
    Nov 11, 2003
  5. Steve Jones

    Ruby Mastermind Game

    Steve Jones, Oct 30, 2008, in forum: Ruby
    Replies:
    4
    Views:
    118
    Todd Benson
    Nov 1, 2008
Loading...

Share This Page