java - 计算 cardSum(字符串的手)
问题描述
我正在尝试创建二十一点游戏。游戏本身很简单(至少我的版本)。玩家从洗好的牌组中抽出一张牌并计算已抽出的牌的总和。我有一个字符串类型的卡片组列表,现在这对我来说是个大问题。我不知道如何计算总和,因为它们是字符串类型。你对我能做什么有任何指导吗?我发现的唯一解决方案非常糟糕,是将卡片与字符串进行比较并给它一个值。例如,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();
}
}
解决方案
创建一个枚举来表示 Face
enum Face {
Ace(number), //I don't know what number is for Ace and others.
Deuce(number),
Three(3),
Four(4),
Five(5),
Six(6),
Seven(7),
Eight(8),
Nine(9),
Ten(10),
Jack(number),
Queen(number),
King(number);
private final int number;
Faces(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
}
将面的类型从字符串更改为面。
class Card {
private Face face;
private String suit;
public Card (Face cardFace, String cardSuit){card's face and suit
this.face = cardFace;
this.suit = cardSuit;
}
public String toString(){
return face + " of " + suit;
}
public int getNumber() {
return face.getNumber();
}
}
添加一个从卡片类中获取人脸号码的方法,然后相应地使用它。您还需要更改其他部分或您的项目,但我会留给您去做。
还建议对 Suit 使用枚举。
推荐阅读
- kotlin - 如何使用组合函数压缩超过 8 个单声道
- sql - 连接表和临时表时执行计划中的常量扫描和计算标量
- react-native - 弹出 React Native 项目时,Bare 和 ExpoKit 有什么区别?
- android - Jetpack 撰写新源无法正常工作
- haskell - 共享一个无点函数,但评估两次
- java - Spring Cloud Kubernetes 无法设置 ConfigMap 值
- r - 使用基于 ID 列的一列的串联可能值创建一列
- javascript - 如何从 vuejs 中的对象数组中跟踪更改的值
- javascript - 如何用对象中的键对数组进行分组?
- sql - 如何在此查询中打印学生姓名?