lundi 17 décembre 2018

Why is the sum of my blackjack score not working properlly?

I am quite new at programing, and i have been trying to create a Simple BlackJack game on java, however i am having issues with the score counting.

Sometimes it recognizes the actual card value and works fine, others it just defines a random value and messesup with the whole game. i have used numbers from 1 to 52 to randomly define cards into an "array" and so set them as a "jlabel" as the game flows. the score counting is set inside an "switch" which defines the value of each card.

Here is my code please help me:

    public Form2() {
    initComponents();
BilleteraT = Double.toString(Billetera);    
jTextField1.setText("0");
jTextField2.setText(BilleteraT);}



/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jButton5 = new javax.swing.JButton();
    jTextField1 = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    jLabel8 = new javax.swing.JLabel();
    jLabel9 = new javax.swing.JLabel();
    jLabel10 = new javax.swing.JLabel();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jTextField4 = new javax.swing.JTextField();
    jLabel11 = new javax.swing.JLabel();
    jLabel12 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jLabel6 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setResizable(false);
    getContentPane().setLayout(null);

    jButton5.setBackground(new java.awt.Color(0, 0, 0));
    jButton5.setFont(new java.awt.Font("Helvetica", 1, 48)); // NOI18N
    jButton5.setForeground(new java.awt.Color(255, 255, 255));
    jButton5.setText("Apostar");
    jButton5.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton5ActionPerformed(evt);
        }
    });
    getContentPane().add(jButton5);
    jButton5.setBounds(218, 187, 230, 60);
    getContentPane().add(jTextField1);
    jTextField1.setBounds(526, 254, 100, 35);

    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/back.png"))); // NOI18N
    jLabel1.setToolTipText("");
    getContentPane().add(jLabel1);
    jLabel1.setBounds(199, 312, 90, 138);

    jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/back.png"))); // NOI18N
    getContentPane().add(jLabel2);
    jLabel2.setBounds(307, 312, 90, 138);

    jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/back.png"))); // NOI18N
    getContentPane().add(jLabel3);
    jLabel3.setBounds(199, 11, 90, 138);

    jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/back.png"))); // NOI18N
    getContentPane().add(jLabel4);
    jLabel4.setBounds(307, 11, 90, 138);

    jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    jLabel7.setForeground(new java.awt.Color(255, 255, 255));
    jLabel7.setText("Puntos:");
    getContentPane().add(jLabel7);
    jLabel7.setBounds(20, 340, 120, 14);

    jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    jLabel8.setForeground(new java.awt.Color(255, 255, 255));
    jLabel8.setText("Puntos:");
    getContentPane().add(jLabel8);
    jLabel8.setBounds(30, 60, 112, 14);

    jLabel9.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    jLabel9.setForeground(new java.awt.Color(255, 255, 255));
    jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel9.setText("Apuesta");
    getContentPane().add(jLabel9);
    jLabel9.setBounds(526, 228, 100, 20);

    jLabel10.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    jLabel10.setForeground(new java.awt.Color(255, 255, 255));
    jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel10.setText("Billetera");
    getContentPane().add(jLabel10);
    jLabel10.setBounds(526, 161, 100, 20);

    jTextField2.setEditable(false);
    getContentPane().add(jTextField2);
    jTextField2.setBounds(526, 187, 100, 35);

    jTextField3.setEditable(false);
    getContentPane().add(jTextField3);
    jTextField3.setBounds(50, 360, 90, 30);

    jTextField4.setEditable(false);
    getContentPane().add(jTextField4);
    jTextField4.setBounds(50, 80, 90, 30);

    jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    getContentPane().add(jLabel11);
    jLabel11.setBounds(415, 312, 90, 138);

    jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    getContentPane().add(jLabel12);
    jLabel12.setBounds(415, 11, 90, 138);

    jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/deck.png"))); // NOI18N
    getContentPane().add(jLabel5);
    jLabel5.setBounds(20, 140, 134, 171);

    jButton2.setText("Pedir");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });
    getContentPane().add(jButton2);
    jButton2.setBounds(212, 273, 77, 23);

    jButton3.setText("Plantarse");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });
    getContentPane().add(jButton3);
    jButton3.setBounds(306, 273, 77, 23);

    jButton4.setText("Desistir");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton4ActionPerformed(evt);
        }
    });
    getContentPane().add(jButton4);
    jButton4.setBounds(397, 273, 77, 23);

    jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/table.png"))); // NOI18N
    getContentPane().add(jLabel6);
    jLabel6.setBounds(0, 0, 650, 470);

    setSize(new java.awt.Dimension(666, 509));
    setLocationRelativeTo(null);
}// </editor-fold>                        
int []card;
int p1,p2,p3,p4,c1,c2,c3,c4,k;
double scoreP=0,scoreC=0,scorep1,scorep2,scorep3,scorep4,scorec1,scorec2,scorec3,scorec4;
Random rnd=new Random();
boolean step1 = false;
boolean step2 = false;


