0

"twist" is presumably something roughly like
Card newCard = deck.deal(1);
so you will have a reference to the new card before youadd that to your hand.
If you have a current tota; value then you can update that at the same time
totalValue += newCard.getRank().getValue();
userList.add(newCard);

You can simplify this by adding a getValue method in the Card class that simply retruns this.rank.getValue(), then you can jus write
totalValue += newCard.getValue();

I don't think the "current card" variable is useful - if you really do need it then it's just the last card in the list.

Aces being 1 or 11 is always going to be messy to code. You may not need to store 1 or 11 because the user is free to change their mind and opt for 1 or 11 whenever they want.

YOu currently have one class for user and dealer and end up with duplicated variables etc. I suggest one class (with just one list variable) then create a Dealer subclass for any special logic that applies to dealers only.

0

Hey, I implemented this at some god forsaken hour this morning:

Store cardCount
+1 cardCount everytime card added
CalcCard called every time add card is called
Reference cardCount when calculating card for most recent card
add to u/dValue accordingly.

Deck.Java

    private int userCardCount = -1;
    private int dealerCardCount = -1;
    private int userDecision;


    public void addCardUser() {
        user.userList.add(deckList.get(0));
        deckList.remove(0);
        userCardCount++;
        calcUserValue();

    }
    public void addCardDealer() {
        dealer.dealerList.add(deckList.get(0));
        deckList.remove(0);
        dealerCardCount++;
        calcDealerValue();
    }

    public int calcUserValue() {
        if(user.getUserList().get(userCardCount).getRank() ==  Rank.Ace) {

            System.out.println("You Have Recieved An Ace! What Would You Like To Do?");
            System.out.println("[1] + 1");
            System.out.println("[2] + 11");



            userDecision = kb.nextInt();

            if(userDecision == 1) {
                userValue += 1;
                return userValue;
            } else if (userDecision == 2) {
                userValue += 11;
                return userValue;
            }

        } else {
            userValue += user.getUserList().get(userCardCount).getRank().getValue();

            return userValue;
        }

        return userValue;


    }
    public int calcDealerValue() {
        if(dealer.getDealerList().get(dealerCardCount).getRank() ==  Rank.Ace) {

            if(dealerValue > 10) {
                dealerValue += 1;
                return dealerValue;
            } else if (dealerValue <= 10) {
                dealerValue += 11;
                return dealerValue;
            }

        } else {
            dealerValue += dealer.getDealerList().get(dealerCardCount).getRank().getValue();

            return dealerValue;
        }

        return dealerValue;

    }

For all intents and purposes this works, but is it abiding by OOP and general coding standards?

1

Where is that code? Most of it related to things that a Player does, so they should be inside the Player class. That way you avoid all those complicated user. and dealer. calls
If you make ordinary users and dealers part of the same class structure, like I suggested earlier, you can have one set of code without all the duplication between dealers and users (variables and code) that you have now.

Votes + Comments
Thank you, changing the structure now.
0

Hey guys, been away for a while and have not had time to code!

I ran into this problem:
I cannot get my Player class to interact with the deck. How do I call upon Deck.java's functions?

