Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Bulls&Cows game - how to figure out no. of cows

Posted on 2006-07-05
Medium Priority
Last Modified: 2008-02-01
Hi Experts
I'm writing an applet, Bulls&Cows game, as part of my java exam.
I have difficulties to check how many cows ( same color - different position) there are after comparing combination of user chosen colors (userGuesses[number_of_guesses][i]) and secretCode colors (secretCode[i]).
Retrieving number of bulls (same color - same position) is working OK, I think.
This is very urgent for me.

Thanks a lot in advance!

Here is my code for doComparing method:

      public void doComparing() {      
      int bulls = 0; // number of bulls
      int cows = 0;  // number of cows
           // four pegs have been selected
   // count up the number of bulls and cows
   if ((userGuesses[number_of_guesses][0]).fillCircle.equals((secretCode[0]).fillCircle)) {bulls++;}  
          else if ((userGuesses[number_of_guesses][0]).fillCircle.equals((secretCode[1]).fillCircle)) {cows++;}
         else if ((userGuesses[number_of_guesses][0]).fillCircle.equals((secretCode[2]).fillCircle)) {cows++;}
         else if ((userGuesses[number_of_guesses][0]).fillCircle.equals((secretCode[3]).fillCircle)) {cows++;}
         if ((userGuesses[number_of_guesses][1]).fillCircle.equals((secretCode[1]).fillCircle)) {bulls++;}
           else if ((userGuesses[number_of_guesses][1]).fillCircle.equals((secretCode[0]).fillCircle)) {cows++;}
           else if ((userGuesses[number_of_guesses][1]).fillCircle.equals((secretCode[2]).fillCircle)) {cows++;}
           else if ((userGuesses[number_of_guesses][1]).fillCircle.equals((secretCode[3]).fillCircle)) {cows++;}
         if ((userGuesses[number_of_guesses][2]).fillCircle.equals((secretCode[2]).fillCircle)) {bulls++;}
           else if ((userGuesses[number_of_guesses][2]).fillCircle.equals((secretCode[0]).fillCircle)) {cows++;}
           else if ((userGuesses[number_of_guesses][2]).fillCircle.equals((secretCode[1]).fillCircle)) {cows++;}
           else if ((userGuesses[number_of_guesses][2]).fillCircle.equals((secretCode[3]).fillCircle)) {cows++;}
         if ((userGuesses[number_of_guesses][3]).fillCircle.equals((secretCode[3]).fillCircle)) {bulls++;}
           else if ((userGuesses[number_of_guesses][3]).fillCircle.equals((secretCode[0]).fillCircle)) {cows++;}
           else if ((userGuesses[number_of_guesses][3]).fillCircle.equals((secretCode[1]).fillCircle)) { cows++;}         
           else if ((userGuesses[number_of_guesses][3]).fillCircle.equals((secretCode[2]).fillCircle)) {cows++;}
         for (int i = 0; i<bulls; i++) {
                    (bullsCowsScore[number_of_guesses][i]).fillCircle = Color.BLACK;
         for (int j = bulls; j<(bulls + cows); j++) {
                   (bullsCowsScore[number_of_guesses][j]).fillCircle = Color.WHITE;
            if (bulls==4) {
                  winLooseLabel.setText(" YOU WON! ");
                  restartLabel.setText("<html> Click the green peg to </n> restart </n><html>");
                  finished =true;
            //      doReset();
            if ((number_of_guesses == 9) && (bulls!=4)) {
                  finished = true;
                  winLooseLabel.setText(" YOU LOOSE! ");                  
                  restartLabel.setText("<html> Click the green peg to </n> restart </n><html>");
Question by:BUCKA
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2

Expert Comment

ID: 17042706
Tut tut, homework questions are not allowed on EE, never mind exam questions...

If you tell us more about the problem you're having we can steer you in the right direction, but not do it for you!!!

Author Comment

ID: 17042927
Fair enough,
I didn't expect code of course, just idea where I'm making mistake in logic for checking equality.
I was trying to do checking using 2 nested for loops and excluding number of found bulls but it doesn't make much more difference. That's my problem.

Expert Comment

ID: 17043245
Try using == instead of .equals().  I'm not sure, but this might make a difference since its static final constants you're using to check the equality with (i.e. Color.BLACK etc.).

== checks to see if the references being cmpared are in fact poiting to the same ovbject. As such, since the Color.BLACK etc is static and final, if you use these static final fields to set your colours, then the == should work (as static means 'only one copy of, belonging to the class).
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

LVL 13

Expert Comment

ID: 17043767

You should use 'for' loop instead of multiple if.

you need an array of boolean (local variable)   used=new boolean[4]; // 4 ==secretCode.length

first found the correct colors at correct place :
  - for each i in 0..3 :
       - if userGuesses[i] == secretCode[i] then   bulls++  used[i]=true

then find the other guesses in secretcode :
  - for each i in 0..3 :
    - for each j in 0..3 :
       -if  !used[j] and userGuesses[i] == secretCode[j] then   cows++  used[j]=true
LVL 13

Accepted Solution

Webstorm earned 2000 total points
ID: 17043803
>>then find the other guesses in secretcode :
correction, you also need a second boolean array, initialized by the first one :
  boolean[] used_g=(boolean[])used.clone();
  - for each i in 0..3 :
    -if  !used_g[i]
     - for each j in 0..3 :
       -if  !used[j] and userGuesses[i] == secretCode[j] then   cows++  used[j]=true


Author Comment

ID: 17046228
Hi Webstorm
You are genious!!!
I implemented what you suggested and it works perfectly now.
Thank you, thank you, thank you!
Well deserved 500 points
LVL 13

Expert Comment

ID: 17049130

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question