Results 1 to 9 of 9
  1. #1
    Zoralink23 is offline Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Issue with Java wanting a variable

    Hi there:
    Trying to create a game of Blackjack for a school assignment but getting hung up on a bit of code. I'm attempting to write a quick method to read what card it is and then assigning a value to it (For example, a card has a value within an Array list of TWO and I want to assign it a numerical value of 2.) However, Eclipse seems determined for me to use a variable of some kind in it. I've tried all the variations I can think of, to no avail. Any help? Thanks in advance!

    Java Code:
    package blackjack;
    import java.util.ArrayList;
    import java.util.Scanner;
    import blackjack.Card.FaceValue;
    
    public class Blackjack {
    	private static Scanner keyboard;
    	/**
    	 * 
    	 * @author Anonymous
    	 * @since 10/15/12
    	 */
    	
    	//Problem Area is here
    	private int NumValue(FaceValue a){
    		if(a.equals(String(TWO))){
    			return 2;}
    	}
    	public static void main(String[] args) {
    		Deck GameDeck = new Deck();
    		Hand PlayerHand = new Hand();
    		Hand DealerHand = new Hand();
    		
    		PlayerHand.addCard(GameDeck.dealRandomCard());
    		PlayerHand.addCard(GameDeck.dealRandomCard());
    		Card PC0 = PlayerHand.getCardAt(0);
    		FaceValue PFV0 = PC0.getFaceValue();
    		Card PC1 = PlayerHand.getCardAt(1);
    		FaceValue PFV1 = PC1.getFaceValue();
    		
    		DealerHand.addCard(GameDeck.dealRandomCard());
    		DealerHand.addCard(GameDeck.dealRandomCard());
    		
    		System.out.println(PlayerHand.toString());
    		//System.out.println(DealerHand.toString());
    			
    
    	}
    
    }
    Java Code:
    package blackjack;
    
    
    public class Card {
    
    	// enumerations of the possible suits and face values
    	public static enum Suit {
    		DIAMOND, CLUB, HEART, SPADE
    	}
    	public static enum FaceValue {
    		TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
    	}
    
    	private Suit suit;
    	private FaceValue faceValue;
    	
    	/**
    	 * Constructor for the class Card
    	 * Note: there is no no arg constructor, we have to know the suit
    	 * and value in order to create a card
    	 * @param suit
    	 * @param faceValue
    	 */
    	public Card(Suit suit, FaceValue faceValue) {
    		this.suit = suit;
    		this.faceValue = faceValue;
    	}
    	
    	/** 
    	 * @return the card's suit
    	 */
    	public Suit getSuit() {
    		return suit;
    	}
    	
    	/**
    	 * @return the card's face value
    	 */
    	public FaceValue getFaceValue() {
    		return faceValue;
    	}
    	
    	@Override
    	public String toString() {
    		return suit + " of " + faceValue;
    	}
    	
    	public static void main(String[] args) {
    		Card c = new Card(Suit.CLUB, FaceValue.THREE);
    		System.out.println(c.toString());
    	}
    }
    Java Code:
    package blackjack;
    
    import java.util.ArrayList;
    import java.util.Random;
    
    import blackjack.Card.FaceValue;
    import blackjack.Card.Suit;
    
    
    public class Deck {
    	
    	public ArrayList<Card> deck;
    	
    	/**
    	 * Constructor for deck
    	 * initializes the ArrayList of cards to the standard 52 cards
    	 */
    	public Deck() {
    		deck = new ArrayList<Card>(52);
    		for (Suit suit : Card.Suit.values())
    			for (FaceValue value : Card.FaceValue.values())
    				deck.add(new Card(suit, value));
    	}
    	
    	/**
    	 * The deck's size will decrease by one after this method is called.
    	 * @return a card randomly selected from the deck
    	 */
    	public Card dealRandomCard() {
    		Random random = new Random();
    		Card dealCard = deck.remove(random.nextInt(deck.size()));
    		return dealCard;
    	}
    
    	
    }
    Java Code:
    package blackjack;
    
    import java.util.ArrayList; 
    import java.util.Collections;
    import java.util.Iterator;
    
    public class Hand {
    
    	private ArrayList cardList;
    
    	/**
    	 * Construct a new card. Instantiate a new ArrayList to hold the cards.
    	 */
    	public Hand() {
    		cardList = new ArrayList();
    	}
    
    	/**
    	 * @return the cards in the hand
    	 */
    	public ArrayList getCards() {
    		return cardList;
    	}
    
    	/**
    	 * Returns the card at a location in the hand. First card at position 0.
    	 * 
    	 * @param cardPosition
    	 *            integer position of the card in the list. zero based indexing.
    	 */
    	public Card getCardAt(int cardPosition) {
    		if (cardList.size() > cardPosition) {
    			Card c = (Card) cardList.get(cardPosition);
    			return c;
    		} else
    			return null;
    	}
    
    	/**
    	 * Adds a card passed to it to the hand.
    	 * 
    	 * @param card
    	 *            the card to be added to the hand
    	 */
    	public void addCard(Card card) {
    		if (card != null)
    			cardList.add(card);
    	}
    
    	/**
    	 * Removes a card passed to it to the hand.
    	 * 
    	 * @param card 
    	 *            the card to be removed to the hand
    	 */
    	public void removeCard(Card card) {
    		if (card != null)
    			cardList.remove(card);
    	}
    
    	/**
    	 * Returns a string representation of a Hand object
    	 * 
    	 * @override toString
    	 * 			the representation of the object
    	 */
    	public String toString() {
    		String handString = "Hand: ";
    		Iterator handIterator = cardList.iterator();
    		while (handIterator.hasNext()) {
    			Card c = (Card) handIterator.next();
    			handString = handString + c.getFaceValue() + " of " + c.getSuit()
    					+ "\r";
    		}
    		return handString;
    	}
    
    	public static void main(String[] args) {
    		Hand h = new Hand();
    		h.addCard(new Card(Card.Suit.CLUB, Card.FaceValue.KING));
    		h.addCard(new Card(Card.Suit.DIAMOND, Card.FaceValue.TWO));
    		h.addCard(new Card(Card.Suit.SPADE, Card.FaceValue.ACE));
    		h.addCard(new Card(Card.Suit.SPADE, Card.FaceValue.NINE));
    		System.out.println(h.toString());
    		h.removeCard(h.getCardAt(2));
    		System.out.println(h.toString());
    
    	}
    }
    Last edited by Zoralink23; 10-16-2012 at 03:14 AM.

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default Re: Issue with Java wanting a variable

    Can you not just have an array of type int and automatically assign it a value of 2?
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    Zoralink23 is offline Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Re: Issue with Java wanting a variable

    That won't work when it comes to cards such as Aces, or Queens. I wish I could do that, would certainly make it simpler!

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Issue with Java wanting a variable

    Perhaps you can have your Card class have both a numerical and String representation. Also provide appropriate methods.

  5. #5
    Zoralink23 is offline Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Re: Issue with Java wanting a variable

    Added the other classes being used to the original post.

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default Re: Issue with Java wanting a variable

    Quote Originally Posted by Zoralink23 View Post
    That won't work when it comes to cards such as Aces, or Queens. I wish I could do that, would certainly make it simpler!
    Wouldn't the face cards just be value 10? And ace would be 1 or 11?
    Sincerely, Joshua Green
    Please REP if I help :)

  7. #7
    Zoralink23 is offline Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Re: Issue with Java wanting a variable

    We need to have a visual representation of our cards in our hand, unfortunately. So I need to have it say something along the lines of "QUEEN of SPADES", rather than a "10 of SPADES."

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Issue with Java wanting a variable

    Have you tried what I suggested above?

  9. #9
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default Re: Issue with Java wanting a variable

    For your card objects can you just create another variable for the integer representation?
    Sincerely, Joshua Green
    Please REP if I help :)

Similar Threads

  1. Replies: 2
    Last Post: 11-03-2011, 11:49 PM
  2. Replies: 16
    Last Post: 07-17-2011, 12:10 AM
  3. Wanting to change from a single object to an array.
    By david522 in forum New To Java
    Replies: 6
    Last Post: 05-24-2011, 02:24 PM
  4. Replies: 8
    Last Post: 03-04-2011, 01:50 PM
  5. Issue with saving multiple times to a variable
    By sidd0123 in forum New To Java
    Replies: 3
    Last Post: 06-06-2010, 02:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •