私はCSの学生です。私は牛と雄牛のゲームで立ち往生しています。それはコンパイルされ、実行されますが、私はそれが牛とブルズの誤った数(予想されるサンプルと私の出力のサンプル)につながることを見ていない何らかのエラーがあります。牛と雄牛のコード - Java
牛と雄牛の説明:雄牛 - 正しい桁数。牛 - 正しい位置にあった場合に正しい数字の桁数。 NumberPicker、ゲーム、および結果:
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:
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'が全く同じである場合に起こります。その場合、Cows = 4とBulls = 4 – mangusta
@mangusta彼の問題の1つです。ブルにはカウントに含めてはいけません。彼はまた、いくつかの牛を数えて二倍にしている。 – user254948
野生の推測:あなたは4桁の数字からなる数字を生成することになっていました。そしてあなたの雄牛と牛の数は、そのような数に対して正しく働きます。代わりに、1234から9876までの間に数値を生成します。したがって、たとえば1555を得ることができ、いくつかの同じ数字では、あなたのメソッドは正しく機能しないように見えます。 –