private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
card= new int[53];
Amount = Double.parseDouble(Apuesta);
Apuesta = jTextField1.getText();      
BilleteraT = Double.toString(Billetera-Amount); 
jTextField2.setText(BilleteraT);


if (Apuesta.equalsIgnoreCase("0")) {
JOptionPane.showMessageDialog(null, "Por Favor, ingrese una apuesta");}

else {
step2 = false;
if(step1=true){
Apuesta = jTextField1.getText();
Amount = Double.parseDouble(Apuesta);
Billetera=Billetera-Amount;
BilleteraT = Double.toString(Billetera); 
jTextField2.setText(BilleteraT);}


while(Amount <= Billetera && Amount!=0 || Billetera>=0&& Amount!=0){

jButton5.setVisible(false);
jButton5.invalidate();    

for (k=1;k<=6;k++){    
card[k]=rnd.nextInt(53)+1;

switch (k){
    case 1: p1= card[1];
    case 2: p2= card[2];
    case 3: c1= card[3];
    case 4: c2= card[4];
    case 5: p3= card[5];
    case 6: c3= card[6];}}

jLabel1.setIcon(new ImageIcon(getClass().getResource("/img/"+p1+".png")));
jLabel2.setIcon(new ImageIcon(getClass().getResource("/img/"+p2+".png")));
jLabel3.setIcon(new ImageIcon(getClass().getResource("/img/"+c1+".png")));
jLabel4.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));

