2013-06-24 28 views
0

所以我創建了這個使用許多不同類的BlackJack Java遊戲來學習如何使用OO進行編程。然而,我真的被困在這個特定的部分,讓代碼在手中顯示正確的卡片。它只顯示一個奇怪的字符串,例如[email protected],或者具有不同的字符顯示。想知道如何將其轉換爲實際的卡片價值和等級?在BlackJack Java卡上總有一些問題ouptput

公共類套裝{

public static final int SPADE = 0, HEART = 1, DIAMOND = 2, CLUB = 3; 

/** 
* Maps the names for the suits as a string. 
*/ 
private Map<Integer, String> suitMap = new HashMap<Integer, String>(); 

/** 
* ID for the suit 
*/ 
private int suitId = 0; 

/** 
* sets the id for the suit to get. 
*/ 
public Suit(int suitId) { 
    suitMap.put(SPADE, "Spades"); 
    suitMap.put(HEART, "Hearts"); 
    suitMap.put(DIAMOND, "Diamonds"); 
    suitMap.put(CLUB, "Clubs"); 

    this.suitId = suitId; 
} 


public String toString() { 
    return suitMap.get(suitId); 
} 

/** 
* @param suitId 
* @return suitMap 
*/ 
public String getSuitNameById(int suitId) { 
    return suitMap.get(suitId); 
} 

/** 
* @return id of suit 
*/ 
public String getSuitName() { 
    return suitMap.get(suitId); 
} 

/** 
* @return the suitId 
*/ 
public int getSuitId() { 
    return suitId; 
} 

/** 
* @param suitId the suitId to set 
*/ 
public void setSuitId(int suitId) { 
    this.suitId = suitId; 
} 

/** 
* @return the suitMap 
*/ 
public Map<Integer, String> getSuitMap() { 
    return suitMap; 
} 

/** 
* @param suitMap the suitMap to set 
*/ 
public void setSuitMap(Map<Integer, String> suitMap) { 
    this.suitMap = suitMap; 
} 

}

公共類卡{

private boolean stateOfCard = false; 

/** 
* Gives the constant values to ace jack queen and king for being special 
* types of cards. 
*/ 
public final static int ACE = 1, JACK = 11, QUEEN = 12, KING = 13; 

/** 
* Maps names to the ace jack queen and king cards. 
*/ 
private Map<Integer, String> nameMap = new HashMap<Integer, String>(); 

/** 
* Rank or type of card (2-9, J,Q,K, A) 
*/ 
private String cardRank = null; 

/** 
* Card suit 
*/ 
private Suit suit = null; 

/** 
* Numeric value of the card 
*/ 
private int cardValue = 0; 

/** 
* Gives you the suit, cardRank, and the cardValue 
*/ 
public Card(Suit suit, String cardRank, int cardValue) { 
    nameMap.put(ACE, "Ace"); 
    nameMap.put(JACK, "Jack"); 
    nameMap.put(QUEEN, "Queen"); 
    nameMap.put(KING, "King"); 

    if (cardValue >= 11 || cardValue == 1) cardRank = nameMap.get(cardValue); 

    this.suit = suit; 
    this.cardRank = cardRank; 
    this.cardValue = cardValue; 
} 
/** 
* Displays to user the rank and suit of the card. 
*/ 
public String toString() { 
    return cardRank + " of " + suit.getSuitName(); 
} 

/** 
* @return the cardRank 
*/ 
public String getCardRank() { 
    return cardRank; 
} 

/** 
* @param cardRank the cardRank to set 
*/ 
public void setCardRank(String cardRank) { 
    this.cardRank = cardRank; 
} 

/** 
* @return the suit 
*/ 
public Suit getSuit() { 
    return suit; 
} 

/** 
* @param suit the suit to set 
*/ 
public void setSuit(Suit suit) { 
    this.suit = suit; 
} 

/** 
* @return the cardValue 
*/ 
public int getCardValue() { 
    return cardValue; 
} 

/** 
* @param cardValue the cardValue to set 
*/ 
public void setCardValue(int cardValue) { 
    this.cardValue = cardValue; 
} 

/** 
* @return the nameMap 
*/ 
public Map<Integer, String> getNameMap() { 
    return nameMap; 
} 

/** 
* @param nameMap the nameMap to set 
*/ 
public void setNameMap(Map<Integer, String> nameMap) { 
    this.nameMap = nameMap; 
} 
/** 
* @return the stateOfCard 
*/ 
public boolean isStateOfCard() { 
    return stateOfCard; 
} 

/** 
* @param stateOfCard the stateOfCard to set 
*/ 
public void setStateOfCard(boolean stateOfCard) { 
    this.stateOfCard = stateOfCard; 
} 

}

