2015-11-30 109 views
1

我即將完成我的tictactoe遊戲,但我無法弄清楚如何讓我的重置按鈕啓動遊戲結束。我也希望比賽在有人獲勝或有平局時重置。任何幫助將不勝感激。TicTacToe GUI應用程序重置按鈕

這裏是我的代碼(遺憾的是,有這麼多的話):

package tictactoe; 
import javax.swing.JFrame; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.LineBorder; 
public class TicTacToe 
{ 


    public static void main(String[] args) 
    { 
     JFrame ticTacToe = new TicTacToeFrame(); 
     ticTacToe.setTitle("Phantom TicTacToe Game!"); 
     ticTacToe.setSize(600, 600); 
     ticTacToe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     ticTacToe.setLocationRelativeTo(null); 
     ticTacToe.setVisible(true); 
    } 

} 
class TicTacToeFrame extends JFrame implements ActionListener 
{ 

    private char whoseTurn = 'X'; 
    private boolean gameOver = false; 


    private Cell[][] cells = new Cell[3][3]; 


    JLabel jlblStatus = new JLabel("X's turn to play"); 


    public TicTacToeFrame() 
    { 
     JButton btn = new JButton("Reset"); 
     JPanel panel = new JPanel(new GridLayout(3, 3, 0, 0)); 
     for (int i = 0; i < 3; i++) 
      for (int j = 0; j < 3; j++) 
       panel.add(cells[i][j] = new Cell()); 


     add(btn, BorderLayout.SOUTH); 
     add(panel, BorderLayout.CENTER); 
     add(jlblStatus, BorderLayout.NORTH); 
     jlblStatus.setOpaque(true); 
     jlblStatus.setBackground(Color.YELLOW); 
    } 


    public boolean isFull() 
    { 
     for (int i = 0; i < 3; i++) 
      for (int j = 0; j < 3; j++) 
       if (cells[i][j].getToken() == ' ') 
        return false; 
     return true; 
    } 


    public boolean isWon(char token) 
    { 

     for (int i = 0; i < 3; i++) 
      if ((cells[i][0].getToken() == token) 
        && (cells[i][1].getToken() == token) 
        && (cells[i][2].getToken() == token)) 
      { 
       return true; 
      } 


     for (int j = 0; j < 3; j++) 
      if ((cells[0][j].getToken() == token) 
       && (cells[1][j].getToken() == token) 
       && (cells[2][j].getToken() == token)) 
      { 
       return true; 
      } 

     if ((cells[0][0].getToken() == token) 
       && (cells[1][1].getToken() == token) 
       && (cells[2][2].getToken() == token)) 
      { 
       return true; 
      } 

     if ((cells[0][2].getToken() == token) 
       && (cells[1][1].getToken() == token) 
       && (cells[2][0].getToken() == token)) 
      { 
       return true; 
      } 

     return false; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     JButton bttn = (JButton) e.getSource(); 
     if(bttn == btn) 
     { 

     } 
    } 


    public class Cell extends JPanel 
    { 

     private char token = ' '; 


     public Cell() 
     { 
      setBorder(new LineBorder(Color.black, 1)); 
      addMouseListener(new MyMouseListener()); 
     } 


     public char getToken() 
     { 
      return token; 
     } 


     public void setToken(char c) 
     { 
      token = c; 
      repaint(); 
     } 

     @Override 
     protected void paintComponent(Graphics g) 
     { 
      super.paintComponent(g); 

      if (token == 'X') 
      { 
       g.drawLine(10, 10, getWidth() - 10, getHeight() - 10); 
       g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10); 
      } 

      else if (token == 'O') 
      { 
       g.drawOval(10, 10, getWidth() - 20, getHeight() - 20); 
      } 
     } 

     private class MyMouseListener extends MouseAdapter 
     { 
      @Override 
      public void mouseClicked(MouseEvent e) 
      { 
       if (gameOver) 
        return; 


       if (token == ' ' && whoseTurn != ' ') 
        setToken(whoseTurn); 


       if (isWon(whoseTurn)) 
       { 
        jlblStatus.setText(whoseTurn + " won! Game over!"); 
        whoseTurn = ' '; 
        gameOver = true; 
       } 
       else if (isFull()) 
       { 
        jlblStatus.setText("Tie game! Game over!"); 
        whoseTurn = ' '; 
        gameOver = true; 
       } 
       else 
       { 
        whoseTurn = (whoseTurn == 'X') ? 'O' : 'X'; 
        jlblStatus.setText(whoseTurn + "'s turn."); 
       } 
      } 

     } 
    } 
} 
+2

重用你的'換loop's從你的構造函數...?也許把它包裝在一個方法中,你可以更容易地重新使用 – MadProgrammer

+0

把它放到一個叫做'reset()'的方法中。 –

+0

你應該首次初始化遊戲,就像遊戲第一次開始時一樣,將這個功能包裝在@MadProgrammer所說的方法中。 – charliebrownie

回答

0

你需要重新設置程序的狀態,包括無論是你的一些用戶界面的娛樂或復位(標籤並且具有九個單元的面板需要改變)。

問題看一看:

  • TicTacToeFrameactionPerformed方法並實現了ActionListener接口,但復位按鈕沒有動作偵聽器之中;
  • 爲復位:例如,您可以設置whoseTurngameOver領域爲它們的初始值,設定在jlblStatus標籤的文本,並
    • 選項1:從panel刪除所有細胞,並添加新的(就像你在構造函數中已經做的 - 這可能是一個initializeCells法)或
    • 選項2(如果你願意回收):添加一個reset方法將Cell類清除token場,稱這種復位方法對所有細胞,並告訴panel重繪)。

小東西要記住:

    如果你想使用構造之外 panelbutton部件,改變他們到字段(而不是局部變量)
  • ;
  • actionPerformed方法中的if語句使用==運算符而不是equals方法 - 您確定嗎? (提示:見What is the difference between == vs equals() in Java?)。
0

您已擁有該代碼。

JFrame ticTacToe = new TicTacToeFrame(); 
    ticTacToe.setTitle("Phantom TicTacToe Game!"); 
    ticTacToe.setSize(600, 600); 
    ticTacToe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    ticTacToe.setLocationRelativeTo(null); 
    ticTacToe.setVisible(true); 

只要把它startNewGame()方法中,在開始通話它既而當比賽贏;)