Question: Memory- Java (multiple parts) Write a Java program that implements a game loosely based on a card game called memory (https://en.wikipedia.org/wiki/Concentration_(game)) In this game, an
Memory- Java (multiple parts)
Write a Java program that implements a game loosely based on a card game called memory (https://en.wikipedia.org/wiki/Concentration_(game)) In this game, an array of integers will be created with numbers ranging from 1 to n, where n is specified by the user. In such array, each number will appear twice and their position in the array will be randomly generated. The user will then guess which positions contain two equal entries. If their guess is correct, the two numbers in such positions will be removed from the array by setting their values to be equal to ?1. Either way, the user will make another guess, and they will continue to guess until all values of the array are set to be ?1. At this point, the game is over and the program will print how many guesses it took the user to correctly guess all pairs in the cards.
1a) Generating the array of cards
Write a method generateCards that takes as input an int n which represents the number of pairs contained by the set of cards. The method creates and returns an array of integers of size 2*n. The array should contain all the numbers from 1 to n repeated twice. Specifically, if the method is called with input equal to 5, the array returned by the method should be exactly the following: {1,2,3,4,5,1,2,3,4,5}.
1b) Method for shuffling the cards
Write a method shuffleCards that takes an array of integers as input representing the cards of the game, and returns no value. This method should shuffle the cards around. To do so, create an object of type Random (remember that to use Random you should add the appropriate import statement at the beginning of your file). To make your program easier to debug (and grade), you must provide a seed equal to 123 for the Random object.
The method should then do the following:
Use the Random object to generate two random integers i and j representing two indices of the input array
. Swap the value at position i with the value at position j.
Repeat the above steps 100,000 times (100,000 is not a special number. It is just a very large number).
1c) Method for displaying the cards to the user
Write a method displayCards. This method takes as input an array of integers (the cards), and two integers, x and y, representing the last guess made by the user.
The method goes through all the elements of the array and displays the following:
The actual value, if the element is in one of the two positions guessed by the user.
A star, *, if the value stored in the array is a -1.
A dash, -, for all other elements
1d) Method to check if the guess is valid
Write a method isValidGuess that takes as input an array of integers (the cards), and two integers, x and y, representing the last guess made by the user. The method returns true if the guess is valid, false otherwise. Note that, a guess is considered to be valid if both positions represent a possible index of the array and if they do not refer to a card/element that has already been guessed (i.e. that has value equal to -1). If at least one of the two integers is out of bounds, then the method should print a message indicating this and reminding the user of the correct range for the indices. If both integers are in the correct range, but at least one of them refers to a card that has already been guessed, then the method should print a message indicating this to the user. Note that, if the guess is valid the method should not print anything.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
