Java 从井字游戏中获得赢家
所以我明天要交一场抽签游戏。我设法编写了几乎所有的代码,但我无法获得它来获得冠军 到目前为止,这就是我所拥有的:Java 从井字游戏中获得赢家,java,swing,jframe,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;
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;
}
}
加纳多=真} 但它说它缺少返回声明。然后我修改了代码,以便使用.equals,但它说它又丢失了一个return语句
if(jButton1.getText().equals(jButton2.getText()) && (jButton2.getText.equals(jButton3.getText)) && (jButton2.getText() != "") {
ganador = true;
}
另外,我将这部分代码放在末尾,但不确定这是否是问题所在。AbstractButton
中的getText()
函数返回一个字符串,在java中,您需要使用.equals(otherString)
函数比较字符串
编辑:关于您丢失的返回语句。这也意味着那里的代码不完整,因为所有这些方法都是void
。但是如果你说一个函数是:publicbooleandosomething()
,那么你需要一个实际返回boolean
的return语句,比如:returnganador
为了进一步说明Simon所说的内容,您希望按照以下思路实施:
jButton1.getText().equals(jButton2.getText())
我不想费心去筛选所有的代码来尝试找到你需要帮助的方法或组件。你能澄清一下吗:我有点错误,所以我可以扩展我的答案。在信息如此之少的情况下,需要转换大量的代码。顺便说一句,如果您采用了适当的面向对象方法(如果您要使用像Java这样的面向对象语言,这是您应该做的),那么这个项目可能更容易模块化。您的整个程序都是在一个类中编写的,包含着可怕的意大利面条代码。你也可以用BASIC写这篇文章。我编辑了我的代码,但它仍然不起作用。这应该放在代码中的什么位置?
if(jButton1.getText().equals(jButton2.getText()) && (jButton2.getText.equals(jButton3.getText)) && (jButton2.getText() != "") {
ganador = true;
}