Мне нужно, чтобы в игре говорилось, какой игрок выиграл. Прямо сейчас говорится, что игра окончена, но я не уверен, как заставить его сказать, какой игрок выиграл? Я предполагаю, что мне нужно сравнить строки, помещенные в каждую кнопку, но я не уверен, как это сделать. Я знаю, что текст, в котором говорится, что игрок 1 выигрывает неправильно. Это только там временно, пока я не смогу заставить это работать должным образом.
// Array for the buttons.
JButton buttons[] = new JButton[9];
// if it is the first, third, fifth etc... then it is an x. if it is second,
// fourth sixth etc... it is an o
int alternate = 0;
// This sets up the buttons and adds them to the pane. It sets the font, size,
// and stes the default button as nothing.
public void buttonSetup() {
for (int j = 0; j <= 8; j++) {
buttons[j] = new JButton();
buttons[j].setText("");
buttons[j].setFont(new Font("Arial", Font.PLAIN, 150));
buttons[j].addActionListener(new buttonListener());
add(buttons[j]);
}
}
// Sets the grid size to be used and adds the buttons to it.
public TicTacToe() {
setLayout(new GridLayout(3, 3));
buttonSetup();
}
// This starts the following methods after a button is clicked and reacts to the
// buttons being pressed.
private class buttonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// This takes the input for which button is pressed and sets the display to
// either X or O.
JButton pressed = (JButton) e.getSource();
if (alternate % 2 == 0)
pressed.setText("X");
else
pressed.setText("O");
// This calls winCheck to see if somebody won, if they did it displays the text.
if (winCheck() == true) {
JOptionPane.showConfirmDialog(null, "The End Player 1 wins");
newGame();
}
alternate++;
}
// This is the method that checks to see which symbols are on which button to
// see who won.
public boolean winCheck() {
// This looks at the inputs in a horizontal line to see if they won.
if (sideCheck(0, 1) && sideCheck(1, 2))
return true;
else if (sideCheck(3, 4) && sideCheck(4, 5))
return true;
else if (sideCheck(6, 7) && sideCheck(7, 8))
return true;
// This looks at the inputs in a vertical line to see if they won.
else if (sideCheck(0, 3) && sideCheck(3, 6))
return true;
else if (sideCheck(1, 4) && sideCheck(4, 7))
return true;
else if (sideCheck(2, 5) && sideCheck(5, 8))
return true;
// This looks at the inputs in a diagonal line to see if they won.
else if (sideCheck(0, 4) && sideCheck(4, 8))
return true;
else if (sideCheck(2, 4) && sideCheck(4, 6))
return true;
else
return false;
}
// This method sets the game board to a new one by seting all the buttons to
// nothing.
public void newGame() {
for (int i = 0; i <= 8; i++) {
buttons[i].setText("");
}
}
// This is the method that allows winCheck to see if the symbols are the same.
public boolean sideCheck(int x, int y) {
if (buttons[x].getText().equals(buttons[y].getText()) && !buttons[x].getText().equals(""))
return true;
else
return false;
}
}
}
I need to make it so the buttons are only clickable one time. I am unsure how to make it say which player won? Thanks