我想寫一個記憶遊戲。當我們運行應用程序時,首先出現「菜單」。選擇是一個球員和兩個球員。當我們點擊一個玩家「級別」選擇部分出現。這裏的選擇很簡單,中等,很難。然後點擊其中一個,遊戲開始。但是在執行如何檢查選定的卡片時,我遇到了麻煩。當一張卡片被創建時,一個ID被分配給它。當點擊兩張牌時,我正在檢查ID。如果id相同,則返回「匹配」,如果不是,那麼「close」返回,這意味着卡片面朝下。我正在使用MVC模式。我有9個班。卡,遊戲,級別(枚舉),菜單,狀態(枚舉);查看CardButton,GamePanel,LevelPanel,MenuPanel。 類的GamePanel:java中的記憶遊戲
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
...什麼是空的,在哪裏呢? – 2013-05-01 18:07:57
在課堂遊戲中,「v2 = b2.getCard()。getValue();」並在GamePanel中,「currentState = model.compareCards(b,b2);」 – user1422167 2013-05-01 18:11:12
你需要把它分解成小的,可管理的任務。 – 2013-05-01 18:11:22