QuestionQuestion

1. Getting Started

In this assignment, you will be writing a game that makes use of arrays and static methods. A class called RandomLetters has been posted along with this assignment. You will be using a method in this class for generating an array containing four unique letters (as char data) chosen from the letters ranging from a to f. Download RandomLetters.java to the same folder as your homework assignment code. You may need to refresh the folder for the file to appear in Eclipse: select the folder and either press the F5 function key or select File > Refresh from the menu. Do not make any changes to the RandomLetters class and do not submit it. Only submit the one java program you will be writing.

2. Programming Assignment

Matching Game

Your program will be calling a static a method in the RandomLetters class that returns an array containing a random sequence of four letters, where each letter is in the range from a to f and there are no repeating letters. The player has up to 10 chances to guess the correct sequence of letters, with your code providing feedback after each guess. The game ends when the user has either guessed correctly or used up all 10 guesses. After the last guess, the player is informed if she has won or lost. If she has lost, the correct sequence is displayed.

You will be completing this assignment in three passes. In the first pass, you will assume that the payer enters only letters and that none of them repeat. After the player has guessed four letters, you will display the number of exact matches (i.e., correct letter in the correct position). The player will be allowed up to 10 chances to try to match the four-letter sequence, as explained above.

In the second pass, you will add functionality that shows the number of partial matches (correct letter but not in correct position) in addition to exact matches. You will be writing a static method for use in providing this functionality.

In the third pass, you will add functionality to verify that each character entered by the player is a letter and that it has not already been entered for the current guess. You can ignore whether a letter is uppercase or lowercase; the random letter sequence will always be in lowercase, and uppercase letters will not count as matches. You will be using a built-in static method from the Character class as well as the static method you wrote in the second pass for validating the input.

In the following sample interactions, the player's input appears in bold. Instructions on how to play the game are provided once, at the start of a round of play. The player is then asked for a total of four letters, numbered 1 through 4. The number of the guess followed by all the letters entered by the player are then displayed on one line. This is followed by the number of exact matches and the number of partial matches.

After the player's last guess (either because it is the 10th guess or because the player has guessed correctly), the player is informed if she has won or lost. If the player has lost, the correct sequence is displayed.

Sample 1: player wins (note: partial matches will not be displayed until Pass 2)

You have a maximum of 10 chances to correctly guess a sequence of four non-repeating letters ranging from a to f.

Enter letter #1: e

Enter letter #2: a

Enter letter #3: f

Enter letter #4: c


Guess #1: eafc

exact (correct letter and position): 2

partial (correct letter, incorrect position): 1

Enter letter #1: e

Enter letter #2: d

Enter letter #3: f

Enter letter #4: a


Guess #2: edfa

exact (correct letter and position): 4

partial (correct letter, incorrect position): 0


You won!


Sample 2: player loses, with correct sequence shown at end (again, partial matches not displayed until Pass 2)

You have a maximum of 10 chances to correctly guess a sequence of four

Non-repeating letters ranging from a to f.


Enter letter #1: e

Enter letter #2: f

Enter letter #3: a

Enter letter #4: b


Guess #1: efab

exact (correct letter and position): 0

partial (correct letter, incorrect position): 3


Enter letter #1: e

Enter letter #2: b

Enter letter #3: f

Enter letter #4: a


Guess #2: ebfa

exact (correct letter and position): 1

partial (correct letter, incorrect position): 2


... (guesses 3-9 left out)


Enter letter #1: c

Enter letter #2: b

Enter letter #3: a

Enter letter #4: f


Guess #10: cbaf

exact (correct letter and position): 0

partial (correct letter, incorrect position): 3


You lost. The correct sequence was: acfe


Sample 3: player enters invalid data (Pass 3)

You have a maximum of 10 chances to correctly guess a sequence of four

non-repeating letters ranging from a to f.


Enter letter #1: 8

*** Enter a unique letter ***

Enter letter #1: a

Enter letter #2: a

*** Enter a unique letter ***

Enter letter #2: b

Enter letter #3: a

*** Enter a unique letter ***

Enter letter #3: b

*** Enter a unique letter ***

Enter letter #3: c

Enter letter #4: f


Guess # 1: abcf

exact (correct letter and position): 1

partial (correct letter, incorrect position): 2


(play continues)

