我目前正在爲我的CS課程開發一個項目,該項目涉及製作允許用戶玩卡片遊戲濃度(或內存)的GUI遊戲。我所有的代碼工作得很好,直到我開始比較每張牌的值。當他們沒有相同的價值時,我告訴牌翻轉,但他們保持不變。我已經查看了我的代碼,並且每當我擁有代碼時:即使我看到它們不是,我也會看到這些值總是相等的。比較整數的麻煩
下面是該問題的代碼的其餘部分,我將不勝感激給予任何幫助:
//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;
}
}
我不知道究竟是如何工作的。我試了一下,現在它所做的只是讓我雙擊將卡片翻轉到正面,但它仍然不會比較這些值。 – Suhvezdia 2014-11-07 01:49:27
@Suvvezdia你應該選擇兩張不同的卡片。如果你只想選擇一張牌,那麼整個比較就沒有意義了。 – Tom 2014-11-07 01:51:11
我正在選擇卡片,需要兩次點擊才能使卡片正面朝上,但仍然沒有什麼比較正確。 – Suhvezdia 2014-11-07 01:54:24