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.
http://i51.tinypic.com/mbrakj.jpg
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.
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..
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.
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...
Code :
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 =/
Code Java:
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.
Re: BlackJack.. Random Card Assignment?
Yikes. Let me see if I can give you a hint. Say you have a card Object:
Code java:
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?