我正在用java用swing做一个2人的tic-tac-toe游戏。ui按预期工作,但我无法让它检查是否有赢家。
我上了三节课 Main
, gui
,和 winnerDisplay
. 这个 gui
类负责创建3x3网格和按钮等 winnerDisplay
本课程旨在向获奖者表示祝贺。我假设第一个玩家选择了“x”符号。另外,3x3网格以行大的方式从0到8进行编号(这里的grid是一般意义上的,而不是gridlayout/2d矩阵意义上的。
我想在每个球员的每一回合后检查是否有一个胜利者(我稍后会优化它,因为至少三步之后才能有一个胜利者)。如果有一个赢家或平局,我希望另一个窗口弹出并显示赢家的名字。
现在的问题是:无论我在哪里 isWon()
方法,它不会停止游戏的主ui线程,也不会在每次移动后检查它。我知道ui运行在不同的线程上,但我如何实现我的目标?
这就是 Main
班级:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoeDivyanshuVarma;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
*
* @author divyanshuvarma
*/
public class Main {
/**
* @param args the command line arguments
*/
static boolean playerOneTurn = true;
static boolean isFilled0 = false, isFilled1 = false, isFilled2 = false;
static boolean isFilled3 = false, isFilled4 = false, isFilled5 = false;
static boolean isFilled6 = false, isFilled7 = false, isFilled8 = false;
//static JButton button;
static boolean gameWon = false;
public static void main(String[] args) {
// TODO code application logic here
gui board = new gui();
winnerDisplay winnerDisplayWindow = new winnerDisplay();
setBoardProperties(board);
initializeBoard(board);
initializeGame(board);
/* if (isWon(board)) {
winnerDisplayWindow.setVisible(true);
winnerDisplayWindow.setTitle("Congrats Winner !");
winnerDisplayWindow.setResizable(false);
winnerDisplayWindow.setAlwaysOnTop(true);
JLabel winnerLabel = new JLabel("Winner is ");
winnerDisplayWindow.setWinnerLabel(winnerLabel);
}*/
System.out.println("\nThread count: "+Thread.activeCount());
}
public static void setBoardProperties(gui board) {
board.setVisible(true);
board.setTitle("Tic-Tac-Toe (2-player)");
board.setResizable(false);
}
public static void initializeGame(gui board) {
JButton button0, button1, button2, button3, button4, button5, button6, button7, button8;
button0 = board.getButton0();
button0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled0) {
if (playerOneTurn) {
button0.setText("X");
} else {
button0.setText("O");
}
isFilled0 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button1 = board.getButton1();
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled1) {
if (playerOneTurn) {
button1.setText("X");
} else {
button1.setText("O");
}
isFilled1 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button2 = board.getButton2();
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled2) {
if (playerOneTurn) {
button2.setText("X");
} else {
button2.setText("O");
}
isFilled2 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button3 = board.getButton3();
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled3) {
if (playerOneTurn) {
button3.setText("X");
} else {
button3.setText("O");
}
isFilled3 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button4 = board.getButton4();
button4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled4) {
if (playerOneTurn) {
button4.setText("X");
} else {
button4.setText("O");
}
isFilled4 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button5 = board.getButton5();
button5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled5) {
if (playerOneTurn) {
button5.setText("X");
} else {
button5.setText("O");
}
isFilled5 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button6 = board.getButton6();
button6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled6) {
if (playerOneTurn) {
button6.setText("X");
} else {
button6.setText("O");
}
isFilled6 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button7 = board.getButton7();
button7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled7) {
if (playerOneTurn) {
button7.setText("X");
} else {
button7.setText("O");
}
isFilled7 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});
button8 = board.getButton8();
button8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!isFilled8) {
if (playerOneTurn) {
button8.setText("X");
} else {
button8.setText("O");
}
isFilled8 = true;
// I HAVE TRIED PLACING isWon() HERE
if (playerOneTurn) {
playerOneTurn = false;
} else {
playerOneTurn = true;
}
}
}
});// I HAVE TRIED PLACING isWon() HERE TOO
}
public static boolean isWon(gui board) {
String symbol0, symbol1, symbol2, symbol3, symbol4, symbol5, symbol6, symbol7, symbol8;
symbol0 = board.getButton0().getText();
symbol1 = board.getButton1().getText();
symbol2 = board.getButton2().getText();
symbol3 = board.getButton3().getText();
symbol4 = board.getButton4().getText();
symbol5 = board.getButton5().getText();
symbol6 = board.getButton6().getText();
symbol7 = board.getButton7().getText();
symbol8 = board.getButton8().getText();
if (symbol0.equals(symbol4) && symbol4.equals(symbol8)) //main diagonal
{
gameWon = true;
}
if (symbol2.equals(symbol4) && symbol4.equals(symbol6)) //other diagonal
{
gameWon = true;
}
if (symbol0.equals(symbol1) && symbol1.equals(symbol2)) //first row
{
gameWon = true;
}
if (symbol3.equals(symbol4) && symbol4.equals(symbol5)) //second row
{
gameWon = true;
}
if (symbol6.equals(symbol7) && symbol7.equals(symbol8)) //third row
{
gameWon = true;
}
if (symbol0.equals(symbol3) && symbol3.equals(symbol6)) //first column
{
gameWon = true;
}
if (symbol1.equals(symbol4) && symbol4.equals(symbol7)) //second column
{
gameWon = true;
}
if (symbol2.equals(symbol5) && symbol5.equals(symbol8)) //third column
{
gameWon = true;
}
if (gameWon) {
return true;
} else {
return false;
}
}
public static void initializeBoard(gui board) {
JButton button;
button = board.getButton0();
button.setText("");
button = board.getButton1();
button.setText("");
button = board.getButton2();
button.setText("");
button = board.getButton3();
button.setText("");
button = board.getButton4();
button.setText("");
button = board.getButton5();
button.setText("");
button = board.getButton6();
button.setText("");
button = board.getButton7();
button.setText("");
button = board.getButton8();
button.setText("");
}
}
这就是 gui
班级:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoeDivyanshuVarma;
import javax.swing.JButton;
/**
*
* @author divyanshuvarma
*/
public class gui extends javax.swing.JFrame {
/**
* Creates new form gui
*/
public gui() {
initComponents();
}
public void setButton0Text(String symbol) {
this.button0.setText(symbol);
}
public void setButton1Text(String symbol) {
this.button1.setText(symbol);
}
public void setButton2Text(String symbol) {
this.button2.setText(symbol);
}
public void setButton3Text(String symbol) {
this.button3.setText(symbol);
}
public void setButton4Text(String symbol) {
this.button4.setText(symbol);
}
public void setButton5Text(String symbol) {
this.button5.setText(symbol);
}
public void setButton6Text(String symbol) {
this.button6.setText(symbol);
}
public void setButton7Text(String symbol) {
this.button7.setText(symbol);
}
public void setButton8Text(String symbol) {
this.button8.setText(symbol);
}
public JButton getButton0() {
return button0;
}
public JButton getButton1() {
return button1;
}
public JButton getButton2() {
return button2;
}
public JButton getButton3() {
return button3;
}
public JButton getButton4() {
return button4;
}
public JButton getButton5() {
return button5;
}
public JButton getButton6() {
return button6;
}
public JButton getButton7() {
return button7;
}
public JButton getButton8() {
return button8;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
button1 = new javax.swing.JButton();
button3 = new javax.swing.JButton();
button5 = new javax.swing.JButton();
button2 = new javax.swing.JButton();
button6 = new javax.swing.JButton();
button8 = new javax.swing.JButton();
button4 = new javax.swing.JButton();
button0 = new javax.swing.JButton();
button7 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
button1.setText("jButton1");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button1ActionPerformed(evt);
}
});
button3.setText("jButton1");
button5.setText("jButton1");
button2.setText("jButton1");
button6.setText("jButton1");
button8.setText("jButton1");
button4.setText("jButton1");
button7.setText("jButton1");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(button0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(72, 72, 72)
.addComponent(button1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(button3)
.addGap(72, 72, 72)
.addComponent(button4))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(button6)
.addGap(72, 72, 72)
.addComponent(button7)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(button8, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(button5, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(button2, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button0, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, Short.MAX_VALUE)
.addComponent(button8, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(32, 32, 32)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(button6, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button7, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new gui().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton button0;
private javax.swing.JButton button1;
private javax.swing.JButton button2;
private javax.swing.JButton button3;
private javax.swing.JButton button4;
private javax.swing.JButton button5;
private javax.swing.JButton button6;
private javax.swing.JButton button7;
private javax.swing.JButton button8;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
这就是 winnerDisplay
班级:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoeDivyanshuVarma;
import javax.swing.JLabel;
/**
*
* @author divyanshuvarma
*/
public class winnerDisplay extends javax.swing.JFrame {
/**
* Creates new form winnerDisplay
*/
public winnerDisplay() {
initComponents();
}
public JLabel getWinnerLabel() {
return winnerLabel;
}
public void setWinnerLabel(JLabel winnerLabel) {
this.winnerLabel = winnerLabel;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
winnerLabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
winnerLabel.setText("jLabel1");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(77, 77, 77)
.addComponent(winnerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(80, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(82, Short.MAX_VALUE)
.addComponent(winnerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(94, 94, 94))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(winnerDisplay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new winnerDisplay().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel winnerLabel;
// End of variables declaration
}
2条答案
按热度按时间omtl5h9j1#
这是我最后的密码:
(基于用户建议的更改)
vbn
)班级
gui
和以前一样。更新的
main
班级:这是最新的
winnerDisplay
班级:hxzsmxv22#
代码构造看起来不错,不明白为什么方法iswon-没有被调用。在debug中逐步完成代码-以了解更多信息。
匿名操作侦听器将对类中定义的静态方法具有可见性。
winnerdisplay框架可以从iswon()方法中调用,如果您使用对话框而不是jframe来发出警报,您将从用户那里得到终止程序的反馈。