Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-17-2007, 10:44 PM
Member
 
Join Date: Nov 2007
Posts: 2
javakid9000 is on a distinguished road
BlackJack help please,, adding hand values
Hi, i'm doing a BlackJack program for homework with three classes, a Hand class, Deck class, and a Driver (called Game21). I wrote the Deck and Hand classes already and now i'm working on the driver. I am able to draw cards and give them to each of the players, but I cannot figure out how to save the values of their cards so I can actually make it a game. Here's my code below, any help would be greatly aprecciated =)

***************
***************
Driver
***************
***************

public class Game21
{
public static void main(String[] args)
{
Deck playerDeck, compDeck;

playerDeck = new Deck();
compDeck = new Deck();
int playerHand;
int compHand;


playerDeck.shuffle();

playerDeck.dealCard();

System.out.println("this: " + playerDeck);

System.out.println("Your 1st card: " + playerDeck.dealCard());
playerDeck.dealCard();
System.out.println("Your 2nd card: " + playerDeck.dealCard());

compDeck.shuffle();

compDeck.dealCard();
System.out.println("Dealer's 1st card: ???");
compDeck.dealCard();
System.out.println("Dealer's 2nd card: " + compDeck.dealCard());

}
}

**********
**********
Deck Class
**********
**********


/*
An object of type Deck represents an ordinary deck of 52 playing cards.
The deck can be shuffled, and cards can be dealt from the deck.
*/

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];
}

} // end class Deck



**********
**********
Card Class
**********
**********


/*
An object of class card represents one of the 52 cards in a
standard deck of playing cards. Each card has a suit and
a value.
*/


public class Card {

public final static int SPADES = 0, // Codes for the 4 suits.
HEARTS = 1,
DIAMONDS = 2,
CLUBS = 3;

public final static int ACE = 1, // Codes for the non-numeric cards.
JACK = 11, // Cards 2 through 10 have their
QUEEN = 12, // numerical values for their codes.
KING = 13;

private final int suit; // The suit of this card, one of the constants
// SPADES, HEARTS, DIAMONDS, CLUBS.

private final int value; // The value of this card, from 1 to 11.


public Card(int theValue, int theSuit) {
// Construct a card with the specified value and suit.
// Value must be between 1 and 13. Suit must be between
// 0 and 3. If the parameters are outside these ranges,
// the constructed card object will be invalid.
value = theValue;
suit = theSuit;
}

public int getSuit() {
// Return the int that codes for this card's suit.
return suit;
}

public int getValue() {
// Return the int that codes for this card's value.
return value;
}

public String getSuitAsString() {
// Return a String representing the card's suit.
// (If the card's suit is invalid, "??" is returned.)
switch ( suit ) {
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "??";
}
}

public String getValueAsString() {
// Return a String representing the card's value.
// If the card's value is invalid, "??" is returned.
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 "??";
}
}

public String toString() {
// Return a String representation of this card, such as
// "10 of Hearts" or "Queen of Spades".
return getValueAsString() + " of " + getSuitAsString();
}


} // end class Card
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-18-2007, 06:26 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
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 playOn = true; do { playerHand = 0; compHand = 0; start(); if(playersTurn()) dealersTurn(); 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 } 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); } } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing boolean Values of another values in one class. a_iyer20 Advanced Java 4 04-15-2008 03:04 PM
I must create a robotic hand with java .... any one can help ?? yashar New To Java 9 03-25-2008 05:46 PM
Adding taglibs in JSP Java Tip Java Tips 0 01-14-2008 01:43 AM
Adding numbers in array Shaolin New To Java 1 11-15-2007 08:30 PM
Adding a Restock Fee Nexcompac New To Java 2 07-31-2007 04:46 PM


All times are GMT +3. The time now is 03:59 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org