Java中的BlackJack与Dealer-Player相同套牌

问题描述:

我正在用Java编写一个经典的BlackJack游戏程序。Java中的BlackJack与Dealer-Player相同套牌

规则是一样的,我们做出选择,因为玩家和经销商(CPU)按照一定的规则进行游戏。

我的代码就在下面,它有2个独立的甲板堆栈,一个用于玩家,一个用于经销商,每个从一个不同的甲板绘制,但我想让他们两个(玩家和经销商)相同的甲板。

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

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),我注意到另一个问题是,在课堂上手,我有//代码不工作作为它没有让我有一个的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); 

我想这样做,所以我会说有多少甲板用户想玩

+0

SO是代码审查的地方。看看这里:http://codereview.stackexchange.com/ – FazoM

你可以做的更好,更容易的代码是面向对象。 即Collections.shuffle(Deck)取代了整个随机难题 ,其中Deck是您创建的Object,由诸如LinkedList和计数器[甲板值] 等属性组成,希望有所帮助。

从主甲板的链接列表中移除物品并将其移动到玩家的甲板上。是的,你可以按照你想要的方式创建尽可能多的套牌。

对象卡片具有属性值和套装。