Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code to implement logic for the Bulls and Cows game:

Bulls and Cows is a 2 player game. The computer thinks of a number, while the other player tries to guess it.

The number to be guessed must be a 4-digit number, using only digits from 1 - 9 and between the numbers 1000 and 9999, each digit at most once. e.g. 1234 is valid, 0123 is not valid, 9877 is not valid, 9876 is valid.

For every guess that the player makes, he gets 2 values : the number of bulls and the number of cows.

1 bull means the guess contains and the target number have 1 digit in common, and in the correct position.
1 cow means the guess and the target have 1 digit in common, but not in correct position.
Let the target be 1234. Guessing 4321 will give 0 bulls and 4 cows. 3241 will give 1 bull and 3 cows

When I try it, I get the wrong output:

Enter a Number : 
3054
Number Generated by Computer: 2441
Input was : 3054
0 bulls and 2 cows


There should be 1 cow instead of 2 cows.

What is wrong with the code, and how do I fix it? What changes should I make in my conditional statements to get the accurate answer? please suggest

What I have tried:

import java.util.*;
public class Bulls_Cows
{
    int num,bulls,cows,random_int,temp,temp_rand,temp1_rand;
    static int digit[] = new int[4];
    static int digit_rand[] = new int[4];
    public void input()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a Number : ");
        num = sc.nextInt();
    }
    
    public void checkBullorCow()
    {
        temp = num;
        random_int = (int)(Math.random()*9000+1000);
        temp_rand = random_int;
        temp1_rand = random_int;
        for(int i=0;i<4;i++)
        {
            digit[i]=temp/(int)Math.pow(10,i)%10;
            digit_rand[i]=temp_rand/(int)Math.pow(10,i)%10;
        }
        
        for (int j = 0; j < digit.length; j++) 
        {
          if (digit[j] == digit_rand[j]) 
            bulls++;
        
        for (int k = 0; k < digit_rand.length; k++) 
        {
          if (k!=j&&digit[j]==digit_rand[k]) 
            cows++;
        }
        }   
    }
    
    public void output()
    {
        System.out.println("Number Generated by Computer: "+temp1_rand);
        System.out.println("Input was : "+num);
        System.out.println(bulls+" bulls and "+cows+" cows");
    }
    //Main method 
    public static void main()
    {
      Bulls_Cows ob = new Bulls_Cows();
      ob.input();
      ob.checkBullorCow();
      ob.output();
    }
}
Posted
Updated 4-Jun-23 2:01am
Comments
Richard MacCutchan 4-Jun-23 4:28am    
Each digit in the guess can be either a bull or a cow, or neither. So you first need to check if it's a bull. If so, then stop looking any further. If it is not a bull, then check if it is a cow. But only use a flag for yes, or no, not a count.

I see a problem in your sample!
Quote:
each digit at most once. e.g. 1234 is valid, 0123 is not valid, 9877 is not valid, 9876 is valid.

Your computer generated input is
Java
Number Generated by Computer: 2441

2441 is not valid as per the rule.

Enforcing this rule or not changes the solving algorithm.
 
Share this answer
 
You only need to count for cows once, so change the search to the following:
Java
for (int i = 0; i < 4; ++i) {
    if (digit[i] == digit_rand[i]) {
        bulls++;
    }
    else { // only do the second test if not a bull
        for (int j = 0; j < 4; ++j) {
            if (j != i && digit[j] == digit_rand[i]) {
                cows++; // a match but not in the right place
                break;  // stop searching if a match was found
            }
        }
    }
 
Share this answer
 
Comments
@Coder10thgrader 6-Jun-23 3:41am    
Thank you so much @Richard MacCutchan
Richard MacCutchan 6-Jun-23 4:08am    
You are welcome.
Start by thinking about how you would do it manually: I'd make a copy of the target, then I'd count and remove all the bulls from the target copy by replacing it with an unmatchable value. If you don't do that, then if the target is 1234 and the guess is 5226 it will identify 1 bull and 1 cow because it will match the second two to the already found bull.

Then check for cows and count them, again removing matched values so that 1234 doesn't find 2 cows with 4456 as a guess.

This may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
@Coder10thgrader 4-Jun-23 7:23am    
Hi @OriginalGriff
Could you please write the code? Because i am not understanding as to how should I do it
OriginalGriff 4-Jun-23 7:43am    
No - that would defeat the purpose of you being given the homework! :D
Read the link I gave you and give it a try. It's not complicated really - it just seems that way because you haven't built up the skills yet.
@Coder10thgrader 6-Jun-23 3:45am    
True @OriginalGriff
I am a striving coder who is just trying to build up the skills. You were right, it was not complicated after all. It's just that I am not as experienced as you are sir ;D
OriginalGriff 6-Jun-23 4:07am    
You'll get there!
The way to build the skills is to try and use them ... and remember that you learn more by making mistakes than you do by getting it right. :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900