1
I’m doing a simple program of the old game next to Swing. I wanted to know why, when shooting an event click by the method ActionPerfomed
, my program freezes. I did some research and found that these are normal interface problems because of Thread execution. If anyone can explain the mistake and why it happens I would be grateful.
Comparison class and value insertion
public class JogoVelha {
private int [][] tabuleiro = new int[3][3];
public int[][] getTabuleiro()
{
return this.tabuleiro;
}
//Seta valores do tabuleiro
public void setTabuleiroValor(int posicaoLinha,int posicaoColuna,int
jogador)
{
this.tabuleiro[posicaoLinha][posicaoColuna] = jogador;
}
//Cria valores dos jogadores
public String jogadores(int jogador)
{
Map<Integer,String> jogadores = new HashMap<Integer,String>();
jogadores.put(1, "JOGADOR 1");
jogadores.put(2, "JOGADOR 2");
return jogadores.get(jogador);
}
@SuppressWarnings("unused")
public boolean verificarPartida(int [][] tabuleiro,int jogador)
{
//compara os valores da linha/coluna
for(int i = 0; i <2;i++)
{
if( tabuleiro[0][i] == jogador && tabuleiro[1][i] == jogador &&
tabuleiro[2][i] == jogador)
{
return true;
}
continue;
}
//compara os valores coluna/ linha
for(int i= 0;i<2;i++)
{
if(tabuleiro[i][0] == jogador && tabuleiro[i][1] == jogador &&
tabuleiro[i][2] == jogador)
{
return true;
}
continue;
}
//Comparar os valores das linhas que interceptam no meio
if(tabuleiro[0][0] == jogador && tabuleiro[1][1] == jogador
&&tabuleiro[2][2] == jogador)
return true;
else if (tabuleiro[0][2] == jogador && tabuleiro[1][1] == jogador
&&tabuleiro[2][0] == jogador)
return true;
return false;
}
Creation class of the UI
public class View extends JFrame implements ActionListener {
public static int jogadorValue = 1;
public static Integer turnos = 1;
JLabel lblJogoVelha = new JLabel("Jogo da Velha");
JLabel lblTurno = new JLabel("Turno");
JLabel lblTurnoNumber = new JLabel();
JLabel lblAjogar= new JLabel();
JLabel lblJogador = new JLabel();
public JButton b1 = new JButton("b1");
public JButton b2 = new JButton("b2");
public JButton b3 = new JButton("b3");
public JButton b4 = new JButton("b4");
public JButton b5 = new JButton("b5");
public JButton b6 = new JButton("b6");
public JButton b7 = new JButton("b7");
public JButton b8 = new JButton("b8");
public JButton b9 = new JButton("b9");
JogoVelha jogoVelha = new JogoVelha();
public void setarListener()
{
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
}
public void adicionarViewButtons()
{
getContentPane().add(b1);
getContentPane().add(b2);
getContentPane().add(b3);
getContentPane().add(b4);
getContentPane().add(b5);
getContentPane().add(b6);
getContentPane().add(b7);
getContentPane().add(b8);
getContentPane().add(b9);
}
public void atribuirValorTabuleiro(String name)
{
switch(name)
{
case "b1":
jogoVelha.setTabuleiroValor(0, 0, jogadorValue);
break;
case "b2":
jogoVelha.setTabuleiroValor(0, 1, jogadorValue);
break;
case "b3":
jogoVelha.setTabuleiroValor(0, 2, jogadorValue);
break;
case "b4":
jogoVelha.setTabuleiroValor(1, 0, jogadorValue);
break;
case "b5":
jogoVelha.setTabuleiroValor(1, 1, jogadorValue);
break;
case "b6":
jogoVelha.setTabuleiroValor(1, 2, jogadorValue);
break;
case "b7":
jogoVelha.setTabuleiroValor(2, 0, jogadorValue);
break;
case "b8":
jogoVelha.setTabuleiroValor(2, 1, jogadorValue);
break;
case "b9":
jogoVelha.setTabuleiroValor(2, 2, jogadorValue);
break;
default:
break;
}
}
@Override
public void actionPerformed(ActionEvent e) {
turnos ++;
lblTurnoNumber.setText(turnos.toString());;
if(turnos == 10)
{
JOptionPane.showMessageDialog(null, "Empate");
}
else
{
if(jogadorValue == 1)
{
setEnabled(false);
atribuirValorTabuleiro(((JButton)e.getSource()).getText());
((JButton) e.getSource()).setText("X");
((JButton) e.getSource()).revalidate();
if(turnos >=5)
{
jogoVelha.verificarPartida(jogoVelha.getTabuleiro(), jogadorValue);
JOptionPane.showConfirmDialog(null, "Vencedor é: " + jogoVelha.jogadores(jogadorValue), "Vencedor", 3, 4);
}
jogadorValue ++;
}
else
{
atribuirValorTabuleiro((((JButton)e.getSource()).getText()));
((JButton) e.getSource()).setText("O");
((JButton) e.getSource()).revalidate();
if(turnos >=5)
{
jogoVelha.verificarPartida(jogoVelha.getTabuleiro(), jogadorValue);
JOptionPane.showConfirmDialog(null, "Vencedor é: " + jogoVelha.jogadores(jogadorValue), "Vencedor", 3, 4);
}
jogadorValue --;
}
}
}
public View()
{
getContentPane().add(lblJogoVelha);
getContentPane().add(lblTurno);
getContentPane().add(lblTurnoNumber);
this.adicionarViewButtons();
this.setarListener();
setLayout(new GridLayout(4, 3));
lblTurnoNumber.setText(turnos.toString());
setSize(500, 500);
setVisible(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
View tela = new View();
tela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tela.setVisible(true);
}
});
}
}
Thank you Articuno, if you want to quote about the other problems of the program I am open to criticism =)
– Diogo Martins Silva
@Diogomartinssilva are more problems turned to logic, as the if of this joptionpane that I mentioned, you consider that 5 moves is game over, but it is wrong. The minimum possible moves for someone to win the game is just 5, and the game is getting into that if before anyone can win. And I think this logic is wrong, you can’t use the amount of turns to evaluate if the match has come to an end, from a search on tic tac toe to see how the logic of a real old game works.
– user28595
I’m sorry, I already fixed that part. The comparison is to see if it is in the 5 turn (because only in this turn ahead some player can win),I put the function to check inside a new if,and left so because I did not want it to be executed before that turn (because in the check function there are some loops).
– Diogo Martins Silva
@Diogomartinssilva’s logic is still bad. Check this link here: https://codereview.stackexchange.com/questions/166209/tic-tac-toe-in-swing
– user28595
I had made it very similar in my first attempt, only I was not able to rescue the value of the object to set the values (then I went to see that I was only doing a cast of jbutton in the event variable, I haven’t changed with manipulation of visual objects ).And yes, it was quite flawed in this part of simply comparing the text to set the values and the fact that I didn’t use a loop to create the buttons. Thanks for everything and hug the/
– Diogo Martins Silva