公共類甲板{

/** 
* Deck of cards created 
*/ 
private ArrayList<Card> deck = new ArrayList<Card>(); 

/** 
* Keeps track of the cards that are dealt and no longer available 
*/ 
private List<Card> cardUsed = new ArrayList<Card>(); 

/** 
* The array of cards in a set. Can be shuffled and drawn from. Deck of any #. 
* @param cards 
*/ 
public Deck(int numCards) { 
    this.createDeck(numCards, 4); 
} 

/** 
* creates a deck that has cards in it with 4 suits of each 13 cards. 
* @param numCards 
* @param numSuits 
*/ 
private void createDeck(int numCards, int numSuits) { 
    deck = new ArrayList<Card>(); 
    cardUsed = new ArrayList<Card>(); 
    if ((numCards % numSuits) > 0) return; 
    for (int i=0; i < numSuits; i++) { 

     for(int j=1; j <= (numCards/numSuits); j++) { 
      deck.add(new Card(new Suit(i), j + "", j)); 
     } 
    } 
} 

/** 
* Deals a card to the hand. Sends dealt card to the cardUsed list so that 
* a used card will not be drawn again unless deck is shuffled. 
* @return dealtCard 
*/ 
public Card dealCard() { 

    Card dealtCard = null; 
    if (deck.size() == 0){ 
     deck.addAll(cardUsed); 
     this.shuffle(); 
     cardUsed = new ArrayList<Card>(); 
    } 

    dealtCard = deck.get(0); 
    deck.remove(0); 
    cardUsed.add(dealtCard); 

    return dealtCard; 
} 

/** 
* Shuffles the cards after every round. 
*/ 
public void shuffle() { 
    Collections.shuffle(deck); 
} 

/** 
* @return the deck 
*/ 
public ArrayList<Card> getDeck() { 
    return deck; 
} 

/** 
* @param deck the deck to set 
*/ 
public void setDeck(ArrayList<Card> deck) { 
    this.deck = deck; 
} 

/** 
* Returns the number of used cards 
* @return 
*/ 
public int getNumUsedCards() { 
    return cardUsed.size(); 
} 

/** 
* @return the cardUsed 
*/ 
public List<Card> getCardUsed() { 
    return cardUsed; 
} 

/** 
* @param cardUsed the cardUsed to set 
*/ 
public void setCardUsed(List<Card> cardUsed) { 
    this.cardUsed = cardUsed; 
} 

}

公共類手{

private ArrayList<Card> hand = new ArrayList<Card>(); 
int total = 0; 

/** 
* A list of cards that can be defined as hand. Player has a list of these 
* cards in a hand. 
*/ 
public Hand(){ 

} 

public boolean isAce() { 


    return false; 
} 

public boolean discardHand(){ 
    if (hand.isEmpty()) { 
     hand = new ArrayList<Card>(); 
    } 
    return false; 
} 
/** 
* Adds the card to the hand in a list of cards. 
* @param c 
*/ 
public void addCard(Card c) { 
    this.hand.add(c); 
} 

/** 
* Gets the place value of the card in the hand. 
* @param index 
* @return index of card in hand 
*/ 
public Card getPosition(int index){ 
    return hand.get(index); 
} 

/** 
* Gets how many cards are in the player's hand. 
* @return hand size 
*/ 
public int handCardCount(){ 
    return hand.size(); 
} 

/** 
* @return the total 
*/ 
public int getTotal() { 
    return total; 
} 

/** 
* @param total the total to set 
*/ 
public void setTotal(int total) { 
    this.total = total; 
} 

/** 
* @return the hand 
*/ 
public ArrayList<Card> getHand() { 
    return hand; 
} 

/** 
* @param hand the hand to set 
*/ 
public void setHand(ArrayList<Card> hand) { 
    this.hand = hand; 
} 

}

公共類播放器擴展人{

private Hand myCards = new Hand(); 
private int playerCashAmount = 100; 

public Player() { 

} 

public Player(String name) { 
    this.name = name; 
} 

/** 
* Gets the name of the user from the parent class 
*/ 
public String getName() { 
    return super.getName(); 
} 


/** 
* @return the playerCashAmount 
*/ 
public int getPlayerCashAmount() { 
    return playerCashAmount; 
} 

/** 
* @param playerCashAmount the playerCashAmount to set 
*/ 
public void setPlayerCashAmount(int playerCashAmount) { 
    this.playerCashAmount = playerCashAmount; 
} 

/** 
* @return the myCards 
*/ 
public Hand getMyCards() { 
    return myCards; 
} 

/** 
* @param myCards the myCards to set 
*/ 
public void setMyCards(Hand myCards) { 
    this.myCards = myCards; 
} 

}

