1

Java を学ぶためにこのプログラムを作成しましたが、実行するたびに同じカードが何度も出てきます。問題が本当に見つからないのに、カードがランダムにならないのはなぜですか? 何が欠けているのかよくわかりません。シャッフル方法が間違っていますか?

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;
 }
}
4

3 に答える 3

3

関連するコードを示していないため、わかりません。ただし、結果がランダムでない場合は、おそらくDeck.dealCard()がランダムではないことが原因です。

アップデート

投稿した新しいコードを読んだ後。作成後にデッキをシャッフルしていません。

于 2013-02-07T23:48:25.103 に答える
0

DealCardは非常に奇妙です。新しいデッキが作成されると、cardsUsedは0ですが、cardsUsedが52の場合にのみシャッフルします。

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

}

デッキを構築した直後に、シャッフルの呼び出しをコンストラクターに移動する必要があると思います。

于 2013-02-08T00:01:00.100 に答える
0

ランダムでDeck::dealCard()はないので、ランダムではありません。

于 2013-02-07T23:45:14.123 に答える