I'm a CS student just starting out and I'm stuck with a Cows and Bulls game. It compiles and runs, but there's an error of some kind somewhere that I'm not seeing that leads to the incorrect number of Cows and Bulls (sample of expected vs. my output).
Explanation of Cows and Bulls: Bulls - the number of correct digits. Cows - the number of digits that would be correct if they were in the right position.
public class CowsAndBulls{
//Constants
public final static int NUM_DIGITS = 4;
public final static int MAX_VALUE = 9876;
public final static int MIN_VALUE = 1234;
public final static int MAX_GUESSES = 10;
//
// instances
private NumberPicker randomNumber;
private int answer;
private int guessesCopy;
private int bullStored;
private int cowStored;
//
public CowsAndBulls(int seed){
randomNumber = new NumberPicker(seed, MIN_VALUE, MAX_VALUE);
answer = randomNumber.nextInt();
guessesCopy = MAX_GUESSES;
}
////////////////////////////////////////////////////////
//Stuff between the comments is from a previous question that needs to be used in CowsAndBulls (not in a package) - I know it works as it's supposed to.
public static int[] toArray(int number){
String numString = Integer.toString(number);
int[] someArray = new int[numString.length()];
for (int i = 0; i < numString.length(); i++){
char c = numString.charAt(i);
int cVal = Character.getNumericValue(c);
someArray[i] = cVal;
}
return someArray;
}
public static int countMatches(int a, int b){ //Bulls
String stringA = Integer.toString(a);
int lengthAB = stringA.length();
int count = 0;
int[] arrayOutA = toArray(a);
int[] arrayOutB = toArray(b);
for (int i = 0; i < lengthAB; i++){
if (arrayOutA[i] == arrayOutB[i])
count += 1;
}
return count;
}
public static int countIntersect(int numA, int numB){ //Cows
String stringA = Integer.toString(numA);
int lengthAB = stringA.length();
int count = 0;
int[] arrayOutA = toArray(numA);
int[] arrayOutB = toArray(numB);
for (int i = 0; i < lengthAB; i++){
for (int j = 0; j < lengthAB; j++){
if ( arrayOutA[i] == arrayOutB[j]){
count += 1;
}
}
}
return count;
}
//////////////////////////////////////////////////////////////////
public int guessesRemaining(){
return guessesCopy;
}
public Result guess(int guessNumber){
int bulls = countMatches(answer, guessNumber);
bullStored = bulls;
int cows = countIntersect(answer, guessNumber);
cowStored = cows;
guessesCopy--;
return (new Result(cows, bulls));
}
public int giveUp(){
return (answer);
}
public boolean gameOver(){
if (guessesCopy == 0 || bullStored == 4)
return true;
else
return false;
}
Below are the supplied classes that we're supposed to use and can't edit it any way: NumberPicker, Game, and Result. NumberPicker:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* Given a number range, a NumberPicker returns the numbers in the range in a random order
*/
public class NumberPicker {
private List<Integer> numbers;
/**
* Create a NumberPicker that uses the given seed value for randomisation and that
* returns the numbers in the range min to max (inclusive) in a random order.
* /
public NumberPicker(final int seed, final int min, final int max) {
numbers = new ArrayList<Integer>();
final Random random = new Random(seed);
for(int i = min; i<max+1; i++) {
numbers.add(i);
}
Collections.shuffle(numbers, random);
}
/**
* Determine whether the NumberPicker contains any more numbers..
*/
public boolean hasNext() { return !numbers.isEmpty(); }
/**
* Return a randomly selected number from the range.
*/
public int nextInt() { return numbers.remove(0); }
}
Game class:
import java.util.Scanner;
public class Game {
private Game() {}
public static void main(String[] inputs) {
Scanner input = new Scanner(System.in);
System.out.println("Your challenge is to guess a secret " + CowsAndBulls.NUM_DIGITS + " digit number.");
System.out.println("Enter randomisation seed value:");
CowsAndBulls cowsAndBulls = new CowsAndBulls(input.nextInt());
System.out.println("Make a guess:");
Result answer = cowsAndBulls.guess(input.nextInt());
while(!answer.isCorrect()&&cowsAndBulls.guessesRemaining()>0) {
System.out.println("Sorry that's incorrect.");
System.out.println("You have "+answer+".");
System.out.printf("You have %d guesses remaining\n", cowsAndBulls.guessesRemaining());
System.out.println("Make a guess:");
answer = cowsAndBulls.guess(input.nextInt());
}
if (answer.isCorrect()) {
System.out.println("Correct !");
}
else {
System.out.println("Sorry, you lose.");
}
}
}
And finally, Result class:
/**
* A Result object records the outcome of a guess in the Cows and Bulls guessing game.
*
*
*/
public class Result {
private int cows;
private int bulls;
public Result(int cows, int bulls) {
assert(cows+bulls<=4);
this.cows=cows;
this.bulls=bulls;
}
public int cows() { return cows; }
public int bulls() { return bulls; }
public boolean isCorrect() { return bulls==4; }
public boolean equals(Object o) {
if (!(o instanceof Result)) {
return false;
}
else {
Result other = (Result)o;
return this.cows()==other.cows()&&this.bulls()==other.bulls();
}
}
public String toString() {
String result = this.cows()+(this.cows()!=1 ? " cows" : " cow");
result = result+" and "+this.bulls()+(this.bulls()!=1 ? " bulls" : " bull");
return result;
}
}
That's all the code. To reiterate: I can't change any of the classes except CowsAndBulls and I have to use Game, Result, and NumberPicker. Below are expected output vs. what my program is producing...
Trial 1: Output not correct
The expected output was:
10
false
8913
true
Your program produced:
10
false
7407
false
Input supplied to your program:
construct 3
guessesRemaining()
gameOver()
giveUp()
gameOver()
Q
-------------------------------------
Trial 2: Output not correct
The expected output was:
10
0 cows and 0 bulls
9
1 cow and 0 bulls
8
2 cows and 0 bulls
7
3 cows and 0 bulls
6
4 cows and 0 bulls
5
Your program produced:
10
1 cow and 0 bulls
9
0 cows and 0 bulls
8
1 cow and 0 bulls
7
2 cows and 0 bulls
6
2 cows and 0 bulls
5
Input supplied to your program:
construct 4
guessesRemaining()
guess() 2358
guessesRemaining()
guess() 1235
guessesRemaining()
guess() 1735
guessesRemaining()
guess() 1749
guessesRemaining()
guess() 1746
guessesRemaining()
Q
----------------------------------------------
Trial 3: Output not correct
The expected output was:
10
0 cows and 0 bulls
9
0 cows and 1 bull
8
0 cows and 2 bulls
7
0 cows and 3 bulls
6
0 cows and 4 bulls
5
true
Your program produced:
10
1 cow and 0 bulls
9
1 cow and 0 bulls
8
1 cow and 0 bulls
7
1 cow and 0 bulls
6
1 cow and 0 bulls
5
false
Input supplied to your program:
construct 8
guessesRemaining()
guess() 2358
guessesRemaining()
guess() 2758
guessesRemaining()
guess() 2748
guessesRemaining()
guess() 6748
guessesRemaining()
guess() 6741
guessesRemaining()
gameOver()
Q
It might just be something dumb that I'm not seeing, but any help is greatly appreciated. I'm new to S.E (and Java), so the formatting of my code might be weird - if so, I'll edit it. Thanks :).
assert(cows+bulls<=4);
is checked. their sum can be 8 at max. that happens whenanswer
andguessNumber
are absolutely identical, in that case cows=4 and bulls=4