for (k=1;k<=53;k++){      
switch(k){
    case 1:case 14:case 27:case 40:
    if(k==p1){scorep1=2;} if(k==p2){scorep2=2;}
    if(k==c1){scorec1=2;} if(k==c2){scorec2=2;}

    if (k==p3){scorep3=2;}
    if (k==c3){scorec3=2;}
    break;
    case 2:case 15:case 28:case 41:
    if(k==p1){scorep1=3;} if(k==p2){scorep2=3;}
    if(k==c1){scorec1=3;} if(k==c2){scorec2=3;}

    if (k==p3){scorep3=3;}
    if (k==c3){scorec3=3;}
    break;
    case 3:case 16:case 29:case 42:
    if(k==p1){scorep1=4;} if(k==p2){scorep2=4;}
    if(k==c1){scorec1=4;} if(k==c2){scorec2=4;}

    if (k==p3){scorep3=4;}
    if (k==c3){scorec3=4;}
    break;
    case 4:case 17:case 30:case 43:
    if(k==p1){scorep1=5;} if(k==p2){scorep2=5;}
    if(k==c1){scorec1=5;} if(k==c2){scorec2=5;}

    if (k==p3){scorep3=5;}
    if (k==c3){scorec3=5;}
    break;
    case 5:case 18:case 31:case 44:
    if(k==p1){scorep1=6;} if(k==p2){scorep2=6;}
    if(k==c1){scorec1=6;} if(k==c2){scorec2=6;}

    if (k==p3){scorep3=6;}
    if (k==c3){scorec3=6;} 
    break;
    case 6:case 19:case 32:case 45:
    if(k==p1){scorep1=7;} if(k==p2){scorep2=7;}
    if(k==c1){scorec1=7;} if(k==c2){scorec2=7;}    

    if (k==p3){scorep3=7;}
    if (k==c3){scorec3=7;} 
    break;
    case 7:case 20:case 33:case 46:
    if(k==p1){scorep1=8;} if(k==p2){scorep2=8;}
    if(k==c1){scorec1=8;} if(k==c2){scorec2=8;}

    if (k==p3){scorep3=8;}
    if (k==c3){scorec3=8;} 
    break;
    case 8:case 21:case 34:case 47:
    if(k==p1){scorep1=9;} if(k==p2){scorep2=9;}
    if(k==c1){scorec1=9;} if(k==c2){scorec2=9;}    

    if (k==p3){scorep3=9;}
    if (k==c3){scorec3=9;} 
    break;
    case 9:case 11:case 12:case 13:case 22:case 24:case 25:case 26:case 35:case 37:case 38:case 39:case 48:case 50:case 51:case 52:
    if(k==p1){scorep1=10;} if(k==p2){scorep2=10;}
    if(k==c1){scorec1=10;}  if(k==c2){scorec2=10;}

    if (k==p3){scorep3=10;}
    if (k==c3){scorec3=10;} 
    break;
    case 10:case 23:case 36:case 49:
    if(k==p1){scorep1=1;} if(k==p2){scorep2=1;}
    if(k==c1){scorec1=1;} if(k==c2){scorec2=1;}

    if (k==p3){scorep3=1;}
    if (k==c3){scorec3=1;} 
    break;}}

    scoreP=scorep1+scorep2;
    scoreC=scorec1;
    score1 = Double.toString(scoreP);
    score2 = Double.toString(scoreC);
    jTextField3.setText(score1);
    jTextField4.setText(score2); 
    step1=true;}

    if(Amount!=0&&Billetera>0 || Billetera<=0 ){JOptionPane.showMessageDialog(null,"No podeis apostar lo que no tenes!!!");} 


}

}                                        
/** PEDIR BUTTON  */
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

