Question

In: Computer Science

1. Display a total count of guesses with a new generated guess, which should be a 5-char string with '@' always included and the rest four characters drawn from any of the below 3 sets

Create Java game on How to play Bulls and Cows between you (the host) and your computer (the player)

When completed, your program allows you to host the game (i.e., you create a secret word and score every guess) and it makes guesses until it wins.

In this player mode, your program should:

1. Display a total count of guesses with a new generated guess, which should be a 5-char string with '@' always included and the rest four characters drawn from any of the below 3 sets

characters:

{A, B, C, D, E}

{ 5, 6, 7, 8, 9 }

{ =, ?, %, @, $ }

2. Wait for a score (e.g., '2Bull, 3Cow') to be returned and entered by the host, who is you. It's suggested the score is entered on the same line of the the count and guess displayed in the above step. So, it would look like


    • Guess #3 8@XYZ   1Bull, 2Cow

    • Guess #4 5@WYZ   1Bull, 1Cow

    • Guess #5 @8=%Z 4Bull, 0Cow   

  1. Repeat the above two steps until the guess wins a score of '5Bull, 0Cow' as shown below.

    • Guess #3 8@XYZ   1Bull, 2Cow

    • Guess #4 5@WYZ   1Bull1Cow

    • Guess #5 @8=%Z 4Bull, 0Cow

    • Guess #6 @8=6Z 5Bull, 0Cow -- You won!!

Except for the very first guess, your program should implement a strategy or formula to generate a new guess based on all scores collected. A good strategy or formula should use as much as possible the information or hints provided by the scores to quickly approach the secret word. This would be the most challenging component to be implemented in the entire program.

Hint: one possible method is to generate a list of all possible secret words (using all scores returned) that could be the next guess, then to prune the list by keeping only those words that would give an equivalent score to how the last guess was scored. Then, the next guess can be any word from the pruned list. Either the guess correctly hits the secret word or run out of words to guess, which indicates a problem with the scoring or a problem with the list itself.

Please, keep in mind that the computer is the player, the one that has to guess the inputted String by the user.

Solutions

Expert Solution

java code:


package bullandcowdriver;

import java.util.Random;
import java.util.Scanner;


public class BullAndCowDriver {

  
public static void main(String[] args) {
  
//ariable declaration
String secret = "" , guess;
char[] secret1=new char[5];
Scanner sc=new Scanner(System.in);
Random rn=new Random();
int n=0,r,flag=0;
boolean over=true;
  
//characters
char[] alpha={'A','B','C','D','E', '=', '?', '%', '$' };
  
//place '@'
r= rn.nextInt(5);
secret1[r]='@';
  
//place other characters in the secret word
while(n<5){
flag=0;
r= rn.nextInt(9);
  
//check for char already present
for(int i=0;i {
if(secret1[i]==alpha[r]){
flag=1;
n--;
}
  
}
//if not place the character
if(secret1[n]!='@' && flag==0){
secret1[n]=alpha[r];
  
}
n++;
}
  
//assign to string
for(int i=0;i secret+=secret1[i];

int k=1;
  
//call the check functon
do{
System.out.print("Guess # "+k+": ");
guess=sc.next();
  
//if won print the msg
if(check(secret,guess)==1){
System.out.println(" -- You won!!");
over=false;
}
k++;
}while(over);


  
}

//check the words
static public int check(String secret, String guess) {
int countBull=0;
int countCow =0;

//check the bull
for(int i=0; i<5; i++){
if(secret.charAt(i)==guess.charAt(i))
countBull++;
}
//check the cow
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
if(secret.charAt(i)==guess.charAt(j))
countCow++;
}
}
//actuall cow count
countCow = countCow-countBull;

//print the no
System.out.println(countBull+"Bull "+countCow+"Cow");

if(countBull==5)
return 1;
return 0;
}
}

output:


Related Solutions

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT