java中的记忆游戏

时间:2013-05-01 18:04:24

标签: java model-view-controller

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

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

1 个答案:

答案 0 :(得分:1)

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

看起来这不是一个编码错误,因为你的设计需要一些补充。主要原因是每张卡都有自己的CardButtonActionListener实例,点击后,此监听器类不知道已经点击过的任何其他卡。要快速解决此问题,您可以在游戏类中添加public static Card lastClicked;成员变量(免责声明:注意我说'快速',而不是'',因为这违反了良好的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;
     }
}