英文:
The Tic Tac Toe game does not show the winner at the right time
问题
import java.awt.BorderLayout;
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 {
// ... (Same code as provided)
private void showWinner() {
if (findWinner()) {
if (getPlayerName().equalsIgnoreCase("x")) {
statusBar.setText("Winner is Player X");
} else if (getPlayerName().equalsIgnoreCase("o")) {
statusBar.setText("Winner is Player O");
}
} else if (!findWinner() && count == 9) {
statusBar.setText("It was a draw, no one.");
}
// Remove the following lines to remove the need for an additional click
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
board[i][j].removeActionListener(this);
}
}
}
// ... (Rest of the code remains the same)
}
Note: The provided code had HTML entities like "
which represent double quotes ("
) and <
which represent the less-than symbol (<
). I've replaced them with actual double quotes and less-than symbols in the translated code.
英文:
my code is tic tac toe and i have problem with show winner.
i get winner but not when X or O in correct direction.
and show winner after click on button.
code :
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();
}
}
in this image Winner is X but wait for Player O
and after click on other button show Winner.
i need show winner when X or O in correct Direction
and no wait for click on other button
答案1
得分: 2
我想我明白了。
当按钮被按下时,按以下顺序执行操作:
- 将状态栏更改为显示轮到下一位玩家
- 设置相关按钮文本以显示刚刚选择该按钮的玩家
- 禁用该按钮
- 更改轮换值以反映下一位玩家
- 基于当前玩家的名称检查是否有获胜者,但您已经将其更改为下一位玩家,而不是刚刚执行移动的玩家。
当X进行获胜移动时,游戏在检查是否有获胜者之前会将当前玩家名称更改为O,因此它会检查是否O已经获胜。您需要重新排序操作:
- 更改按钮文本
- 禁用该按钮
- 基于当前玩家名称检查是否有获胜者,如果当前玩家获胜或者出现平局,则结束游戏
- 如果游戏仍在进行,现在是更改轮换值并更新状态栏以显示轮到下一位玩家的时候了。
英文:
I think I see it.
When a button is pressed, you do stuff in this order:
- change the status bar to say it's the next player's turn
- set the relevant button text to show the player who just picked that button
- disable the button
- change the turn value to reflect the next player
- check for a winner, based on the current player's name but you have already changed this to the next player, not the player who just made the move.
When X makes a winning move, the current player name is changed to O before the game checks for a winner, so it checks to see if O has won. You need to reorder stuff:
- change the button text
- disable the button
- check for a winner based on the current player name, end the game if the current player has won or if there is a draw
- if the game is still going, now is the time to change the turn value and update the status bar to show that it's the next player's turn.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论