Binary Code Game

Hello, i am new to C++ and doing an assignment, "Binary Code Breaker"

what i have to do is make a game where the player has to guess a four digit code made up of 1's and 0's. The game can either be made so that the play guesses all 4 digits at once, or one digit at a time. I am trying to do a 1 digit at a time.

Right now, i'm just trying to get it functional, and would gladly take any help or tips on how to go about doing this.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <string>


using namespace std;


int main()
{
    srand(static_cast<unsigned int>(time(0)));

    int userSelection;

    const short PLAY_GAME = 1;
    const short QUIT_GAME = 2;

    do
    {

        cout << "[1] Start Game." << endl;
        cout << "[2] Quit Game." << endl;
        cout << "> ";
        cin >> userSelection;

        switch (userSelection)
        {
        case PLAY_GAME:
            break;
        case QUIT_GAME:
            cout << endl;
            cout << "Goodbye!" << endl;
            cout << endl;
            break;
        default:
            cout << endl;
            cout << "That is not a valid option!" << endl << endl;
            cout << endl;
        }
        while (userSelection == 1)
        {
            const int CODES = 10;

            string BIN[CODES] =
            {
                "0100",
                "1010",
                "1110",
                "1011",
                "0001",
                "0010",
                "0000",
                "1111",
                "1000",
                "0100"
            };

            const int TRIES = 5;
            string guess;

            while (true)
            {
                cout << "play Game? (y/n) " << endl << endl;
                char decision = _getch();

                    if (decision == 'y')
                    {
                        int outCode = (rand() % CODES);
                        string secretCode = BIN[outCode];

                        for (int i = 0; i < TRIES; i++)
                        {
                            int counter = 0;

                            cout << "Enter Number" " You have (" << 5 - i << ") Attempts left. ->";
                            cout << "Enter your guess -> ";
                            getline(cin, guess);

                            for (size_t i = 0; i < guess.size(); i++)
                            {
                                if (guess[i] == secretCode[i])
                                {                                   
                                    cout << guess[i];
                                    ++counter;
                                    cout << "Is correct" << endl;
                                }
                                else
                                {
                                    cout << endl << endl << "\t\t** WRONG ** " << endl << endl;
                                }
                            } cout << endl;
                            if (counter == 5)
                            {
                                cout << "You win" << endl;
                            }
                            else
                            {
                                cout << "Sorry, you lose." << endl;
                            }
                        }
                    }
                    else if (decision == 'n')
                    {
                        cout << "Goodbye";
                    }
                } 
            } 
    } while (userSelection != 2);

    system("PAUSE");
    return 0;
}

When i..
[1]start game,
Press - y - to play
it produces the first two lines as if i have already made a guess.

"Enter digit #1" " You have (5) Attempts left. ->"
"Enter your guess -> ";
sorry, you lose.
"Enter digit #1" " You have (4) Attempts left. ->"
"Enter your guess -> "

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories