Reputation: 11
I am currently working on a project for my CS class that involves making a GUI game that allows a user to play the card game Concentration (or Memory). All of my code works perfectly fine, until i start comparing the values of each card. I tell the cards to flip back over when they do not have the same value, but they remain up. I have looked through my code, and whenever i have the code:
if (card1.returnValue() == card2.returnValue())
it states that the values are always equal, even when I can see that they are not.
Here is the rest of the code for the problem, I would greatly appreciate any help given:
//FORMAT TAKEN FROM HANDOUT IN CLASS
import java.awt.*;
import javax.swing.*;
import javax.swing.Timer;
import java.util.*;
import java.awt.event.*;
public class Cards extends JPanel
{
private static final long serialVersionUID = 1L;
ImageIcon back;
ActionListener timerPerformer;
Timer buttonTimer;
private int count = 0;
private int turnCounter = 0;
private int matchCounter = 0;
//randomization
public static void randomize(Button[] x) //http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle for algorithm
{
Random randomButton = new Random();
for (int i = x.length - 1; i > 0; i--)
{
int temp = randomButton.nextInt(i + 1);
Button button = x[temp];
x[temp] = x[i];
x[i] = button;
}
}
public Cards()
{
setBackground(Color.gray);
//CARD BACK
back = new ImageIcon(getClass().getResource("b2fv.png"));
Image i1 = back.getImage();
Image i2 = i1.getScaledInstance(75, 100, Image.SCALE_DEFAULT);
back.setImage(i2);
//CARD FACES
ImageIcon[] faces = new ImageIcon[55]; //creates an array of ImageIcons
for (int i = 1; i <= 54; i++)
{
faces[i] = new ImageIcon(getClass().getResource(i + ".png"));
i1 = faces[i].getImage();
i2 = i1.getScaledInstance(75, 100, Image.SCALE_DEFAULT);
faces[i].setImage(i2);
}
//CREATE BUTTONS
Button[] buttons = new Button[54]; //creates an array of Buttons
for(int i = 0; i < 54; i++)
{
if(i / 4 == 0)
buttons[i] = new Button(faces[i + 1], back, 14);
if(i / 4 == 1)
buttons[i] = new Button(faces[i + 1], back, 13);
if(i / 4 == 2)
buttons[i] = new Button(faces[i + 1], back, 12);
if(i / 4 == 3)
buttons[i] = new Button(faces[i + 1], back, 11);
if(i / 4 == 4)
buttons[i] = new Button(faces[i + 1], back, 10);
if(i / 4 == 5)
buttons[i] = new Button(faces[i + 1], back, 9);
if(i / 4 == 6)
buttons[i] = new Button(faces[i + 1], back, 8);
if(i / 4 == 7)
buttons[i] = new Button(faces[i + 1], back, 7);
if(i / 4 == 8)
buttons[i] = new Button(faces[i + 1], back, 6);
if(i / 4 == 9)
buttons[i] = new Button(faces[i + 1], back, 5);
if(i / 4 == 10)
buttons[i] = new Button(faces[i + 1], back, 4);
if(i / 4 == 11)
buttons[i] = new Button(faces[i + 1], back, 3);
if(i / 4 == 12)
buttons[i] = new Button(faces[i + 1], back, 2);
if(i / 4 == 13)
buttons[i] = new Button(faces[i + 1], back, 1);
}
//LISTENER
for (int i = 0; i < 54; i++)
{
buttons[i].addActionListener(new GameLogic());
}
//ADD
randomize(buttons);
for (int i = 0; i < 54; i++)
{
add(buttons[i]);
}
//TIMER - handout in class
timerPerformer = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i < 54; i++)
{
buttons[i].turn = buttons[i].turn = true;
buttons[i].turn();
}
}
};
buttonTimer = new Timer(500, timerPerformer);
buttonTimer.setRepeats(false);
} // end Cards()
public class GameLogic implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Button card1 = (Button) e.getSource();
Button card2 = (Button) e.getSource();
if(!card1.turn && !buttonTimer.isRunning())
{
card1.turn();
count++;
}
if (card1.returnValue() == card2.returnValue())
{
if (count > 1)
{
matchCounter += 1;
turnCounter += 1;
count = 0;
}
}
if (card1.returnValue() != card2.returnValue())
{
if (count > 1)
{
buttonTimer.start();
turnCounter += 0;
count = 0;
}
}
}
}
}
//FORMAT TAKEN FROM HANDOUT IN CLASS
import javax.swing.*;
public class Button extends JButton
{
private static final long serialVersionUID = 1L;
ImageIcon face, back;
int value;
boolean turn;
boolean faceUp;
boolean faceDown;
public Button(ImageIcon face, ImageIcon back, int value)
{
this.face = face;
this.back = back;
this.value = value;
turn = true;
turn();
}
public void turn()
{
turn = !turn;
if (turn)
this.setIcon(face);
else
this.setIcon(back);
}
public boolean cardUp()
{
return faceUp;
}
public boolean cardDown()
{
return !faceUp;
}
public int returnValue()
{
return value;
}
}
Upvotes: 0
Views: 44
Reputation: 17567
Your compiler is telling you that the values are equal because of this lines:
Button card1 = (Button) e.getSource();
Button card2 = (Button) e.getSource();
You're assigning the exact same Button
instance to two different variables.
Therefore if (card1.returnValue() == card2.returnValue())
will always be true. if (card1 == card2)
is also true, due to the same Button
reference.
You could try the following approach:
Add an instance variable Button firstSelectedCard
and then inside the actionPerformed
method:
public void actionPerformed(ActionEvent e) {
if (firstSelectedCard == null) {
firstSelectedCard = (Button) e.getSource();
} else {
Button secondSelectedCard = (Button) e.getSource();
// ... perform checks
firstSelectedCard = null;
}
Upvotes: 0
Reputation: 201439
Correct. They are always the same.
Button card1 = (Button) e.getSource();
Button card2 = (Button) e.getSource();
Since e.getSource()
is the same card1 == card2
.
Upvotes: 1