公共類經銷商擴展人{

private String dealCards = null; 
private String playCards = null; 
private String shuffleDeck = null; 
private Hand computerCards = new Hand(); 

/** 
* 
*/ 
public Dealer() { 

} 

/** 
* @return the dealCards 
*/ 
public String getDealCards() { 
    return dealCards; 
} 

/** 
* @param dealCards the dealCards to set 
*/ 
public void setDealCards(String dealCards) { 
    this.dealCards = dealCards; 
} 

/** 
* @return the playCards 
*/ 
public String getPlayCards() { 
    return playCards; 
} 

/** 
* @param playCards the playCards to set 
*/ 
public void setPlayCards(String playCards) { 
    this.playCards = playCards; 
} 

/** 
* @return the shuffleDeck 
*/ 
public String getShuffleDeck() { 
    return shuffleDeck; 
} 

/** 
* @param shuffleDeck the shuffleDeck to set 
*/ 
public void setShuffleDeck(String shuffleDeck) { 
    this.shuffleDeck = shuffleDeck; 
} 

/** 
* @return the computerCards 
*/ 
public Hand getComputerCards() { 
    return computerCards; 
} 

/** 
* @param computerCards the computerCards to set 
*/ 
public void setComputerCards(Hand computerCards) { 
    this.computerCards = computerCards; 
} 

}

公共類Person {

保護的字符串名稱= NULL; 手h =新手();

/** 
* 
*/ 
public Person() { 

} 

/** 
* @return the name 
*/ 
public String getName() { 
    return name; 
} 

/** 
* @param name the name to set 
*/ 
public void setName(String name) { 
    this.name = name; 
} 

/** 
* @return the h 
*/ 
public Hand getH() { 
    return h; 
} 

/** 
* @param h the h to set 
*/ 
public void setH(Hand h) { 
    this.h = h; 
} 

}

公共類GameEngine {

static Scanner in = new Scanner(System.in); 
private Dealer dealer = null; 
List<Player> players = new ArrayList<Player>(); 
Hand h = new Hand(); 
Person p = new Player(); 
Player pl = new Player(); 
int bet = 0; 
Deck d = null; 
int num = 0; 

public GameEngine() { 

} 

/** 
* Plays the game by creating a new instance of the gameplay class. 
* @param args 
*/ 
public static void main(String[] args) { 
    GameEngine ge = new GameEngine(); 
    ge.gamePlay(); 
} 

/** 
* Game of BlackJack, runs all the methods that it will call to. 
*/ 
public void gamePlay() { 
    // States the title of the game. 
    //this.welcomeToGame(); 

    // Shuffles the deck and creates the number of cards for it. 
    this.deckStart(); 

    // Prompts user for number of players. 
    //this.howManyPlayers(num); 

    // Displays welcome message and asks for user's name. 
    //this.greetUser(); 

    // Deal initial cards 
    this.beginCardDeal(); 

    // Place bet on the current round. 
    //this.betAmount(pl, bet); 

    // Evaluate, get player choice (stay or hit or bust) 
    //this.evaluatePlayer(); 

    // Evaluate if bust. 
    //this.isBust(); 

    // Deal more cards to each user. 
    //this.takeHitOrStay(); 

    // All players bust/stay. 


    // Evaluate winner. 


} 

/** 
* Initializes the deck and shuffles it. 
*/ 
public void deckStart() { 
    d = new Deck(52); 
    d.shuffle(); 
} 

/** 
* Displays welcome message to the player. 
*/ 
public void welcomeToGame(){ 
    System.out.println("This is BlackJack (Double Exposure)\n\n"); 
} 

/** 
* Asks for name input and then welcomes them with name. 
*/ 
public void greetUser() { 
    System.out.print("Enter your name: "); 
    p.setName(in.next()); 
    System.out.println("Welcome, " + p.getName() + "."); 
} 

/** 
* Prompts user to input how many opponents they would like to face in the game of BlackJack. 
* @param num 
*/ 
public void howManyPlayers(int num) { 
    players = new ArrayList<Player>(); 
    System.out.print("How many other players would you like for this game? Enter here: "); 
    num = in.nextInt(); 
    for (int i=0; i < num; i++) { 
     players.add(new Player("Player " + i)); 
     System.out.println(players); 
    } 
} 

/** 
* Asks the player how much money they will bet on the hand. 
* @param pl 
* @param bet 
*/ 
public void betAmount(Player pl, int bet) { 
    while (bet == 0 || bet > pl.getPlayerCashAmount()) { 
     System.out.print("You have $" + pl.getPlayerCashAmount() + " as of now. Please enter a bet amount: "); 
     bet = in.nextInt(); 
     System.out.println("You are going to bet $" + bet + "."); 
     if (bet < 0 || bet > pl.getPlayerCashAmount()) 
      System.out.println("\nYour answer must be between $0 and $" + pl.getPlayerCashAmount()); 
     else if (bet == pl.getPlayerCashAmount()) 
      System.out.println("All in!"); 
    } 
} 

/** 
* Deals the cards to each of the players. 
*/ 
public void beginCardDeal() { 
    for (int x = 0; x < 2; x++) { 
     System.out.print("\nDealer is dealing a card. . . \n"); 
     d.dealCard(); 
     System.out.println("\nYour hand: " + pl.getMyCards()); 
     //System.out.println("New hand value test::: " + pl.getMyCards().addCard(d.dealCard())); 
    } 
} 

/** 
* Checks to see if the player's hand value is over 21. If yes then player 
* loses that round. 
*/ 
public void isBust(){ 
    if (h.getTotal() > 21) { 
     System.out.println("Sorry, you have gone over 21 and busted."); 
     int money = pl.getPlayerCashAmount() - bet; 
     System.out.println("You lost $"+bet+", and now you have "+money); 
    } 
} 

/** 
* Counts the total value of cards each player holds in their hand. 
*/ 
public void evaluatePlayer(){ 
    System.out.println("Your hand value is: "); 
    System.out.println(pl.getMyCards().getTotal()); 

} 

public String takeHitOrStay(){ 
    while (pl.getMyCards().getTotal() < 21){ 
     System.out.println("Would you like to hit[h] or stay[s]? "); 
     String choice = in.next(); 
     if (choice.equalsIgnoreCase("h")){ 
      pl.getMyCards(); 
      //System.out.println(pl.getMyCards()); 
      if (choice.equalsIgnoreCase("s")){ 
       System.out.println("Null"); 
       //finish this. 
      } 
     } 
     return choice; 
    } 
    return null; 
} 

/** 
* @return the deck 
*/ 
public Deck getDeck() { 
    return d; 
} 

/** 
* @param deck the deck to set 
*/ 
public void setDeck(Deck deck) { 
    this.d = deck; 
} 

/** 
* @return the player 
*/ 
public Player getPlayer() { 
    return pl; 
} 

/** 
* @param player the player to set 
*/ 
public void setPlayer(Player player) { 
    this.pl = player; 
} 

/** 
* @return the dealer 
*/ 
public Dealer getDealer() { 
    return dealer; 
} 

/** 
* @param dealer the dealer to set 
*/ 
public void setDealer(Dealer dealer) { 
    this.dealer = dealer; 
} 

}

