java - 从 Tic Tac Toe 中获得获胜者

标签 java swing jframe

所以我明天必须交一盘井字游戏。我设法编写了几乎所有内容,但我无法获得获胜者。

到目前为止,这就是我所拥有的:

package TicTacToe;

public class TicTacToeMain {

public static void main(String[] args) {
    // TODO code application logic here
    TicTacToe t = new TicTacToe();
    t.setVisible(true);
}


}


package TicTacToe;

import javax.swing.JOptionPane;

public class TicTacToe extends javax.swing.JFrame {
public TicTacToe() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jRadioButton2 = new javax.swing.JRadioButton();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jButton5 = new javax.swing.JButton();
    jButton6 = new javax.swing.JButton();
    jButton7 = new javax.swing.JButton();
    jButton8 = new javax.swing.JButton();
    jButton9 = new javax.swing.JButton();
    jButton10 = new javax.swing.JButton();
    jButton11 = new javax.swing.JButton();
    jButton12 = new javax.swing.JButton();
    jRadioButton1 = new javax.swing.JRadioButton();
    jRadioButton3 = new javax.swing.JRadioButton();

    jRadioButton2.setText("jRadioButton2");

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setEnabled(false);
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jButton2.setEnabled(false);
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    jButton3.setEnabled(false);
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });

    jButton4.setEnabled(false);
    jButton4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton4ActionPerformed(evt);
        }
    });

    jButton5.setEnabled(false);
    jButton5.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton5ActionPerformed(evt);
        }
    });

    jButton6.setEnabled(false);
    jButton6.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton6ActionPerformed(evt);
        }
    });

    jButton7.setEnabled(false);
    jButton7.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton7ActionPerformed(evt);
        }
    });

    jButton8.setEnabled(false);
    jButton8.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton8ActionPerformed(evt);
        }
    });

    jButton9.setEnabled(false);
    jButton9.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton9ActionPerformed(evt);
        }
    });

    jButton10.setText("New Game");
    jButton10.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton10ActionPerformed(evt);
        }
    });

    jButton11.setText("Reset");
    jButton11.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton11ActionPerformed(evt);
        }
    });

    jButton12.setText("Quit");
    jButton12.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton12ActionPerformed(evt);
        }
    });

    jRadioButton1.setText("Player 1 ( X )");
    jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jRadioButton1ActionPerformed(evt);
        }
    });

    jRadioButton3.setText("Player 1 ( O )");
    jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jRadioButton3ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jRadioButton1)
            .addGap(62, 62, 62)
            .addComponent(jRadioButton3)
            .addGap(93, 93, 93))
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jButton10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jButton11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jButton12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))))
            .addGap(0, 0, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jRadioButton1)
                .addComponent(jRadioButton3))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton10)
                .addComponent(jButton11)
                .addComponent(jButton12)))
    );

    pack();
}// </editor-fold>                        

