Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: BlackJack.. Random Card Assignment?

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default BlackJack.. Random Card Assignment?

    Hi guys, for a project I'm doing I want to create a program that plays Black Jack.
    But as I got thinking about it, I realized I have no idea how I should declare different suits and cards.
    I need to be able to randomly choose a card as if drawn from a deck and just need some ideas on how to do it.
    I'm really new to java so my understanding is kind of limited. But from what I can tell I can randomly choose an enum from a group of them. and I'm thinking that may be what I have to do to get a random card draw. But that would mean I'd have to assign an enum for every card and its suit right? Also these enums, or whatever I use need to be able to store the value of their card so that I can check the points.
    I'm just curios how you guys might go about doing a program like this?

    This is kind of how I plan to have it look like when I'm finished. I realize that this is really novice looking, but I'm sort of restricted to how I'm allowed to do this and by what I know how to do. =/ A more sophisticated GUI isn't an option.



  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: BlackJack.. Random Card Assignment?

    Instead of having 52 different enum constants, you could have two different enum types: one for suit, one for number. Then have a Card Object that contains an enum constant from each enum type.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Offtopic (April 14th, 2011)

  4. #3
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: BlackJack.. Random Card Assignment?

    Do you think an array of cards would be better? Cause I can't think of a way to use the enum types for suit and numbers together to make a deck..

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: BlackJack.. Random Card Assignment?

    Well yeah, you'd store the Card Objects in an array or a List or something. It's at creation time that the enums come in handy- you'd just need a nested for loop instead of 52 initializations.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Offtopic (April 14th, 2011)

  7. #5
    Member
    Join Date
    Jan 2011
    Posts
    78
    My Mood
    Confused
    Thanks
    23
    Thanked 1 Time in 1 Post

    Default Re: BlackJack.. Random Card Assignment?

    Just kidding i see the error. I'll fix it.

    well I got bored and made a program to initialize them for me =/. Although.. I think I'm confused about what cards are suppose to go in the deck cause i get 56 every time...
    public final static int Diamond1 = 1;
    public final static int Diamond2 = 2;
    public final static int Diamond3 = 3;
    public final static int Diamond4 = 4;
    public final static int Diamond5 = 5;
    public final static int Diamond6 = 6;
    public final static int Diamond7 = 7;
    public final static int Diamond8 = 8;
    public final static int Diamond9 = 9;
    public final static int Diamond10 = 10;
    public final static int DiamondJ = 11;
    public final static int DiamondQ = 12;
    public final static int DiamondK = 13;
    public final static int Club1 = 14;
    public final static int Club2 = 15;
    public final static int Club3 = 16;
    public final static int Club4 = 17;
    public final static int Club5 = 18;
    public final static int Club6 = 19;
    public final static int Club7 = 20;
    public final static int Club8 = 21;
    public final static int Club9 = 22;
    public final static int Club10 = 23;
    public final static int ClubJ = 24;
    public final static int ClubQ = 25;
    public final static int ClubK = 26;
    public final static int Heart1 = 27;
    public final static int Heart2 = 28;
    public final static int Heart3 = 29;
    public final static int Heart4 = 30;
    public final static int Heart5 = 31;
    public final static int Heart6 = 32;
    public final static int Heart7 = 33;
    public final static int Heart8 = 34;
    public final static int Heart9 = 35;
    public final static int Heart10 = 36;
    public final static int HeartJ = 37;
    public final static int HeartQ = 38;
    public final static int HeartK = 39;
    public final static int Spade1 = 40;
    public final static int Spade2 = 41;
    public final static int Spade3 = 42;
    public final static int Spade4 = 43;
    public final static int Spade5 = 44;
    public final static int Spade6 = 45;
    public final static int Spade7 = 46;
    public final static int Spade8 = 47;
    public final static int Spade9 = 48;
    public final static int Spade10 = 49;
    public final static int SpadeJ = 50;
    public final static int SpadeQ = 51;
    public final static int SpadeK = 52;

    Here is my program =/
     
    public class DeckVariableDeclaration
    {
     
     
    	public static void main(String[] args)
    	{
    		String declare = "public final static int ";
    		String di = "Diamond";
    		String cl = "Club";
    		String he = "Heart";
    		String sp = "Spade";
    		int x = 0;
    		int c = 1;
    		int cardnum = 1;
     
     
     
    		x=0;
    		c=1;
    		do
    		{
    			if (x < 10)
    			{
    		System.out.printf("" + declare + di + c + " = " + cardnum + ";" + "\n");
    			}
     
    			if (x == 10)
    			{
    				System.out.printf("" + declare + di + "J" + " = " + cardnum + ";" + "\n");
    			}
    			if (x == 11)
    			{
    				System.out.printf("" + declare + di + "Q" + " = " + cardnum + ";" + "\n");
    			}
    			if (x == 12)
    			{
    				System.out.printf("" + declare + di + "K" + " = " + cardnum + ";" + "\n");
    			}
    				c++;
    			x++;
    			cardnum++;
    		}while (x <= 13);
     
     
    		x=0;
    		c=1;
    		cardnum--;
    		do
    		{
    			if (x < 10)
    			{
    		System.out.printf("" + declare + cl + c + " = " + cardnum + ";" + "\n");
    			}
     
    			if (x == 10)
    			{
    				System.out.printf("" + declare + cl + "J" + " = " + cardnum + ";" + "\n");
    			}
    			if (x == 11)
    			{
    				System.out.printf("" + declare + cl + "Q" + " = " + cardnum + ";" + "\n");
    			}
    			if (x == 12)
    			{
    				System.out.printf("" + declare + cl + "K" + " = " + cardnum + ";" + "\n");
    			}
    				c++;
    			x++;
    			cardnum++;
    		}while (x <= 13);
     
     
     
     
    		x=0;
    		c=1;
    		cardnum--;
    		do
    		{
    			if (x < 10)
    			{
    		System.out.printf("" + declare + he + c + " = " + cardnum + ";" + "\n");
    			}
     
    			if (x == 10)
    			{
    				System.out.printf("" + declare + he + "J" + " = " + cardnum + ";" + "\n");
    			}
    			if (x == 11)
    			{
    				System.out.printf("" + declare + he + "Q" + " = " + cardnum + ";" + "\n");
    			}
    			if (x == 12)
    			{
    				System.out.printf("" + declare + he + "K" + " = " + cardnum + ";" + "\n");
    			}
    				c++;
    			x++;
    			cardnum++;
    		}while (x <= 13);
     
     
    		x=0;
    		c=1;
    		cardnum--;
    		do
    		{
    			if (x < 10)
    			{
    		System.out.printf("" + declare + sp + c + " = " + cardnum + ";" + "\n");
    			}
     
    			if (x == 10)
    			{
    				System.out.printf("" + declare + sp + "J" + " = " + cardnum + ";" + "\n");
    			}
    			if (x == 11)
    			{
    				System.out.printf("" + declare + sp + "Q" + " = " + cardnum + ";" + "\n");
    			}
    			if (x == 12)
    			{
    				System.out.printf("" + declare + sp + "K" + " = " + cardnum + ";" + "\n");
    			}
    				c++;
    			x++;
    			cardnum++;
    		}while (x <= 13);
    	}
     
    }


    To be honest I don't know if that will even help me. I need to learn arrays better.
    Last edited by Hallowed; April 4th, 2011 at 03:20 PM.

  8. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: BlackJack.. Random Card Assignment?

    Yikes. Let me see if I can give you a hint. Say you have a card Object:

    public class Card{
     
       Suit suit;
       Face value;
     
       public Card(Suit suit, Face value){
          this.suit = suit;
          this.value = value;
       }
    }

    Suit and Face are both enum types (I might even define them in the Card class). Now do you see how easy it would be to initialize all 52 Cards, just by looping through the enum constants?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. random uniform distribution with probability assignment
    By blascobz in forum Object Oriented Programming
    Replies: 0
    Last Post: February 28th, 2011, 09:16 AM
  3. Help with blackjack game
    By santosd1118 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 12th, 2010, 12:55 AM
  4. A little help with my Blackjack code
    By Neophyte in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 3rd, 2010, 01:13 PM
  5. Creating program Blackjack - Dr.Java
    By TheUntameable in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2010, 12:54 PM