if (step1=true && Billetera>0){

    if (step2=true && Billetera>0){
    p4=rnd.nextInt(53)+1;
     for (k=1;k<=53;k++){      
switch(k){
    case 1:case 14:case 27:case 40:    
    if (k==p4){scorep4=2;}
    if (k==c4){scorec4=2;}
    break;
    case 2:case 15:case 28:case 41:
    if (k==p4){scorep4=3;}
    if (k==c4){scorec4=3;}
    break;
    case 3:case 16:case 29:case 42:
    if (k==p4){scorep4=4;}
    if (k==c4){scorec4=4;}
    break;
    case 4:case 17:case 30:case 43:
    if (k==p4){scorep4=5;}
    if (k==c4){scorec4=5;}
    break;
    case 5:case 18:case 31:case 44:
    if (k==p4){scorep4=6;}
    if (k==c4){scorec4=6;} 
    break;
    case 6:case 19:case 32:case 45:
    if (k==p4){scorep4=7;}
    if (k==c4){scorec4=7;} 
    break;
    case 7:case 20:case 33:case 46:
    if (k==p4){scorep4=8;}
    if (k==c4){scorec4=8;} 
    break;
    case 8:case 21:case 34:case 47:
    if (k==p4){scorep4=9;}
    if (k==c4){scorec4=9;} 
    break;
    case 9:case 11:case 12:case 13:case 22:case 24:case 25:case 26:case 35:case 37:case 38:case 39:case 48:case 50:case 51:case 52:
    if (k==p4){scorep4=10;}
    if (k==c4){scorec4=10;} 
    break;
    case 10:case 23:case 36:case 49:
    if (k==p4){scorep4=1;}
    if (k==c4){scorec4=1;} 
    break;}}
    scoreP=scorep4+scoreP;
    score1 = Double.toString(scoreP);
    jTextField3.setText(score1);
    jLabel11.setIcon(new ImageIcon(getClass().getResource("/img/"+p4+".png")));
    }

    jLabel11.setIcon(new ImageIcon(getClass().getResource("/img/"+p3+".png")));

    scoreP=scorep3+scoreP;
    score1 = Double.toString(scoreP);
    jTextField3.setText(score1);
    step2 = true;


    if (scoreP==21){JOptionPane.showMessageDialog(null,"Felicitaciones, Ganaste!");
    Billetera=Billetera+Amount*2;
    BilleteraT = Double.toString(Billetera); 
    jTextField2.setText(BilleteraT);

    res=JOptionPane.showInputDialog("Queres intentar otra vez?");

    if (res.equalsIgnoreCase("si")){
    JOptionPane.showMessageDialog(null,"Sin problemas, ingrese nueva Aposta");
    jButton5.setVisible(true);
    jButton5.validate();
    jLabel1.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel2.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel3.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel11.setIcon(new ImageIcon(getClass().getResource("")));
    scoreC=0;
    scoreP=0;
    score1 = Double.toString(scoreP);
    score2 = Double.toString(scoreC);
    jTextField3.setText(score1);
    jTextField4.setText(score2);} 

    else if(res.equalsIgnoreCase("no")){
    JOptionPane.showMessageDialog(null,"Bueno, Hasta la proxima! Adios");
    System.exit(0);

    }else{JOptionPane.showMessageDialog(null,"Respuesta Invalida");}}       

    if (scoreP>21){JOptionPane.showMessageDialog(null,"Uhh,Pasaste, mas suerte en la proxima!");

    res=JOptionPane.showInputDialog("Queres intentar otra vez?");
    if (res.equalsIgnoreCase("si")){
    JOptionPane.showMessageDialog(null,"Sin problemas, ingrese nueva Aposta");
    jButton5.setVisible(true);
    jButton5.validate();
    jLabel1.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel2.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel3.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel11.setIcon(new ImageIcon(getClass().getResource("")));
    scoreC=0;
    scoreP=0;
    score1 = Double.toString(scoreP);
    score2 = Double.toString(scoreC);
    jTextField3.setText(score1);
    jTextField4.setText(score2);}

    else if(res.equalsIgnoreCase("no")){
    JOptionPane.showMessageDialog(null,"Bueno, Hasta la proxima! Adios");
    System.exit(0);

    }else{JOptionPane.showMessageDialog(null,"Respuesta Invalida");}}

}
}                                        
/** PLANTARSE BUTTON  */
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
JOptionPane.showMessageDialog(null,"Okay, Mi turno!");

