-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMemoryCardGameGUI.java
More file actions
143 lines (123 loc) · 4.27 KB
/
MemoryCardGameGUI.java
File metadata and controls
143 lines (123 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* 🎮 Memory Card Matching Game (GUI)
*
* A simple card-matching game using Java Swing.
* Players flip cards to find matching pairs.
* Game ends when all pairs are matched.
*
* Complexity:
* - Time: O(n^2) in worst case for flipping all cards
* - Space: O(n^2) for storing card states
*
* Author: Pradyumn Pratap Singh (Strange)
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Collections;
import java.util.ArrayList;
public class MemoryCardGameGUI extends JFrame implements ActionListener {
private JButton[] buttons; // Buttons representing cards
private String[] cardValues; // Values for cards
private int firstIndex = -1; // First card clicked
private int secondIndex = -1; // Second card clicked
private Timer flipBackTimer; // Timer to flip back unmatched cards
private int matchesFound = 0; // Count of matched pairs
public MemoryCardGameGUI() {
setTitle("🎮 Memory Card Matching Game");
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Grid layout for cards (4x4)
setLayout(new GridLayout(4, 4, 5, 5));
buttons = new JButton[16]; // 16 cards
cardValues = new String[16];
// Prepare card values (8 pairs)
ArrayList<String> valuesList = new ArrayList<>();
for (int i = 1; i <= 8; i++) {
valuesList.add(String.valueOf(i));
valuesList.add(String.valueOf(i));
}
Collections.shuffle(valuesList); // Shuffle cards
for (int i = 0; i < 16; i++) cardValues[i] = valuesList.get(i);
// Initialize buttons
for (int i = 0; i < 16; i++) {
buttons[i] = new JButton("?");
buttons[i].setFont(new Font("Arial", Font.BOLD, 24));
buttons[i].addActionListener(this);
add(buttons[i]);
}
// Timer for flipping back unmatched cards
flipBackTimer = new Timer(1000, e -> flipBack());
flipBackTimer.setRepeats(false);
setVisible(true);
}
/**
* Handles card clicks
*/
@Override
public void actionPerformed(ActionEvent e) {
JButton clickedButton = (JButton) e.getSource();
int index = -1;
// Find index of clicked button
for (int i = 0; i < buttons.length; i++) {
if (buttons[i] == clickedButton) {
index = i;
break;
}
}
// Ignore clicks if card already matched or currently flipped
if (buttons[index].getText().equals(cardValues[index]) || flipBackTimer.isRunning()) return;
// Flip card
buttons[index].setText(cardValues[index]);
// Check if first or second card
if (firstIndex == -1) {
firstIndex = index;
} else {
secondIndex = index;
// Check for match
if (cardValues[firstIndex].equals(cardValues[secondIndex])) {
matchesFound++;
firstIndex = -1;
secondIndex = -1;
// Check if all matches found
if (matchesFound == 8) {
JOptionPane.showMessageDialog(this, "🎉 Congratulations! You matched all pairs!");
resetGame();
}
} else {
// Start timer to flip back unmatched cards
flipBackTimer.start();
}
}
}
/**
* Flip back unmatched cards
*/
private void flipBack() {
if (firstIndex != -1 && secondIndex != -1) {
buttons[firstIndex].setText("?");
buttons[secondIndex].setText("?");
}
firstIndex = -1;
secondIndex = -1;
}
/**
* Reset the game
*/
private void resetGame() {
matchesFound = 0;
firstIndex = -1;
secondIndex = -1;
ArrayList<String> valuesList = new ArrayList<>();
for (int i = 1; i <= 8; i++) {
valuesList.add(String.valueOf(i));
valuesList.add(String.valueOf(i));
}
Collections.shuffle(valuesList);
for (int i = 0; i < 16; i++) {
cardValues[i] = valuesList.get(i);
buttons[i].setText("?");
}
}
}