2014-01-05 3 views
0

Для школьного проекта, я создал следующую программу:Black Jack Java Project

BlckJckUI.java

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 




import javax.swing.*; 

public class BlckJckUI { 

public static void main(String args[]) 
{ 

    JFrame GUI = new JFrame("Blackjack Advisor"); 
    GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    GUI.setSize(800,800); 
    GUI.setVisible(true); 
    ImageIcon Ace = new ImageIcon("C:\\Users\\Hieu Vo\\workspace\\Deck-2014-01-03\\Deck\\Ace.jpg"); 
    JButton ace = new JButton(Ace); 
    ace.setSize(300, 100); 
    ace.setLocation(100, 100); 
    ace.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      Arrays array = new Arrays(); 
      Math math = new Math(); 
      if (array.playerhandtotal <= 21) 
      { 
       math.cardvalue = 11; 
      } 
      else 
      { 
       math.cardvalue = 1; 
      } 
      array.clicktracker++; 
      JOptionPane.showMessageDialog(null,array.result); 
     } 
    }); 
    GUI.add(ace); 
    ImageIcon Two = new ImageIcon("C:\\Users\\Hieu Vo\\workspace\\Deck-2014-01-03\\Deck\\2.jpg"); 
    JButton two = new JButton(Two); 
    two.setSize(300, 100); 
    two.setLocation(100, 200); 
    two.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      Arrays array = new Arrays(); 
      Math math = new Math(); 
      math.cardvalue = 2; 
      array.clicktracker++; 

      JOptionPane.showMessageDialog(null,array.result); 
     } 
    }); 
    GUI.add(two); 
    ImageIcon Three = new ImageIcon("C:\\Users\\Hieu Vo\\workspace\\Deck-2014-01-03\\Deck\\3.jpg"); 
    JButton three = new JButton(Three); 
    three.setSize(300, 100); 
    three.setLocation(100, 300); 
    three.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      Arrays array = new Arrays(); 
      Math math = new Math(); 
      math.cardvalue = 3; 
      array.clicktracker++; 
      ; 
      JOptionPane.showMessageDialog(null,array.result); 
     } 
    }); 
    GUI.add(three); 
    ImageIcon Four = new ImageIcon("C:\\Users\\Hieu Vo\\workspace\\Deck-2014-01-03\\Deck\\4.jpg"); 
    JButton four = new JButton(Four); 
    four.setSize(300, 100); 
    four.setLocation(100, 400); 
    four.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      Arrays array = new Arrays(); 
      Math math = new Math(); 
      math.cardvalue = 4; 
      array.clicktracker++; 
      ; 
      JOptionPane.showMessageDialog(null,array.result); 
     } 
    }); 
    GUI.add(four); 
    ImageIcon Five = new ImageIcon("C:\\Users\\Hieu Vo\\workspace\\Deck-2014-01-03\\Deck\\5.jpg"); 
    JButton five = new JButton(Five); 
    five.setSize(300, 100); 
    five.setLocation(100, 500); 
    five.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      Arrays array = new Arrays(); 
      Math math = new Math(); 
      math.cardvalue = 5; 
      array.clicktracker++; 
      ; 
      JOptionPane.showMessageDialog(null,array.result); 
     } 
    }); 
    GUI.add(five); 
    ImageIcon Six = new ImageIcon("C:\\Users\\Hieu Vo\\workspace\\Deck-2014-01-03\\Deck\\6.jpg"); 
    JButton six = new JButton(Six); 
    six.setSize(300, 100); 
    six.setLocation(900, 100); 
    six.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 

      Arrays array = new Arrays(); 
      Math math = new Math(); 
      math.cardvalue = 6; 
      array.clicktracker++; 
      ; 
      JOptionPane.showMessageDialog(null,array.result); 
     } 

    }); 
    GUI.add(six); 
    ImageIcon Seven = new ImageIcon("C:\\Users\\Hieu Vo\\workspace\\Deck-2014-01-03\\Deck\\7.jpg"); 
    JButton seven = new JButton(Seven); 
    seven.setSize(300, 100); 
    seven.setLocation(900, 200); 
    seven.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      Arrays array = new Arrays(); 
      Math math = new Math(); 
      math.cardvalue = 7; 
      array.clicktracker++; 
      ; 
      JOptionPane.showMessageDialog(null,array.result); 
     } 
    }); 
    GUI.add(seven); 
    ImageIcon Eight = new ImageIcon("C:\\Users\\Hieu Vo\\workspace\\Deck-2014-01-03\\Deck\\8.jpg"); 
    JButton eight = new JButton(Eight); 
    eight.setSize(300, 100); 
    eight.setLocation(900, 300); 
    eight.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      Arrays array = new Arrays(); 
      Math math = new Math(); 
      math.cardvalue = 8; 
      array.clicktracker++; 
      ; 
      JOptionPane.showMessageDialog(null,array.result); 
     } 
    }); 
    GUI.add(eight); 
    ImageIcon Nine = new ImageIcon("C:\\Users\\Hieu Vo\\workspace\\Deck-2014-01-03\\Deck\\9.jpg"); 
    JButton nine = new JButton(Nine); 
    nine.setSize(300, 100); 
    nine.setLocation(900, 400); 
    nine.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      Arrays array = new Arrays(); 
      Math math = new Math(); 
      math.cardvalue = 9; 
      array.clicktracker++; 
      ; 
      JOptionPane.showMessageDialog(null,array.result); 
     } 
    }); 
    GUI.add(nine); 
    ImageIcon Ten = new ImageIcon("C:\\Users\\Hieu Vo\\workspace\\Deck-2014-01-03\\Deck\\ten.jpg"); 
    JButton ten = new JButton(Ten); 
    ten.setSize(300, 100); 
    ten.setLocation(900, 500); 
    ten.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      Arrays array = new Arrays(); 
      Math math = new Math(); 
      math.cardvalue = 10; 
      array.clicktracker++; 
      JOptionPane.showMessageDialog(null,array.result); 
     } 
    }); 
    GUI.add(ten); 
    JButton start = new JButton("Start/Reset"); 
    start.setSize(300, 100); 
    start.setLocation(500,500); 
    start.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      ; 
      Arrays array = new Arrays(); 
      array.playerhand.clear(); 
      array.dealer = 0; 
      array.clicktracker = 0; 
      array.playerhandtotal = 0; 
      array.result = null; 
      JOptionPane.showMessageDialog(null,"Please select the card \nthat the dealer is showing :)"); 

     } 
    }); 
    GUI.add(start); 
    GUI.setLayout(null); 
    GUI.pack(); 
    //start.setSize(somethinghere); 
    //start.setLocation(Somethinghere); 


} 
} 

