Guessing game with binary numbers

I'm recently new to c++ and was wondering if I could get some help. I am trying to make a computer guess a binary number which is entered but I have no clue how to do this, any ideas or help pages would be appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <bitset>
#include <string>
#include <bitset>
#include <time.h>

using namespace std;

string index2code(int i)
{
    return bitset<9>(i).to_string();
}

    void breakyourcode() //Computer breaks users code
{

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

    int tries = 8;
    int guess;
    int compGuess = rand() % 512;


    cout << "Please enter your number (e.g. 101010101): ";
    cin >> guess;

    unsigned int countTries = 8;

    for (unsigned int i = 1; i < countTries; ++i)
    {
        cout << "Trial " << i << "/7. Computers guess? " << guess << endl;

        cout << "Press ENTER to continue..." << endl;
        cin.get();
    }

}

    void breakmycode() //User breaks computers code
    {
        cout << "You have picked break my code" << endl;
        string code;

        srand(time(NULL));
        int index = rand() % 512;

        string cCode = index2code(index);

        unsigned int countTries = 8;

        for (unsigned int i = 1; i < countTries; ++i) 
        {
            cout << "Trial " << i << "/7. Your guess? ", code;
            cin >> code;

            if (code == cCode) 
            {
                cout << "Moo!!! you found the secret code." << endl;
                cin.ignore();
                system("PAUSE");
                break;
            }
        }

        cout << "The correct code was " << cCode << endl;
        system("PAUSE");
    }

    void menu()
    {
        cout << "---------------Main Menu---------------" << endl;
        cout << "---Starting the BULLS and COWS game---" << endl;
        cout << "" << endl;
    }

    void userchoice()
    {
        cout << "Enter 1 for you to break my code." << endl;
        cout << "Enter 2 for computer to break your code." << endl;
        cout << "Enter anything else to quit." << endl;
        int choice;
        cin >> choice;
        {
            if (choice == 1) {
                breakmycode();
            }
            else if (choice == 2) {
                breakyourcode();
            }
            else {
                exit(0);
            }
        }
    }

    int main()
    {
        menu();
        userchoice();
        return 0;
    }
Your trying to do something and don't know how, ok, pls explain why you would want to do it ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <bitset>
#include <string>
#include <bitset>
//#include <time.h>
#include <ctime>
#include <cstdlib> // required for std::srand(), std::rand()
// #include <random> uncomment to use the C++ random number library
// using namespace std;

const unsigned int NBITS = 9 ; // unsigned
const unsigned int UBOUND = 1U << NBITS ; // one grater than the largest unsigned value
                                          // than NBITS can hold
const int MAX_TRIES = 8 ;

std::string index2code( unsigned int number )
{
    number %= UBOUND ; // abundant caution
    return std::bitset<NBITS>(number).to_string() ;
}

bool is_valid_code( std::string code ) // exactly 'NBITS' characters in [ '0', '1' ]
{
    // http://en.cppreference.com/w/cpp/string/basic_string/find_first_not_of
    return code.size() == NBITS && code.find_first_not_of( "01" ) == std::string::npos ;
}

std::string get_code()
{
    std::cout << "please enter a binary number with exactly " << NBITS << " digits: " ;
    std::string code ;
    if( std::cin >> code && is_valid_code(code) ) return code ;

    // error in input
    std::cout << "invalid input. please try again\n" ;
    return get_code() ; // try again
}

void breakmycode() // User breaks computers code
{
    std::cout << "\nYou have picked break my code\n" ;

    const std::string code = index2code( std::rand() % UBOUND );

    for( unsigned int i = 0 ; i < MAX_TRIES ; ++i )
    {
        std::cout << "\nTrial " << i+1 << ".\\tYour guess?\n" ;
        const std::string guess = get_code() ;
        if( guess == code )
        {
            std::cout << "\nMoo!!! you found the secret code.\n" ;
            break;
        }
        else if( guess > code ) std::cout << "that is too high\n" ;
        else std::cout << "that is too low\n" ;
    }

    std::cout << "The correct code was " << code << '\n' ;
}

void breakyourcode() // Computer breaks users code
{
    std::cout << "You have picked break computers code\n" ;

    // TO Do : implement the function
    std::cout << "This is not yet implemented\n" ;

}

void menu()
{
    std::cout << "---------------Main Menu---------------\n"
                 "---Starting the BULLS and COWS game---\n\n" ;
}

int userchoice()
{
    std::cout << "Enter 1 for you to break my code.\n"
              << "Enter 2 for computer to break your code.\n"
              << "Enter anything else to quit.\n" ;

    int choice;
    std::cin >> choice ;
    std::cin.ignore(10000,'\n') ; // throw the rest of this line away
    return choice ;
}

int main()
{
    // consider using facilities declared in the standard header <random>
    // http://en.cppreference.com/w/cpp/numeric/random
    std::srand( std::time(nullptr) ) ; // seed the rng

    menu();
    switch( userchoice() )
    {
        case 1 : breakmycode() ; break ;
        case 2 : breakyourcode() ; break ;
        default : std::cout << "quitting\n" ;
    }
}
Topic archived. No new replies allowed.