重新開始遊戲 (Restarting a game)


問題描述

重新開始遊戲 (Restarting a game)

I am creating a simple game of TicTacToe, but I got stuck in restarting the game whenever the game ends and when I click the restart button. I just did the first horizontal buttons. When you press them a window will pop-up and tell you that there is a winner. I just did it for testing. But how do I restart the game when I press restart?I'm just new to java, and I'm trying to make games on my own to improve.  Here are some parts of my code:

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!");
     }
 }

參考解法

方法 1:

It seems that the only state you have is text on buttons. So to restart game you only need to set empty text (what was the initial text?) and enable buttons. Something like this:

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

Some additional notes:

All if in your actionPerformed method can be replaced with this:

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

Also it's REALLY bad idea to check string equality using ==. So I advice you to replace all b1.getText() == b2.getText() with b1.getText().equals(b2.getText()). See this link.

(by ickyrrMikita Belahlazau)

參考文件

  1. Restarting a game (CC BY-SA 3.0/4.0)

#java #tic-tac-toe #game-loop






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論