Boa Tarde Gente!
Alguém consegue me ajudar com o meu código?
Não consigo implementar quando da empate. Desde já agradeço.
Segue o código:
package jogodavelha;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JOptionPane;
/**
* CRIADO POR LUCIANO QUEIROZ GUSSO
*/
public class User_X_User extends javax.swing.JFrame {
int player;
int qtde;
int mat[][] = new int [3][3];
JButton b[] = new JButton [9];
String ganhador = "", player1 = "X", player2= "O",deuempate = "";
int vplayer1=0, lplayer1=0, vplayer2=0, lplayer2=0, empate=0;
public User_X_User() {
initComponents();
player = 1;
qtde = 1;
b[0] = B1;
b[1] = B2;
b[2] = B3;
b[3] = B4;
b[4] = B5;
b[5] = B6;
b[6] = B7;
b[7] = B8;
b[8] = B9;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
B2 = new javax.swing.JButton();
B1 = new javax.swing.JButton();
B3 = new javax.swing.JButton();
B6 = new javax.swing.JButton();
B5 = new javax.swing.JButton();
B4 = new javax.swing.JButton();
B9 = new javax.swing.JButton();
B8 = new javax.swing.JButton();
B7 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
SPlayer1Ganhou = new javax.swing.JLabel();
SPlayer1Perdeu = new javax.swing.JLabel();
SPlayer1Empatou = new javax.swing.JLabel();
SPlayer2Perdeu = new javax.swing.JLabel();
SPlayer2Ganhou = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
SPlayer2Empatou = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
B2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B2ActionPerformed(evt);
}
});
B1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B1ActionPerformed(evt);
}
});
B3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B3ActionPerformed(evt);
}
});
B6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B6ActionPerformed(evt);
}
});
B5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B5ActionPerformed(evt);
}
});
B4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B4ActionPerformed(evt);
}
});
B9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B9ActionPerformed(evt);
}
});
B8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B8ActionPerformed(evt);
}
});
B7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
B7ActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("PLAYER1");
jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel5.setText("Vitórias:");
jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel6.setText("Derrotas:");
jLabel7.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel7.setText("Empates:");
SPlayer1Ganhou.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
SPlayer1Ganhou.setForeground(new java.awt.Color(0, 0, 204));
SPlayer1Ganhou.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
SPlayer1Ganhou.setText("0");
SPlayer1Perdeu.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
SPlayer1Perdeu.setForeground(new java.awt.Color(0, 0, 204));
SPlayer1Perdeu.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
SPlayer1Perdeu.setText("0");
SPlayer1Empatou.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
SPlayer1Empatou.setForeground(new java.awt.Color(0, 0, 204));
SPlayer1Empatou.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
SPlayer1Empatou.setText("0");
SPlayer2Perdeu.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
SPlayer2Perdeu.setForeground(new java.awt.Color(0, 0, 204));
SPlayer2Perdeu.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
SPlayer2Perdeu.setText("0");
SPlayer2Ganhou.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
SPlayer2Ganhou.setForeground(new java.awt.Color(0, 0, 204));
SPlayer2Ganhou.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
SPlayer2Ganhou.setText("0");
jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("PLAYER2");
SPlayer2Empatou.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
SPlayer2Empatou.setForeground(new java.awt.Color(0, 0, 204));
SPlayer2Empatou.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
SPlayer2Empatou.setText("0");
jLabel3.setFont(new java.awt.Font("Aharoni", 0, 24)); // NOI18N
jLabel3.setText("JOGO DA VELHA");
jLabel8.setFont(new java.awt.Font("Times New Roman", 1, 11)); // NOI18N
jLabel8.setText("X");
jMenu1.setText("Games");
jMenuItem2.setText("User x User");
jMenu1.add(jMenuItem2);
jMenuItem3.setText("User x Computer");
jMenu1.add(jMenuItem3);
jMenuBar1.add(jMenu1);
jMenu2.setText("Sair");
jMenuItem1.setText("Exit");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem1);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(14, 14, 14)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(B7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(B4, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(B1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(B8, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(B5, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(B2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(B6, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(B3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(B9, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel7)
.addGap(38, 38, 38))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(jLabel6))
.addGap(36, 36, 36)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(SPlayer1Ganhou, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(SPlayer1Perdeu)
.addComponent(SPlayer1Empatou)))
.addGroup(layout.createSequentialGroup()
.addGap(60, 60, 60)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel8)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(SPlayer2Ganhou)
.addComponent(SPlayer2Perdeu)
.addComponent(SPlayer2Empatou))
.addGap(23, 23, 23)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10, 10, 10)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(SPlayer1Ganhou)
.addComponent(SPlayer2Ganhou))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(SPlayer1Perdeu)
.addComponent(SPlayer2Perdeu))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(SPlayer1Empatou)
.addComponent(SPlayer2Empatou)))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(B2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(B3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(B1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(B5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(B6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(B4, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(B8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(B9, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(B7, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(35, 35, 35))
);
pack();
}// </editor-fold>
public void jogada(JButton b, int x, int y){
if(player == 1){
mat[x][y] = 1;
b.setText("X");
b.setFont(new Font("Arial", Font.PLAIN, 20));
b.setEnabled(false);
player = 2;
ganhador = player1;
Checagem_Da_Jogada(1);
}
else if (player == 2) {
mat[x][y] = 2;
b.setText("O");
b.setFont(new Font("Arial", Font.PLAIN, 20));
b.setEnabled(false);
player = 1;
ganhador = player2;
Checagem_Da_Jogada(2);
}
qtde++;
}
public void Checagem_Da_Jogada(int x){
try {
if(vitoria(x) == true){
Quem_Venceu();
Fim_De_Jogo();
}
}
catch (Exception e){
e.printStackTrace();
}
}
public boolean vitoria(int x){
for(int i = 0; i < mat.length; i++){
if(mat[i][0] == x && mat[i][1] == x && mat[i][2] == x){
return true;
}
else if(mat[0][i] == x && mat[1][i] == x && mat[2][i] == x){
return true;
}
}
if(mat[0][0] == x && mat[1][1] == x && mat[2][2] == x){
return true;
}
if(mat[0][2] == x && mat[1][1] == x && mat[2][0] == x){
return true;
}
else{
return false;
}
}
public void Quem_Venceu() {
try {
if (ganhador.equals(player1)){
Player1_Venceu();
}
else if (ganhador.equals(player2)) {
Player2_Venceu();
}
}
catch (Exception e){
e.printStackTrace();
}
}
public void Player2_Venceu() {
vplayer2++;
lplayer1++;
SPlayer2Ganhou.setText(""+vplayer2);
SPlayer1Perdeu.setText(""+lplayer1);
JOptionPane.showMessageDialog(null,"Player 2 Ganhou!!!", " ",
JOptionPane.INFORMATION_MESSAGE);
}
public void Player1_Venceu() {
vplayer1++;
lplayer2++;
SPlayer1Ganhou.setText(" "+vplayer1);
SPlayer2Perdeu.setText(" "+lplayer2);
JOptionPane.showMessageDialog(null,"Player 1 Ganhou!!!", "",
JOptionPane.INFORMATION_MESSAGE);
}
public void Empate() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (mat[i][j] == ' '){
Empatou();
}
}
}
}
public void Empatou() {
empate++;
SPlayer1Empatou.setText(" "+empate);
SPlayer2Empatou.setText(" "+empate);
JOptionPane.showMessageDialog(null,"Houve Empate!!!", "",
JOptionPane.INFORMATION_MESSAGE);
}
public void Fim_De_Jogo(){
for(int i = 0; i < 9; i++){
limpar();
}
}
public void limpar(){
for(int i = 0; i < 9; i++){
b[i].setEnabled(true);
b[i].setText(" ");
}
for(int x = 0; x < 3; x++){
for(int y = 0; y < 3; y++){
mat[x][y] = 0;
}
}
player = 1;
//player = 2;
ganhador = "";
}
private void B1ActionPerformed(java.awt.event.ActionEvent evt) {
jogada(B1, 0, 0);
}
private void B2ActionPerformed(java.awt.event.ActionEvent evt) {
jogada(B2, 0, 1);
}
private void B3ActionPerformed(java.awt.event.ActionEvent evt) {
jogada(B3, 0, 2);
}
private void B4ActionPerformed(java.awt.event.ActionEvent evt) {
jogada(B4, 1, 0);
}
private void B5ActionPerformed(java.awt.event.ActionEvent evt) {
jogada(B5, 1, 1);
}
private void B6ActionPerformed(java.awt.event.ActionEvent evt) {
jogada(B6, 1, 2);
}
private void B7ActionPerformed(java.awt.event.ActionEvent evt) {
jogada(B7, 2, 0);
}
private void B8ActionPerformed(java.awt.event.ActionEvent evt) {
jogada(B8, 2, 1);
}
private void B9ActionPerformed(java.awt.event.ActionEvent evt) {
jogada(B9,2, 2);
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(User_X_User.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new User_X_User().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton B1;
private javax.swing.JButton B2;
private javax.swing.JButton B3;
private javax.swing.JButton B4;
private javax.swing.JButton B5;
private javax.swing.JButton B6;
private javax.swing.JButton B7;
private javax.swing.JButton B8;
private javax.swing.JButton B9;
private javax.swing.JLabel SPlayer1Empatou;
private javax.swing.JLabel SPlayer1Ganhou;
private javax.swing.JLabel SPlayer1Perdeu;
private javax.swing.JLabel SPlayer2Empatou;
private javax.swing.JLabel SPlayer2Ganhou;
private javax.swing.JLabel SPlayer2Perdeu;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
// End of variables declaration
}