I made a small application to show my problem; whenever the player makes a sequence in the diagonal, wins, if nobody does it, it draws; do not look at the fact that the second player will never win ... I just did an example to show that when winning or tying, I wanted the images to appear a little before being canceled; for example, when someone wins an image appears and the other disappears, when the message of the winner appears. Look. The two images have size 70 pixels ... the code compiles into the boa. Look:
package example;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
import javax.swing.JOptionPane;
public class Example extends Application{
private final Button[] BUTTONS = new Button[4];
private boolean JOGADOR_UM = true;
private boolean JOGADOR_DOIS = false;
private static DropShadow DROPSHADOW = new DropShadow();
private final String [][] MATRIZ = new String[2][2];
private final String [] TABELA = new String[2];
private final Image[] IMAGE = {
new Image(Example.class.getResource("0.png").toString()),
new Image(Example.class.getResource("1.png").toString();
};
public Parent createContent(){
StackPane root = new StackPane();
TilePane tilePane = new TilePane();
tilePane.setAlignment(Pos.CENTER);
tilePane.setPrefColumns(2);
for (int i = 0; i<4; i++){
BUTTONS[i] = new Button();
BUTTONS[i].setPrefSize(90, 90);
tilePane.getChildren().add(BUTTONS[i]);
}
BUTTONS[0].setOnAction((ActionEvent) ->{
if(JOGADOR_UM== true){
if(BUTTONS[0].getEffect() == null){
BUTTONS[0].setGraphic(new ImageView(IMAGE[0]));
BUTTONS[0].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(0, "X");
}
} else{
if(BUTTONS[0].getEffect() == null){
BUTTONS[0].setGraphic(new ImageView(IMAGE[1]));
BUTTONS[0].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(0, "O");
}
}
});
BUTTONS[1].setOnAction((ActionEvent) ->{
if(JOGADOR_UM == true){
if(BUTTONS[1].getEffect() == null){
BUTTONS[1].setGraphic(new ImageView(IMAGE[0]));
BUTTONS[1].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(1, "X");
}
} else{
if(BUTTONS[1].getEffect() == null){
BUTTONS[1].setGraphic(new ImageView(IMAGE[1]));
BUTTONS[1].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(1, "O");
}
}
});
BUTTONS[2].setOnAction((ActionEvent) ->{
if(JOGADOR_UM == true){
if(BUTTONS[2].getEffect() == null){
BUTTONS[2].setGraphic(new ImageView(IMAGE[0]));
BUTTONS[2].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(2, "X");
}
} else{
if (BUTTONS[2].getEffect() == null){
BUTTONS[2].setGraphic(new ImageView(IMAGE[1]));
BUTTONS[2].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(2, "O");
}
}
});
BUTTONS[3].setOnAction((ActionEvent) ->{
if(JOGADOR_UM == true){
if(BUTTONS[3].getEffect() == null){
BUTTONS[3].setGraphic(new ImageView(IMAGE[0]));
BUTTONS[3].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(3, "X");
}
} else{
if(BUTTONS[3].getEffect() == null){
BUTTONS[3].setGraphic(new ImageView(IMAGE[1]));
BUTTONS[3].setEffect(DROPSHADOW);
validarJogada();
registrarJogada(3, "O");
}
}
});
root.getChildren().add(tilePane);
return root;
}
public void validarJogada(){
if(JOGADOR_UM == true){
JOGADOR_UM = false;
JOGADOR_DOIS = true;
}else{
JOGADOR_UM = true;
JOGADOR_DOIS = false;
}
}
public void registrarJogada(int p, String jogada){
if(p == 0){
MATRIZ[0][0] = jogada;
}else if(p == 1){
MATRIZ[0][1] = jogada;
}
if(p == 2){
MATRIZ[1][0] = jogada;
}else if(p == 3){
MATRIZ[1][1] = jogada;
}
verificarGanhador();
}
public void verificarGanhador(){
TABELA[0] = MATRIZ[0][0] + MATRIZ[1][1];
TABELA[1] = MATRIZ[1][0] + MATRIZ[0][1];
for (String tabela: TABELA) {
switch (tabela) {
case "XX":
ganhador("X");
break;
case "OO":
ganhador("O");
break;
}
}
if (BUTTONS[0].getEffect()!= (null) &&
BUTTONS[1].getEffect() != (null) &&
BUTTONS[2].getEffect() != (null) &&
BUTTONS[3].getEffect() != (null)){
ganhador("XO");
}
}
public void ganhador(String ganhador){
switch(ganhador){
case "X":
JOptionPane.showMessageDialog(null, "Jogador 1 ganhou!");
limparJogo();
break;
case "O":
JOptionPane.showMessageDialog(null, "Jogador 2 ganhou!");
limparJogo();
break;
case "XO":
JOptionPane.showMessageDialog(null, "Empatou");
limparJogo();
break;
}
}
public void limparJogo(){
BUTTONS[0].setGraphic(null);
BUTTONS[1].setGraphic(null);
BUTTONS[2].setGraphic(null);
BUTTONS[3].setGraphic(null);
BUTTONS[0].setEffect(null);
BUTTONS[1].setEffect(null);
BUTTONS[2].setEffect(null);
BUTTONS[3].setEffect(null);
for (String[] m : MATRIZ) {
for (int i = 0; i<MATRIZ.length; i++) {
m[i] = ("");
}
}
JOGADOR_UM = true;
JOGADOR_DOIS = false;
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(createContent(), 200, 200));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String [] args){launch(args);}
}