Bulls and Cows C++ Code Create a program that will play the \"Bulls and Cows\" g
ID: 3841260 • Letter: B
Question
Bulls and Cows C++ Code
Create a program that will play the "Bulls and Cows" game with the user. The game works like this: Randomly generate a 4-digit number where all four digits are unique. Ask the user 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.
This assignment is one to give you practice with vector.
You will also find yourself implementing a linear search and having a need for a nested for-loop.
Populate your vector(s) with the push_back method.
DO NOT use any string methods or a character array to isolate the individual digits within a user's guess or any functionality from <algorithm>.
Instead use integer division and remainders to isolate the individual digits.
And DO NOT use any functions from <algorithm>.
BUT do feel free to use any vector member functions.
All text produced by your program should have a format identical to what is shown below.
Here is a sample run, where the program picked 9736:
Enter a 4 digit number(unique digits): 0987
bulls = 0 and cows = 2
Enter a 4 digit number(unique digits): 9078
bulls = 1 and cows = 1
Enter a 4 digit number(unique digits): 9780
bulls = 2 and cows = 0
Enter a 4 digit number(unique digits): 9580
bulls = 1 and cows = 0
Enter a 4 digit number(unique digits): 9740
bulls = 2 and cows = 0
Enter a 4 digit number(unique digits): 9721
bulls = 2 and cows = 0
Enter a 4 digit number(unique digits): 9768
bulls = 2 and cows = 1
Enter a 4 digit number(unique digits): 9785
bulls = 2 and cows = 0
Enter a 4 digit number(unique digits): 9756
bulls = 3 and cows = 0
Enter a 4 digit number(unique digits): 9706
bulls = 3 and cows = 0
Enter a 4 digit number(unique digits): 9716
bulls = 3 and cows = 0
Enter a 4 digit number(unique digits): 9726
bulls = 3 and cows = 0
Enter a 4 digit number(unique digits): 9736
bulls = 4 and cows = 0
You guessed it in 13 tries, taking 2 minutes and 49 seconds
Explanation / Answer
Solution for the problem(C++):-
Program:-
#include<iostream>
int findChance(int,int,int,int);
using namespace std;
int main()
{
int a,b,c,d,num=9736,a1,b1,c1,i=1;
a=num%10;
a1=num/10;
b=a1%10;
b1=a1/10;
c=b1%10;
c1=b1/10;
d=c1%10;
int bull=findChance(a,b,c,d);
while(bull!=4)
{
i++;
bull=findChance(a,b,c,d);
}
cout<<"Game over in "<<i<<" chances"<<endl;
return 0;
}
int findChance(int w,int x,int y,int z)
{
int digit,a,b,c,d,a1,b1,c1,cow=0,bull=0;;
cout<<"Enter a 4 digit number(unique digits):";
cin>>digit;
a=digit%10;
a1=digit/10;
b=a1%10;
b1=a1/10;
c=b1%10;
c1=b1/10;
d=c1%10;
if(a==w && b==x && c==y && z==d)
{
bull=4;
}
else if((a==w && b==x && c==y) ||(a==w && b==x && d==z) || (a==w && c==y && d==z) || (b==x && c==y && d==z) )
{
bull=3;
}
else if((a==w && b==x) || (a==w && c==y) || (a==w && d==z) || ( b==x && c==y) || ( b==x && d==z) || (c==y && z==d))
{
bull=2;
}
if(a!=w && (a==x || a==y || a==z))
{
cow++;
}
if(b!=x && (b==w || b==y || b==z))
{
cow++;
}
if(c!=y && (c==w || c==x || b==z))
{
cow++;
}
if(d!=z && (d==w || d==y || d==x))
{
cow++;
}
cout<<"bull= "<<bull<<"cow= "<<cow<<endl;
return bull;
}
Output:-
Enter a 4 digit number(unique digits):9857
bull= 0cow= 1
Enter a 4 digit number(unique digits):4587
bull= 0cow= 1
Enter a 4 digit number(unique digits):9785
bull= 2cow= 0
Enter a 4 digit number(unique digits):9731
bull= 3cow= 0
Enter a 4 digit number(unique digits):9751
bull= 2cow= 0
Enter a 4 digit number(unique digits):9738
bull= 3cow= 0
Enter a 4 digit number(unique digits):
9734
bull= 3cow= 0
Enter a 4 digit number(unique digits):9736
bull= 4cow= 0
Game over in 8 chances
Note:- If your number starting with zero or ending with zero this code creates problem because of % operation.If the solution permit to use integer array then problem will be solve perfectly.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.