我目前正在为我的CS课程开发一个项目,该项目涉及制作一个允许用户玩纸牌游戏浓度(或记忆)的GUI游戏。我的所有代码都完美无缺,直到我开始比较每张卡的值。当他们没有相同的价值时,我告诉他们要翻转,但他们仍然保持正常状态。我查看了我的代码,每当我有代码时:
if (card1.returnValue() == card2.returnValue())
它表明这些值总是相等的,即使我可以看到它们不是。
以下是该问题的其余代码,我非常感谢给予的任何帮助:
//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;
}
}
答案 0 :(得分:1)
正确。它们总是一样的。
Button card1 = (Button) e.getSource();
Button card2 = (Button) e.getSource();
由于e.getSource()
与card1 == card2
相同。
答案 1 :(得分:0)
你的编译器告诉你这些值是相等的:
Button card1 = (Button) e.getSource();
Button card2 = (Button) e.getSource();
您将完全相同的Button
实例分配给两个不同的变量。
因此if (card1.returnValue() == card2.returnValue())
永远都是真的。由于if (card1 == card2)
引用相同,Button
也是如此。
您可以尝试以下方法:
添加实例变量Button firstSelectedCard
,然后在actionPerformed
方法中添加:
public void actionPerformed(ActionEvent e) {
if (firstSelectedCard == null) {
firstSelectedCard = (Button) e.getSource();
} else {
Button secondSelectedCard = (Button) e.getSource();
// ... perform checks
firstSelectedCard = null;
}