我是一名CS學生,剛剛開始,我被困在奶牛和公牛隊的比賽中。它編譯並運行,但是某種地方存在某種錯誤,我沒有看到導致牛和公牛的錯誤數量(預期與我的輸出樣本)。奶牛和公牛隊代碼--Japan
母牛和公牛的解釋:公牛 - 正確的位數。母牛 - 如果它們處於正確位置,那麼它們的位數就是正確的。
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;
}
下面是我們應該使用並不能編輯它的任何方式所提供的課程:NumberPicker,遊戲,和結果。 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); }
}
遊戲類:
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.");
}
}
}
最後,結果等級:
/**
* 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;
}
}
這是所有的代碼。重申:除了CowsAndBulls之外,我不能更改任何類,我必須使用Game,Result和NumberPicker。下面是預期產出與什麼我的計劃是生產...
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
這可能只是愚蠢的事情是我沒有看到,但任何幫助是極大的讚賞。我是S.E(和Java)的新手,所以我的代碼的格式可能很奇怪 - 如果是這樣,我會編輯它。謝謝 :)。
我不明白爲什麼'assert(cows + bulls <= 4);'被選中。他們的總和最多可以是8。當'answer'和'guessNumber'完全相同時,會發生這種情況,在這種情況下,奶牛= 4,公牛= 4 – mangusta
@mangusta這是他的一個問題。公牛不應該包括在計數中。他還對一些奶牛進行了重複計數。 – user254948
只是一個瘋狂的猜測:你應該生成一個由4個不同的數字組成的數字。而你的公牛和奶牛的數量對於這樣一個數字是正確的。相反,你在1234到9876的間隔內產生一個數字。所以你可能會得到,例如1555,並且有一些相同的數字,你的方法似乎不再能正確工作。 –