Question

For those who aren't familiar, the game is a number guessing game, where a number is chosen (non repeating; e.g 1223 is NOT chosen) and the user makes a guess and obtains information whether the number AND digit is correct, number is correct but in a wrong digit, or the number is not contained. http://en.wikipedia.org/wiki/Bulls_and_cows

(e.g number chosen => 1234, guessing 3789 will give 1 cow)

Instead of the computer choosing the number and telling the properties and player guesses, I would like to do the reverse; I type in a number and the properties - the computer gives a list of possible numbers.

Anyways, my method is:

  1. Add all numbers that do not repeat itself to the arraylist
  2. Delete numbers that do not satisfy the conditions.

Here is how the cow cases are done :

     //Case 5: property is 4 cows;
     if (property.equals("040")) {
        //delete if numbers don't appear EXACTLY 4 times
        if (contains != 4) { numbers.remove(i); }

        //removes if the digits of the number tried corresponds with the actual number (Cow!)
        else if (n.charAt(0) == first.charAt(0)) { numbers.remove(i); }
        else if (n.charAt(1) == second.charAt(0)) { numbers.remove(i); }
        else if (n.charAt(2) == third.charAt(0)) { numbers.remove(i); }
        else if (n.charAt(3) == fourth.charAt(0)) { numbers.remove(i); }
     }

It has worked for cows. Upon trying to implement bulls, it seems like using this sort of approach won't be possible. How can I do a method for bulls!? Would I need to create four more arraylists and calculate for each case? Or is ArrayList not the way to go?

For example, 1234 with 1bull would mean the number to guess is 1XXX, X2XX, XX3X or XXX4 but I can't use this approach as it will delete all number except the input.

Thanks.

Was it helpful?

Solution

You can try this algorithm for solving -

String userAnswer = // ... getUserAnswer();        
if(userAnswer == null || userAnswer.equals("")
        || !userAnswer.matches("^-?\\d+$") 
            || userAnswer.split("(?<=\\G.{1})").length < 4) {
    // error    
}                

int[] secret = (int[])// ... getSecret(request);        
int[] seq = {1,2,3,4};

for(int i = 0; i < userAnswer.split("(?<=\\G.{1})").length; i++) {
    seq[i] = Integer.parseInt(s[i]);
}

int bullCount = 0;
int cowCount = 0;

for(int i = -1; ++i < secret.length;) {
  if(secret[i] == seq[i]) {
    bullCount++;
  }
}

for(int i = -1; ++i < secret.length;) {
  for(int j = -1; ++j < secret.length;) {
    if(secret[i] == seq[j] && i != j) {
      cowCount++;
    }
  }
}

String snswer = bullCount + "b" + cowCount +  "c";            
if(Arrays.equals(secret, seq))
 // win! 
else
// fail!

OTHER TIPS

It's wrong as if the code is 1634 and the user enters 6113, the program returns 4 cows when there is in fact 3.

Try this:

    private static String findNumberOfCowsAndBulls(String firstString, String secondString) {

    if(firstString.equals(secondString))
        return "All Bulls:" + firstString.length();

    char[] fSArr = firstString.toCharArray();
    char[] sSArr = secondString.toCharArray();
    int countCow = 0;
    int countBull = 0;
    Map<String, Integer> fSMap = new HashMap<>();
    Map<String, Integer> sSMap = new HashMap<>();

    for (int i = 0; i < fSArr.length; i++) {
        if(i < sSArr.length){
            if(fSArr[i] == sSArr[i]){
                countBull++;
            }
            else{
                updateMapOfCharsCount(fSMap, fSArr[i]);
                updateMapOfCharsCount(sSMap, sSArr[i]);
            }
        }
        else{  //fSArr is bigger than sSArr
            updateMapOfCharsCount(fSMap, fSArr[i]);
        }
    }

    if(fSArr.length < sSArr.length){  //fSArr is shorter than sSArr
        for(int i = fSArr.length; i < sSArr.length - fSArr.length; i++){
            updateMapOfCharsCount(sSMap, sSArr[i]);
        }
    }

    for (Map.Entry<String, Integer> entry : fSMap.entrySet()) {
        String key = entry.getKey();
        if(sSMap.containsKey(key)){
            if(sSMap.get(key) <= fSMap.get(key))
                countCow = countCow + sSMap.get(key);
            else
                countCow = countCow + fSMap.get(key);
        }
    }

    return "countCow = " + countCow + " countBull = " + countBull;
}

private static void updateMapOfCharsCount(Map<String, Integer> fsMap, char c) {
    String key1 = String.valueOf(c);
    if (fsMap.containsKey(key1)) {
        fsMap.put(key1, fsMap.get(key1) + 1);
    } else
        fsMap.put(key1, 1);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top