Java中的BlackJack与Dealer-Player相同的Deck(s)

时间:2013-03-28 19:45:18

标签: java object blackjack

我正在用Java编写一个经典的BlackJack游戏程序。

规则是相同的,我们根据某些规则选择玩家和经销商(CPU)。

我的代码,在下面,制作2个单独的Deck(s)堆栈,一个用于Player,一个用于经销商,每个从不同的Deck中抽取,但​​我想让它们(玩家和经销商)从同样的甲板。

我的代码有任何建议/更正吗?

import java.util.Random;
import java.util.Scanner;


public class River 
{

    private int CardNumber;

    private int BeginCards;

    private int Decks;

    private int[] PartialSumArray = {4,8,12,16,20,24,28,32,36,52}; 

    private int[] BeginPartialSumArray = {4,8,12,16,20,24,28,32,36,52};

    private int PickedCard;

    private Random randomGenerator = new Random();

    //Constructor without definition
    public River()
    {
        CardNumber = 52;
        BeginCards = 52;
    }

    //Constructor with definition
    public River(int Decks)
    {
        CardNumber = Decks * 52;

        BeginCards = CardNumber;
        this.Decks = Decks; 

        //Initialize partial sum array for many decks of cards
        for (int i=0; i<10; i++)
        {
            PartialSumArray[i] = PartialSumArray[i] * Decks;
            BeginPartialSumArray[i] = PartialSumArray[i] * Decks;
        }
        System.out.println();
    }

    //Create random numbers
    private int computeRandomSteps(int CardNumber)
    {
        //System.out.print("stin random , cardnumber is" + CardNumber);
        int randomSteps = randomGenerator.nextInt(CardNumber-1);
        return randomSteps;
    }


    public int nextCard()
    {

        int steps = computeRandomSteps(CardNumber);
        int position=0;

        for (int i=0; i<CardNumber; i++)
        {
            if (steps<= PartialSumArray[i])
            {
                position = i+1;
                break;
            }
        }

        CardNumber--;
        return position;
    }


    public int start()
    {
        int ShuffleLimit;
        PickedCard = nextCard();
        System.out.println("Picked card is :" + PickedCard);

        int HelpVariable = PickedCard-1;

        for (int i=0; i<10; i++)
        {
            if (i >= HelpVariable)
            {
                PartialSumArray[HelpVariable] = PartialSumArray[i]-1;
                HelpVariable++;
            }
        }


        ShuffleLimit = BeginCards/4; 

        if (CardNumber<ShuffleLimit)
        {
            for (int i=0; i<9; i++)
            {
                BeginPartialSumArray[i] = BeginPartialSumArray[i] * Decks;
            }
        }
        return PickedCard;
    }


    public int ReturnCardNumber()
    {
        System.out.println("return cardnumber is " + CardNumber);
        return CardNumber;
    }

}



class Hand
{

    private int points;
    private int SumPoints=0;
    private boolean Ace = true;


    Scanner input = new Scanner(System.in);
    //Scanner input3 = new Scanner(System.in);

    //int Decks = input3.nextInt();

    River myRiver = new River();
    //River myRiver = new River(Decks);



    public int getPoints()
    {
        points = myRiver.start();
        if (points == 1 && Ace)
        {
            System.out.println("It is an Ace. Do you want to count 1 or 11?");

            points = input.nextInt();
            Ace = false;
        }
        SumPoints += points;
        System.out.println("Points are : " + SumPoints);
        return SumPoints;
    }

    public int getPointsDealer()
    {
        points = myRiver.start();
        if (points == 1 && Ace)
        {
            if (SumPoints + 11 > 21)
            {
                points = 1;
            }
            else
            {
                points = 11;
            }
            Ace = false;
        }
        SumPoints += points;
        System.out.println("Points are : " + SumPoints);
        return SumPoints;
    }
}


class Player
{
    private int points;
    private double account=0;
    private double bet;
    private boolean WinOrLose;

    Scanner input1 = new Scanner(System.in);
    Scanner input2 = new Scanner(System.in);

    public double placeBet()
    {
        System.out.println("How much do you want to bet?");
        bet = input1.nextDouble();

        return bet;
    }

    public double profit(boolean WinOrLose)
    {
        if (WinOrLose)
        {
            account += bet;
            return account;
        }
        else
        {
            account -= bet;
            return account;
        }
    }


