GOAL: Write a graphical version of a card matching game using Swing. Your progra
ID: 641191 • Letter: G
Question
GOAL: Write a graphical version of a card matching game using Swing. Your program should have 4 rows and 5 columns of "cards" that are blank on one side and have a number on the other. The game is played by clicking on two cards which are turned over. If they match, they stay visible. If they don't, they are flipped back face down. Your program should have a "Continue" button which flips back two unmatching cards and a timer that displays the number of seconds since the game started.
METHOD: I would suggest that each "card" be represented by a JButton which changes its text and color to indicate "face down" or "face up" as needed. The board on the screen can be displayed by a JPanel with GridLayout set. You'll want another JPanel to hold the timer and "Continue" button.
Each "card" should know what its numeric value is and whether or not it is "face up". I would recommend creating a subclass of JButton, let's say CardButton, to describe each button. ACardButton will still have all the usual methods of a JButton, but can also be given instance variables to store the number value of the card and "face up" or not, define a flipOver() method, etc.
It will probably be simplest to have one ActionListener that listens to all the CardButtons. Remember that the getSource() method in the ActionEvent class will give you a reference to theCardButton object that was pressed (You'll need to cast it to a CardButton). What happens when a card is pressed depends on the state of the card and also on how many cards are currently "face up". You don't want to count matched cards from previous turns, so this count will always be 0, 1, or 2. This value could be stored in a static variable of the CardButton class or as an instance variable of your main game class.
Since there are 20 cards and we want to match pairs, the cards should be given random values from 1 to 10 with each value being used twice. This should be a random assignment so that the game will be different each time it's played.
We'll talk about implementing the timer in class.
COMMENTS: If the two cards turned over match, you should change the background color and either keep the value visible or not (sort of corresponding to just marking those cards as "matched" or taking away the matched cards). You'll want to reset the number of "face up" cards to 0.
If the cards don't match, wait until the player presses the "Continue" button, then flip them back and again reset the number of "face up" cards.
If you click on a "face up" card, it should just be ignored.
If there are already two "face up" cards, you shouldn't be able to turn over another.
The "Continue" button should only work if there are two non-matching cards turned up (i.e. not when only one card is turned over, etc.).
Explanation / Answer
import java.awt.*; import javax.swing.*; import javax.swing.JFrame; import java.awt.event.*; //this class gonna control the basic ops of the game public class MemoControl extends JFrame{ public JLabel label; public JButton button; //images public ImageIcon image1; public JLabel label1; public ImageIcon image2; public JLabel label2; public ImageIcon image3; public JLabel label3; public ImageIcon image4; public JLabel label4; public ImageIcon image5; public JLabel label5; public MemoControl(){ setLayout(new FlowLayout()); image1 = new ImageIcon(getClass().getResource("card_cover1.jpg")); label1 = new JLabel(image1); add(label1); image2 = new ImageIcon(getClass().getResource("card_cover1.jpg")); label2 = new JLabel(image2); add(label2); image3 = new ImageIcon(getClass().getResource("card_cover1.jpg")); label3 = new JLabel(image3); add(label3); image4 = new ImageIcon(getClass().getResource("card_cover1.jpg")); label4 = new JLabel(image4); add(label4); image5 = new ImageIcon(getClass().getResource("card_cover1.jpg")); label5 = new JLabel(image5); add(label5); /*label = new JLabel("Welcome to AMY Memo Game"); add(label);*/ /*textField = new JTextField(15); add(textField);*/ button = new JButton("Flip"); add(button); EventClass event = new EventClass(); button.addActionListener(event); }//MyMemo constr end private class EventClass implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getSource() == button){ image1 = new ImageIcon(getClass().getResource("deer_card.jpg")); label1 = new JLabel(image1);} } }//Event class end public static void main(String args[]){ MemoControl gui = new MemoControl(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.pack(); gui.setVisible(true); gui.setTitle("My Memo"); }//main end }//AMYMemo class end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.