Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My assignment is to write a program in Java for the game Bulls and Cows. The pro

ID: 3537773 • Letter: M

Question

My assignment is to write a program in Java for the game Bulls and Cows. The program is supposed to generate a random 4-digit number with unique digits. Players then guess the number and are told how many digits they guessed in the right place (bulls) and how many digits they guessed in the wrong place (cows). Ex: if the computer-generated number is 0950, the guess 0123 would return 1 bull and 0 cows. The guess 1565 would return 0 bulls and 1 cow (as there is only one 5 in the answer, only one of the 5s in the guess is counted as a cow.) I have attempted the assignment and realize that the algorithm is fairly easy but am having a lot of trouble with Java syntax and need some help.



My first class is a very simple tester class:



public class Tester {

public static void main (String[] args) {

Game g = new Game();

g.play();

}

}



The game class orders the turns of the computer class, which I have called the oracle, and the player class:



public class Game {

int turns = 0;

Player p = new Player();

Oracle o = new Oracle();

public void play () {

o.generateNumber();

int bulls = 0;

int cows = 0;

while (bulls<4){

p.makeGuess();

o.compare ();

System.out.println(bulls + " bull(s) and " + cows + " cow(s)");

turns++;

}

System.out.println("Correct! You took " + turns + " guesses.");

}

}



Player class:



import java.util.Scanner;

public class Player {

String yourGuess;

public String makeGuess() {

Scanner in = new Scanner(System.in);

System.out.println("Guess a four-digit number");

String yourGuess = in.next();

return yourGuess;

}

public String getString(String yourGuess){

yourGuess=yourGuess;

}

return yourGuess;

}

}



Oracle class:



import java.util.Random;

public class Oracle {

public Oracle {}

public String generateNumber() {

Random generator = new Random();

int a = 0;

int b = 0;

int c = 0;

int d = 0;

while(a==b|a==c|a==d|b==c|b==d|c==d){

a = generator.nextInt(10);

b = generator.nextInt(10);

c = generator.nextInt(10);

d = generator.nextInt(10);

}

String generatedNumber = "" +a+b+c+d;

return generatedNumber;

}

public static void compare (String generatedNumber, String yourGuess){

p.getString();

for (int i=0; i<4; i++) {

for (int j=0; j<4; j++) {

if (str1.charAt(i)==str2.charAt(j)) {

if (i==j) {

bulls++;}

else {

cows++;

}

}

}

}

}

}


Any help would be appreciated.

Explanation / Answer

public class Tester
{
public static void main (String[] args)
{
Game g = new Game();
g.play();
}
}

public class Game
{
int turns = 0;
Player p = new Player();
Oracle o = new Oracle();
public void play ()
{
String generated_number = o.generateNumber();
System.out.println(" generated_number is " + generated_number);
int[] a= {0,0};
while(a[0]<4)
{
String Guessed_number = p.makeGuess();
// a[0] represents bulls and a[1] represents cows.
// we need this information to be returned from compare function
// so used array here.
// array is the only way to return multiple values from function.
o.compare(generated_number,Guessed_number, a);
System.out.println(a[0] + " bull(s) and " + a[1] + " cow(s)");
turns++;
}
System.out.println("Correct! You took " + turns + " guesses.");
}
}

import java.util.Scanner;
public class Player
{
String yourGuess;
public Player(){}
public String makeGuess()
{
Scanner in = new Scanner(System.in);
System.out.println("Guess a four-digit number");
return in.next();
}
}

import java.util.Random;
public class Oracle
{
public Oracle() {}
public String generateNumber()
{
Random generator = new Random();
int a = 0;
int b = 0;
int c = 0;
int d = 0;
while(a==b||a==c||a==d||b==c||b==d||c==d)
{
a = generator.nextInt(10);
b = generator.nextInt(10);
c = generator.nextInt(10);
d = generator.nextInt(10);
}
String generatedNumber = "" +a+b+c+d;
return generatedNumber;
}
public static void compare(String generatedNumber, String yourGuess, int[] a)
{
a[0] = a[1]=0;
for (int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
if (generatedNumber.charAt(i)==yourGuess.charAt(j))
{
if (i==j)
{
a[0]++;
}
else
{
a[1]++;
}
}
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote