1

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

规则是一样的,我们作为玩家和庄家(CPU)在某些规则下进行选择。

我的代码,在下面,制作了 2 个单独的牌组,一个供玩家使用,一个供经销商使用,每个都从不同的牌组抽牌,但我想让它们(玩家和庄家)都从同一个牌组抽牌( s)。

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

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);

我想这样做是为了说明用户想玩多少套牌

4

1 回答 1

0

你可以做得更好更容易的代码是面向对象的。即 Collections.shuffle(Deck) 替换了整个随机难题,其中 Deck 是您创建的由 LinkedList 和 counter[deck value] 等属性组成的对象,希望对您有所帮助。

从主卡组的链表中删除对象卡并将其移动到玩家的卡组中。是的,您可以通过这种方式创建任意数量的套牌。

对象卡具有属性值和花色。

于 2014-02-17T15:46:06.733 回答