Question

/**    * Returns the sum of "black hits" and "white hits" between the hiddenCode and...

/**
   * Returns the sum of "black hits" and "white hits" between the hiddenCode and
   * guess. A "black hit" indicates a matching symbol in the same position in the
   * hiddenCode and guess. A "white hit" indicates a matching symbol but different
   * position in the hiddenCode and guess that is not already accounted for with
   * other hits.
   *
   * Algorithm to determine the total number of hits:
   *
   * Count the number of each symbol in the hiddenCode, and separately count the
   * number of each symbol in the guess. For each symbol, determine the minimum of
   * the count of that symbol in the hiddenCode and the count of that symbol found
   * in the guess. The total number of hits, black and white, is the sum of these
   * minimums for all the symbols.
   *
   * Algorithm based on Donald Knuth, 1976, The Computer As Master Mind, J.
   * Recreational Mathematics, Vol. 9(1)
   *
   * Suggestion: To do the count, create an array of int the length of the number
   * of symbols. For each symbol use the indexOf method you wrote to determine the
   * index in the array to increment the symbols count.
   *
   * @param hiddenCode The code hidden from the user.
   * @param guess The user's guess of the code.
   * @param symbols The possible symbols in the hiddenCode and guess.
   * @return The total number of hits.
   */
   public static int countAllHits(char[] hiddenCode, char[] guess, char[] symbols) {
       int a[] = new int[symbols.length];
       int b[] = new int[symbols.length];
       int c, count = 0;
       for (int i = 0; i < symbols.length; i++) {
           for (int j = 0; j < hiddenCode.length; j++) {
               if (symbols[i] == hiddenCode[j]) {
                   a[i]++; // counting the minimum of count of each symbol in hiddenCode
                   // Storing it in the array a at the index of symbol used
                   break;
               }
           }
       }
       for (int i = 0; i < symbols.length; i++) {
           for (int j = 0; j < guess.length; j++) {
               if (symbols[i] == guess[j]) {
                   b[i]++; // counting the minimum of count of each symbol in Guess
                   // Storing it in the array a at the index of symbol used
                   break;
               }
           }
       }
       for (c = 0; c < a.length; ++c)
           if (a[c] == 1 && b[c] == 1) // comparing two arrays if they match that means a hit so increment count
               count++;
       return count;
   }

   /**
   * Returns the number of each kind of hit the guess has with the code. The
   * results are an array of length Config.HITS_ARRAY_LENGTH. The count of the
   * number of symbols in the guess that correspond in position and symbol with
   * the hidden code are recorded in the Config.BLACK_HITS_INDEX position within
   * the result array. The number of symbols that match between the guess and the
   * hidden code but are in different positions and not otherwise counted are
   * recorded in the Config.WHITE_HITS_INDEX within the result array.
   *
   * Algorithm: Count the number of black hits - the number of positions in the
   * guess and hidden code that have the same symbol. Count the total number of
   * hits using countAllHits and subtract to find the number of white hits. White
   * hits are symbols that match between guess and hiddenCode that are not in the
   * same position and not already accounted for with other hits.
   *
   * @param hiddenCode The code the user is trying to guess.
   * @param guess The user's guess.
   * @param symbols The possible symbols in the hiddenCode and guess.
   * @return The array containing the number of "black hits" and "white hits".
   */
   public static int[] determineHits(char[] hiddenCode, char[] guess, char[] symbols) {
       return null; // TODO replace
   }

   /**
   * Prints out the game board showing the guesses and the corresponding hits. See
   * output examples. Game board example: 6) [4, 5, 2, 4] BBBB 5) [4, 4, 2, 5]
   * BBWW 4) [4, 4, 2, 4] BBB 3) [1, 3, 3, 3] 2) [2, 3, 3, 3] W 1) [1, 1, 2, 2] B
   *
   * Only rows with non-null guesses are shown. The number on the left is the
   * guess, so the guesses are shown from last to first. Looking at one line in
   * detail: 5) [4, 4, 2, 5] BBWW ^^ 2 white hits, the 2nd 4 and 5 (we don't know
   * which until solved) ^^ 2 black hits, the 1st 4 and 2 (we don't know which
   * until solved) ^^^^^^^^^^^^ the guess output using Arrays.toString() ^^ the
   * guess number The symbols B and W are the characters from
   * Config.BLACK_HIT_SYMBOL and Config.WHITE_HIT_SYMBOL. All the black hits will
   * be shown before the white hits. The length of all arrays should be determined
   * using the array .length attribute, not assumed from a constant.
   *
   * @param guesses The array of guesses. Each row is a guess or null (meaning no
   * guess yet).
   * @param hits The array of hits. Each row is the hits from determineHits for
   * the corresponding guess in the parallel guesses array, or
   * null.
   */
   public static void printBoard(char[][] guesses, int[][] hits) {
   }

So my question is how to wrtie the second two. I wrote the first one. Let me know if more information is needed.

Below are any needed constants...

