Results 1 to 2 of 2
  1. #1
    RvcaGuy is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Talking single player Blackjack no dealer

    Hello World!

    I'm trying to write a program that basically plays blackjack with myself, no dealer. i cant figure out how to add a counter to total up the card
    value. basically no penalty for under 21. you win if you hit 21 and if you go over 21 you lose. i have attached what i have done so far any help would be awesome and gratefully appreciated!!!!

    ***DisplayDeck****

    import java.util.Scanner;

    public class DisplayDeck
    {
    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 playOn = true;
    do {
    playerHand = 0;
    compHand = 0;
    start();
    if(playersTurn())
    checkScores();
    System.out.println("Play again? y/n");
    if(scanner.nextLine().charAt(0) != 'y')
    playOn = false;
    } while(playOn);
    }

    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

    }
    }

    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 checkScores() {
    if(playerHand > 21)
    System.out.println("You lose with " + playerHand);
    else if (playerHand == 21)
    System.out.println("You win with " + playerHand);
    else {
    String result;
    if(playerHand < 21)
    result = "You win: " + playerHand ;
    else
    result = "youlose " + playerHand;
    System.out.println(result);
    }
    }
    }





    ****Deck Class*****



    public class Deck {

    private Card[] deck; // An array of 52 Cards, representing the deck.
    private int cardsUsed; // How many cards have been dealt from the deck.

    public Deck() {
    // Create an unshuffled deck of cards.
    deck = new Card[52];
    int cardCt = 0; // How many cards have been created so far.
    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];
    }

    }


    *****Card Class****

    public class Card {

    public final static int SPADES = 0,
    HEARTS = 1,
    DIAMONDS = 2,
    CLUBS = 3;

    public final static int ACE = 1,
    JACK = 11,
    QUEEN = 12,
    KING = 13;

    private final int suit;

    private final int value;


    public Card(int theValue, int theSuit) {

    value = theValue;
    suit = theSuit;
    }

    public int getSuit() {

    return suit;
    }

    public int getValue() {

    return value;
    }

    public String getSuitAsString() {

    switch ( suit ) {
    case SPADES: return "Spades";
    case HEARTS: return "Hearts";
    case DIAMONDS: return "Diamonds";
    case CLUBS: return "Clubs";
    default: return "??";
    }
    }

    public String getValueAsString() {

    switch ( value ) {
    case 1: return "Ace";
    case 2: return "2";
    case 3: return "3";
    case 4: return "4";
    case 5: return "5";
    case 6: return "6";
    case 7: return "7";
    case 8: return "8";
    case 9: return "9";
    case 10: return "10";
    case 11: return "Jack";
    case 12: return "Queen";
    case 13: return "King";
    default: return "??";
    }
    }

    @Override
    public String toString() {

    return getValueAsString() + " of " + getSuitAsString();
    }


    }
    Attached Files Attached Files

  2. #2
    javaTurtle is offline Member
    Join Date
    Nov 2011
    Location
    Arizona
    Posts
    10
    Rep Power
    0

    Default Re: single player Blackjack no dealer

    Looks to me that you are putting your number values in a String. I see the switch.
    so, maybe convert thoses string to int?


    int aInt = Integer.parseInt(aString);

Similar Threads

  1. Need help...blackjack not working.
    By g2mediagroup in forum New To Java
    Replies: 5
    Last Post: 05-03-2011, 07:40 AM
  2. Creating a game of blackjack
    By adjit in forum New To Java
    Replies: 3
    Last Post: 04-29-2011, 03:14 PM
  3. Help With BlackJack Game !
    By TangoMango in forum New To Java
    Replies: 1
    Last Post: 01-24-2011, 06:40 PM
  4. blackjack help
    By jordaejava in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 06:44 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •