Calculate cardSum (Hands of Strings)

I am trying to create a Blackjack game. The game itself is pretty easy (at least my version). A player draws a card from a shuffled deck and calculates the sum of the cards which has been drawn. I have a decklist of cards with the type String and that is now a big problem for me. I have no clue how I can calculate the sum since they are of the type String. Do you have any guidelines on what I can do? The only solution I've figured out is really bad and is to compare the card with a String and give it a value. For example, 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();

    }
}

Upvotes: 0

Views: 191

Answers (2)

John Shanks
John Shanks

Reputation: 15

I couldn't see the deck list there, but if your card Strings always follow the same naming convention (i.e. "four of hearts") you could make your job a little easier by splitting each string by the " " space fields and then comparing the first word in the string to get the number (or corresponding 10 for king/queen etc..)

 String cardName = "four of hearts" (or whatever the variable name is);
   String[] parts = string.split(" ");
   String number = parts[0];

thus number would equal "four" only instead of having to compare "four of hearts" to return the number 4.

Hope that helps

Upvotes: 0

Sneh
Sneh

Reputation: 3737

Create an enum to represent 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;
    }
}

Change type of face from String to Face.

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();
    }
}

Add a method to get the face number from card class and then use it accordingly. You will need to change other parts or your project as well but I will leave that for you to do.

Also suggest using enum for Suit.

Upvotes: 2

Related Questions