2015-05-22 80 views
2

甲板找不到“.dealcard”方法和“.shuffle”方法。

import java.util.ArrayList; 
import java.util.ListIterator; 
import java.util.Random; 

public class DeckOfCards 
{ 
ArrayList deck = new ArrayList(); 
ListIterator deckIterator, tempDeckIterator; 
int suit; 
Random gen; 

// Constructor 
public DeckOfCards() 
{ 
    gen = new Random(); // use this in a later method 

    // for loop to create all cards in the deck 
    for(int i = 0; i < 52; i++) 
    { 
     if(i < 13) 
     { 
      suit = 0; 
     } 
     else if(i < 26) 
     { 
      suit = 1; 
     } 
     else if(i < 39) 
     { 
      suit = 2; 
     } 
     else 
     { 
      suit = 3; 
     } 

     // create and add the card to the deck 
     deck.add(new PlayingCard((i + 1) % 13, suit)); 
    } 

    // shuffle the deck 
    for(int i = 0; i < 7; i++) 
    { 
     shuffleDeck(); 
    } 

    //printDeck(); 
} 

public void printDeck() 
{ 
    deckIterator = deck.listIterator(); 

    while(deckIterator.hasNext()) 
    { 
     System.out.println(deckIterator.next()); 
    } 
} 

// shufle the deck of cards 
public void shuffleDeck() 
{ 
    ArrayList tempDeck = new ArrayList(); // used to hold rhe shuffled cards 
    int pos; // the postion of the randomly generated card 

    while(deck.size() > 0) 
    { 
     // Generate the card 
     pos = gen.nextInt(deck.size()); 
     tempDeck.add((PlayingCard)deck.get(pos)); 
     deck.remove(pos); 
    } 

    // put all cards back into the deck 
    tempDeckIterator = tempDeck.listIterator(); 
    while(tempDeckIterator.hasNext()) 
    { 
     deck.add((PlayingCard)tempDeckIterator.next()); // top card from temp puts it in deck 
    } 
} 
// get top card from the deck 
public PlayingCard getTopCard() 
{ 
    PlayingCard temp = (PlayingCard)deck.get(0); 
    deck.remove(0); 
    return temp; 
} 
// determine if any carsd are left int the deck 
public boolean isEmpty() 
{ 
    if(deck.size() == 0) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

}

这是我BlackJackGui类。 这个类是问题所在。 他们找不到符号“.dealcard”和“.shuffle” 是否必须更改我的甲板课程还是可以解决问题? 感谢您的帮助提前。

if (gameInProgress == false) { 
     message = "Click \"New Game\" to start a new game."; 
     repaint(); 
     return; 
     } 
     playerHand.addCard(BJDeck.dealCard()); 
     if (playerHand.getBlackjackValue() > 21) { 
     message = "You've busted! Sorry, you lose."; 
     gameInProgress = false; 
     } 
     else if (playerHand.getCardCount() == 5) { 
     message = "You win by taking 5 cards without going over 21."; 
     gameInProgress = false; 
     } 
     else { 
     message = "You have " + playerHand.getBlackjackValue() + ". Hit or Stand?"; 
     } 
     repaint(); 
    } 


    void doStand() { 
      // This method is called when the user clicks the "Stand!" button. 
      // Check whether a game is actually in progress. If it is, 
      // the game ends. The dealer takes cards until either the 
      // dealer has 5 cards or more than 16 points. Then the 
      // winner of the game is determined. 
     if (gameInProgress == false) { 
     message = "Click \"New Game\" to start a new game."; 
     repaint(); 
     return; 
     } 
     gameInProgress = false; 
     while (dealerHand.getBlackjackValue() <= 16 && dealerHand.getCardCount() < 5) 
     dealerHand.addCard(BJDeck.dealCard()); 
     if (dealerHand.getBlackjackValue() > 21) 
      message = "You win! Dealer has busted with " + dealerHand.getBlackjackValue() + "."; 
     else if (dealerHand.getCardCount() == 5) 
      message = "Sorry, you lose. Dealer took 5 cards without going over 21."; 
     else if (dealerHand.getBlackjackValue() > playerHand.getBlackjackValue()) 
      message = "Sorry, you lose, " + dealerHand.getBlackjackValue() 
              + " to " + playerHand.getBlackjackValue() + "."; 
     else if (dealerHand.getBlackjackValue() == playerHand.getBlackjackValue()) 
      message = "Sorry, you lose. Dealer wins on a tie."; 
     else 
      message = "You win, " + playerHand.getBlackjackValue() 
              + " to " + dealerHand.getBlackjackValue() + "!"; 
     repaint(); 
    } 


    void doNewGame() { 
      // Called by the constructor, and called by actionPerformed() if 
      // the use clicks the "New Game" button. Start a new game. 
      // Deal two cards to each player. The game might end right then 
      // if one of the players had blackjack. Otherwise, gameInProgress 
      // is set to true and the game begins. 
     if (gameInProgress) { 
       // If the current game is not over, it is an error to try 
       // to start a new game. 
     message = "You still have to finish this game!"; 
     repaint(); 
     return; 
     } 
     DeckOfCards BJDeck = new DeckOfCards(); // Create the deck and hands to use for this game. 
     dealerHand = new BlackjackHand(); 
     playerHand = new BlackjackHand(); 
     BJDeck.shuffle(); 
     dealerHand.addCard(BJDeck.dealCard()); // Deal two cards to each player. 
     dealerHand.addCard(BJDeck.dealCard()); 
     playerHand.addCard(BJDeck.dealCard()); 
     playerHand.addCard(BJDeck.dealCard()); 
+0

什么是'BJDeck'? –

+0

哪里是错误(行号或可以指出你的代码片段错误的地方)? – Abubakkar

+0

你确定*你确信你已经声明了'dealCard'方法或'shuffle'方法吗?仔细看看你已经*声明的名字。 –

回答

0

DeckOfCards有一个名为getTopCard()shuffleDeck(),不dealCard()shuffle()方法。

变化

BJDeck.shuffle(); 

BJDeck.shuffleDeck(); 

dealerHand.addCard(BJDeck.dealCard()); 

dealerHand.addCard(BJDeck.getTopCard());