我遇到的新問題是,我得到的結果是,現在空黑桃或0黑桃作爲輸出。我在Hand類中添加了toString()函數。

public String toString(){ 
    return c.getCardValue() + " of " + s.getSuitName(); 
} 

所以這已被添加到手,並清除了我的第一個問題,只獲得每個輸出的哈希碼。關於爲什麼我將黑桃作爲輸出項的任何想法?

+0

你需要一個'的toString ()''手'的方法。而且,這比需要的複雜10倍多。很高興你避免了菜鳥程序員使用字符串代替整數來表示卡牌等級和套裝的最常見的錯誤。但是當你使用HashMap來獲取字符串時,它可能是一個普通的數組。您有不同名稱的多個重複方法 - 這是不可維護性的一個方法。卡有成員的西裝和等級 - 這很好 - 但'cardValue'應該是什麼?一張牌是一套西裝和一個等級,就是這樣。再次,一個簡單的排名名稱數組。 –

+0

我打算使用cardValue來獲得它在21點遊戲中給你的點數。例如,一個國王的等級爲13,X的套裝,並且在BlackJack中的卡片價值爲10.讓我知道在我的思維方面是否有缺陷,因爲我仍在努力處理返回正確值的代碼。 – DarkZal

回答

0

您已在多個課程上覆蓋toString,但尚未在Hand課程上覆蓋。覆蓋Hand上的toString以提供您自己的自定義字符串轉換,否則您將獲得Object執行toString,這將使用類名稱,@符號和對象的地址。

引述的Javadoc:Object類

toString方法返回由 其中物體是一個實例,所述-SIGN 字符`的類的名稱的字符串@」,以及該對象的散列 代碼的無符號十六進制表示。換句話說,該方法返回一個字符串等於 ,它的值:

的getClass()的getName()+ '@' + Integer.toHexString(hashCode()方法)

+0

謝謝!我怎麼在我的課堂上覆蓋它,所以輸出是卡?不太確定,因爲我一個月前纔開始學習Java。 – DarkZal

+0

將'Hand'的'Cards'連接成'String'。 Java會在任何被連接成String的對象上調用'toString()'。所以實際上,你的'Hand'' toString()'實現將依次調用'''''toString()'(隱式地)。 – rgettman

+0

非常感謝。我被困在那裏一天半了。我很感激幫助。 – DarkZal