2013-06-24 29 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