2017-05-31 35 views
0

我試圖編寫代碼,並且在用戶輸入y或yes(不區分大小寫)到InputDialog後嘗試循環我的主代碼時使用了do while循環。用於循環主代碼的java幫助

這裏是代碼,唯一的錯誤是a}需要放在do之後{但是如果我把它放到我想要的地方,它不起作用,並且它不能識別thw儘管在底部的聲明,請幫助。

package tictactoemain; 

import javax.swing.JOptionPane; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.LineBorder; 

/** 
* JFrame to hold TicTacToe board. 
*/ 
public class TicTacToeFrame extends JFrame 
{  

    **do {** //do starts here 
    private static final long serialVersionUID = 1L; 

    // Indicate whose turn it is 
    private char whoseTurn = 'X'; 
    private boolean gameOver = false; 

    // Create cell grid using an Array 
    private Cell[][] cells = new Cell[3][3]; 

    // Create a status label 
    JLabel jlblStatus = new JLabel("X's turn to play"); 

    /** 
    * No-argument Constructor 
    * @return 
    */ 
    public TicTacToeFrame() 
    { 
     // Panel to hold cells 
     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()); 

     panel.setBorder(new LineBorder(Color.black, 1)); 
     jlblStatus.setBorder(new LineBorder(Color.black, 1)); 

     add(panel, BorderLayout.CENTER); 
     add(jlblStatus, BorderLayout.SOUTH); 
    } 
    // Determine if it's a tie 
    /** 
    * Determine if game board is full. 
    * @return True, if game board is full. Otherwise, false. 
    */ 
    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; 
    } 
    // Check to see if a player (Token) has won 
    /** 
    * Determines if a given token has won. 
    * @param token Token to test for winning 
    * @return True, if the token has won. Otherwise, false. 
    */ 
    public boolean isWon(char token) 
    { 
     // check rows 
     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; 
      } 

     // check columns 
     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; 
      } 
     // check diagonals 
     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; 
    } 

    /** 
    * Defines a cell in a TicTacToe game board. 
    */ 
    public class Cell extends JPanel 
    { 
     /** 
     * 
     */ 
     private static final long serialVersionUID = 1L; 
    // token of this cell 
     private char token = ' '; 

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

     /** 
     * Gets the token of the cell. 
     * @return The token value of the cell. 
     */ 
     public char getToken() 
     { 
      return token; 
     } 

     /** 
     * Sets the token of the cell. 
     * @param c Character to use as token value. 
     */ 
     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); 
      } 
     } 
     // MouseListener to listen for a click to place a token 
     private class MyMouseListener extends MouseAdapter 
     { 

     private String playAgain; 
     private Object playYes; 
     private Object playNo; 

     @Override 
      public void mouseClicked(MouseEvent e) 
      { 
       if (gameOver) 
        return; 

       // if condition not met, the game is not over 
       if (token == ' ' && whoseTurn != ' ') 
        setToken(whoseTurn); 

       // Check game status 
       if (isWon(whoseTurn)) 
       { 
        jlblStatus.setText("Congratulations " + whoseTurn + ", You Are Winnner! Game Over! Exit And Restart The Program To Play Again!"); 
        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 to play."); 

       } 
      // and ends here, while statement is below 
       { 
       // if the game is over, this will run   
       if (gameOver == true) 

        playAgain = JOptionPane.showInputDialog("Would you like to play again?"); 

       // if statement that either restarts the game or closes it depending on the yes/y (cases are ignored) answer 

       while (playAgain.equalsIgnoreCase("y") | playAgain.equalsIgnoreCase("yes")) 
       { 
        playYes = ("Yes? Okay, have fun!"); 

        JOptionPane.showMessageDialog(null, playYes); 
     } 
    }   
    }// End of Win condition checker 
    } // End class MyMouseListener   
    } 
} // End class TicTacToeFrame 
+0

假設您使用do while循環來解決您的問題。這些[post1](https://stackoverflow.com/questions/34029538/loop-to-repeat-a-game-of-tic-tac-toe)&[post2](https://stackoverflow.com/questions/ 28388383/tic-tac-toe-program-loop-problems)解決了我認爲的相同問題。 –

+0

我注意到你正在使用singulars'|' - 確定你明白這個https://stackoverflow.com/a/5564484/2310289 –

+0

爲什麼你不使用'main(null)'如果用戶想玩再次?這運行的主要方法。 – CodingNinja

回答

0

您可以使用單一方法初始化所有變量。這樣,您可以在程序開始時使用它初始化遊戲,如果玩家再次請求玩遊戲,則可以調用相同的方法,從而使程序恢復到原始狀態。

private void init() { 
    whoseTurn = 'X'; 
    gameOver = false; 
    jlblStatus = new JLabel("X's turn to play"); 
    for(Cell[] ca : cells) { 
     for(Cell c : ca) { 
      c.setToken(' '); 
     } 
    } 
} 

我的例子可能不包括所有的變量,但你可以調整它來適應你的需求。

do-while循環無法識別,因爲它在方法之外。

+0

這將允許我詢問用戶是否想再次播放,並根據他們的回答重新啓動程序? –

+0

是的。如果他們說是,那麼調用init()方法,如果配置正確,它應該重置遊戲,如果他們說不,那麼就像平常那樣簡單地退出遊戲。 – Tomahawk2001913