Jogo da Velha com Swing
Jogo da Velha com Swing
Tela Inicial
Tela do Jogo
Código Tela Inicial
package interfacesGUI;
import java.awt.Color;
public TelaInicial() {
initComponents();
jBtnIniciar.setBackground(c1);
@SuppressWarnings("unchecked")
………
jBtnIniciar.setBackground(c2);
jBtnIniciar.setBackground(c1);
this.setVisible(false);
jv1.setVisible(true);
/**
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
*/
try {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
new TelaInicial().setVisible(true);
});
}
Tela do Jogo
package interfacesGUI;
import java.awt.Color;
import javax.swing.JButton;
public JogoDaVelha() {
initComponents();
@SuppressWarnings("unchecked")
………
vez(jBtn21, 2, 1);
vez(jBtn22, 2, 2);
vez(jBtn11, 1, 1);
vez(jBtn12, 1, 2);
vez(jBtn13, 1, 3);
vez(jBtn23, 2, 3);
}
private void jBtn31ActionPerformed(java.awt.event.ActionEvent evt) {
vez(jBtn31, 3, 1);
vez(jBtn32, 3, 2);
vez(jBtn33, 3, 3);
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
*/
try {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
new JogoDaVelha().setVisible(true);
}
});
btn.setBackground(Color.BLACK);
btn.setForeground(Color.WHITE);
btn.setText("X");
this.res[linha][coluna] = 1;
this.contagem++;
btn.setBackground(Color.GRAY);
btn.setForeground(Color.BLACK);
btn.setText("O");
this.res[linha][coluna] = 100;
this.contagem++;
this.vez = !this.vez;
verificar();
somaDP += this.res[i][i];
somaL += this.res[i][j];
somaC += this.res[j][i];
if (somaL == 3 || somaC == 3) {
this.player1++;
limpar();
limpar();
if (somaDP == 3 || somaDS == 3) {
this.player1++;
limpar();
this.player2++;
limpar();
limpar();
this.total++;
jLbPlayer1.setText("0");
jLbPlayer2.setText("0");
this.total = 0;
}else{
jLbPlayer1.setText(String.valueOf(this.player1));
jLbPlayer2.setText(String.valueOf(this.player2));
jProgressBar1.setValue(this.total);
this.contagem = 0;
this.res[i][j] = 0;
}
jBtn11.setBackground(Color.WHITE);
jBtn11.setText("");
jBtn12.setBackground(Color.WHITE);
jBtn12.setText("");
jBtn13.setBackground(Color.WHITE);
jBtn13.setText("");
jBtn21.setBackground(Color.WHITE);
jBtn21.setText("");
jBtn22.setBackground(Color.WHITE);
jBtn22.setText("");
jBtn23.setBackground(Color.WHITE);
jBtn23.setText("");
jBtn31.setBackground(Color.WHITE);
jBtn31.setText("");
jBtn32.setBackground(Color.WHITE);
jBtn32.setText("");
jBtn33.setBackground(Color.WHITE);
jBtn33.setText("");