简体   繁体   English

为什么我的卡片不随机?

[英]Why Aren't My Cards Random?

I created this program in my quest to learn Java but every time I run it I get the same cards over and over again. 我在学习Java的过程中创建了这个程序,但每次运行它都会一遍又一遍地获得相同的卡片。 I can't really find the problem, so why aren't my cards random? 我真的找不到问题,为什么我的卡片不随机? I don't really get what I'm missing, is my shuffle method incorrect? 我真的不知道我错过了什么,我的洗牌方法是不正确的?

package blackjack;

import java.util.Scanner;

public class BlackJack {

 public static void main(String[] args) {

      System.out.println("Are you ready to play BlackJack?");
      System.out.println();


      playBlackjack();
      System.out.println("Thanks for playing!");


   } 


   static boolean playBlackjack() {

      Deck deck;                  
      Hand dealerHand;   
      Hand playerHand;    

      deck = new Deck();
      dealerHand = new Hand();
      playerHand = new Hand();


      dealerHand.addCard( deck.dealCard() );
      playerHand.addCard( deck.dealCard() ); 
      dealerHand.addCard( deck.dealCard() );
      playerHand.addCard( deck.dealCard() );


      System.out.println();


      if (dealerHand.getBlackjackValue() == 21) {
           System.out.println("The dealer have the " + dealerHand.getCard(0)
                                   + " and the " + dealerHand.getCard(1) + ".");
           System.out.println("You have the " + playerHand.getCard(0)
                                     + " and the " + playerHand.getCard(1) + ".");
           System.out.println();
           System.out.println("The dealer has Blackjack! The dealer wins!");
           return false;
      }

      if (playerHand.getBlackjackValue() == 21) {
           System.out.println("Dealer has the " + dealerHand.getCard(0)
                                   + " and the " + dealerHand.getCard(1) + ".");
           System.out.println("User has the " + playerHand.getCard(0)
                                     + " and the " + playerHand.getCard(1) + ".");
           System.out.println();
           System.out.println("You have Blackjack.  You win.");
           return true;
      }
      while (true) {

           // User decides whether to hit or stand

           System.out.println();
           System.out.println();
           System.out.println("Your cards are:");
           for ( int i = 0; i < playerHand.getCardCount(); i++ )
              System.out.println("    " + playerHand.getCard(i));
           System.out.println("The total of your hand is " +     playerHand.getBlackjackValue());
           System.out.println();
           System.out.println("The dealer is showing the " + dealerHand.getCard(0));
           System.out.println();
           System.out.println("Do you hit(type 0) or stand(type 1)? ");
           Scanner input = new Scanner(System.in);
           int choice = input.nextInt();
              if (choice != 0 && choice != 1){
                 System.out.println("0 or 1 must be inputted to continue ");}
            while (choice != 0 && choice != 1);

           if ( choice == 1 ) {
               break;
           }
           else {
               Card newCard = deck.dealCard();
               playerHand.addCard(newCard);
               System.out.println();
               System.out.println("You hits and your card is the " + newCard);
               System.out.println("The total of the hand is now " + playerHand.getBlackjackValue());
               if (playerHand.getBlackjackValue() > 21) {
                   System.out.println();
                   System.out.println("Fool of a Took! You went over 21 and busted!");
                   return false;  
               }
           }

      }
      //User has stood at this point. Poor fool


      System.out.println();
      System.out.println("You stand. Wouldn't it be nice to sit sometime?");
      System.out.println("The dealer's cards are "+ dealerHand.getCard(0) + " and the " + dealerHand.getCard(1));
      while (dealerHand.getBlackjackValue() <= 16) {
         Card newCard = deck.dealCard();
         System.out.println("The dealer hits and gets the " + newCard);
         dealerHand.addCard(newCard);
         if (dealerHand.getBlackjackValue() > 21) {
            System.out.println();
            System.out.println("The dealer busted! You win!");
            return true;
         }
      }
      System.out.println("The dealer's total is " + dealerHand.getBlackjackValue());
      System.out.println();
      if (playerHand.getBlackjackValue() == dealerHand.getBlackjackValue()) {
         System.out.println("The house always wins on a tie. You lose :(");
         return false;
      }
      else if (dealerHand.getBlackjackValue() >= playerHand.getBlackjackValue()) {
         System.out.println("The dealer wins as he has " + dealerHand.getBlackjackValue() 
                          + " points to your " + playerHand.getBlackjackValue() + "!");
         return false;
      }
      else {
         System.out.println("You win because you had " + playerHand.getBlackjackValue() 
                          + " points while the dealer only had " + dealerHand.getBlackjackValue() + "!");
         return true;
      }

   }
}



package blackjack;

public class Card {

private int suit;
private int rank;
public final static int SPADES = 0,       // Codes for the 4 suits.
                        HEARTS = 1,
                        DIAMONDS = 2,
                        CLUBS = 3;

public final static int ACE = 1,          
                  JACK = 11,            
                  QUEEN = 12,
                  KING = 13;



public Card(int cRank, int cSuit) {
    rank = cRank;
    suit = cSuit;
}

public int getSuit() {
    return suit;
}

public int getRank() {
    return rank;
}

public String getSuitString() {
        // Return a String representing the card's suit.
        // (If the card's suit is invalid, "??" is returned.)
    switch ( suit ) {
       case SPADES:   return "Spades";
       case HEARTS:   return "Hearts";
       case DIAMONDS: return "Diamonds";
       case CLUBS:    return "Clubs";
       default: return "I don't even know";

    }
}

public String getRankString() {
    switch ( rank ) {
       case 1:   return "Ace";
       case 2:   return "2";
       case 3:   return "3";
       case 4:   return "4";
       case 5:   return "5";
       case 6:   return "6";
       case 7:   return "7";
       case 8:   return "8";
       case 9:   return "9";
       case 10:  return "10";
       case 11:  return "Jack";
       case 12:  return "Queen";
       case 13:  return "King";
       default: return "This is impossible!";
    }


}

@Override
public String toString() {
    return getRankString() + " of " + getSuitString();
}


}



package blackjack;

public class Deck {
private Card[] deck;   
private int cardsUsed; 

public Deck() {
   deck = new Card[52];
   int l = 0;
   for ( int suit = 0; suit <= 3; suit++ ) {
      for ( int value = 1; value <= 13; value++ ) {
         deck[l] = new Card(value,suit);
         l++;
      }
   }
   cardsUsed = 0;
}

public void shuffle() {
    for ( int i = 51; i > 0; i-- ) {
        int rand = (int)(Math.random()*(i+1));
        Card temp = deck[i];
        deck[i] = deck[rand];
        deck[rand] = temp;
    }
    cardsUsed = 0;
}

public int cardsLeft() {
    return 52 - cardsUsed;
}

public Card dealCard() {
    if (cardsUsed == 52){
       shuffle();}
    cardsUsed++;
    return deck[cardsUsed - 1];

}
}



package blackjack;

import java.util.*;

public class Hand {

private ArrayList hand; 

public Hand() {
   hand = new ArrayList();
}

public void clear() {
  hand.clear();
}

public void addCard(Card c) {
  if (c != null){
     hand.add(c);
  }
}

public void removeCard(Card c) {
  hand.remove(c);
}

public void removeCard(int location) {
  if (location >= 0 && location < hand.size()){
     hand.remove(location);
  }
   }

public int getCardCount() {
  return hand.size();
}

public Card getCard(int location) {
  if (location >= 0 && location < hand.size()){
     return (Card)hand.get(location);}
  else{
     return null;
  }
 } 
public void sortBySuit() {
  ArrayList newHand = new ArrayList();
  while (hand.size() > 0) {
     int pos = 0; 
     Card c = (Card)hand.get(0); 
     for (int i = 1; i < hand.size(); i++) {
        Card c1 = (Card)hand.get(i);
        if ( c1.getSuit() < c.getSuit() ||
                (c1.getSuit() == c.getSuit() && c1.getRank() < c.getRank()) ) {
            pos = i;
            c = c1;
        }
     }
       Object remove = hand.remove(pos);
     newHand.add(c);
  }
  hand = newHand;
 }


   public int getBlackjackValue() {


     int val;      
     boolean ace;  
     int cards;    

     val = 0;
     ace = false;
     cards = getCardCount();

     for ( int i = 0;  i < cards;  i++ ) {

         Card card;     
         int cardVal;  
         card = getCard(i);
         cardVal = card.getRank();  
         if (cardVal > 10) {
             cardVal = 10;   
         }
         if (cardVal == 1) {
             ace = true;     
         }
         val = val + cardVal;
      } 

      if ( ace == true  &&  val + 10 <= 21 ){
          val = val + 10;


      }
      return val;
 }
}

It's impossible to tell because you have not shown us the relevant code. 这是不可能的,因为你没有向我们展示相关的代码。 However, if your results are not random, it's probably because Deck.dealCard() is not random. 但是,如果你的结果不是随机的,那可能是因为Deck.dealCard()不是随机的。

Update 更新

After reading the new code you posted. 阅读完您发布的新代码后。 You are not shuffling your deck after you create it. 创建它后,您不会洗牌。

这不是随机的,因为Deck::dealCard()不是随机的。

dealCard is quite strange: when a new Deck is created, cardsUsed is 0, yet you shuffle only if cardsUsed is 52. dealCard很奇怪:当创建一个新的Deck时,cardsUsed为0,但只有当cardsUsed为52时你才会洗牌。

public Card dealCard() {
    if (cardsUsed == 52){
       shuffle();}
    cardsUsed++;
    return deck[cardsUsed - 1];

}

I think you should move the call to shuffle to the constructor, right after constructing the deck. 我认为你应该在构建甲板后立即将调用移动到构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM