hello currently a student, fairly new to this and taking a hard java class. Need help doing some hw. Please PM, the question is below.
The Interface to Play the Memory Game
You are to simulate the game by having a JFrame which will use the BorderLayout. In one of the panels you will use the GridLayout to show a 4 x 4 grid representing the 16 cards. for example the game will be made up of 8 pairs of words. You can use either JButton or JTextField as your tiles in the grid.
The use of JTextFields means that you will have to use a MouseListener instead of an ActionListener.
In another panel, you should have another JPanel which will keep score. this will tell how many guesses have been taken so far, and the score is for each player. There should also be a button to click. When someone clicks the button, it should restart the game.
An example of what the GUI might look like at the start of the game is:
The Implementation of the Memory Game
In the constuctor of the Memory Game Panel (i.e., the 4 x 4 grid), you should call a public method of reset, which is a method of this class. (Remember that a JPanel is a class.) The reset method will assign the 8 words that you have chosen twice to each of one of the 16 tiles (JButtons or JTextFields). This should be done by using the Random class. You can do this by going through the tiles one by one and having the object of the Random class choose a random number between 0 and 7 and assigning the corresponding word to this tile. You just have to make sure that you assign the word exactly twice. Equivalently, you can go through the word list twice and have the object of the Random class choose a random number between 0 and 15 and assign the word to that tile. Here you have to make sure that the tile is not chosen twice. Both ways are essentially equivalent.
To play the game you will need an ActionListener if you chose JButtons or a MouseListener if you chose JTextFields. If the user clicks on a tile, you will show the word chosed for that tile. You must keep track of the parity of the number of clicks. I.e., for the first click, you just show the tile. For the second click, you show the tile and notify the user if they matched or not. If they did not match, after the user responds to the notification (a JOptionPane.showMessageDialog Box will do this), you just reset the tile to be "blank". If they match, you keep them showing the matching work (or you could "hide" them). You also will notify the other JPanel of the matching result and which player matched.
Note: Be sure to handle the case where the player clicks on the same tile both times. An error message should be shown to the player and ask the player to click on another tile.
We will implement the game to play as follows: The game will start with player 1 starting. If player 1 matches a pair of tiles, then player 1 will play again. I.e., a player continues to play until they fail to match two tiles. After a player fails, the other player will start to play. So you will need a variable to tell whose turn it is to play. After each guess, the JPanel which is keeping track of the game must be notified that a new guess has taken place and whether a player has correctly found a pair of matching tiles. The game ends when all of the tiles have been matched, and the winner is the player with the most matches.
There should be JButton in the results JPanel which tells allows the user to start a new game. In this case, both the number of guesses and the correct guesses for each player is set to zero, and the JPanel which is in charge of playing the game is notified. The game playing JPanel will call the method reset which will "re-shuffle" the words to new positions for a new game.