Bulls and Cows game

Hi, I'm reading this book Principles and Practice using C++ and I'm a total beginner(only about a month), I finished this exorcise on chapter 5, just wanted to ask you if you could give this program a brief look as I'm self studying programming and have no idea whether I'm doing things right or not to this point. the program does what I want it to do, just need some opinions....

The exercise is the Bull and Cow game where you have to guess four numbers between 0 to 9, if you got the number and its position right you get 1 bull, right number but the wrong position 1 cow. then it says player should be able to play repeatedly until got the numbers right and that numbers should be random each play using randint() and srand().

So far the book has only introduced a handful of functions from the libraries, I am familiar with vectors, conditional statements,loops and handling errors and I don't really want to use anything that is not yet covered in the book(unless if I have to).

I'd also really appreciate if you could give some advice on these questions ?
- You'd notice that I have used a lot of this variable 'track' in the program which is really handy to prevent the loops from acting weird, Is it ok to use such variables ?

- What's the difference with declaring variables in or out of main()?

- The book has not explained anything about generating random numbers yet, in the program the random numbers should not be repeated, is there any other way to this than the way I have done it ?(without relying much on the functions that the book has not covered)

Thanks....
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
#include <iostream>
#include <std_lib_facilities.h>

vector<int> nums; //vector for user's guesses
vector<int> key; //the 4 digit code
int track=0; //this variable helps handling various trackings in the while loop and increases by 1 each time that the user enters a number
int bull=0;
int cow=0;
int const guess=4; //each try user guess 4 digits

//this function adds numbers 1 to 9 to key vector then shuffles them
//only the first 4 elements of the key vector will be used in this program
//this function will generate non-repeating random numbers between 1 to 9
int rand_geneator(int y){
    srand(y);
    for(int i=0; i<9; ++i){
        key.push_back(i+1);
    }
    random_shuffle(key.begin(),key.end());
}

//this function takes input from the user and check them
//user can only enter 4 digits in each try
//each single digit cannot be larger than 9
//each digit entered has to be unique and cannot be repeated in each try
int input_check(int x){
    for(int i=0; i<nums.size(); ++i){
            if(track>0 && x==nums[i]){error("Same numbers are not allowed\n");}
    }
        if(x>9){error("Number larger than 9");}
        nums.push_back(x);
}

//this function checks how many guess the user got right
void check_bullcow(){
    if(nums[track]==key[track]){ //track is 0 after the very first input is entered in each try, check every user's input individually
            bull +=1;
    }
        for(int i=0; i<guess; ++i){
            if(nums[track]==key[i] && nums[track]!=key[track]){ //compare each input individually with the 4 digit answer
                cow +=1;
            }
        }
}

//this function will be activated once the user got all 4 guesses right
int win_game(int x){
    if(x==guess){
    std::cout<<"You got it right!!!\n";
        return 0;
    }
}

// this function is activated after the user has entered 4 digits
// this function display how many guess the user got right
void end_guess(){
   if(track==guess && bull!=guess){
        std::cout<<"Bull: "<<bull<<"   Cow: "<<cow<<'\n';
        nums.erase (nums.begin(),nums.end()+0); //erase the user's guesses at the end of each try to replace with new guesses
        std::cout<<"Try again....\n";
        bull=0;
        cow=0;
        track=0;
        // set variables to 0 again for the new try
    }
}

int main()
try{
    std::cout<<"Please enter a number: ";
    int n=0;
    std::cin>>n;
    rand_geneator(n);
    std::cout<<"Enter 4 digits, press Enter after each digit.\n";
    int inputs;
    while(nums.size()<guess && std::cin>>inputs){
        input_check(inputs);
        check_bullcow();
        ++track;
        win_game(bull);
        end_guess();
    }
    if(nums.size()>guess){error("only 4 guess");}
}
catch(exception& e){
    cerr<<"error: "<<e.what();
}
Last edited on
Topic archived. No new replies allowed.