首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java中的Memory Game

java中的Memory Game
EN

Stack Overflow用户
提问于 2013-05-02 02:04:24
回答 1查看 3.9K关注 0票数 1

我正在试着写一个记忆游戏。当我们运行app时,“菜单”首先出现。有一个玩家和两个玩家可供选择。当我们点击一个玩家的“关卡”选择部分出现。这里的选择是简单的,中等的,困难的。然后点击其中一个,游戏就开始了。但我在实现如何检查选定的卡片相等或不相等时遇到了问题。创建卡时,会为其分配一个id。当两张卡被点击时,我会检查身份证。如果is相同,则返回"match",如果不同,则返回"close“,表示纸牌反面朝下。我使用的是MVC模式。我有9节课。卡片,游戏,级别(枚举),菜单,状态(枚举);查看CardButton,GamePanel,LevelPanel,MenuPanel。GamePanel类:

代码语言:javascript
复制
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){

        }
    }

}
}

班级游戏:

代码语言:javascript
复制
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方法(在游戏中)。当我运行代码时,我得到空指针异常。因为我不能拿两个扣子的信息。其中一个总是保持为空。我可能需要更改这一部分:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-02 02:57:02

创建CardButtonActionListener时,b和b2变量为null。因此,当第一次调用actionPerformed()时,当您执行if(i==0)条件语句时,b或b2中只有一个会从e.getSource()中获得返回值。因此,当您调用currentState = model.compareCards(b, b2);时,b或b2仍然为null,正如您发现的那样,这将导致该方法抛出一个null指针异常。

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

代码语言:javascript
复制
@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;
     }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16323736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档