public class Config {

/**
* A debugging technique is to add statements like the following at key places in your program:
* if (Config.DEBUG) { System.out.println("DEBUG: value=" + value); } Then you can turn on or
* off all these statements simply by changing the value of DEBUG here.
*/
static final boolean DEBUG = false;

/**
* The acceptable set of symbols and length for a code. Your code should
* use CODE_SYMBOLS and CODE_POSITIONS for the symbols and number of positions.
*
* With 6 symbols and 4 positions there are 6*6*6*6 == 6^4 == 1,296 possible
* codes. With 8 symbols and 6 positions there are 8*8*8*8*8*8 == 8^6 == 262,144
* possible codes. This is referred to as exponential growth. A problem that
* seems just a little bit bigger actually may be much more difficult to solve.
*/
public static final char[] CODE_SYMBOLS = new char[] {'1', '2', '3', '4', '5', '6'};
public static final int CODE_POSITIONS = 4;

/**
* The maximum number of guesses for a user to win.
*/
public static final int MAX_GUESSES = 10;

/**
* The length of the hits array
*/
public static final int HITS_ARRAY_LENGTH = 2;

/**
* A "black hit" indicates a matching symbol in the same position in the
* hidden code and guess.
*/
public static final int BLACK_HITS_INDEX = 0;

/**
* A "white hit" indicates a matching symbol but different position in the
* hidden code and guess that is not already accounted for with other
* hits.
*/
public static final int WHITE_HITS_INDEX = 1;

/**
* The symbol that indicates a black hit.
*/
public static final char BLACK_HITS_SYMBOL = 'B';

/**
* The symbol that indicates a white hit.
*/
public static final char WHITE_HITS_SYMBOL = 'W';
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hits.java:

import java.util.*;
public class Hits {
public static void main(String args[]) {
int k,len;
Scanner in = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
System.out.println("Enter length of hiddenCode array");
len = in.nextInt();
char hiddenCode[] = new char[len];


System.out.println("Enter array elements of hiddenCode:");
for(int i=0;i<len;i++)
{
hiddenCode[i] = sc.nextLine().charAt(0);
}
System.out.println("Enter length of guess array");
len = in.nextInt();
char guess[] = new char[len];
System.out.println("Enter array elements of Guess:");
for(int i=0;i<len;i++)
{
guess[i] = sc.nextLine().charAt(0);
}
System.out.println("Enter length of symbols array");
len = in.nextInt();
char symbols[] = new char[len];
System.out.println("Enter array elements of symbols:");
for(int i=0;i<len;i++)
{
symbols[i] = sc.nextLine().charAt(0);
}
k = countAllHits(hiddenCode, guess, symbols);
System.out.println("The number of hits are: "+ k); //print no.of black and white hits

}
public static int countAllHits(char[] hiddenCode, char[] guess, char[] symbols) {
int a[] = new int[symbols.length];
int b[] = new int[symbols.length];
int k, count = 0;
for (int i = 0; i < symbols.length; i++) {
for (int j = 0; j < hiddenCode.length; j++) {
if (symbols[i] == hiddenCode[j]) {
a[i]++; //counting the minimum of count of each symbol in hiddenCode
//Storing it in the array a at the index of symbol used
break;
}
}
}
for (int i = 0; i < symbols.length; i++) {
for (int j = 0; j < guess.length; j++) {
if (symbols[i] == guess[j]) {
b[i]++; //counting the minimum of count of each symbol in Guess
//Storing it in the array a at the index of symbol used
break;
}
}
}
for (k = 0; k < a.length; k++)
if (a[k]==1&&b[k]==1) //comparing two arrays if they match that means a hit so increment count
count++;
return count;
}

}

Output:

Add a comment
Know the answer?
Add Answer to:
/**    * Returns the sum of "black hits" and "white hits" between the hiddenCode and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {...

    Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {            if(arr == null || arr.length == 0) {                return -1;            }            for (int i = 0; i < arr.length; i++) {                if(arr[i] == ch) {                    return i;                }            }        return -1;       ...

  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • I need help with this. I need to create the hangman game using char arrays. I've...

    I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In t...

    c++ PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In this game, the user will need to match up the pairs symbols A,B,C,D,E on a 4x4 array. For example, the array could be initialized like the following: In this case, X represents an empty slot. The goal is for the user to match the A to the A, the B to the B, etc, until all pairs are matched up to win the...

  • Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview The diagram below illustrates a banner constructed...

    Objectives Problem solving using arrays and ArrayLists. Abstraction. Overview The diagram below illustrates a banner constructed from block-letters of size 7. Each block-letter is composed of 7 horizontal line-segments of width 7 (spaces included): SOLID                        as in block-letters F, I, U, M, A, S and the blurb RThere are six distinct line-segment types: TRIPLE                      as in block-letter M DOUBLE                   as in block-letters U, M, A LEFT_DOT                as in block-letters F, S CENTER_DOT          as in block-letter...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT