Bom Dia Gente!
O meu jogo da velha está funcionando, porém não consigo colocar a condição de empate. Estou precisando entregar este projeto. Alguém poderia me ajudar por favor.
Segue o código:
package jogodavelha;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JOptionPane;
public class User_X_User extends javax.swing.JFrame {
int player;
int qtde;
//char[][] mat = new char[3][3];
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")
public void jogada(JButton b, int x, int y){
if(player == 1){
mat[x][y] = 1;
b.setText("X");
b.setFont(new Font("Arial Black", Font.PLAIN, 22));
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 Black", Font.PLAIN, 22));
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){
//ganhador=x;
return true;
}
else if(mat[0][i] == x && mat[1][i] == x && mat[2][i] == x){
//ganhador = x;
return true;
}
}
if(mat[0][0] == x && mat[1][1] == x && mat[2][2] == x){
//ganhador = x;
return true;
}
if(mat[0][2] == x && mat[1][1] == x && mat[2][0] == x){
//ganhador = x;
return true;
}
else{
for(int i = 0; i<mat.length; i++) {
for(int j=0; j<mat[i].length;j++) {
if(mat[i][j] == 0) { //zero é o valor que a matriz tem se ninguém jogou nesta posição ainda
return false;
}
}
}
return true; //se chegou aqui então tudo está preenchido e é empate
}
}
public void Quem_Venceu() {
try {
if (ganhador.equals(player1)){
Player1_Venceu();
}
else if (ganhador.equals(player2)) {
Player2_Venceu();
}
else {
Empatou();
}
}
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 boolean Empate() {
for(int i = 0; i<mat.length; i++) {
for(int j=0; j<mat[i].length;j++) {
if(mat[i][j] == 0) { //zero é o valor que a matriz tem se ninguém jogou nesta posição ainda
return false;
}
}
}
return true; //se chegou aqui então tudo está preenchido e é empate
}
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 UxUSairActionPerformed(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.JButton UxUSair;
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;
// End of variables declaration
}