【发布时间】:2019-03-02 02:26:30
【问题描述】:
我正在尝试创建二十一点游戏。游戏本身很简单(至少我的版本)。玩家从洗好的牌堆中抽一张牌,并计算已抽出的牌的总和。我有一个 String 类型的卡片组列表,现在这对我来说是个大问题。我不知道如何计算总和,因为它们是字符串类型。你对我能做什么有任何指导吗?我发现的唯一解决方案非常糟糕,是将卡片与字符串进行比较并给它一个值。比如drawnCard.equals("Four of hearts") = "4";
public class Player {
private String nickName;
private int playerNumOfCards;
ArrayList<Card> playerHand = new ArrayList<>();
public Player (String name){
this.nickName = name;
}
public String getNickName() {
return nickName;
}
public void addCard(Card aCard){
playerHand.add(aCard);
this.playerNumOfCards++;
}
public void getHandSum(){
}
public void getPlayerHand(){
for(Card cards: playerHand){
System.out.println(cards.toString());
}
}
}
public class DeckOfCards {
private Card[] deck;
private static final Random random = new Random();
private int currentCard; //index of next Card to be deal (0-51)
private static int NUMBER_OF_CARDS = 52; //Constant number of cards
public DeckOfCards(){
String [] faces = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack","Queen", "King"};
String [] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
deck = new Card [NUMBER_OF_CARDS]; // create array with Cards (52)
currentCard = 0;
//Populate deck with Cards
for(int count = 0; count < deck.length; count++)
deck [count] = new Card(faces [count % 13], suits [count / 13]);
}
public void shuffleDeck(){
currentCard = 0;
for (int first = 0; first < deck.length; first++){
int second = random.nextInt(NUMBER_OF_CARDS); //Select a random card from number 0-51 (Number_of_cards)
//Loops through all the cards and swaps it with the "Second" card which is randomly chosen card from hte same list.
Card temp = deck[first];
deck [first] = deck [second];
deck [second] = temp;
}
}
public void getCardDeck(){
int start = 1;
for(Card k : deck) {
System.out.println("" + start + "/52 " + k);
start++;
}
}
public Card dealNextCard(){
//Get the top card
Card topCard = this.deck[0];
//shift all the subsequent cards to the left by one index
for(int currentCard = 1; currentCard < NUMBER_OF_CARDS; currentCard ++){
this.deck[currentCard-1] = this.deck[currentCard];
}
this.deck[NUMBER_OF_CARDS-1] = null;
//decrement the number of cards in our deck
this.NUMBER_OF_CARDS--;
return topCard;
}
}
public class Card {
private String face; //Face of card, i.e "King" & "Queen"
private String suit; //Suit of card, i.e "Hearts" & "diamonds"
public Card (String cardFace, String cardSuit){ //Constructor which initializes card's face and suit
this.face = cardFace;
this.suit = cardSuit;
}
public String toString(){ //return String representation of Card
return face + " of " + suit;
}
}
public class BlackJackGame {
public static void main(String[] args) {
DeckOfCards deck1 = new DeckOfCards();
Player player1 = new Player("mille");
deck1.shuffleDeck();
}
}
【问题讨论】:
-
这个问题的答案将取决于你打算用抽到的牌做什么。如果你想有多个参赛者,那么考虑实施一个“手”类来评估它自己的重量也许是一个更好的主意。手类也应该很容易与其他手类进行比较。考虑从牌库中取出抽到的牌,然后将它们加入玩家的手牌。
-
字符串是为人设计的——计算机用数字做事。如果您将卡片表示为数字,并且只转换为字符串进行输出,您将省去很多麻烦,并使您的代码更小、更快。
标签: java arrays arraylist blackjack