我正在尝试写一个记忆游戏。当我们运行应用程序时,“菜单”首先出现。选择是一名球员和两名球员。当我们点击一个玩家时,“等级”选择部分出现。这里的选择是简单的、中等的、困难的。然后单击其中一个并开始游戏。但是我在实施如何检查所选卡片是否相等时遇到了麻烦。创建卡片时,会为其分配一个 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