Witam, mam do szkoły zrobić projekt gry, polegającej na jak najszybszym znalezieniu par (w moim przypadku liczb - klasyczna gra memory).
Projekt ma wykorzystywać SWING. Jestem dopiero w początkowej fazie budowy tej gry, ale natrafiłem na problem. Nie wiem jak sprawdzić czy dwie wybrane komórki sa takie same (czy maja ta sama wartość). Poniżej kod. Wspomnianego sprawdzenia dokonuje w metodzie buttonClicked().
Generalnie nie zwiększa mi sie licznik iteration, i przez to nie moge przypisac drugiej karty (secondCard).
Prawdopodobnie błąd jest na poziomie logicznym. Niestety dopiero uczę się GUI, a jak widzicie zaczynam od początku tj. od SWING.
Poniżej kod gry:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFrame extends JFrame {
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 300;
private static final int X = 4;
private static final int Y = 5;
private static Card firstCard;
private static Card secondCard;
private List<Card> cards;
private JPanel cardsPanel;
private JPanel buttonsPanel;
private JButton replayButton;
private JButton exitButton;
public MyFrame() {
super("Memory Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setLocation(100, 100);
setVisible(true);
cardsPanel = new JPanel();
this.setUpCardsPanel();
this.add(cardsPanel,BorderLayout.CENTER);
buttonsPanel = new JPanel();
this.setUpButtonsPanel();
this.add(buttonsPanel,BorderLayout.SOUTH);
//System.out.println(cards);
}
private void setUpButtonsPanel() {
replayButton = new JButton("Restart");
exitButton = new JButton("Exit");
buttonsPanel.add(replayButton);
buttonsPanel.add(exitButton);
buttonsPanel.setLayout(new GridLayout(1, 0));
}
private void setUpCardsPanel() {
cardsPanel.setLayout(new GridLayout(X, Y));
setUpCardsList();
for(Card card : cards) {
card.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
buttonClicked((Card) e.getSource());
}
});
cardsPanel.add(card);
}
}
private void setUpCardsList() {
cards = new ArrayList<>();
Random r = new Random();
int counter = 0;
int sign = r.nextInt(99)+1;
for (int i = 0; i < X; i++) {
for (int j = 0; j < Y; j++) {
if(counter == 0) {
cards.add(new Card(sign,i,j));
counter++;
} else if (counter == 1) {
cards.add(new Card(sign,i,j));
sign = r.nextInt(99)+1;
counter = 0;
}
}
}
Collections.shuffle(cards);
}
public boolean sameValues() {
if (firstCard == secondCard) {
return true;
}
return false;
}
private void buttonClicked(Card source) {
Timer t = new Timer();
int iteration = 0;
for (Card item : cards) {
if (source == item) {
source.setPicked(true);
source.setText("" + source.getSign());
source.setEnabled(false);
iteration++;
if (iteration == 3) {
if (sameValues()) {
firstCard.setEnabled(false);
secondCard.setEnabled(false);
} else {
firstCard.setEnabled(true);
secondCard.setEnabled(true);
}
iteration = 1;
}
if (iteration == 1) {
firstCard = item;
System.out.println("counter:" + 1);
}
if (iteration == 2) {
secondCard = item;
System.out.println("counter:" + 2);
}
}
}
System.out.println(source);
}
}
klasa card:
import javax.swing.JButton;
public class Card extends JButton {
private int sign;
private int posX;
private int posY;
private boolean picked;
public Card(int sign, int x, int y) {
this.sign = sign;
this.posX = x;
this.posY = y;
this.picked = false;
}
public int getSign() {
return sign;
}
public void setSign(int sign) {
this.sign = sign;
}
public int getPosX() {
return posX;
}
public void setPosX(int x) {
this.posX = x;
}
public int getPosY() {
return posY;
}
public void setPosY(int y) {
this.posY = y;
}
public boolean isPicked() {
return picked;
}
public void setPicked(boolean picked) {
this.picked = picked;
}
@Override
public String toString() {
return "Card [sign=" + sign + ", x=" + posX + ", y=" + posY + ", picked=" + picked + "]";
}
}
prośba o pomoc, sugestie ewentualnie wyprowadzenie ze ślepej uliczki jezeli w takową zabrnąłem.