Math.java

public class Math 
{ 
public int cardvalue; 
public Math() 
{ 

    Arrays array = new Arrays(); 

    if (array.clicktracker == 1) 
    { 
     array.dealer = cardvalue; 
     array.result = "Please select the first card you have :)"; 


    } 
    else if (array.clicktracker == 2) 
    { 

     array.playerhand.add(cardvalue); 
     array.result = "Please select the second card you have :)"; 

    } 
    else if (array.clicktracker >= 3) 
    { 
     array.playerhand.add(cardvalue); 
     if (array.playerhandtotal <= 8) 
     { 

      // array.result = result statement 
      array.result = "You should just hit until you're safe. If the dealer 6 or below,\n" 
       + " the chances are that he'll bust and if not, remain low above 17.\n" 
       + " As long as you can pull a 17 or higher, you should be safe. Pick \n" 
       + "another card or reset."; 

     } 
     else if (array.playerhandtotal == 9) 
     { 
      if (3 <= array.dealer && array.dealer <= 6) 
       { 

        array.result = "Double down. The chances of him busting is high,\n" 
         + "chances of him not having a high total is high as well. \n" 
         + "Chances you'll get a 10 is high as well. Pick another card or reset."; 

       } 

      else 
       { 

        array.result = "Your best option is to Hit, and it's a luck thing from there. The dealer has some room with a 2; it's highly likely for him to get right below 21 and well above 16, and given that you'll have to beat him in the end, you have to hit until you're high. With 7 plus, he's set: He gets a 10 and he can stop, and again in the end, you'll have to beat his score. May the Goddess of Luck favor you, because you'll need it. Pick another card or reset."; 

       } 
     } 
     else if (array.playerhandtotal == 10) 
     { 
      if (2 <= array.dealer && array.dealer <= 9) 
       { 

        array.result = "Double down, since Assumption Rule wins out in your favor, if you have a 10, you're golden. The dealer has very little chance of beating you: chances are that he busts or loses to your almighty 20. Even if you bust, the chance that he busts is sufficiently high as well. Very unlikely for him to get 21. Pick another card or reset."; 

       } 
      else 
       { 


        array.result = "Hit. Dealer is in green zone, Assumption Rule states that he'll get a 20 or 21. Gotta beat him, no way around it. Hope for the best. A double will never cut it, since you're gambling on 1 card to get 20-21. Better safe than sorry. Pick another card or reset."; 

       } 
     } 
     else if (array.playerhandtotal == 11) 
     { 
      if (2 <= array.dealer && array.dealer <= 10) 
       { 

        array.result = "Double down, since Assumption Rule wins out in your favor, if you have a 10, you're golden. The dealer has very little chance of beating you: chances are that he busts ors loses to your almighty 21. Even if you bust, the chance that he busts is sufficiently high as well. Pick another card or reset."; 

       } 
      else 
       { 

        array.result = "Hit. He's going to have a 10 due to Assumption Rule. You may as well, but remember, you're less likely to. Thus, hit to be safe. You have to beat to win, and if you Double Down, you lock yourself into a position where the chances of you beating the dealer is slim. (At least, slimmer than hitting.) Pick another card or reset."; 

       } 
     } 
     else if (array.playerhandtotal == 12) 
     { 
      if ((array.dealer == 2 || array.dealer == 3) || (array.dealer >= 7)) 
       { 

        array.result = "Hit. You're likely to lose anyways, per the Assumption Rule. However, there's always a chance that you won't. Doubling makes no sense, and it will reduce your chance of winning. The dealer has some room with a 2; it's highly likely for him to get right below 21 and well above 16, and given that you'll have to beat him in the end, you have to hit until you're high. With 7 plus, he's set: He gets a 10 and he can stop, and again in the end, you'll have to beat his score. Pick another card or reset."; 

       } 
      else 
       { 

        array.result = "Stand. You have a high chance of bust, but the dealer does as well. If you stay, chances are, dealer will bust. Pick another card or reset."; 

       } 
     } 
     else if (13 <= array.playerhandtotal && array.playerhandtotal <= 16) 
     { 
      if (array.dealer >= 7) 
       { 

        array.result = "Hit. Might as well. Chances are against you severely, but you still have to play on chance. Hitting is the safest option. Pick another card or reset."; 

       } 
      else 
       { 

        array.result = "Stand. Dealer may bust; ride on that chance. Pick another card or reset."; 

       } 
     } 
     else if (17 <= array.playerhandtotal && array.playerhandtotal <= 21) 
      { 

       array.result = "Stand. You're golden. Just hope for the best and let Lady Luck work her magic. Rest please."; 

      } 
     else if (array.playerhandtotal >= 22) 
      { 

       array.result = " Oi. You've busted. Reset please."; 

      } 

    } 



} 
} 