    public int play(River other)
    {

        Hand myHand = new Hand();
        bet = placeBet();
        points = myHand.getPoints();
        boolean end = true;
        String Choice;

        while (end)
        {
            System.out.println("Make a choice");
            Choice = input2.nextLine();


            switch(Choice)
            {
            case "DoubleBet":
                bet = bet *2;
                points = myHand.getPoints();

                if (points > 21)
                {
                    System.out.print("Burned!");
                    WinOrLose = false;
                    account = profit(WinOrLose);
                    end = false;
                    break;
                }
                else if (points == 21)
                {
                    System.out.print("You won!");
                    WinOrLose = true;
                    account = profit(WinOrLose);
                    end = false;
                    break;
                }
                else 
                {
                    System.out.println("Your points are :" + points);
                    end = false;
                    break;
                }

            case "stop":
                System.out.println("Your points are :" + points);
                end = false;
                break;

            case "Hit":
                points = myHand.getPoints();
                if (points > 21)
                {
                    System.out.print("Burned!");
                    WinOrLose = false;
                    account = profit(WinOrLose);
                    end = false;
                    break;
                }
                else if (points == 21)
                {
                    System.out.print("You won!");
                    WinOrLose = true;
                    account = profit(WinOrLose);
                    end = false;
                    break;
                }
                break;

            default:
                System.out.println("That is not a choice.");
                end = false;
                break;
            }

        }
        return points;
    }
}

class BlackJack
{

    public static void main(String args[])
    {
        int SumPointsPlayer;
        int SumPointsDealer;
        boolean WinOrLose = true;
        double account;
        int Decks;
        int BeginCards;
        int ThisMomentCards;

        Scanner input1 = new Scanner(System.in);
        Scanner input2 = new Scanner(System.in);

        System.out.println("How many decks do you want to begin with?");
        Decks = input1.nextInt();


        River myRiver = new River(Decks);
        Player myPlayer = new Player();

        //Calculate the cards we have when the game starts
        BeginCards = 52 * Decks;

        System.out.println("Do you want to start the game? Yes or No.");
        String Repeat;

        Repeat = input2.nextLine();

        while (Repeat.equals("Yes"))
        {
            ThisMomentCards = myRiver.ReturnCardNumber();
            System.out.println("Cards are : " + ThisMomentCards);

            //Player's points for 1 round
            SumPointsPlayer = myPlayer.play(myRiver);

            //If player catches 21 he wins instantly
            if(SumPointsPlayer == 21)
            {
                account = myPlayer.profit(WinOrLose);
                System.out.println("Your account has :" + account + "dollars!");
            }
            //If player catches >21 he loses instantly
            else if(SumPointsPlayer > 21)
            {
                WinOrLose = false;
                account = myPlayer.profit(WinOrLose);
                System.out.println("Your account has :" + account + "dollars!");
            }
            //Compare the hand of player and dealer and the bigger wins
            else
            {
                //Dealer's points for 1 round
                SumPointsDealer = playDealer(myRiver);

                //If dealer catches >21 he loses instantly
                if(SumPointsDealer>21)
                {
                    System.out.println("Player wins!");
                    account = myPlayer.profit(WinOrLose);
                    System.out.println("Your account has :" + account + "dollars!");
                }
                //Hand of player bigger than the hand of the dealer , player wins
                else if (SumPointsPlayer>SumPointsDealer)
                {
                    WinOrLose = true;
                    account = myPlayer.profit(WinOrLose);
                    System.out.println("Player wins. Your account has :" + account + "dollars!");

                }
                //Hand of player smaller than the hand of the dealer , dealer wins
                else if (SumPointsPlayer<SumPointsDealer)
                {
                    WinOrLose = false;
                    account = myPlayer.profit(WinOrLose);
                    System.out.println("Player lost. Your account has :" + account + "dollars!");
                }
                //Hand of player is equal with the hand of the dealer , it is tie
                else
                {
                    System.out.println("Player and Dealer are tie!!");
                }
            }
            System.out.println("Do you want to continue the game? Yes or No.");
            Repeat = input2.nextLine();
        }

    }

    public static int playDealer(River other)
    {
        boolean bountry = true;

        System.out.println("Dealer plays :");

        Hand myHand = new Hand();
        int SumPointsDealer = myHand.getPointsDealer();

        while (bountry)
        {
            if (SumPointsDealer<17)
            {
                SumPointsDealer = myHand.getPointsDealer();
            }
            else if (SumPointsDealer>21)
            {
                System.out.println("Dealer burned!");
                bountry = false;
            }
            else
            {
                bountry = false;
            }
        }
        return SumPointsDealer;
    }
}

一些澄清:

1)我们随机抽取卡片的方式是基于一种奇怪的方式,但这不是问题,这就是程序从甲板上随机抽取卡片的方式

2)我注意到的另一个问题是,在Hand类中,我所拥有的代码不起作用,因为它不允许我拥有System.out.println()

Scanner input = new Scanner(System.in);
    //Scanner input3 = new Scanner(System.in);

    //int Decks = input3.nextInt();

    River myRiver = new River();
    //River myRiver = new River(Decks);

我想这样做,以便我会说出用户想玩多少套牌

1 个答案:

答案 0 :(得分:0)

您可以更好,更轻松地将代码定位到对象中。 即Collections.shuffle(Deck)取代整个随机难题 Deck是你创建的Object,由LinkedList和counter [deck deck]等属性组成 希望有所帮助。

从主甲板的链接列表中删除对象卡并将其移动到玩家的牌组中。是的,你可以按照你想要的方式创建任意数量的套牌。

对象卡具有属性Value和Suit。