Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Black Jack: How would it look?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Black Jack: How would it look?

    Using the Deck / Card / Suit classes, Deal and play a “21” or Black Jack game with one deck

    “The Deal”
    deal () out 2 cards face up to the player
    deal() out 2 cards, 1 face-up and 1 face down to the dealer
    The “Play”
    player can choose to “Hit” (deal another card) or “Stand’ (pass on the turn).
    If the “Hit” is on
    The player can “Hit” as long as the total is less then “21”
    If the total is greater than “21” then the message “Bust” is displayed and the game is reset. (new hand)
    If the “Stand” is on
    Total is stored and the dealer has a turn

    The “Dealer”
    dealer will turn his one card face up and a total will be set. If the total is greater than or equal to 17, the dealer must stand and
    the dealer total is equated to the player total. If the dealer total is greater than the player total the player loses. If the dealer total is less than the player total then the player wins. If the dealer total and the player totals are equal, then there is a push (tie)
    Keep track of player wins, loses and ties

    if dealer total is less than 17, dealer must “Hit” until total is greater than or equal to 17

    When all 52 cards are dealt, game ends.
    The “End of the Game”
    Display the players wins, loses and ties
    “thank-you for playing”

    How would this code look?

    [/code]
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package blackjack;

    /**
    *
    *
    */
    public class Card implements Comparable{

    /**
    * @param args
    */

    private Suit suit;
    private int rank;
    private boolean faceUp;

    public Card(Suit suit, int rank) {
    this.suit = suit;
    this.rank = rank;
    faceUp = false;
    }

    public boolean equals(Object other){
    if(this==other)
    return true;
    else if (! (other instanceof Card))
    return false;
    else{
    Card otherCard = (Card)other;
    return rank == otherCard.rank;

    }
    }

    public int compareTo(Object other){
    if (! (other instanceof Card))
    throw new IllegalArgumentException("Parameter must be a Card");
    Card otherCard = (Card)other;
    return rank - otherCard.rank;
    }

    public int getRank() {
    return rank;
    }

    public Suit getSuit() {
    return suit;
    }

    public boolean isFaceUp(){
    return faceUp;
    }

    public boolean isRed(){
    return suit == Suit.heart || suit == Suit.diamond;
    }

    public void turn() {
    faceUp = ! faceUp;
    }

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

    private String rankToString() {
    if (rank ==1)
    return "Ace";
    else if (rank ==11)
    return "Jack";
    else if (rank ==12)
    return "Queen";
    else if (rank ==13)
    return "King";
    else
    return "" + rank;
    }
    }

    [/code]
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package blackjack;

    /**
    *
    *
    */

    public class Suit implements Comparable {

    /**
    * @param args
    */
    static public final Suit spade = new Suit(4, "spades");
    static public final Suit heart = new Suit(3, "hearts");
    static public final Suit diamond = new Suit(2, "diamonds");
    static public final Suit club = new Suit(1, "clubs");

    private int order;
    private String name;

    private Suit(int ord, String nm){
    name = nm;
    order = ord;
    }

    public int compareTo(Object other){
    if (! (other instanceof Suit))
    throw new IllegalArgumentException("Parameter must be a Suit");
    Suit otherSuit = (Suit)other;
    return order - otherSuit.order;
    }

    public String toString() {
    return name;
    }

    }

    [/code]
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package blackjack;

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

    public class Deck {

    /**
    * @param args
    */

    public static final int MAX_SIZE = 52;
    private ArrayList<Card> cards;

    public Deck(){
    reset();
    }

    public void reset() {
    cards = new ArrayList<Card>();
    addSuit(Suit.spade);
    addSuit(Suit.heart);
    addSuit(Suit.diamond);
    addSuit(Suit.club);
    }

    private void addSuit(Suit suit){
    for (int i = 1; i <=13; i++)
    cards.add(new Card(suit, i));
    }

    public boolean isEmpty(){
    return cards.isEmpty();
    }

    public int size(){
    return cards.size();
    }

    public Card deal() {
    if (isEmpty())
    return null;
    else
    return cards.remove(cards.size() - 1);
    }

    public Card[] deal(int number) {
    if (number > cards.size())
    return null;
    else{
    Card[] hand = new Card[number];
    for (int i =0; i < hand.length; i++)
    hand[i] = deal();
    return hand;
    }
    }
    public void shuffle(){
    if (cards.size() < MAX_SIZE)
    return;
    Random gen= new Random();

    Card[] array = new Card[MAX_SIZE];
    while (cards.size() > 0){
    Card card = cards.remove(cards.size() - 1);
    int i = gen.nextInt(MAX_SIZE);
    while (array[i] != null)
    i = gen.nextInt(MAX_SIZE);
    array[i] = card;
    }
    for( Card card : array)
    cards.add(card);
    }
    public String toString(){
    String result = "";
    for (Card card : cards)
    result += card + "\n";
    return result;
    }

    }
    [/code]


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Black Jack: How would it look?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

Similar Threads

  1. JAVA game black jack need help on finding an error!!
    By Clyde90 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 13th, 2012, 09:40 AM
  2. black jack game
    By 314159 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 6th, 2011, 11:42 AM
  3. (URGENT) Plz help me with the black jack program.
    By diogo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 4th, 2011, 05:08 AM
  4. Black Jack Statistics game? lost.
    By mindlessn00b in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 20th, 2010, 08:20 AM