Here is my code thus far: (excluding card/rank/suit java as they're not needed for my problem)

/*
 * Imports
 */
import java.util.*;

public class BlackJack{

    BlackJack() {       
        /*
         * Variables
         */
        boolean twisting = true;
        int userDecision = 0;
        Scanner kb = new Scanner(System.in);
        /*
         * Welcome Message
         */

        System.out.println(" <><><><><><><><><><><><><><><><>");
        System.out.println("|\tWelcome To BlackJack!\t  | ");
        System.out.println(" <><><><><><><><><><><><><><><><>");

        /*
         * Create New Deck
         * 52 Randomised Cards
         */
        Deck deck =  new Deck();

        /*
         * Create User && Dealer
         */
        Player user = new Player();
        Player dealer = new Player();


        /*
         * Initial Gameplay
         * Add Two User Cards
         * Add One Dealer Card
         */
        user.addCardUser();
        user.addCardUser();

        dealer.addCardDealer();

        /*
         * Print Initial Cards
         */
        System.out.println("The Dealer Has: " + dealer.getDealerList());
        System.out.println("You Have: " + user.getUserList());
        System.out.println("User Value: " + user.getUserValue());
        System.out.println("Dealer Value: " + dealer.getDealerValue());

        /*
         * Act On  User Input Stick or Twist
         */
        do {
            /*
             * Continually Checks
             * For BlackJack && Busts
             * Each Loop
             */
            user.checkBlackJack();
            user.checkBust();

            System.out.println("Would You Like To: ");
            System.out.println("[1] Stick");
            System.out.println("[2] Twist");

            userDecision = kb.nextInt();

            switch(userDecision) {
            case 1:
                /*
                 * Dealers Second Card Added
                 */
                System.out.println("Adding Dealers Card: ");
                dealer.addCardDealer();

                do {
                    /*
                     * User Sticks
                     * Dealer list && value printed
                     * Dealer check bust
                     * Dealer CheckBlackJack
                     * Calculate Dealers Decision
                     * Act Upon Decision
                     */
                    twisting = false;
                    System.out.println("Dealer Has: " + dealer.dealerList + "\n");
                    System.out.println("Dealer's Value: " + dealer.getDealerValue() + "\n");
                    dealer.dealersDecision();   
                }while(dealer.getDealerValue() < 16);

                break;

            case 2:
                /*
                 * User Twists
                 * Card Added
                 * Get User Value
                 */
                user.addCardUser();
                System.out.println("You Have: " + user.getUserList());
                System.out.println("User Value: " + user.getUserValue());
                twisting = true;
                break;
            }

        }while(twisting == true);

        /*
         * Compares The Two Values Once Loops Complete
         * Used As A Fail Safe, The Program Should Exit Before This Happens.
         */
        user.compareValues();
    }
    /*
     * Main Method
     * Creates New Object of BlackJack 
     * To Run Outside of Static
     */
    public static void main(String[] args) {
        new BlackJack();
    }
}




import java.util.ArrayList;
import java.util.Scanner;

public class Player {


    /*
     * Variables
     */
    private int userValue;
    private int dealerValue;
    private int userCardCount = -1;
    private int dealerCardCount = -1;
    private int userDecision;

    Scanner kb = new Scanner(System.in);


    /*
     * ArrayLists To Hold Cards
     */
    public ArrayList<Card> userList = new ArrayList<Card>();
    public ArrayList<Card> dealerList = new ArrayList<Card>();

    /*
     * Constructor
     */
    Player() {
    }

    ///////////////////////////////////////USER\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    /*
     * Add Card Dealer
     */
    public void addCardUser() {

        userList.add(getDeckList().get(0));//Retrieves the first card in in the deck list
        deckList.remove(0);//Removes The Card Retrieved
        userCardCount++;//Increases Card Count Used for Calculations
        calcUserValue();
    }
    /*
     * Get User Value
     */
    public int getUserValue() {
        return userValue;
    }
    /*
     * Calculate User Value
     */
    public int calcUserValue() {
        /*
         * If The New Card Added Is An Ace:
         * Print Dialogue Showing Ace Has Been Found
         * If The User Types: 1
         * Take ace as 1
         * If the User Types: 11
         * Take as as 11
         */
        if(getUserList().get(userCardCount).getRank() ==  Rank.Ace) {

            System.out.println("You Have Recieved An Ace! What Would You Like To Do?");
            System.out.println("[1] + 1");
            System.out.println("[2] + 11");

            userDecision = kb.nextInt();

            if(userDecision == 1) {
                userValue += 1;
                return userValue;
            } else if (userDecision == 2) {
                userValue += 11;
                return userValue;
            }
        } else {
            userValue += getUserList().get(userCardCount).getRank().getValue();

            return userValue;
        }
        return userValue;
    }

    public ArrayList<Card> getUserList() {
        return userList;
    }


    ///////////////////////////////////////DEALER\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

    /*
     * Add Card Dealer
     */
    public void addCardDealer() {
        dealerList.add(getDeckList().get(0));//Retrieves the first card in the deck list
        deckList.remove(0);//Removes The Card Retrieved
        dealerCardCount++;//Inreases Card Count Used For Calculations
        calcDealerValue();
    }
    /*
     * Get Dealer Value
     */
    public int getDealerValue() {
        return dealerValue;
    }

    public ArrayList<Card> getDealerList() {
        return dealerList;
    }

    /*
     * Calc Value Dealer
     */
    /*
     * If The New Dealer Card Added Is An Ace:
     * If The Dealer Value Is < 10 Use 11
     * If The Dealer Value Is >= 10 Use 1
     */
    public int calcDealerValue() {
        if(getDeckList().get(dealerCardCount).getRank() ==  Rank.Ace) {

            if(dealerValue > 10) {
                dealerValue += 1;
                return dealerValue;
            } else if (dealerValue <= 10) {
                dealerValue += 11;
                return dealerValue;
            }

        } else {
            dealerValue += getDeckList().get(dealerCardCount).getRank().getValue();

            return dealerValue;
        }

        return dealerValue;

    }



    //////////////////////////////COMPARISONS && CHECKS\\\\\\\\\\\\\\\\\\\\\\\\\\\\

    public void checkBust() {
        //USER BUST CHECK
        if(getUserValue() > 21) {
            System.out.println("You Bust With The Value: "  + getUserValue() + "\nYou Lose!");
            System.exit(0);

        }

        //DEALER BUST CHECK
        if(getDealerValue() > 21) {
            System.out.println("Dealer Bust With The Value: " + getDealerValue() +  "\n and the cards: " +
                    getDeckList() + "'\nYou Win!");
            System.exit(0);
        }
    }

    public void checkBlackJack() {
        //USER BLACKJACK
        if(getUserValue() == 21) {
            System.out.println("You Have BlackJack!");          
        }

        //DEALER BLACKJACK
        if(getDealerValue() == 21) {
            System.out.println("Dealer Has BlackJack!");
            compareValues();
        }


    }

    public void compareValues() {
        //USER LESS THAN DEALER
        if(getUserValue() < getDealerValue()) {
            System.out.println("Dealer Wins!\nYour Value: " + getUserValue() + "\nDealer Value: " + getDealerValue());
            System.exit(0);
        }
        //USER GREATER THAN DEALER
        if(getUserValue() > getDealerValue()) {
            System.out.println("You Win!\nYour Value: " + getUserValue() + "\nDealer Value: " + getDealerValue());
            System.exit(0);
        }

        //DRAW
        if(getUserValue() == getDealerValue()) {
            System.out.println("Draw!!\nYour Value: " + getUserValue() + "\nDealer Value: " + getDealerValue());
            System.exit(0);
        }
    }

    public void dealersDecision() {

        //LESS THAN 16 DRAW
        if (getDealerValue() <=16) {
            System.out.println("Dealer's Value Is Less Than 16! Adding Another Card!");
            addCardDealer();
        }
        //== 17 STICK
        if (getDealerValue() == 17) {
            System.out.println("Dealer Has: 17 - Sticking!");
            compareValues();
        }

        if(getDealerValue() > 17 &&  getDealerValue()<= 21) {
            System.out.println("Dealer Has: " + getDealerValue() + " - Sticking!");
            compareValues();
        }

        checkBust();
    }

}




/*
 * Imports
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Deck {
    /*
     * Variables
     */
    Scanner kb = new Scanner(System.in);
    ArrayList<Card> deckList = new ArrayList<Card>();


    /*
     * Constructor - Executed Upon Load of Deck.Java
     * Create Shuffled Deck
     */
    Deck() {    
        /*
         * New Array List Formed
         */
        deckList = new ArrayList<Card>();
        /*
         * For Loop Through Enum: Suit and retrieves Suit.values() each Loop
         * For Loop Through Enum: Rank and retrieves Rank.values() each loop
         * Adds each object of Card taken from rank and suit to the deckList
         */
        for (Suit suit : Suit.values()) {
            for (Rank rank : Rank.values()) {   
                deckList.add(new Card(rank, suit)); 
            }
        }
        Collections.shuffle(deckList);
    }

    public ArrayList<Card> getDeckList() {
        return deckList;
    }
}

Edited by speakon

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.