1

我正在尝试写一个记忆游戏。当我们运行应用程序时,“菜单”首先出现。选择是一名球员和两名球员。当我们点击一​​个玩家时,“等级”选择部分出现。这里的选择是简单的、中等的、困难的。然后单击其中一个并开始游戏。但是我在实施如何检查所选卡片是否相等时遇到了麻烦。创建卡片时,会为其分配一个 ID。当点击两张卡片时,我正在检查 ID。如果 id 相同,则返回“匹配”,否则返回“关闭”,这意味着卡片转为面朝下。我正在使用 MVC 模式。我有9节课。卡片、游戏、关卡(枚举)、菜单、状态(枚举);视图 CardButton、GamePanel、LevelPanel、MenuPanel。类游戏面板:

package view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.Game;
import model.State;

public class GamePanel extends JPanel {
private Game model;
ActionListener cardButtonActionListener;
private JFrame frame;

public GamePanel(Game gameModel, JFrame frame) {
    int width=gameModel.getRowSize();
    int height=gameModel.getColumnSize();
    setLayout(new GridLayout(width,height));

    this.model = gameModel;
    this.cardButtonActionListener = new CardButtonActionListener();
    this.frame = frame;
    show();
}

public void show() {
    for(int row=0; row<model.getRowSize(); row++) {
        for (int col=0; col<model.getColumnSize(); col++) {
            CardButton cardBtn = new CardButton(model.getCard(row, col));
            cardBtn.addActionListener(cardButtonActionListener);
            add(cardBtn);
        }
    }

    frame.repaint();
}

public class CardButtonActionListener implements ActionListener {
    private int i = 0;
    CardButton b ,b2;
    State currentState;

    @Override
    public void actionPerformed(ActionEvent e) {

        if(i==0){
            b = (CardButton) e.getSource();
            b.setFaceUp();
            JOptionPane.showInputDialog("id "+b.getCard().getValue());
            i++;
        }
        else{
            b2 = (CardButton) e.getSource();
            b2.setFaceUp();
            i--;
        }


        currentState = model.compareCards(b, b2);

        if(currentState == State.Match){
            b.setVisible(false);
            b2.setVisible(false);               
        }

        if(currentState == State.Close){
            b.setFaceDown();
            b2.setFaceDown();               
        }

        if(currentState == State.Continue){

        }
    }

}
}

课堂游戏:

package model;

import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JPanel;

import view.CardButton;


public class Game extends JPanel {

private Level level;
private ArrayList<ArrayList<Card>> board;
private int rowSize;
private int colSize;
private ArrayList<Card> cardList;

public Game() {

}

public void setLevel(Level level,int x) {
    this.level = level;
    initBoard(x);
}

private void initBoard(int x) {
    // Create board according to level variable
    int s = x;
    rowSize = s;
    colSize = s;
    int a=rowSize*colSize/2;
    int b=0;
    this.board = new ArrayList<ArrayList<Card>>();
    for(int row=0; row<s; row++) {
        this.cardList = new ArrayList<Card>();
        for (int col=0; col<s/2; col++) {
            cardList.add(new Card(b));
            cardList.add(new Card(b));
            b++;
        }
        board.add(getCardList());
    }
    Collections.shuffle(board);
}

public ArrayList<Card> getCardList(){
    Collections.shuffle(cardList);
    return cardList;
}

public int getRowSize() {
    return rowSize;
}

public int getColumnSize() {
    return colSize;
}

public Card getCard(int row, int col) {
    return board.get(row).get(col);
}




public State compareCards(CardButton b, CardButton b2) {
    int v1, v2;
    v1 = b.getCard().getValue();
    v2 = b2.getCard().getValue();
    if(b.getCard()!= null && b2.getCard()!= null){
        return State.Continue;      
    }else{
        if(v1 == v2){
            return State.Match;
        }
        else{
            return State.Close;
            }
    }

}
}

GamePanel 获取状态信息并决定卡片应该留在哪个位置:面朝上、面朝下。但我无法正确实现动作执行方法(在 GamePanel 中)和 compareCards 方法(在游戏中)。当我运行代码时,我得到空指针异常。因为我不能拿 2 个按钮信息。其中之一始终保持为空。我可能需要更改这部分:

if(i==0){
        b = (CardButton) e.getSource();
        b.setFaceUp();
        JOptionPane.showInputDialog("id "+b.getCard().getValue());
        i++;
    }
    else{
        b2 = (CardButton) e.getSource();
        b2.setFaceUp();
        i--;
    }

但我不知道我该如何纠正。谢谢你。编辑:整个项目在这里http://cnv.as/21qoh

4

1 回答 1

1

创建时 b 和 b2 变量为空CardButtonActionListener。所以第一次actionPerformed()被调用并且你通过你的if(i==0)条件语句,只有 b 或 b2 中的一个被分配了来自 e.getSource() 的返回值。因此,当您调用currentState = model.compareCards(b, b2);b 或 b2 时,仍将为 null,您发现这将导致该方法引发空指针异常。

看起来这与其说是编码错误,不如说是您的设计需要一些补充。主要原因是每张卡片都有自己的实例,CardButtonActionListener当被点击时,这个监听器类不知道任何其他卡片已经被点击过。为了快速解决这个问题,您可以public static Card lastClicked;在 Game 类中添加一个成员变量(免责声明:注意我说的是“快速”,而不是“”,因为这违反了良好的 OOP 设计,并且在多线程应用程序中会给您带来很多麻烦......但是对于像你这样的单线程应用程序,如果你只是想让它工作,这对你来说可能没问题 - 但请注意,使用像这样的公共静态变量绝对不是养成的好习惯)。然后你可以像这样修改你的 CardButtonActionListener.actionPerformed() (注意我去掉了 'i' 变量):

@Override
public void actionPerformed(ActionEvent e) {
    CardButton justClickedButton = e.getSource();
    justClickedButton.setFaceUp();
    CardButton previouslyClickedButton = Game.lastClicked;
    if(previouslyClickedButton == null){
        JOptionPane.showInputDialog("id "+justClickedButton.getCard().getValue());
        previouslyClickedButton = justClickedButton;
    }
    else{
         currentState = model.compareCards(justClickedButton, previouslyClickedButton);

         if(currentState == State.Match){
             justClickedButton.setVisible(false);
             previouslyClickedButton.setVisible(false);               
         }

         if(currentState == State.Close){
             justClickedButton.setFaceDown();
             previouslyClickedButton.setFaceDown();               
         }

         if(currentState == State.Continue){
         }

         previouslyClickedButton = null;
     }
}
于 2013-05-01T18:57:02.427 回答