首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Black Jack Java项目

Black Jack Java项目
EN

Stack Overflow用户
提问于 2014-01-05 08:52:06
回答 1查看 713关注 0票数 0

对于一个学校项目,我创建了以下程序:

BlckJckUI.java

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
    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;
     }
     }
}

该程序应该是一个黑杰克咨询工具,当你点击“开始/重置”时,它应该要求你点击一张牌(这是发牌商显示的牌)。当您单击一张卡片时,它会要求您单击您拥有的第一张卡片。当你点击它的时候,它就会询问你的第二张卡片,等等。

当你点击开始,它可以正常工作,但是,当你点击任何数字卡时,JOptionPanel不会显示任何信息。

我很确定这是因为它应该输出的字符串的值是"null“,而我不希望它是”null“。

我如何让它在if-else语句中接受array.result的值?

EN

回答 1

Stack Overflow用户

发布于 2014-01-05 09:03:51

检查你的数组类。在for语句周围缺少方法定义。我最好的猜测是给结果变量一个缺省的空值->

public String result = "";

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20928699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档