1. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
2
Rep Power
0

#### help a newbie

Hey, I'm trying to write a for loop for my simple game of 21 and have run into problems.

Basically, the user enters the amount of cards that he wants to draw, and I use the random library to give him the values from 1 to 10 for each of his cards (Computer always draws 3). However I can't figure out the for loop I need to do this...I think it would go something like

for(i=0; i<=drawn; i++)//not sure if this would be right
{
//dont know what goes in here!
}

But I don't know what to put inside of itt. A sample run of the program would look like this:

How many cards do you want? 4
Your Cards: 3 4 2 9
Computer: 4 7 1
You have 18 and I have 12 so you win. Play again? [Y/N]

Well if you can help me please do! Essentially I dont know how to make new cards for the user based off of his choice...thanks!
2. No Profile Picture
Offensive Member
Devshed Novice (500 - 999 posts)

Join Date
Oct 2002
Location
in the perfect world
Posts
622
Rep Power
31
In 21 (blackjack) you want to see each card before you get the next

So you will not know how many times the loop should run.

This means, usually that you should use a while or do while

You will need a 'deck'. Array with the card face (ie "Jack Hearts" to display) and value (ie10). Know about structures yet?

Start with two arrays. One in order (ie an unshuffled deck) use a random generator (srand() and rand() ) to swap the cards randomly into the other deck (ie the shuffled deck).

When the user draws a card increment the index of the shffled deck. When it gets to the end, reshuffle.

Breaking this into very small functions that can be reused, will reduce the size of the code (ie structured programming)

Code:
```//TODO draw a card each
//TODO display on screen
//will loop at least once so used do / while
do
{
iHit=FALSE
//TODO draw a card as players must have at least two
iTotal+=iNewCardValue;
//TODO update screen
if(iTotal<21)
//TODO ask if user wants new card
if(iUserWantsNewCard==TRUE)
iHit=TRUE;

}while( ( iTotal<=21) && (iHit==TRUE) )```
I would just draw for the dealer until the total is 17 or higher. Rather than a set number of cards.
Last edited by TechNoFear; December 10th, 2002 at 02:24 AM.
3. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
2
Rep Power
0
you misunderstand, my problem is easier than that :)

its a simplified blackjack, and the user doesn't get to make new cards, only the number he specifies in the beggining (which can be anything).

the thing is I don't know how to make the for loop that generates new cards for the user input, then stores them in memory to be added together :(

thanks
4. No Profile Picture
Offensive Member
Devshed Novice (500 - 999 posts)

Join Date
Oct 2002
Location
in the perfect world
Posts
622
Rep Power
31
Have you done arrays?

int iCardArray[21];

get teh number of cards needed by user

for(i=0;i<iDraw;i++)

in each loop

get new card
store in array

on exit
print to screen
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2002
Location
Flint, MI
Posts
328
Rep Power
16
Pariah,

If you're a newbie, you'll have to work with a simplified solution that isn't a perfect conceptual match for a deck of cards, but will have to do.

If you are experienced enough to use a struct, then you will want the following:
Code:
```#define SPADE 0
#define DIAMOND 1
#define HEART 2
#define CLUB 3

struct card {
int value;
int suit;
};```
Now make a big fat array (52 members) to hold all of your cards. Put them in there in order. That's so you can be sure that you have everything. Select two cards randomly and rotate them (if rand() drops you on the same card for each one, move forward or backward in the list by one card so that you still have two selected). Lather, rinse, repeat. Now you can deal 3 off the top for the dealer and however many the player needs.

If you can't handle structs yet, you can get the same effect by using two arrays, but you need to make sure that your rotations move both the suit and the value.

Also remember that while there are 13 different card values in a poker deck. J Q and K are all considered to be worth 10 in blackjack. Also remember that A can be 1 or 10, depending on the need.
6. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2002
Posts
56
Rep Power
16
also remember that if you get 5 cards without going over 21, you automatically win.
7. Originally posted by ShawnD

also remember that if you get 5 cards without going over 21, you automatically win.

Depends where you play (not in my house!) :)
8. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2002
Posts
56
Rep Power
16
casino rules :)

it's really hard to do it actually so dont depend on it.