This assignment is about arrays, which must therefore be used for storing the letters to be guessed as well as the letters the player guesses. Representing the correct sequence and the guessed sequence as Strings and using String methods or as Lists and using list methods for finding exact and partial matches, for example, are not acceptable solutions.

While you are only required to write one static method for this assignment, you are always free to write other methods for providing the required functionality.

Pass 1: Prompt for input, calculate and display number of exact matches, and print final outcome

1. To generate a random sequence of letters, you will be using the static generateLetters() method in the RandomLetters class, which will return an array of type char containing four   letters. Use the following code to store four randomly generated, non-repeating letters between a and f to an array of type char (called correctLetters in this example):

// array storing random sequence of letters for the player to guess

char[] correctLetters = RandomLetters.generateLetters();

Initially, do not use this method in your code, as it is very difficult to debug errors if a different random sequence is generated every time you run the program. Instead, set the sequence to a fixed set of letters. For example:

char[] correctLetters = {'a', 'e', 'b', 'd'};

Once all of your code is working for the array you hardcoded into your program, use generateLetters(), as shown above, for the final version you will be submitting.

2. The user is allowed a maximum of 10 guesses. Store this value to a named constant. For testing purposes, you may want to allow a smaller number of guesses. Just be sure to set the value to 10 for final testing and submission.

3. Prompt the user to enter each letter using a looping structure. In each loop iteration, use the charAt() method demonstrated in class in conjunction with the next() method to get the first
character in the user╒s input. Then add that character to the next available position in the array storing the user╒s guess.

4. After getting all four inputs, print the number of the guess followed by the guessed sequence.

5. Determine the number of exact matches for each guess and display that number (you should use a loop here as well). For example, if the correct sequence contains {a,b,c,d} and the user has guessed {a,e,c,b}, then there are 2 exact matches (a and c).

6. Continue to prompt for guesses until the player has either guessed the correct letters in the correct positions (i.e., all letters are exact matches) or has used up the maximum number of allowed guesses.

7. After the last guess, state whether the player has won or lost. If it is the latter, show the correct sequence.

Pass 2: Display partial matches

1. Write a static method called search() that is passed (1) an array of type character, (2) the number of characters in the array, and (3) a single character to search for in the array. (Note that the number of elements in an array is not always the same as its length; an array may have room for 100 elements, for example, but contain only 2. It is inefficient to search empty cells and can cause errors in some cases. You may assume that any unused cells are at the end of the array, i.e., after any data stored to the array.)


Search the filled portion of the array for the value of the character parameter. If it is found, return a
boolean value of true. If the character is not found, return a boolean value of false.

2. If a letter entered by the user is not an exact match, it could still be a partial match (correct letter, incorrect place in sequence). Add code to check for partial matches. For full credit, use your search() method to provide this functionality.

3. Update your output to display the number of partial matches in addition to exact matches. Using the same example as above, if the correct sequence contains {a,b,c,d} and the user has guessed {a,e,c,b} then there are 2 exact matches (a and c) and one partial match (b).

Pass 3: Verify valid input

1. After you have the above logic working correctly, modify your code to verify that the user enters only letters and that the same letter is not entered more than once. (You will not be verifying if the letters entered are between a and f or if they are lowercase; these should be accepted as valid inputs.) If the value entered is not valid, print a message telling the user to enter a unique letter and re-prompt. For full credit, invoke the following methods within the loop getting user input:
. Use the static method isLetter() in the Character class to verify that the character input by the user is a letter.
. Use your search() method to determine if the current value input by the user has already been entered for the current guess.

For example, if the array containing the user╒s input contains {a,b} so far and the user enters a non-letter or another a or b for letter #3, that input should not be added to the array. Instead, print a message requesting that a unique letter be entered (as in Sample 3) and prompt again for that same letter. Continue to prompt for that letter until a valid value has been entered, at which point it can be added to the array.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import java.util.Scanner;

public class MatchingGame {
   
    private static final int maximum = 10;
   
    public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
      
       int choice = 0;
      
       System.out.print("Enter your choice\n"
                + "1. test the game in first pass mode\n"
                + "2. test the game in second pass mode\n"
                + "3. test the game in third pass mode\n"
                + "4. play the game\n"
                + "--> ");
       choice = scanner.nextInt();
      
       switch(choice){
            case 1:
                System.out.println("First pass");
                firstpass(scanner);
                break;
            case 2:...
$63.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats