1
\$\begingroup\$

I've been learning java for about a year in University and decided to start this Blackjack game which I can hopefully animate into a full app in the future, although I'm still working on the functionality atm.

The game functions as it should for the most part, but I still can't figure out how can I implement the Ace having two values (1/11), right now it just has the value of 1. I was thinking of adding an - if statement in the player.getSum() where it distinguishes the ace and gives it a value of 11, but I can't get it to work properly. I can submit my test-code if that helps.

The last question I have is about splitting. In blackjack you can split the cards in order to double your profit. Even though there is no in-game currency atm i'd like to implement that.

Suit.java

public enum Suit{

   Hearts(), 
   Spades(), 
   Diamonds(),
   Clubs(); 
}

Face.java

public enum Face
{

Ace(11), Deuce (2), Three (3),
Four(4), Five(5), Six(6),
Seven(7), Eight(8), Nine(9),
Ten(10), Jack(10), Queen(10),
King(10);

private final int faceValue;

private Face(int faceValue)
{
    this.faceValue = faceValue;
}

public int getValue()
{
    return faceValue;
}

public static void main(String[] args) {
}
}

Card.java

public class Card {

private Suit suit;
private Face face;

public Card(Face cardFace, Suit cardSuit)
{
    this.face = cardFace;
    this.suit = cardSuit;
}

public Face getFace()
{
    return face;
}

public Suit getSuit()
{
    return suit;
}

public String toString()
{
    return face + " of " + suit;
}
}

Deck.java

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

public class Deck 
{
private final ArrayList<Card> cards;

public Deck()
{
    cards = new ArrayList<Card>();
    for (Suit suit : Suit.values())
    {
        for (Face face : Face.values())
        {
            cards.add(new Card(face,suit));
        }
    }
}

public void shuffle()
{
    Collections.shuffle(cards);
}

public Card draw()
{
    return cards.remove(0);
}

public String showDeck()
{
    String remainingCards = "";
    for (Card card : cards)
    {
        remainingCards += card.toString() + "\n";
    }
    return remainingCards;
}
}

Player.java

import java.util.ArrayList;

public class Player 
{
private final ArrayList<Card> hand;
private final String name;

public Player(String name)
{
    this.name = name;
    hand = new ArrayList<>();
}

public String getName() {
    return name;
}

public String giveCard(Card card)
{
    hand.add(card);
    return card.toString();
}

public int getSum()
{
    int numOfAces = 0;
    int handSum = 0;
    for (Card card : hand)
    {
        if (card.getFace() == Face.Ace)
        {
            numOfAces ++;
        }
            handSum += card.getFace().getValue();
    }

    //Checking Aces
    if (handSum > 21 && numOfAces == 1)
    {
        handSum -= 10;
    }
    else if (handSum > 21 && numOfAces > 1)
    {
        handSum -= 10 * (numOfAces - 1);
    }
    return handSum;
}

public String showHand(Boolean cheat)
{
        String playerHand = "";
        for (Card card : hand)
        {
                playerHand += "\n" + card.toString();
        }
        if (cheat == true)
        {
            playerHand += "\n" + ("(" + this.getSum() + ")");
        }
        return this.name + ": " + playerHand;
}

public String checkCheat(Boolean bool)
{
    String sum = "";
    if (bool == true)
    {
       sum =  ("(" + this.getSum() + ")") ;
    }
    return sum;
}

}

Blackjack.java

import java.util.Scanner;

public class Blackjack
{
public static void main(String[] args) {
    String nickname ;
    Scanner scanner = new Scanner(System.in);
    Boolean cheatOn;
    String input;
    Card holdCard;

    //Asks for name
    System.out.println("Please insert a nickname:");
    nickname = scanner.nextLine();

    //cheating?
    System.out.println("Would you like to display the sum? (y/n)");
    input = scanner.nextLine();
    System.out.println();
    if (input.equals("n"))
    {
        cheatOn = false;
    }
    else {cheatOn = true ;}

    //Game restarts here when asked for new game
    do {
        Boolean gameOver = false;
        System.out.println("\n" + "A new game has begun:" + "\n");

        //Initiates players and deck
        Player player = new Player(nickname);
        Player dealer = new Player("Dealer");
        Deck deck = new Deck();

        //Shuffle deck
        deck.shuffle();

        //Give first cards to player and dealer
        player.giveCard(deck.draw());
        dealer.giveCard(deck.draw()); // Will be hidden
        player.giveCard(deck.draw());

        System.out.println(player.showHand(cheatOn));

        //Checks if cheat is on and if so, prints the total value of the hand
        //System.out.println(player.checkCheat(cheatOn));

        // As the first card the dealer receives is hidden to the the player,
        // I can't use the function showHand() as it will also show the hidden card.
        holdCard = deck.draw();
        System.out.println("\n" + "Dealer: " + "\n" + "Hidden" + "\n" + dealer.giveCard(holdCard));

        // Can't use checkCheat() either because it would display the value of the hidden card
        if (cheatOn)
        {
        System.out.println("(" + holdCard.getFace().getValue() + ")");
        }

        //Loops the "hit"
        do{  
            //Player's turn
            //checks that input is either "hit" or "stand"
            do {
            System.out.println("\n" + "Would you like one more card? (hit/stand)");
            input = scanner.nextLine();
            }while(!input.equalsIgnoreCase("hit") && !input.equalsIgnoreCase("stand"));

            //Player Hit
            if (input.equalsIgnoreCase("hit"))
            {
                player.giveCard(deck.draw());
                System.out.println("\n" + player.showHand(cheatOn));

                //Player Bust
                if (player.getSum() > 21)
                {
                    gameOver = true;
                    System.out.println("\n" + "You busted! Sorry, dealer wins.");
                }
                //Player Blackjack
                else if (player.getSum() == 21)
                {
                    System.out.println("\n" + "Blackjack! You win!");
                    gameOver = true;
                }
            }
            //Player Stand - doesn't loop
            else if (input.equalsIgnoreCase("stand"))
            {
                System.out.println("\n" + nickname + " stands with " + player.getSum() + "\n");
            }
        } while (input.equalsIgnoreCase("hit") && !gameOver); // loops this part as long as input is "hit" and not a game over

        //Dealer's turn
        //Prints initial dealer's hand without hidden
        if (gameOver == false)
        {
            System.out.println(dealer.showHand(cheatOn));
        }     

        //Dealer hits if his hand sums at least 16, if not stand
        while(dealer.getSum() < 17 && gameOver == false)
            {
                System.out.println(dealer.giveCard(deck.draw()));
                System.out.println(dealer.checkCheat(cheatOn));

                if (dealer.getSum() == 21)
                {
                    System.out.println("\n" + "Blackjack! Dealer wins this time");
                    gameOver = true;
                }
                else if (dealer.getSum() > 21)
                {
                    System.out.println("\n" + "Dealer busted! You win!");
                    gameOver = true;
                }
            }

        //Hand comparison 
        if(!gameOver)
        {    
            System.out.println("\n" + "Dealer: " + "(" + dealer.getSum() + ")");
            System.out.println(nickname + " : " + "(" + player.getSum() + ")");

            if (dealer.getSum() > player.getSum())
            {
                System.out.println("Sorry! Dealer wins");
            }
            else if (dealer.getSum() < player.getSum())
            {
                System.out.println("Congratulations! You win!");
            }
            else 
            {
                System.out.println("You tied!");
            }
        }

        //New Game?
        System.out.println();
        System.out.println("Would you like to start a new game?  'y/n' :");
        do {
            input = scanner.nextLine();
        } while (!input.equalsIgnoreCase("y") && !input.equalsIgnoreCase("n"));

    }while (input.equalsIgnoreCase("y"));

//Closing scanner
scanner.close();    }
}
\$\endgroup\$

closed as off-topic by 200_success, Emily L., pacmaninbw, Sᴀᴍ Onᴇᴌᴀ, Malachi Jul 18 at 0:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – 200_success, Emily L., pacmaninbw, Sᴀᴍ Onᴇᴌᴀ, Malachi
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ The problem you face with the Ace is because of the lack of classes. A blackjack hand has a value, regardless of the values of its underlying cards. This means the value of ace in a hand depends upon the other cards of that hand. \$\endgroup\$ – dfhwze Jul 17 at 8:19
  • \$\begingroup\$ Hi @Nick, the class Blackjack is contained twice.. Looks like you want to show us the Player class :] \$\endgroup\$ – Roman Jul 17 at 9:04
  • \$\begingroup\$ @Roman fixed thank you! \$\endgroup\$ – Nick Jul 17 at 9:54
  • \$\begingroup\$ @dfhwze The Player.java class wasn't properly copied. In this class I have all the functions for the sum of cards. \$\endgroup\$ – Nick Jul 17 at 9:55
  • \$\begingroup\$ ArrayList<Card> should be a class called Hand and getSum one of its methods. \$\endgroup\$ – dfhwze Jul 17 at 18:10