Я пытаюсь создать игру в блэкджек.Сама игра довольно проста (по крайней мере, моя версия).Игрок берет карту из перетасованной колоды и вычисляет сумму карт, которые были разыграны.У меня есть колода карт с типом String, и теперь это большая проблема для меня.Я понятия не имею, как я могу вычислить сумму, так как они имеют тип 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();
}
}