Vous êtes sur la page 1sur 9

Bulls & Cows Game

Group Project By Mo & Kishore


What Is That Game??

• The Bulls & Cows is an old code-breaking mind


game and it is played between 2 players, one
(computer) is the code-maker and chooses a secret
code and the other is the code-breaker(human),
who has to guess the secret code in the minimum
number of guesses.
How Does That Game Works??
• Code maker(computer) randomly generate a 4-digit number where all
four digits are unique.
• Ask the user(human) to guess a 4-digit number.
• For every digit that the digit the user guessed correctly in the correct
place, they have a "bull" (as in bullseye). For every digit the user
guessed correctly in the wrong place is a "cow".
• Every time the user makes a guess, tell them how many "bulls" and
"cows" they have.
• Once the user guesses the correct number, the game is over.
• Keep track of the number of guesses the user makes throughout the
game and the time it takes to correctly guess the 4-digit number.
• At the end of the game tell the user how many guesses and how long
it took to win.
• The objective of the game is to correctly guess the number in the
least number of attempts.
How Does That Game Code Works??
• Code: The chosen code by the code-maker(computer) is of the
format < a1, a2, a3, a4 >, such that ai≠ aj is i ≠ j. I use random
function to generate 4 random numbers which is to be
guessed. Then the double “for loops” check the requirement
of no repeating digits. If the random function created two
same digits, the “for loops” would check it and call the random
function number again to produce a different digit.
• Guess: The codebreaker(human) then responds with a guess
(say < b1, b2, b3,b4>).
• Response: It is given in two parts. Bulls: The code-maker has to
tell the number of correct hits
• (ai= bi), known as bulls. Cows: The number of digits of digits
present in the secret code but not at the right place (ai= bj , i ≠
j).
How Does That Game Code Works??
• #include <iostream>
• #include<vector>
• #include<ctime>
• #include<cstdlib>
• #include<chrono>

using namespace std;

int main()
• {
• int i,j,bulls = 0,cows=0,guess,count=0;
• srand(time(NULL));
• vector<int>random;
• for(i=0;i<4;i++){
• int x=rand()%10;
• for(j=0;j<i;j++){
• if(x==random[j])
• break;
• }
• if(i==j){
• random.push_back(x);
• }
• else {
• i--;
• }
• }
How Does That Game Code Works??
• clock_t begin = clock();
• while (!(bulls == 4)){
• vector<int>guesses; // vector holding the guesses

cout << "Enter a 4 digit number(unique digits): ";
• for(int i=0;i<4;i++){
• cin>>guess;
• guesses.push_back(guess);
• }
• // bulls criterion
• for (unsigned int j = 0; j < guesses.size(); ++j){
• if (guesses[j] == random[j]){
• ++bulls;
• }
• }

// cows criterion
• for (unsigned int m = 0; m < guesses.size(); ++m){
• for (unsigned int n = 0; n < random.size(); ++n){
• if (guesses[m] == random[n] && m != n){
• ++cows;
• }
• }
• }
How Does That Game Code Works??
• if (bulls < 4){
• cout << "bulls = " << bulls << " and cows = " << cows <<endl;

bulls = 0; // reset bulls
• }

guesses.clear(); // empty guesses vector
• cows = 0; // reset cows
• count++;
• }
• clock_t end = clock();
• double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
• int totalSecond=elapsed_secs;
• int minute,second;
• if(totalSecond>=60){
• minute=totalSecond/60;
• second=totalSecond%60;
• }
• cout << "bulls = " << bulls << " and cows = " << cows <<endl;
• cout << "You guessed it in "<<count <<" tries, taking "<<minute<<" minutes and "<<second<<" seconds";

return 0;
• }
The Code Runs Like This

https://www.youtube.com/watch?v=K70BrgDC
zug&t=9s
References

Meyers, S. (2005). Effective C : 55 specific ways to improve your


programs and designs (Third ed.). Upper Saddle River, NJ:
Addison-Wesley.
Stroustrup, B. (2013). The C Programming Language (Fourth
ed.). New Jersey: Pearson Education (US).

Vous aimerez peut-être aussi