For your first programming assignment this semester, you are going to be programming a simple memory game that is typically played with cards.
This game is often just called “Memory”. You may already be familiar with the rules of this game. Pairs of cards are laid face down, and you flip over two cards at a time. If the two cards have matching numbers, then you leave them face up. If they do not match, you flip them so they are face down again. The goal is to find all of the matching pairs of cards, with the challenge being that you are only able to flip a single pair of cards at a time. The player must remember the location of numbers they’ve seen so that they can locate matching pairs of cards.
The program should work similarly to how it appears in the following video.
The skills you will practice on this assignment are:
creating a running program in Java
getting input from the user
creating arrays
using methods
generating random numbers
The Outline
Create a new Java class file called “MemoryGame”. This program must contain the following components:
A 4×4 2D array of integers. This array holds the values of the cards. There should be pairs of the numbers 1-8, for 16 total cards.
A 4×4 2D array of Booleans. This array corresponds to the integer array and holds a value for each card. If the Boolean is true, the card is visible (such as when it is being selected on the player’s turn or if it is already part of a matched pair). If the Boolean is false, the card is not visible and is instead drawn as an asterisk *.
A method called “shuffle” that fills the initial array of integers by randomly assigning the pairs throughout the array.
A method called “display” which displays the current state of the game. The card values should be shown as a 4×4 grid. Face-up cards are shown as their numbers, and face-down cards are shown as asterisks * (the array of Booleans determines whether a card’s value is visible or not). Whenever you show the board, you should also print a large number of newlines so that the previously displayed board is not shown on the screen.
A method called “gameComplete” which checks if the game has ended. The game ends when all card pairs have been revealed (all elements in the Boolean array are true).
A method called “playerTurn” which handles a single turn of the game. A single turn goes as follows:Display the current state of the cards.
Ask the player for the row number of the first card they want to flip. Make sure it is a valid choice before proceeding.
Ask the player for the column number of the first card they want to flip. Make sure it is a valid choice before proceeding.If the card the player has chosen is one that has already been revealed, ask them for a different card by restarting from step 2.
Display the current state of the cards with the card they’ve just chosen revealed.
Ask the player for the row number of the second card they want to flip. Make sure it is a valid choice before proceeding.
Ask the player for the column number of the second card they want to flip. Make sure it is a valid choice before proceeding.If the card the player has chosen is one that has already been revealed, ask them for a different card by restarting from step 5.
Display the state of the cards with the second card revealed. If the two cards the player chose are a match, leave them visible and end this turn. If they are not a match, hide them before beginning the next turn. Allow the player to look at the cards they’ve chosen before you move on.
A main method which does the following:Set up any variables and objects you need.
Call the shuffle method to randomize the values in the card integer array.
In a loop, call the playerTurn method.
At the end of each loop iteration, use the gameComplete method to determine if you should repeat the loop and begin another player turn, or if you should end the program. The loop continues until the game is complete.
All other details about the program are up to your discretion, including the variables you use, the parameters and return types of your methods, and any additional methods you create. Your program will receive full points as long as it has all of the components listed above and behaves correctly.
Whenever your program asks for input, it must check that the user has entered valid numbers and not crash. You can assume that the user will only ever enter numerical inputs (your program does not have to gracefully handle String or character inputs).
Hints
You will need to have your program wait to proceed at certain points until the user types the enter key. You can accomplish this with the following code (assuming you have a Scanner object named “scnr”):System.out.println(“Press enter to start the next turn.”);
if (scnr.hasNextLine()) {
scnr.nextLine();
}
scnr.nextLine();This checks to see if anything is on the input buffer and clears it, then waits for any type of user input (including just the enter key).
There are two ways you can randomly fill the array of integers. One way is by creating the 2D array full of 0’s and then using a Random object to generate random row and column positions in a loop. If the randomly chosen position is 0, then place the number in that position. If it is not 0, then that position is full and you must randomly choose another position for that number. Alternatively, you can start by filling the array with all of the number pairs in order and then use a Random object to generate pairs of random row and column positions in a loop that repeats many times. For each pair of positions you generate, swap the values at those two positions. Remember to fill the array with pairs of numbers, not just a single instance of each number! There should be two 1’s, two 2’s, etc.
Remember that you should only create a single System.in Scanner in any given program. If your Scanner is needed within several different methods, then you must either pass in the Scanner as a parameter of those methods, or make the Scanner a class-level variable (global).
Testing
Once you have completed your program, make sure to test it with all kinds of player behavior. Here are some tests you should perform to make sure your program works properly:
Run the program to make sure it compiles and doesn’t crash immediately
Play a game normally to see if the user can win properly
Enter invalid row and column numbers to see if your program handles them well and continues without any errors
Play a few times to make sure the card positions are random each game
Submission
When you are satisfied with your program, submit the file by clicking “Start Assignment” at the top of the page and then selecting your MemoryGame.java program file for submission. This assignment will only accept the .java file; it will NOT accept an Eclipse project file or a zip file. No points will be given to programs that do not compile.
Always submit the .java files in this course, never the .class files! All references to “class files” mean Java program files that contain classes, not files with the .class extension. The .class files are the post-compilation files that cannot be read or run, and will always earn 0 points.