0% found this document useful (0 votes)
6 views8 pages

Java Memory Card Game Code

The document contains a Java class for a card matching game called 'MatchCards'. It defines a card structure, initializes a game board with card images, and implements game logic for selecting and matching cards, including error tracking and a restart feature. The game uses a graphical user interface (GUI) with buttons and panels to display the game state and interact with the player.

Uploaded by

ankurkumar99421
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Java Memory Card Game Code

The document contains a Java class for a card matching game called 'MatchCards'. It defines a card structure, initializes a game board with card images, and implements game logic for selecting and matching cards, including error tracking and a restart feature. The game uses a graphical user interface (GUI) with buttons and panels to display the game state and interact with the player.

Uploaded by

ankurkumar99421
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

*;
import [Link].*;
import [Link];
import [Link].*;

public class MatchCards {


class Card {
String cardName;
ImageIcon cardImageIcon;

Card(String cardName, ImageIcon cardImageIcon) {


[Link] = cardName;
[Link] = cardImageIcon;
}

public String toString() {


return cardName;
}
}

String[] cardList = { //track cardNames


"darkness",
"double",
"fairy",
"fighting",
"fire",
"grass",
"lightning",
"metal",
"psychic",
"water"
};

int rows = 4;
int columns = 5;
int cardWidth = 90;
int cardHeight = 128;

ArrayList<Card> cardSet; //create a deck of cards with cardNames and cardImageIcons


ImageIcon cardBackImageIcon;

int boardWidth = columns * cardWidth; //5*128 = 640px


int boardHeight = rows * cardHeight; //4*90 = 360px

JFrame frame = new JFrame("Pokemon Match Cards");


JLabel textLabel = new JLabel();
JPanel textPanel = new JPanel();
JPanel boardPanel = new JPanel();
JPanel restartGamePanel = new JPanel();
JButton restartButton = new JButton();

int errorCount = 0;
ArrayList<JButton> board;
Timer hideCardTimer;
boolean gameReady = false;
JButton card1Selected;
JButton card2Selected;
MatchCards() {
setupCards();
shuffleCards();

// [Link](true);
[Link](new BorderLayout());
[Link](boardWidth, boardHeight);
[Link](null);
[Link](false);
[Link](JFrame.EXIT_ON_CLOSE);

//error text
[Link](new Font("Arial", [Link], 20));
[Link]([Link]);
[Link]("Errors: " + [Link](errorCount));

[Link](new Dimension(boardWidth, 30));


[Link](textLabel);
[Link](textPanel, [Link]);

//card game board


board = new ArrayList<JButton>();
[Link](new GridLayout(rows, columns));
for (int i = 0; i < [Link](); i++) {
JButton tile = new JButton();
[Link](new Dimension(cardWidth, cardHeight));
[Link](true);
[Link]([Link](i).cardImageIcon);
[Link](false);
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!gameReady) {
return;
}
JButton tile = (JButton) [Link]();
if ([Link]() == cardBackImageIcon) {
if (card1Selected == null) {
card1Selected = tile;
int index = [Link](card1Selected);
[Link]([Link](index).cardImageIcon);
}
else if (card2Selected == null) {
card2Selected = tile;
int index = [Link](card2Selected);
[Link]([Link](index).cardImageIcon);

if ([Link]() != [Link]()) {
errorCount += 1;
[Link]("Errors: " + [Link](errorCount));
[Link]();
}
else {
card1Selected = null;
card2Selected = null;
}
}
}
}
});
[Link](tile);
[Link](tile);
}
[Link](boardPanel);

//restart game button


[Link](new Font("Arial", [Link], 16));
[Link]("Restart Game");
[Link](new Dimension(boardWidth, 30));
[Link](false);
[Link](false);
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!gameReady) {
return;
}

gameReady = false;
[Link](false);
card1Selected = null;
card2Selected = null;
shuffleCards();

//re assign buttons with new cards


for (int i = 0; i < [Link](); i++) {
[Link](i).setIcon([Link](i).cardImageIcon);
}

errorCount = 0;
[Link]("Errors: " + [Link](errorCount));
[Link]();
}
});
[Link](restartButton);
[Link](restartGamePanel, [Link]);

[Link]();
[Link](true);

//start game
hideCardTimer = new Timer(1500, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
hideCards();
}
});
[Link](false);
[Link]();

void setupCards() {
cardSet = new ArrayList<Card>();
for (String cardName : cardList) {
//load each card image
Image cardImg = new ImageIcon(getClass().getResource("./img/" + cardName +
".jpg")).getImage();
ImageIcon cardImageIcon = new ImageIcon([Link](cardWidth,
cardHeight, [Link].SCALE_SMOOTH));

//create card object and add to cardSet


Card card = new Card(cardName, cardImageIcon);
[Link](card);
}
[Link](cardSet);

//load the back card image


Image cardBackImg = new
ImageIcon(getClass().getResource("./img/[Link]")).getImage();
cardBackImageIcon = new ImageIcon([Link](cardWidth,
cardHeight, [Link].SCALE_SMOOTH));
}

void shuffleCards() {
[Link](cardSet);
//shuffle
for (int i = 0; i < [Link](); i++) {
int j = (int) ([Link]() * [Link]()); //get random index
//swap
Card temp = [Link](i);
[Link](i, [Link](j));
[Link](j, temp);
}
[Link](cardSet);
}
void hideCards() {
if (gameReady && card1Selected != null && card2Selected != null) { //only flip 2 cards
[Link](cardBackImageIcon);
card1Selected = null;
[Link](cardBackImageIcon);
card2Selected = null;
}
else { //flip all cards face down
for (int i = 0; i < [Link](); i++) {
[Link](i).setIcon(cardBackImageIcon);
}
gameReady = true;
[Link](true);
}
}
}

You might also like