CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2010
    Location
    West Coast
    Posts
    5

    Quick question - Blackjack

    I've constructed a code that plays blackjack. I've done the code upto 98% where that 2% needs help.

    Basically, it has card, hand, deck and of course blackjack class. But there's a twist in my code, I've added suits and values as enum type since they're just bunch of constants.

    When executing the code it works flawlessly but it would say something like

    "Your hand is: DUECE of HEART, TEN of DIAMOND"
    "Dealer's hand is: JACK of SPADE, ACE of CLUB"

    I know the reason why it's printing out the word DEUCE instead of a number 2, or TEN instead of 10. That's because I've set the values into enum types which can only take strings.

    How would I go about doing that, converting DEUCE to 2 or TEN to 10? I have couple of ideas in how it's going to be done but please let me know if that's the wrong way of doing it.

    1. Pass an argument in enum. Instead of me just listing all constants in enum, I would list soemthing like this >> DEUCE("2"), FIVE("5"), ACE("A"), etc. and create an instance variable and a getter method that returns the value inside the parameter or 2. Use the switch statement. [ switch (values) {case DEUCE: System.out.println("2"); break;} ].

    Just to give you a better picture of what I'm talking about in VALUES.java

    Code:
    public enum VALUES
    {
    	DEUCE("2"),
    	THREE("3"),
    	FOUR("4"),
    	FIVE("5"),
    	SIX("6"),
    	SEVEN("7"),
    	EIGHT("8"),
    	NINE("9"),
    	TEN("10"),
    	JACK("J"),
    	QUEEN("Q"),
    	KING("K"),
    	ACE("A"),
    	
    		private String numbers;
    
    		public VALUES(String numbers)
    		{
    			this.numbers = numbers;
    		}
    		public String getNumber()
    		{
    			return numbers;
    		}	
    }
    And the switch statement I was talking about

    Code:
    private VALUES values; 
    
    switch(values)
    {
    	case DEUCE:
    		System.out.println("2");
    	break;
    	case THREE:
    		System.out.println("3");
    	break;
    	case FOUR:
    		System.out.println("4");
    	break;
    	case FIVE:
    		System.out.println("5");
    	break;
    	case SIX:
    		System.out.println("6");
    	break;
    	case SEVEN:
    		System.out.println("7");
    	break;
    	case EIGHT:
    		System.out.println("8");
    	break;
    	case NINE:
    		System.out.println("9");
    	break;
    	case TEN:
    		System.out.println("10");
    	break;
    	case JACK:
    		System.out.println("J");
    	break;
    	case QUEEN:
    		System.out.println("Q");
    	break;
    	case KING:
    		System.out.println("K");
    	break;
    	case ACE:
    		System.out.println("A");
    	break;
    }
    Thank you.

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Quick question - Blackjack

    Both of those are viable options. Personally I would prefer the first option as it does not require any use of a switch statement, they simply are what they are. Remember also that (this may not come in handy, but it just might for assigning values) that enum types in Java also have an ordinal value. That is, they have the same value type as a C enum in the sense that the first element's ordinal is 0, next element's ordinal is 1, et cetera.

  3. #3
    Join Date
    Mar 2010
    Location
    West Coast
    Posts
    5

    Re: Quick question - Blackjack

    Admin/Moderator,

    Please close/lock/delete this thread.

    Reason: Started thread for the same question twice. (this and "Quick questions about blackjack").

    Thank you.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured