2012-08-05 137 views
-1

我正在創建一個TicTacToe的簡單遊戲,但當遊戲結束並重新啓動遊戲時,我一直卡在重新啓動按鈕上。我只是做了第一個水平按鈕。當你按下它們時,會彈出一個窗口,告訴你有一個贏家。我只是做了測試。但是當我按下重新啓動時,我該如何重新啓動遊戲?我只是新手,我正在嘗試單獨製作遊戲來改進。 這裏是我的代碼的某些部分:重新啓動遊戲

public class TicTacToe extends javax.swing.JFrame implements ActionListener{ 
int i=0; 
boolean win = false; 
String player[]={"You","Comp"}; 
/** 
* Creates new form TicTacToe 
*/ 
public TicTacToe() { 
    super("TicTacToe (LeeMinHu-bag)"); 
    initComponents(); 
    setResizable(false); 
    setLocationRelativeTo(null); 
    b1.addActionListener(this); 
    b2.addActionListener(this); 
    b3.addActionListener(this); 
    b4.addActionListener(this); 
    b5.addActionListener(this); 
    b6.addActionListener(this); 
    b7.addActionListener(this); 
    b8.addActionListener(this); 
    b9.addActionListener(this); 

} 
//If a button is pressed, its text will become "X" 
public void actionPerformed(ActionEvent e){ 
    if(e.getSource() == b1){ 
     b1.setText("X"); 
     b1.setEnabled(false); 
    }else if(e.getSource() == b2){ 
     b2.setText("X"); 
     b2.setEnabled(false); 
    }else if(e.getSource() == b3){ 
     b3.setText("X"); 
     b3.setEnabled(false); 
    }else if(e.getSource() == b4){ 
     b4.setText("X"); 
     b4.setEnabled(false); 
    }else if(e.getSource() == b5){ 
     b5.setText("X"); 
     b5.setEnabled(false); 
    }else if(e.getSource() == b6){ 
     b6.setText("X"); 
     b6.setEnabled(false); 
    }else if(e.getSource() == b7){ 
     b7.setText("X"); 
     b7.setEnabled(false); 
    }else if(e.getSource() == b8){ 
     b8.setText("X"); 
     b8.setEnabled(false); 
    }else if(e.getSource() == b9){ 
     b9.setText("X"); 
     b9.setEnabled(false); 
    }  
    i++; 
    System.out.println(i); 
    if(i%2==0){ 
     turn.setText(player[0]); 
    }else{ 
     turn.setText(player[1]); 
    } 

    checkIfWin(); 

} 
//check to see if there is a winner 
public void checkIfWin(){ 
    if(b1.getText()==b2.getText() && b2.getText()==b3.getText() && b1.getText()!=""){ 
     win = true; 
    }else if(i==9 && win==false){ 
     JOptionPane.showMessageDialog(null,"Tie!","Game Over",JOptionPane.INFORMATION_MESSAGE); 
    }else{ 
     win=false; 
    }  
    ifWin(win); 
} 
//if there is a winner 
public void ifWin(boolean w){ 
    if(w==true){ 
     JOptionPane.showMessageDialog(null,"WIN!","Game Over",JOptionPane.INFORMATION_MESSAGE); 
     TicTacToe restart = new TicTacToe(); 
     restart.validate(); 
    }else{ 
     System.out.println("The game is still on!"); 
    } 
} 
+0

所有的遊戲狀態,變量等都應該初始化,就像你第一次運行遊戲一樣。 – nullpotent 2012-08-05 14:13:10

+0

您將希望瞭解和使用數組,因爲它們將大大簡化您的代碼並以比其他方式更容易地解決您的問題。 – 2012-08-05 14:30:57

+0

謝謝你的筆記。我還沒有用過數組,但我仍在努力。 – ickyrr 2012-08-05 15:04:18

回答

2

看來你擁有的唯一狀態上的按鈕文本。因此,重新開始遊戲只需要設置空文本(初始文本是什麼?)並啓用按鈕。事情是這樣的:

JButton buttons[] = {b1, b2, b3, b4, b5, b6, b7, b8, b9}; 
for (JButton button : buttons) { 
    button.setText(""); 
    button.setEnabled(true); 
} 

一些其他注意事項:

actionPerformed方法的所有if可以用這個來代替:

JButton button = (JButtton) e.getSource(); 
button.setText("x"); 
button.setEnabled(false); 

而且它的真的壞主意使用來檢查字符串相等==。所以我建議你用b1.getText().equals(b2.getText())替換全部b1.getText() == b2.getText()。請參閱this鏈接。

+0

這樣做而不是創建遊戲的新實例(「'restart'」)。 – 2012-08-05 14:31:37

+0

我嘗試啓用按鈕並將它們設爲null。但結果是在這樣做之後,當你按下一個按鈕時,贏家對話框將顯示。我認爲它只是重新啓動按鈕,而不是遊戲本身。任何其他想法?無論如何你的答案,它給了我一個縮短我的代碼的新方法。 – ickyrr 2012-08-05 14:39:23

+0

@ickyrr您是否將空文本設置爲按鈕?在你的代碼標準中獲獎的是:按鈕b1,b2和b3必須有相同的文本,而不是空的。所以要重新開始遊戲,您需要將空文本設置爲所有按鈕。 – 2012-08-05 14:41:49