The Cows and Bulls game is an old game that asks each person to guess a number. Each incorrect guess returns a hint and each hint consists of the numbers of bulls and cows in the guess. A bull is the correct digit in the correct position. A cow is the correct digit in the wrong position. For example, if the secret number is 2045, and the guess is 5248, the hint would be that the guess has 1 bull (4) and 2 cows (5 and 2).
The following code presents this game in the form of a Java program:
import java.io.File;import java.util.Scanner;import java.io.FileNotFoundException;class CowAndBull {public static String hint(String secret, String guess){// Initializing count variablesint bullCount = 0;int cowCount = 0;// Initializing arrays// Each array contains the count for each digit// at its corresponding indexint []secDigitCount = new int[10];int []guessDigitCount = new int[10];for (int i = 0; i < Math.min(secret.length(), guess.length()); i++){// Getting characterschar secretChar = secret.charAt(i);char guessChar = guess.charAt(i);if (secretChar == guessChar){// Incrementing bull countbullCount += 1;}else {// Incrementing digit countssecDigitCount[secretChar - '0'] += 1;guessDigitCount[guessChar - '0'] += 1;}}// If statements and loops to handle remaining// characters in case of string length differencesif (secret.length() > guess.length()){for (int i = guess.length(); i < secret.length(); i++){secDigitCount[secret.charAt(i) - '0'] += 1;}}else if (secret.length() < guess.length()){for (int i = secret.length(); i < guess.length(); i++){guessDigitCount[guess.charAt(i) - '0'] += 1;}}// Loop to count Cowsfor (int i = 0; i < 10; i++){cowCount += Math.min(secDigitCount[i], guessDigitCount[i]);}return bullCount + " Bulls, " + cowCount + " Cows";}public static void main( String args[] ) {// File reading for inputtry{File myObj = new File("__ed_input.txt");Scanner inReader = new Scanner(myObj);String secret = inReader.nextLine();// Loop for all guesses in input fileString guess;while(inReader.hasNextLine()){guess = inReader.nextLine();System.out.println(hint(secret, guess));}inReader.close();}catch (FileNotFoundException e) {System.out.println("An error occurred.");}}}
Enter the input below to be saved in file __ed_input.txt
This program needs input for it to run. Below is a sample input where the first number is the secret followed by two guesses.
204540252045
This program offers a simple solution to the problem and uses arrays to give an overall time complexity per guess (where n is the number of characters in secret
or guess
, whichever is larger). The program takes input from a file called __ed_input.txt
. The program is designed to take as many guesses as input as needed from the file. As described before, the input should be formatted with the secret being on the top line followed by one guess per line.
The hint()
function generates a hint for each input. The first for loop runs equal to the number of digits in either secret
or guess
, whichever is smaller. This loop traverses the characters and compares them at each index. For every match, the bull-count goes up.
To count cows, the program counts every digit in secret
and guess
that do not form a bull-pair. Two arrays, secDigitCount
and guessDigitCount
, are defined. These arrays store the count for each digit at its respective index. For instance, secDigitCount[4]
contains the number of 4s in the secret. The value assignments for these arrays use the indexing expression [character - '0']
to transpose the converted ASCII digit values into their corresponding integers.
The if statements following the first loop count the remaining digits for the larger string out of secret
and guess
if string sizes differ. In the last for loop, the cows are incremented. The min
function is applied to take care of duplicate digits in the secret or guess. For instance, if the secret is 2045 and the guess is 3225, the number of cows should be 1, not 2.
Free Resources