Card.Java and BlackJack

I need some help with my coding

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 11327 Views - Last Post: 28 January 2009 - 09:26 AM Rate Topic: -----

#16 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Card.Java and BlackJack

Posted 15 January 2009 - 08:58 AM

lol no. I'll see what's going on. I hate zip, it always gives me problems.
Was This Post Helpful? 0
  • +
  • -

#17 Cuzenu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-January 09

Re: Card.Java and BlackJack

Posted 28 January 2009 - 09:26 AM

Okay I just have one question on how I can convert the stings to int values, because my blackjack driver must add the cards together.

It will print the correct result but I'm not sure how to make it print the values sum.

Here is the card program

//********************************
// Card.java represents a deck
// of playing cards.
//
// Author: Melissa Gendron
// Date: 1/17/2009
//********************************

import java.util.Random;
public class Card
{
	// instance variables
	private final int DECK_TOTAL = 52;  // 52 cards in a deck
	private final int SUIT_TOTAL = 13;  // 13 face values
	private int faceValue;
	private int suitNum;
	
	enum Suit {Hearts, Clubs, Diamonds, Spades}  // lists the different suits
	Suit cardSuit = Suit.Spades;
	
	enum FaceCard {Jack, Queen, King, Ace, numberCard}  // Non-number cards have
	FaceCard cardFaceVal = FaceCard.Queen;			  // special values
	
	// delare and initialize the random generator
	Random rand = new Random();
	
	// Constructor for a card
	// Defaults to the Queen of Spades
	public Card()
	{
		// states the card's suit
		suitNum = 4;
		cardSuit = Suit.Spades;
		
		// states the card's face value
		faceValue = 12;
		cardFaceVal = FaceCard.Queen;
	}

	// Deals one random card
	public String dealOne()
	{
		faceValue = rand.nextInt(13) + 2;
		suitNum = rand.nextInt(4) + 1;
		
		switch (suitNum)	// changes the suit
		{
			case 1: cardSuit = Suit.Hearts; break;
			case 2: cardSuit = Suit.Clubs; break;
			case 3: cardSuit = Suit.Diamonds; break;
			case 4: cardSuit = Suit.Spades; break;
			default: cardSuit = Suit.Spades; break;
		}
		
		if (faceValue > 10)
		{
			switch (faceValue)  // changes the face value
			{
				case 11: cardFaceVal = FaceCard.Jack; break;
				case 12: cardFaceVal = FaceCard.Queen; break;
				case 13: cardFaceVal = FaceCard.King; break;
				case 14: cardFaceVal = FaceCard.Ace; break;
				default: cardFaceVal = FaceCard.numberCard; break;
			}
			// returns the card's face and suit
			return "The card is the " + cardFaceVal + " of " + cardSuit;
		}
		else
			// returns the card's face and suit
			return "The card is the " + faceValue + " of " + cardSuit;
	}
	
	// method that returns the card's suit
	public String getSuit()
	{
		return "" + cardSuit;
	}
	
	// method that returns the card's face value
	public String getValue()
	{
		if (faceValue > 10)
			return "" + cardFaceVal;
		else
			return "" + faceValue; 
	}
	// returns the card's value and suit to the user
	public String toString()
	{
		if (faceValue > 10)
			return "The " + cardFaceVal + " of " + cardSuit;
		else
			return "The " + faceValue + " of " + cardSuit;
	}
}





And this is the BlackJack driver
//-----------------------------//
// BlackJack.java deals random //
// playing cards.			  //
//							 //
// Author: Melissa Gendron	 //
// Date: 1/23/2009			  //
//-----------------------------//

import java.util.Scanner;
public class BlackJack
{
	public static void main(String[] args)
	{
		int myTotal, dealerTotal;
		//declares the Scanner object
		Scanner scan = new Scanner(System.in);
		String another = "y";  // if user inputs "y" the game continues
		
		System.out.println("Let's Play a Game of BlackJack");
		
		while (another.equalsIgnoreCase("y"))
		{
		Card myCard = new Card();   // creates a new card
		Card myCard2 = new Card();  // creates a new card
		Card dealerCard = new Card();  // creates a new card
		Card dealerCard2 = new Card();  // craetes a new card
		
		//deals the two cards to each player
		myCard.dealOne();
		myCard2.dealOne();
		dealerCard.dealOne();
		dealerCard2.dealOne();
		
		
		System.out.println("You draw the " + myCard);			 // prints the card
		System.out.println("You draw the " + myCard2);			// prints the 2nd card
		
		System.out.println("The dealer drew the " + dealerCard);		// prints the dealer's top card
		
		
		System.out.println("The dealer's hidden card was " +dealerCard2);	   // prints the dealer's 2nd card

			
			System.out.println("Would you like to play another hand? y/n");
			another = scan.nextLine();
		}
	}
}







Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2