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

# Thread: Black Jack: How would it look?

1. ## 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 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>();
}

for (int i = 1; i <=13; 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)
}
public String toString(){
String result = "";
for (Card card : cards)
result += card + "\n";
return result;
}

}
[/code]