Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help to make the values of Jack, Queen, and King equal to 10
and not (11,12,13 as in my program)

Also, i need help on how to the player have the option of making
Ace = 1 or 11

Java
import java.util.Scanner;
public class G21
{
    static Deck deck; 
    static int playerHand;
    static int compHand;
    static Scanner scanner;
    public static void main(String[] args)
    {
        scanner = new Scanner(System.in);
        deck = new Deck();
        deck.shuffle();
        boolean deal = true;
        do {
            playerHand = 0;
            compHand = 0;
            start();
            if(playersTurn())
                dealersTurn();
            checkScores();
            System.out.println("Play again? y/n");
            if(scanner.nextLine().charAt(0) != 'y')
                deal = false;
        } while(deal);
    }
    private static void start() {
        for(int j = 0; j < 2; j++) {
            Card card = deck.dealCard();
            String s = (j == 0) ? "1st" : "2nd";
            System.out.println("Your " + s + " card: " + card);
            playerHand += card.getValue();
            // check for blackjack
        }
        for(int j = 0; j < 2; j++) {
            Card card = deck.dealCard();
            String s = (j == 0) ? "1st" : "2nd";
            System.out.println("Dealer's " + s + " card: " + card);
            compHand += card.getValue();
            // check for blackjack
        }
    }
    private static boolean playersTurn() {
        boolean takeHit = true;
        while(takeHit) {
            System.out.println("Do you want a hit? y/n");
            if(scanner.nextLine().charAt(0) == 'y') {
                Card card = deck.dealCard();
                System.out.println("Your next card: " + card);
                playerHand += card.getValue();
                if(playerHand > 21)
                    takeHit = false;
            } else {
                takeHit = false;
            }
        }
        return playerHand <= 21;
    }
    private static void dealersTurn() {
        while(compHand < 16) {
            Card card = deck.dealCard();
            System.out.println("Dealer's next card: " + card);
            compHand += card.getValue();
        }
    }
    private static void checkScores() {
        if(playerHand > 21)
            System.out.println("You lose with " + playerHand);
        else if(compHand > 21)
            System.out.println("Dealer loses with " + compHand);
        else if(compHand == 21)
            System.out.println("Dealer wins with " + compHand);
        else if (playerHand == 21)
            System.out.println("You win with " + playerHand);
        else {
            String result;
            if(playerHand > compHand)
                result = "You win: " + playerHand + " to " + compHand;
            else
                result = "Dealer wins with " + compHand + " to " + playerHand;
            System.out.println(result);
        }
    }
}

Class for Deck:
Java
public class Deck {
private Card[] deck; 
private int cardsUsed; 
public Deck() {
deck = new Card[52];
int cardCt = 0; 
for ( int suit = 0; suit <= 3; suit++ ) {
for ( int value = 1; value <= 13; value++ ) {
deck[cardCt] = new Card(value,suit);
cardCt++;
}
}
cardsUsed = 0;
}
public void shuffle() {
// Put all the used cards back into the deck, and shuffle it into
// a random order.
for ( int i = 51; i > 0; i-- ) {
int rand = (int)(Math.random()*(i+1));
Card temp = deck[i];
deck[i] = deck[rand];
deck[rand] = temp;
}
cardsUsed = 0;
}
public int cardsLeft() {
// As cards are dealt from the deck, the number of cards left
// decreases. This function returns the number of cards that
// are still left in the deck.
return 52 - cardsUsed;
}
public Card dealCard() {
// Deals one card from the deck and returns it.
if (cardsUsed == 52)
shuffle();
cardsUsed++;
return deck[cardsUsed - 1];
}
} // end class Deck


Class for Cards
Java
public class Deck {
private Card[] deck; 
private int cardsUsed; 
public Deck() {
// Create an unshuffled deck of cards.
deck = new Card[52];
int cardCt = 0; 
for ( int suit = 0; suit <= 3; suit++ ) {
for ( int value = 1; value <= 13; value++ ) {
deck[cardCt] = new Card(value,suit);
cardCt++;
}
}
cardsUsed = 0;
}
public void shuffle() {
for ( int i = 51; i > 0; i-- ) {
int rand = (int)(Math.random()*(i+1));
Card temp = deck[i];
deck[i] = deck[rand];
deck[rand] = temp;
}
cardsUsed = 0;
}
public int cardsLeft() {
return 52 - cardsUsed;
}
public Card dealCard() {
if (cardsUsed == 52)
shuffle();
cardsUsed++;
return deck[cardsUsed - 1];
}
} // end class Deck
Posted
Updated 10-Dec-10 21:24pm
v2

Quote: I need help to make the values of Jack, Queen, and King equal to 10 and not (11,12,13 as in my program)

How about:
if (value > 10) value = 10;


As to the other one, you just need to check whether the player holds an Ace if his total goes above 21, and reset the Ace value to 1. Or offer an option for the player to decide.
 
Share this answer
 
"As to the other one, you just need to check whether the player holds an Ace if his total goes above 21, and reset the Ace value to 1. Or offer an option for the player to decide."

how do i make the player decide? or write the if statement for the Ace to bust if over 21?
 
Share this answer
 
Comments
Richard MacCutchan 12-Dec-10 3:49am    
if (bust && hand contains Ace)
ask player for confirmation of Ace value
...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900