我的代码是井字游戏,我对表演优胜者有疑问。
我会赢得胜利,但是当X或O指向正确的方向时我不会赢。
并在点击按钮后显示获胜者。
代码:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
public class TicTocToe extends JFrame implements ActionListener {
private static final long serialVersionUID = -7730552764304282715L;
private static final Font BT_FONT = new Font("Segoe UI", 0, 30);
private static final Font LB_FONT = new Font("Consolas", 0, 20);
private JButton[][] board;
private JLabel statusBar;
private JPanel centerPanel, southPanel;
private int turn = 0, count = 0;
private String name;
public static void main(String[] args) {
EventQueue.invokeLater(() -> new TicTocToe().setVisible(true));
}
public TicTocToe() {
super("Tic Toc Toe");
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
initUI();
} catch (Exception e) {
e.printStackTrace();
}
}
private void initUI() {
initCenterPanel();
initSouthPanel();
add(centerPanel);
add(southPanel, BorderLayout.SOUTH);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
}
private void initCenterPanel() {
centerPanel = new JPanel();
board = new JButton[3][3];
for (int i = 0; i < board[0].length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = new JButton("-");
board[i][j].setFont(BT_FONT);
board[i][j].setFocusPainted(false);
board[i][j].setPreferredSize(new Dimension(80, 80));
board[i][j].addActionListener(this);
centerPanel.add(board[i][j]);
}
}
}
private void initSouthPanel() {
southPanel = new JPanel();
southPanel.setLayout(new BorderLayout());
statusBar = new JLabel();
statusBar.setFont(LB_FONT);
statusBar.setText("Click On Button To Start");
statusBar.setHorizontalAlignment(JLabel.CENTER);
statusBar.setBorder(new EtchedBorder());
southPanel.add(statusBar);
}
private void play(JButton button) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (button.equals(board[i][j])) {
count++;
if (turn < 1) {
statusBar.setText("Player " + "O" + " Turn");
button.setText("X");
button.setEnabled(false);
turn++;
} else {
statusBar.setText("Player " + "X" + " Turn");
button.setText("O");
button.setEnabled(false);
turn--;
}
}
}
}
}
private String getPlayerName() {
return (turn < 1) ? "X" : "O";
}
private boolean findWinner() {
name = getPlayerName();
for (int i = 0; i < board.length; i++) {
if (board[i][0].getText().equals(name) && board[i][1].getText().equals(name)
&& board[i][2].getText().equals(name))
return true;
for (int j = 0; j < board[i].length; j++) {
if (board[0][j].getText().equals(name) && board[1][j].getText().equals(name)
&& board[2][j].getText().equals(name))
return true;
if (board[0][0].getText().equals(name) && board[1][1].getText().equals(name)
&& board[2][2].getText().equals(name))
return true;
if (board[0][2].getText().equals(name) && board[1][1].getText().equals(name)
&& board[2][0].getText().equals(name))
return true;
}
}
return false;
}
private void showWinner() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if (findWinner()) {
if (getPlayerName().equalsIgnoreCase("x")) {
statusBar.setText("Winner is Player X");
board[i][j].removeActionListener(this);
}
else if (getPlayerName().equalsIgnoreCase("o")) {
statusBar.setText("Winner is Player O");
board[i][j].removeActionListener(this);
}
} else if (!findWinner() && count == 9) {
statusBar.setText("It was a draw, no wone.");
board[i][j].removeActionListener(this);
}
}
}
}
public void actionPerformed(ActionEvent event) {
JButton button = (JButton) event.getSource();
play(button);
showWinner();
}
public Dimension getPreferredSize() {
return new Dimension(300, 320);
}
public Dimension getMinimumSize() {
return getPreferredSize();
}
}
在此图像中,获胜者是X,但等待玩家O
,然后单击其他按钮,显示获胜者。
我需要在X或O方向正确时显示获胜者
不用等待点击其他按钮
答案 0 :(得分:2)
我想我明白了。
按下按钮时,您将按以下顺序进行操作:
当X进行获胜移动时,在游戏检查获胜者之前,当前玩家名称将更改为O,因此它将检查O是否获胜。您需要重新排序商品: