I'm trying to make a first game. I would like to make memory game where you have to find a color matched field.
I made:
MemoryGame - JFrame where I added mainPanel. In mainPanel I added two another panel becouse both have other layout. The most of the game code is in ButtonsPanel class. Code in this class work almost like I want.
I don't know how to write the BottomPanel class. I wish to make a restart button which will clear all buttons and will start the game from begining. I wish to make two JLablel, one of this will show messages like "this field is already matched" etc. and second one will show a number of showed fields.
Can you check this code and give me some comments and leads?
Maybe I should move most of the code from buttomsPanel to MainPanel.
Card Class:
package memoryGame;
import javax.swing.*;
public class Card extends JButton {
private boolean cardMatched;
private boolean cardSelected;
private String back;
public Card(String back)
{
this.back = back;
cardSelected = false;
cardMatched = false;
}
public String getBack() {
return back;
}
public void setMatched(boolean matched) {
cardMatched = matched;
}
public void setSelected(boolean selected) {
cardSelected = selected;
}
public boolean isCMatched() {
if (cardMatched == true) {
return true;
} else {
return false;
}
}
public boolean isCSelected() {
if (cardSelected == true) {
return true;
} else {
return false;
}
}
}
ButtonsPanel Class - with a most of the game code:
package memoryGame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.util.Random;
import static java.lang.String.valueOf;
public class ButtonsPanel extends JPanel implements ActionListener{
private static final int NUMBER_OF_ROWS = 4;
private static final int NUMBER_OF_COLUMNS = 4;
private static final int HORIZONTAL_GAP = 5;
private static final int VERTICAL_GAP = 5;
private static final int PANEL_BORDER = 20;
private Card[][] cards = null;
private String[] colors = {"Blue", "Blue", "Red", "Red", "Green", "Green", "Magenta", "Magenta", "Orange", "Orange", "Cyan", "Cyan", "Black", "Black", "Pink", "Pink"};
private Random random;
private Field field;
private Color color;
private int score = 0;
private Card selectedCard;
private Card c1;
private Card c2;
private Card c3;
public ButtonsPanel(){
setBackground(Color.BLACK);
GridLayout layout = new GridLayout(NUMBER_OF_ROWS, NUMBER_OF_COLUMNS, HORIZONTAL_GAP, VERTICAL_GAP);
setLayout(layout);
setBorder(BorderFactory.createEmptyBorder(PANEL_BORDER,PANEL_BORDER,PANEL_BORDER,PANEL_BORDER));
cards = new Card[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
shuffle();
setCards();
}
public void setCards() {
int a = 0;
for (int row = 0; row < cards.length; row++) {
for (int column = 0; column < cards[0].length; column++) {
cards[row][column] = new Card(colors[a]);
add(cards[row][column]);
cards[row][column].addActionListener(this);
a++;
}
}
}
public void shuffle() {
random = new Random();
for (int i = 0; i < colors.length; i++) {
int pos = random.nextInt(colors.length);
String temp = colors[i];
colors[i] = colors[pos];
colors[pos] = temp;
}
}
public void setColors(Card card) {
try {
field = Class.forName("java.awt.Color").getField(card.getBack().toUpperCase());
color = (Color) field.get(null);
card.setBackground(color);
} catch (Exception ex) {
color = null;
}
}
public void showCardColor() {
for (int row = 0; row < cards.length; row++) {
for (int column = 0; column < cards[0].length; column++) {
if (selectedCard == cards[row][column]) {
if (!cards[row][column].isCMatched()) {
if (!cards[row][column].isCSelected()) {
setColors(cards[row][column]);
cards[row][column].setSelected(true);
mouseClick++;
System.out.println(mouseClick);
} else {
System.out.println("This field is already selected");
}
} else {
System.out.println("This file is already matched.");
}
}
}
}
}
int mouseClick = 0;
public void hideColor() {
if (mouseClick > 1 && mouseClick % 2 != 0) {
c1.setBackground(new JButton().getBackground());
c2.setBackground(new JButton().getBackground());
c1 = null;
c2 = null;
}
}
public void check() {
if (c1.getBack().equals(c2.getBack())) {
c1.setMatched(true);
c2.setMatched(true);
if (isEndOfGame() == true) {
JOptionPane.showMessageDialog(this, "You won in " + score + " moves !");
}
c1 = null;
c2 = null;
c3 = null;
c1 = selectedCard;
showCardColor();
} else {
showCardColor();
c1.setSelected(false);
c2.setSelected(false);
hideColor();
c1 = c3;
c3 = null;
}
}
public boolean isEndOfGame() {
for (Card[] cards2d : cards) {
for (Card cards1d : cards2d) {
if (cards1d.isCMatched() == false) {
return false;
}
}
}
return true;
}
public void doTurn() {
if (c1 == null && c2 == null) {
if (!selectedCard.isCMatched()) {
c1 = selectedCard;
showCardColor();
}
}
if (c1 != null && c1 != selectedCard && c2 == null) {
if (!selectedCard.isCMatched()) {
c2 = selectedCard;
showCardColor();
}
}
if (c1 != null && c2 != null && c2 != selectedCard && c3 == null) {
c3 = selectedCard;
check();
}
}
Object source;
@Override
public void actionPerformed(ActionEvent e) {
source = e.getSource();
selectedCard = (Card) source;
doTurn();
score++;
}
}
BottomPanel Class:
package memoryGame;
import javax.swing.*;
import java.awt.*;
public class BottomPanel extends JPanel {
private JButton buttonRestart;
private JLabel labelScore;
private JLabel labelMesseges;
private BorderLayout borderLayout;
int score = 0;
public BottomPanel(){
borderLayout = new BorderLayout();
setLayout(borderLayout);
buttonRestart = new JButton("Restart");
buttonRestart.setPreferredSize(new Dimension(150,50));
labelMesseges = new JLabel();
labelScore = new JLabel("" + labelScore, SwingConstants.CENTER);
labelScore.setPreferredSize(new Dimension(50,50));
labelMesseges.setText(" ");
labelMesseges.setBorder(BorderFactory.createMatteBorder(0,2,0,2,Color.black));
labelScore.setFont(new Font("Courier", Font.BOLD, 16));
labelScore.setText("36");
add(buttonRestart);
add(labelMesseges);
add(labelScore);
borderLayout.addLayoutComponent(buttonRestart, BorderLayout.WEST);
borderLayout.addLayoutComponent(labelMesseges,BorderLayout.CENTER);
borderLayout.addLayoutComponent(labelScore,BorderLayout.EAST);
}
}
MainPanel Class:
package memoryGame;
import javax.swing.*;
import java.awt.*;
public class MainPanel extends JPanel{
private BorderLayout borderLayout;
private BottomPanel bottomPanel;
private ButtonsPanel buttonsPanel;
public MainPanel(){
borderLayout = new BorderLayout(1,1);
setLayout(borderLayout);
buttonsPanel = new ButtonsPanel();
bottomPanel = new BottomPanel();
add(buttonsPanel);
add(bottomPanel);
borderLayout.addLayoutComponent(buttonsPanel, BorderLayout.CENTER);
borderLayout.addLayoutComponent(bottomPanel, BorderLayout.SOUTH);
}
}
MemoryGame Class:
package memoryGame;
import javax.swing.*;
import java.awt.*;
public class MemoryGame extends JFrame {
private static final int WINDOW_WIDTH = 900;
private static final int WINDOW_HEIGHT = 900;
private MainPanel mainPanel;
public MemoryGame() {
setSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setResizable(true);
setTitle("Memory Game");
mainPanel = new MainPanel();
add(mainPanel);
setVisible(true);
}
}
Test Class:
package memoryGame;
public class Test{
public static void main(String[] args) throws Exception {
new MemoryGame();
}
}