Arrays.java

import java.util.*; 
    public class Arrays 
    { 
     public String result; 
    ArrayList<Integer> playerhand = new ArrayList<Integer>(); 
    public int dealer = 0; 
    public int clicktracker = 0; 
    public int playerhandtotal = 0; 
    { 
     for (int element: playerhand) 
    { 
     playerhandtotal = element + playerhandtotal; 
    } 
    } 
} 

Программа предполагает d, чтобы быть инструментом поддержки Black Jack, и когда вы нажимаете «start/reset», он должен попросить вас нажать 1 карту (это карта, которую показывает дилер). Когда вы щелкаете по карте, она просит вас нажать первая карта у вас есть. Когда вы нажимаете на это, он затем запрашивает вашу вторую карточку и т. Д.

Когда вы нажимаете кнопку «Пуск», это работает отлично, однако, когда вы нажимаете «ЛЮБАЯ» из числовых карт, JOptionPanel ничего не говорит.

Я уверен, что это связано с тем, что значение строки, которую предполагается выпустить, равно «null», которое я не хочу.

Как это сделать так, чтобы оно принимало значение array.result в операторах if-else?

+2

Путь слишком много кода, особенно потому, что большая часть его повторяется. Где даже вопрос? – Obicere

+2

Пожалуйста, отредактируйте это до минимального количества кода, чтобы продемонстрировать свой вопрос. –

+0

В качестве побочного примечания: Я думаю, вы не должны называть свои классы Math и Arrays, так как уже есть классы на языке с этими именами. – HectorLector

ответ

0

Проверьте класс Arrays. Отсутствует определение метода в вашей инструкции for. И самое лучшее, что я могу предположить, - дать вашей переменной результата значение по умолчанию ->

public String result = "";

Смежные вопросы