├── App.java ├── MatchCards.java ├── README.md └── img ├── back.jpg ├── darkness.jpg ├── double.jpg ├── fairy.jpg ├── fighting.jpg ├── fire.jpg ├── grass.jpg ├── lightning.jpg ├── metal.jpg ├── psychic.jpg └── water.jpg /App.java: -------------------------------------------------------------------------------- 1 | public class App { 2 | public static void main(String[] args) throws Exception { 3 | MatchCards matchCards = new MatchCards(); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /MatchCards.java: -------------------------------------------------------------------------------- 1 | import java.awt.*; 2 | import java.awt.event.*; 3 | import java.util.ArrayList; 4 | import javax.swing.*; 5 | 6 | public class MatchCards { 7 | class Card { 8 | String cardName; 9 | ImageIcon cardImageIcon; 10 | 11 | Card(String cardName, ImageIcon cardImageIcon) { 12 | this.cardName = cardName; 13 | this.cardImageIcon = cardImageIcon; 14 | } 15 | 16 | public String toString() { 17 | return cardName; 18 | } 19 | } 20 | 21 | String[] cardList = { //track cardNames 22 | "darkness", 23 | "double", 24 | "fairy", 25 | "fighting", 26 | "fire", 27 | "grass", 28 | "lightning", 29 | "metal", 30 | "psychic", 31 | "water" 32 | }; 33 | 34 | int rows = 4; 35 | int columns = 5; 36 | int cardWidth = 90; 37 | int cardHeight = 128; 38 | 39 | ArrayList cardSet; //create a deck of cards with cardNames and cardImageIcons 40 | ImageIcon cardBackImageIcon; 41 | 42 | int boardWidth = columns * cardWidth; //5*128 = 640px 43 | int boardHeight = rows * cardHeight; //4*90 = 360px 44 | 45 | JFrame frame = new JFrame("Pokemon Match Cards"); 46 | JLabel textLabel = new JLabel(); 47 | JPanel textPanel = new JPanel(); 48 | JPanel boardPanel = new JPanel(); 49 | JPanel restartGamePanel = new JPanel(); 50 | JButton restartButton = new JButton(); 51 | 52 | int errorCount = 0; 53 | ArrayList board; 54 | Timer hideCardTimer; 55 | boolean gameReady = false; 56 | JButton card1Selected; 57 | JButton card2Selected; 58 | 59 | MatchCards() { 60 | setupCards(); 61 | shuffleCards(); 62 | 63 | // frame.setVisible(true); 64 | frame.setLayout(new BorderLayout()); 65 | frame.setSize(boardWidth, boardHeight); 66 | frame.setLocationRelativeTo(null); 67 | frame.setResizable(false); 68 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 69 | 70 | //error text 71 | textLabel.setFont(new Font("Arial", Font.PLAIN, 20)); 72 | textLabel.setHorizontalAlignment(JLabel.CENTER); 73 | textLabel.setText("Errors: " + Integer.toString(errorCount)); 74 | 75 | textPanel.setPreferredSize(new Dimension(boardWidth, 30)); 76 | textPanel.add(textLabel); 77 | frame.add(textPanel, BorderLayout.NORTH); 78 | 79 | //card game board 80 | board = new ArrayList(); 81 | boardPanel.setLayout(new GridLayout(rows, columns)); 82 | for (int i = 0; i < cardSet.size(); i++) { 83 | JButton tile = new JButton(); 84 | tile.setPreferredSize(new Dimension(cardWidth, cardHeight)); 85 | tile.setOpaque(true); 86 | tile.setIcon(cardSet.get(i).cardImageIcon); 87 | tile.setFocusable(false); 88 | tile.addActionListener(new ActionListener() { 89 | @Override 90 | public void actionPerformed(ActionEvent e) { 91 | if (!gameReady) { 92 | return; 93 | } 94 | JButton tile = (JButton) e.getSource(); 95 | if (tile.getIcon() == cardBackImageIcon) { 96 | if (card1Selected == null) { 97 | card1Selected = tile; 98 | int index = board.indexOf(card1Selected); 99 | card1Selected.setIcon(cardSet.get(index).cardImageIcon); 100 | } 101 | else if (card2Selected == null) { 102 | card2Selected = tile; 103 | int index = board.indexOf(card2Selected); 104 | card2Selected.setIcon(cardSet.get(index).cardImageIcon); 105 | 106 | if (card1Selected.getIcon() != card2Selected.getIcon()) { 107 | errorCount += 1; 108 | textLabel.setText("Errors: " + Integer.toString(errorCount)); 109 | hideCardTimer.start(); 110 | } 111 | else { 112 | card1Selected = null; 113 | card2Selected = null; 114 | } 115 | } 116 | } 117 | } 118 | }); 119 | board.add(tile); 120 | boardPanel.add(tile); 121 | } 122 | frame.add(boardPanel); 123 | 124 | //restart game button 125 | restartButton.setFont(new Font("Arial", Font.PLAIN, 16)); 126 | restartButton.setText("Restart Game"); 127 | restartButton.setPreferredSize(new Dimension(boardWidth, 30)); 128 | restartButton.setFocusable(false); 129 | restartButton.setEnabled(false); 130 | restartButton.addActionListener(new ActionListener() { 131 | @Override 132 | public void actionPerformed(ActionEvent e) { 133 | if (!gameReady) { 134 | return; 135 | } 136 | 137 | gameReady = false; 138 | restartButton.setEnabled(false); 139 | card1Selected = null; 140 | card2Selected = null; 141 | shuffleCards(); 142 | 143 | //re assign buttons with new cards 144 | for (int i = 0; i < board.size(); i++) { 145 | board.get(i).setIcon(cardSet.get(i).cardImageIcon); 146 | } 147 | 148 | errorCount = 0; 149 | textLabel.setText("Errors: " + Integer.toString(errorCount)); 150 | hideCardTimer.start(); 151 | } 152 | }); 153 | restartGamePanel.add(restartButton); 154 | frame.add(restartGamePanel, BorderLayout.SOUTH); 155 | 156 | frame.pack(); 157 | frame.setVisible(true); 158 | 159 | //start game 160 | hideCardTimer = new Timer(1500, new ActionListener() { 161 | @Override 162 | public void actionPerformed(ActionEvent e) { 163 | hideCards(); 164 | } 165 | }); 166 | hideCardTimer.setRepeats(false); 167 | hideCardTimer.start(); 168 | 169 | } 170 | 171 | void setupCards() { 172 | cardSet = new ArrayList(); 173 | for (String cardName : cardList) { 174 | //load each card image 175 | Image cardImg = new ImageIcon(getClass().getResource("./img/" + cardName + ".jpg")).getImage(); 176 | ImageIcon cardImageIcon = new ImageIcon(cardImg.getScaledInstance(cardWidth, cardHeight, java.awt.Image.SCALE_SMOOTH)); 177 | 178 | //create card object and add to cardSet 179 | Card card = new Card(cardName, cardImageIcon); 180 | cardSet.add(card); 181 | } 182 | cardSet.addAll(cardSet); 183 | 184 | //load the back card image 185 | Image cardBackImg = new ImageIcon(getClass().getResource("./img/back.jpg")).getImage(); 186 | cardBackImageIcon = new ImageIcon(cardBackImg.getScaledInstance(cardWidth, cardHeight, java.awt.Image.SCALE_SMOOTH)); 187 | } 188 | 189 | void shuffleCards() { 190 | System.out.println(cardSet); 191 | //shuffle 192 | for (int i = 0; i < cardSet.size(); i++) { 193 | int j = (int) (Math.random() * cardSet.size()); //get random index 194 | //swap 195 | Card temp = cardSet.get(i); 196 | cardSet.set(i, cardSet.get(j)); 197 | cardSet.set(j, temp); 198 | } 199 | System.out.println(cardSet); 200 | } 201 | 202 | void hideCards() { 203 | if (gameReady && card1Selected != null && card2Selected != null) { //only flip 2 cards 204 | card1Selected.setIcon(cardBackImageIcon); 205 | card1Selected = null; 206 | card2Selected.setIcon(cardBackImageIcon); 207 | card2Selected = null; 208 | } 209 | else { //flip all cards face down 210 | for (int i = 0; i < board.size(); i++) { 211 | board.get(i).setIcon(cardBackImageIcon); 212 | } 213 | gameReady = true; 214 | restartButton.setEnabled(true); 215 | } 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Memory-Game-Java 2 | ![image](https://github.com/user-attachments/assets/f3921cad-5626-475f-b90e-6324d24257b7) 3 | 4 | 5 | 6 | 10 | 11 | 12 | 16 | 17 | 18 | 23 | 24 |
7 |

ABOUT

8 |

The Memory Game, often referred to as the Flip Card Game, is a classic card-matching game that challenges players' memory and concentration skills. The game consists of a set of cards placed face down on a surface. Players take turns flipping two cards at a time, trying to find matching pairs. If a player successfully matches two cards, they remove them from the game and earn another turn. If the cards do not match, they are turned back face down, and the next player takes their turn. The game continues until all pairs are matched, and the player with the most pairs wins.

9 |
13 |

INVENTOR & HISTORY

14 |

The origins of the Memory Game can be traced back to traditional card games that have been played for centuries. While it is difficult to pinpoint a specific inventor, variations of memory-based games have appeared in various cultures. The modern version gained popularity in the mid-20th century, especially as educational tools for children to enhance cognitive skills. Over time, it has evolved into numerous digital formats and mobile applications, making it accessible to a wider audience.

15 |
19 |

ALGORITHM

20 |

The algorithm for the Memory Game can be summarized in a few key steps: 21 | The algorithm for the Memory Game consists of several essential steps. First, an array of paired cards, such as images or symbols, is created and shuffled randomly before being displayed face down. During a player's turn, they select two cards to flip over and check for a match. If the cards match, the pair is removed from play, and the player is granted another turn. Conversely, if there is no match, the cards are flipped back face down, and the turn passes to the next player. The game concludes when all pairs have been matched, with the player who collected the most pairs declared the winner. To enhance their chances of success, players should remember the positions of previously flipped cards. More advanced strategies may incorporate dynamic programming techniques to evaluate optimal moves based on the remaining cards.

22 |
25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /img/back.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MainakVerse/Memory-Game-Java/343f060de663126d213da3375418423fb075fe49/img/back.jpg -------------------------------------------------------------------------------- /img/darkness.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MainakVerse/Memory-Game-Java/343f060de663126d213da3375418423fb075fe49/img/darkness.jpg -------------------------------------------------------------------------------- /img/double.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MainakVerse/Memory-Game-Java/343f060de663126d213da3375418423fb075fe49/img/double.jpg -------------------------------------------------------------------------------- /img/fairy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MainakVerse/Memory-Game-Java/343f060de663126d213da3375418423fb075fe49/img/fairy.jpg -------------------------------------------------------------------------------- /img/fighting.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MainakVerse/Memory-Game-Java/343f060de663126d213da3375418423fb075fe49/img/fighting.jpg -------------------------------------------------------------------------------- /img/fire.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MainakVerse/Memory-Game-Java/343f060de663126d213da3375418423fb075fe49/img/fire.jpg -------------------------------------------------------------------------------- /img/grass.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MainakVerse/Memory-Game-Java/343f060de663126d213da3375418423fb075fe49/img/grass.jpg -------------------------------------------------------------------------------- /img/lightning.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MainakVerse/Memory-Game-Java/343f060de663126d213da3375418423fb075fe49/img/lightning.jpg -------------------------------------------------------------------------------- /img/metal.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MainakVerse/Memory-Game-Java/343f060de663126d213da3375418423fb075fe49/img/metal.jpg -------------------------------------------------------------------------------- /img/psychic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MainakVerse/Memory-Game-Java/343f060de663126d213da3375418423fb075fe49/img/psychic.jpg -------------------------------------------------------------------------------- /img/water.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MainakVerse/Memory-Game-Java/343f060de663126d213da3375418423fb075fe49/img/water.jpg --------------------------------------------------------------------------------