if (step1=true&&Billetera>0){
 scoreC=scorec2+scoreC;
 score2 = Double.toString(scoreC);
 jTextField4.setText(score2);
 jLabel4.setIcon(new ImageIcon(getClass().getResource("/img/"+c2+".png")));

 if(scoreC<=16){resc="hit";

 while(resc.equals("hit")){

 jLabel2.setIcon(new ImageIcon(getClass().getResource("/img/"+c3+".png")));
 scoreC=scorec3+scoreC;
 score2 = Double.toString(scoreC);
 jTextField4.setText(score2);

 if(scoreC<=16){resc="hit";}
 if(scoreC>16){resc="stand";}}

 }else if(scoreC>16){resc="stand";}

 while(resc.equals("stand")){

if (scoreC>scoreP&&scoreC<=21){JOptionPane.showMessageDialog(null,"Gano yo, mas suerte en la proxima!");

    res=JOptionPane.showInputDialog("Queres intentar otra vez?");
    if (res.equalsIgnoreCase("si")){
    JOptionPane.showMessageDialog(null,"Sin problemas, ingrese nueva Aposta");
    jButton5.setVisible(true);
    jButton5.validate();
    jLabel1.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel2.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel3.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel11.setIcon(new ImageIcon(getClass().getResource("")));
    jLabel12.setIcon(new ImageIcon(getClass().getResource("")));
    scoreC=0;
    scoreP=0;
    score1 = Double.toString(scoreP);
    score2 = Double.toString(scoreC);
    jTextField3.setText(score1);
    jTextField4.setText(score2); }

    else if(res.equalsIgnoreCase("no")){
    JOptionPane.showMessageDialog(null,"Bueno, Hasta la proxima! Adios");
    System.exit(0);
    }else{JOptionPane.showMessageDialog(null,"Respuesta Invalida");}}

if(scoreP>scoreC&&scoreP<=21){JOptionPane.showMessageDialog(null,"Felicitaciones, Ganaste!");
    Billetera=Billetera+Amount*2;
    BilleteraT = Double.toString(Billetera); 
    jTextField2.setText(BilleteraT);

    res=JOptionPane.showInputDialog("Queres intentar otra vez?");
    if (res.equalsIgnoreCase("si")){
    JOptionPane.showMessageDialog(null,"Sin problemas, ingrese nueva Aposta");
    jButton5.setVisible(true);
    jButton5.validate();
    jLabel1.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel2.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel3.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel11.setIcon(new ImageIcon(getClass().getResource("")));
    jLabel12.setIcon(new ImageIcon(getClass().getResource("")));
    scoreC=0;
    scoreP=0;
    score1 = Double.toString(scoreP);
    score2 = Double.toString(scoreC);
    jTextField3.setText(score1);
    jTextField4.setText(score2);}

    else if(res.equalsIgnoreCase("no")){
    JOptionPane.showMessageDialog(null,"Bueno, Hasta la proxima! Adios");
    System.exit(0);
    }else{JOptionPane.showMessageDialog(null,"Respuesta Invalida");}}


if(scoreP==scoreC&&scoreP<=21){JOptionPane.showMessageDialog(null,"Es un Empate!");

    res=JOptionPane.showInputDialog("Queres jugar de nuevo?");
    if (res.equalsIgnoreCase("si")){
    JOptionPane.showMessageDialog(null,"Sin problemas, ingrese nueva Aposta");
    jButton5.setVisible(true);
    jButton5.validate();
    jLabel1.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel2.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel3.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel11.setIcon(new ImageIcon(getClass().getResource("")));
    jLabel12.setIcon(new ImageIcon(getClass().getResource("")));
    scoreC=0;
    scoreP=0;
    score1 = Double.toString(scoreP);
    score2 = Double.toString(scoreC);
    jTextField3.setText(score1);
    jTextField4.setText(score2); } 

    else if(res.equalsIgnoreCase("no")){
    JOptionPane.showMessageDialog(null,"Bueno, Hasta la proxima! Adios");
    System.exit(0);
    }else{JOptionPane.showMessageDialog(null,"Respuesta Invalida");}
    }

}
} 
}                                        
 /** DESISTIR BUTTON  */
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if (step1=true && Billetera>0){
    JOptionPane.showMessageDialog(null,"Bueno, mas suerte en la proxima!");

    res=JOptionPane.showInputDialog("Queres intentar otra vez?");
    if (res.equalsIgnoreCase("si")){
    JOptionPane.showMessageDialog(null,"Sin problemas, ingrese nueva Aposta");
    jButton5.setVisible(true);
    jButton5.validate();
    jLabel1.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel2.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel3.setIcon(new ImageIcon(getClass().getResource("/img/back.png")));
    jLabel11.setIcon(new ImageIcon(getClass().getResource("")));
    jLabel12.setIcon(new ImageIcon(getClass().getResource("")));
    scoreC=0;
    scoreP=0;
    score1 = Double.toString(scoreP);
    score2 = Double.toString(scoreC);
    jTextField3.setText(score1);
    jTextField4.setText(score2); }

    else if(res.equalsIgnoreCase("no")){
    JOptionPane.showMessageDialog(null,"Bueno, Hasta la proxima! Adios");
    System.exit(0);
    }else{JOptionPane.showMessageDialog(null,"Respuesta Invalida");}}    
} 




Aucun commentaire:

Enregistrer un commentaire