Reputation: 11
So I have to program Black Jack as a console program in C#. I added an enum with all the different card types in it (without colors, just the 9 different numbers from 2 to 10, a value for aces and one for picture cards) having 11 distinct values.
Then I made a method to return a random card from that enum. This works fine. But in the second step of the task I have to add the picture cards and I have to take care of the count of picture cards in the deck. The deck has 52 cards in total and 12 of them are picture cards. I tried the following but it doesn't work.
class Program
{
static Random rnd = new Random(); //Initialisierung Zufallsgenerator
static ECARDS DrawRandomCard() //Methode zum zufälligen ziehen einer Karte aus dem enum ECARDS
{
if ((int)DrawRandomCard() < 12)
{
return ECARDS.Picture;
}
else
{
return (ECARDS)rnd.Next(2, 11);
}
}
enum ECARDS { Zwei = 2, Drei = 3, Vier = 4, Fünf = 5, Sechs = 6, Sieben = 7, Acht = 8, Neun = 9, Zehn = 10, Ass = 11, Picture = 99 }; // enum mit allen verfügbaren Karten
I am a new, so maybe it is very easy for you. Thanks for your help.
Upvotes: 0
Views: 603
Reputation: 3212
Your approach of breaking down the problem to the different types of cards you need for Black Jack is good.
However, as @Plutonix and @Jean-Claude Colette already mentioned, you need to ensure that your 52 card deck initially has exactly the right proportion of different card types (4 for each number, 4 aces and 12 "picture" cards).
Next, in the process of dealing out cards, you must keep track of the cards that have already been dealt out (or, equivalently, which cards are remaining in the deck).
The easiest way to deal with both constraints is to declare an array (or List) of ECARDS
enum values of length 52
and pre-populate it with ECARDS
instances in the correct proportion (order doesn't matter). After that you randomly shuffle the array to obtain a completely random deck (pretty much what happens in the real world).
Now you can start to deal out cards traversing the array from index 0
to 51
. If you've reached the last possible index (51
) you simply re-shuffle the array and start the traversal from index 0
again.
The following example serves to illustrate this idea. It is written in Java, not C#. So, interpret it as pseudo-code.
class Deck {
static final int DECK_SIZE = 52;
ECARDS[] c = new ECARDS[DECK_SIZE];
Random rnd = new Random();
int idxNextCard = 0;
Deck() {
populateDeck();
reshuffleDeck();
}
void reshuffleDeck() {
for (int i = c.length - 1; i > 0; i--) {
int j = rnd.nextInt(i + 1);
ECARDS cj = c[j];
c[j] = c[i];
c[i] = cj;
}
idxNextCard = 0;
}
ECARDS drawNextCard() {
if (idxNextCard == c.length - 1) {
reshuffleDeck();
}
return c[idxNextCard++];
}
void populateDeck() {
int i = 0;
for (ECARDS ec : ECARDS.values()) {
if (ec == ECARDS.Picture) {
for (int j = 0; j < 12; j++) {
c[i++] = ec;
}
} else {
for (int j = 0; j < 4; j++) {
c[i++] = ec;
}
}
}
}
public static void main(String[] args) {
Deck d = new Deck();
for (int i = 0; i < 10 * DECK_SIZE; i++) {
if (i % DECK_SIZE == 0) {
System.out.println();
}
System.out.print(d.drawNextCard() + ", ");
}
}
Upvotes: 2