/**
* Objects in this class represent one card from a deck of 52.
*
* @author Chad Wood
* 3/10/2008
*/
public class Card
{
private final int cardValue;
private final int cardSuit;
public final static int ACE = 1,
JACK = 11,
QUEEN = 12,
KING = 13;
public final static int SPADES = 0,
HEARTS = 1,
DIAMONDS = 2,
CLUBS = 3;
public Card(int theValue, int theSuit)
{
cardValue = theValue;
cardSuit = theSuit;
}
public int getSuit()
{
return cardSuit;
}
public int getValue()
{
return cardValue;
}
public String getValueAsString()
{
switch (cardValue)
{
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 "Invalid";
}
}
public String getSuitAsString()
{
switch (cardSuit)
{
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "Invalid";
}
}
public String toString()
{
return getValueAsString() + " of " + getSuitAsString();
}
}
/**
* This class will represent an array which shall act as a deck of 52 cards.
*
* @author Chad Wood
* 3/10/2007
*/
public class Deck
{
private Card[] deck;
private int cardsDealt;
public Deck()
{
deck = new Card[52];
int cardCounter = 0;
for ( int cardSuit = 0; cardSuit <= 3; cardSuit++ )
{
for ( int cardValue = 1; cardValue <= 13; cardValue++ )
{
deck[cardCounter] = new Card(cardValue,cardSuit);
cardCounter++;
}
}
cardsDealt = 0;
}
public void shuffle()
{
for ( int i = 51; i > 0; i-- )
{
int random = (int)(Math.random()*(i+1));
Card temp = deck[i];
deck[i] = deck[random];
deck[random] = temp;
}
cardsDealt = 0;
}
public int cardsRemaining()
{
return 52 - cardsDealt;
}
public Card dealCard()
{
if (cardsDealt == 52)
shuffle();
cardsDealt++;
return deck[cardsDealt - 1];
}
}
/**
* This class is used to simply represent a hand of cards.
*
* Chad Wood
* 3/10/2007
*/
import java.util.Vector;
public class Hand
{
private Vector hand;
public Hand()
{
hand = new Vector();
}
public void clear()
{
hand.removeAllElements();
}
public void addCard(Card card)
{
if (card != null)
hand.addElement(card);
}
public void removeCard(Card card)
{
hand.removeElement(card);
}
public void removeCard(int position)
{
if (position >= 0 && position < hand.size())
hand.removeElementAt(position);
}
public int getCardCount()
{
return hand.size();
}
public Card getCard(int position) {
if (position >= 0 && position < hand.size())
return (Card)hand.elementAt(position);
else
return null;
}
public void sortBySuit() {
Vector newHand = new Vector();
while (hand.size() > 0) {
int position = 0;
Card card = (Card)hand.elementAt(0);
for (int i = 1; i < hand.size(); i++)
{
Card card1 = (Card)hand.elementAt(i);
if ( card1.getSuit() < card.getSuit() ||
(card1.getSuit() == card.getSuit() && card1.getValue() < card.getValue()) )
{
position = i;
card = card1;
}
}
hand.removeElementAt(position);
newHand.addElement(card);
}
hand = newHand;
}
public void sortByValue()
{
Vector newHand = new Vector();
while (hand.size() > 0)
{
int position = 0;
Card card = (Card)hand.elementAt(0);
for (int i = 1; i < hand.size(); i++)
{
Card card1 = (Card)hand.elementAt(i);
if ( card1.getValue() < card.getValue() ||
(card1.getValue() == card.getValue() && card1.getSuit() < card.getSuit()) )
{
position = i;
card = card1;
}
}
hand.removeElementAt(position);
newHand.addElement(card);
}
hand = newHand;
}
}
/**
* The class is a subclass of the Hand Class. This class
* determines the value of tha hand according to the rules of 21.
* Chad Wood
* 3/10/2007
*/
public class Game21Hand extends Hand
{
public int getGame21Value()
{
int value;
boolean atLeastOneAce;
int cards;
value = 0;
atLeastOneAce = false;
cards = getCardCount();
for ( int i = 0; i < cards; i++ )
{
Card card;
int cardVal;
card = getCard(i);
cardVal = card.getValue();
if (cardVal > 10)
{
cardVal = 10;
}
if (cardVal == 1)
{
atLeastOneAce = true;
}
value = value + cardVal;
}
if ( atLeastOneAce == true && value + 10 <= 21 )
value = value + 10;
return value;
}
}
/**
* This class allows the user to play a game of 21 against the computer.
*
* Chad Wood
* 3/10/2008
*/
import java.util.Scanner;
import java.util.*;
public class Game21
{
public static void main(String[] args)
{
boolean userWins;
System.out.println("Get ready to play 21.");
playGame21();
}
static boolean playGame21()
{
Deck deck;
Game21Hand compHand;
Game21Hand yourHand;
deck = new Deck();
compHand = new Game21Hand();
yourHand = new Game21Hand();
deck.shuffle();
compHand.addCard(deck.dealCard());
compHand.addCard(deck.dealCard());
yourHand.addCard(deck.dealCard());
yourHand.addCard(deck.dealCard());
if (compHand.getGame21Value() == 21)
{
System.out.println("The Dealer has the " + compHand.getCard(0)
+ " and the " + compHand.getCard(1) + ".");
System.out.println("You have the " + yourHand.getCard(0)
+ " and the " + yourHand.getCard(1) + ".");
System.out.println("The Dealer wins with 21!!");
return false;
}
if (yourHand.getGame21Value() == 21)
{
System.out.println("The Dealer has the " + compHand.getCard(0)
+ " and the " + compHand.getCard(1) + ".");
System.out.println("The User has the " + yourHand.getCard(0)
+ " and the " + yourHand.getCard(1) + ".");
System.out.println("21, You win!!.");
return true;
}
while (true)
{
System.out.println("You have:");
for (int i = 0; i < yourHand.getCardCount(); i++)
System.out.println(" " + yourHand.getCard(i));
System.out.println("Your current score is: " + yourHand.getGame21Value());
System.out.println();
System.out.println("The Dealer is showing the " + compHand.getCard(0));
System.out.println("Would You Like to Hit (h) or Stand (s)? ");
char userChoice;
do
{
Scanner Keyboard = new Scanner(System.in);
userChoice = Keyboard.next( ).charAt(0);
if (userChoice != 'h' && userChoice != 's')
System.out.println("Please choose h or s: ");
}
while (userChoice != 's');
if (userChoice == 's')
{
System.out.println("You choose to stand.");
System.out.println("The Dealer's hand is:");
System.out.println(" " + compHand.getCard(0));
System.out.println(" " + compHand.getCard(1));
while (compHand.getGame21Value() <= 16)
{
Card newCard = deck.dealCard();
System.out.println("The Dealer hits and receives the: " + newCard);
compHand.addCard(newCard);
if (compHand.getGame21Value() > 21)
{
System.out.println("The Dealer busted, You win.");
return true;
}
}
if (userChoice != 's')
{
Card newCard = deck.dealCard();
yourHand.addCard(newCard);
System.out.println("You choose to hit.");
System.out.println("You drew the " + newCard);
System.out.println("Your new total is " + yourHand.getGame21Value());
if (yourHand.getGame21Value() > 21)
{
System.out.println("Over 21. You Busted.");
System.out.println("The Dealer's down card was: "
+ compHand.getCard(1));
return false;
}
}
if (userChoice == 's')
System.out.println("The Dealer's score is: " + compHand.getGame21Value());
if (compHand.getGame21Value() == yourHand.getGame21Value())
{
System.out.println("It's a tie, you lose.");
return false;
}
else if (compHand.getGame21Value() > yourHand.getGame21Value())
{
System.out.println("The Dealer wins because, " + compHand.getGame21Value()
+ " beats " + yourHand.getGame21Value() + ".");
return false;
}
else
{
System.out.println("You win because, " + yourHand.getGame21Value()
+ " beats " + compHand.getGame21Value() + ".");
return true;
}
}
return true;
}
}
}
I know the problem lies in the Game21 class, I just can't get it.

New Topic/Question
Reply




MultiQuote





|