int count = 2;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:

    if (jRadioButton1.isSelected()) {
        if (count % 2 == 0) {
            count++;
            jButton1.setText("X");
            jButton1.setEnabled(false);
        } else {
            count++;
            jButton1.setText("O");
            jButton1.setEnabled(false);
        }
    } else {
        if (jRadioButton3.isSelected()) {
            if ((count % 2 == 0)) {
                count++;
                jButton1.setText("O");
                jButton1.setEnabled(false);
            } else {
                count++;
                jButton1.setText("X");
                jButton1.setEnabled(false);
            }
        }
    }
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:

    if (jRadioButton1.isSelected()) {
        if (count % 2 == 0) {
            count++;
            jButton2.setText("X");
            jButton2.setEnabled(false);
        } else {
            count++;
            jButton2.setText("O");
            jButton2.setEnabled(false);
        }
    } else {
        if (jRadioButton3.isSelected()) {
            if ((count % 2 == 0)) {
                count++;
                jButton2.setText("O");
                jButton2.setEnabled(false);
            } else {
                count++;
                jButton2.setText("X");
                jButton2.setEnabled(false);
            }
        }
    }
}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:

    if (jRadioButton1.isSelected()) {
        if (count % 2 == 0) {
            count++;
            jButton3.setText("X");
            jButton3.setEnabled(false);
        } else {
            count++;
            jButton3.setText("O");
            jButton3.setEnabled(false);
        }
    } else {
        if (jRadioButton3.isSelected()) {
            if ((count % 2 == 0)) {
                count++;
                jButton3.setText("O");
                jButton3.setEnabled(false);
            } else {
                count++;
                jButton3.setText("X");
                jButton3.setEnabled(false);
            }
        }
    }
}                                        

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    if (jRadioButton1.isSelected()) {
        if (count % 2 == 0) {
            count++;
            jButton5.setText("X");
            jButton5.setEnabled(false);
        } else {
            count++;
            jButton5.setText("O");
            jButton5.setEnabled(false);
        }
    } else {
        if (jRadioButton3.isSelected()) {
            if ((count % 2 == 0)) {
                count++;
                jButton5.setText("O");
                jButton5.setEnabled(false);
            } else {
                count++;
                jButton5.setText("X");
                jButton5.setEnabled(false);
            }
        }
    }
}                                        

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    if (jRadioButton1.isSelected()) {
        if (count % 2 == 0) {
            count++;
            jButton6.setText("X");
            jButton6.setEnabled(false);
        } else {
            count++;
            jButton6.setText("O");
            jButton6.setEnabled(false);
        }
    } else {
        if (jRadioButton3.isSelected()) {
            if ((count % 2 == 0)) {
                count++;
                jButton6.setText("O");
                jButton6.setEnabled(false);
            } else {
                count++;
                jButton6.setText("X");
                jButton6.setEnabled(false);
            }
        }
    }
}                                        

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    if (jRadioButton1.isSelected()) {
        if (count % 2 == 0) {
            count++;
            jButton4.setText("X");
            jButton4.setEnabled(false);
        } else {
            count++;
            jButton4.setText("O");
            jButton4.setEnabled(false);
        }
    } else {
        if (jRadioButton3.isSelected()) {
            if ((count % 2 == 0)) {
                count++;
                jButton4.setText("O");
                jButton4.setEnabled(false);
            } else {
                count++;
                jButton4.setText("X");
                jButton4.setEnabled(false);
            }
        }
    }
}                                        

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    if (jRadioButton1.isSelected()) {
        if (count % 2 == 0) {
            count++;
            jButton8.setText("X");
            jButton8.setEnabled(false);
        } else {
            count++;
            jButton8.setText("O");
            jButton8.setEnabled(false);
        }
    } else {
        if (jRadioButton3.isSelected()) {
            if ((count % 2 == 0)) {
                count++;
                jButton8.setText("O");
                jButton8.setEnabled(false);
            } else {
                count++;
                jButton8.setText("X");
                jButton8.setEnabled(false);
            }
        }
    }
}                                        

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    if (jRadioButton1.isSelected()) {
        if (count % 2 == 0) {
            count++;
            jButton9.setText("X");
            jButton9.setEnabled(false);
        } else {
            count++;
            jButton9.setText("O");
            jButton9.setEnabled(false);
        }
    } else {
        if (jRadioButton3.isSelected()) {
            if ((count % 2 == 0)) {
                count++;
                jButton9.setText("O");
                jButton9.setEnabled(false);
            } else {
                count++;
                jButton9.setText("X");
                jButton9.setEnabled(false);
            }
        }
    }
}                                        

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    if (jRadioButton1.isSelected()) {
        if (count % 2 == 0) {
            count++;
            jButton7.setText("X");
            jButton7.setEnabled(false);
        } else {
            count++;
            jButton7.setText("O");
            jButton7.setEnabled(false);
        }
    } else {
        if (jRadioButton3.isSelected()) {
            if ((count % 2 == 0)) {
                count++;
                jButton7.setText("O");
                jButton7.setEnabled(false);
            } else {
                count++;
                jButton7.setText("X");
                jButton7.setEnabled(false);
            }
        }
    }
}                                        

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    jButton1.setText("");
    jButton1.setEnabled(false);
    jButton2.setText("");
    jButton2.setEnabled(false);
    jButton3.setText("");
    jButton3.setEnabled(false);
    jButton4.setText("");
    jButton4.setEnabled(false);
    jButton5.setText("");
    jButton5.setEnabled(false);
    jButton6.setText("");
    jButton6.setEnabled(false);
    jButton7.setText("");
    jButton7.setEnabled(false);
    jButton8.setText("");
    jButton8.setEnabled(false);
    jButton9.setText("");
    jButton9.setEnabled(false);
    jRadioButton1.setSelected(false);
    jRadioButton3.setSelected(false);
    JOptionPane.showMessageDialog(null, "Seleccione ( X ) o ( O )");

}                                         

private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    System.exit(0);

}                                         

private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:
    if (jRadioButton1.isSelected()) {
        jButton1.setEnabled(true);
        jButton2.setEnabled(true);
        jButton3.setEnabled(true);
        jButton4.setEnabled(true);
        jButton5.setEnabled(true);
        jButton6.setEnabled(true);
        jButton7.setEnabled(true);
        jButton8.setEnabled(true);
        jButton9.setEnabled(true);
    }

}                                             

private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                              
    // TODO add your handling code here:

    if (jRadioButton3.isSelected()) {
        jButton1.setEnabled(true);
        jButton2.setEnabled(true);
        jButton3.setEnabled(true);
        jButton4.setEnabled(true);
        jButton5.setEnabled(true);
        jButton6.setEnabled(true);
        jButton7.setEnabled(true);
        jButton8.setEnabled(true);
        jButton9.setEnabled(true);
    }
}                                             

private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
    jButton1.setText("");
    jButton1.setEnabled(false);
    jButton2.setText("");
    jButton2.setEnabled(false);
    jButton3.setText("");
    jButton3.setEnabled(false);
    jButton4.setText("");
    jButton4.setEnabled(false);
    jButton5.setText("");
    jButton5.setEnabled(false);
    jButton6.setText("");
    jButton6.setEnabled(false);
    jButton7.setText("");
    jButton7.setEnabled(false);
    jButton8.setText("");
    jButton8.setEnabled(false);
    jButton9.setText("");
    jButton9.setEnabled(false);
    jRadioButton1.setSelected(false);
    jRadioButton3.setSelected(false);
    count=0;
}                                         

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new TicTacToe().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton10;
private javax.swing.JButton jButton11;
private javax.swing.JButton jButton12;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JButton jButton7;
private javax.swing.JButton jButton8;
private javax.swing.JButton jButton9;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
// End of variables declaration                   

//if(jButton1.equals(jButton2) && jButton2.equals(jButton3)&& (jButton2) != "")) {

if(jButton1.getText().equals(jButton2.getText()) && (jButton2.getText.equals(jButton3.getText)) && (jButton2.getText() != "") {
  ganador = true;        
}
}

游戏运行得很好,但我不知道如何获得胜利者。

  1. 我不确定需要将其放置在代码中的确切位置。

  2. 首先我尝试了

    if((jButton1.getText())==(jButton2.getText())&&((jButton2.getText())==(jButton3.getText()){
    

    ganador = true;
    }

但它说它缺少返回语句。然后我更改了代码以使用 .equals 但它又说它缺少 return 语句。

     if(jButton1.getText().equals(jButton2.getText()) && (jButton2.getText.equals(jButton3.getText)) && (jButton2.getText() != "") {
  ganador = true;        
}

另外,我将这部分代码放在最后,但不确定这是否是问题所在。

最佳答案

getText()函数位于 AbstractButton返回 String ,在java中你需要比较Strings使用.equals(otherString)功能

编辑:关于您丢失的返回声明。这也意味着您的代码不完整,因为所有这些方法都是 void 。但如果你说一个函数是:public boolean doSomething()那么你需要一个返回语句,它实际上返回 boolean如:return ganador

关于java - 从 Tic Tac Toe 中获得获胜者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19783697/

相关文章:

java - createStatement()方法如何返回Statement对象?

java - 当 JFrame 中有背景图像时将 JButtons 移至左侧

java - 画笔型 GUI 中的 paintComponent() 与 paint() 和 JPanel 与 Canvas

java - 以编程方式设置 JLabel 文本

java - 为什么屏幕不变色?

java - 将文件中的整数存储到数组中并查找平均 JAVA

java - 将 String(格式与 ArrayList<String> 类似)转换为实际的 ArrayList<String>

java - 使用刚刚访问过的 int 变量通过矩阵查看不同的组合?

java - 按钮大小和位置

Java View 到 Controller 观察者