Solved

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

Posted on 2006-07-05
7
439 Views
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;
                         (bullsCowsScore[number_of_guesses][i]).repaint();
                         
                   }
         
         for (int j = bulls; j<(bulls + cows); j++) {
                   (bullsCowsScore[number_of_guesses][j]).fillCircle = Color.WHITE;
                         (bullsCowsScore[number_of_guesses][j]).repaint();
                         
                   }
                            
            if (bulls==4) {
                  winLooseLabel.setText(" YOU WON! ");
                  
                  restartLabel.setText("<html> Click the green peg to </n> restart </n><html>");
                  showSecretCode();
                  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>");
                showSecretCode();
            }
            
      
}      
0
Comment
Question by:BUCKA
  • 3
  • 2
  • 2
7 Comments
 
LVL 8

Expert Comment

by:colr__
Comment Utility
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!!!
0
 

Author Comment

by:BUCKA
Comment Utility
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.
0
 
LVL 8

Expert Comment

by:colr__
Comment Utility
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).
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
Hi BUCKA,

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
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 500 total points
Comment Utility
>>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

0
 

Author Comment

by:BUCKA
Comment Utility
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
0
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
:-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 70
allStar challenge 1 40
Securing Jmx Console and web console 2 45
ArrayIndexOutOfBoundException 9 29
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now