The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
help a newbie
Discuss help a newbie in the C Programming forum on Dev Shed. help a newbie C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 9th, 2002, 03:52 PM
|
|
Junior Member
|
|
Join Date: Dec 2002
Posts: 2
Time spent in forums: < 1 sec
Reputation 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!
|

December 10th, 2002, 02:16 AM
|
|
Offensive Member
|
|
Join Date: Oct 2002
Location: in the perfect world
|
|
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.
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.
Frank Zappa
Last edited by TechNoFear : December 10th, 2002 at 02:24 AM.
|

December 11th, 2002, 06:46 PM
|
|
Junior Member
|
|
Join Date: Dec 2002
Posts: 2
Time spent in forums: < 1 sec
Reputation 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
|

December 11th, 2002, 09:33 PM
|
|
Offensive Member
|
|
Join Date: Oct 2002
Location: in the perfect world
|
|
|
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
add to total
store in array
on exit
print to screen
|

December 16th, 2002, 02:23 PM
|
|
Contributing User
|
|
Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
|
|
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.
__________________
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/
|

December 28th, 2002, 06:34 AM
|
|
Contributing User
|
|
Join Date: Jul 2002
Posts: 56
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
|
also remember that if you get 5 cards without going over 21, you automatically win.
__________________
PHP is fun
|

December 28th, 2002, 10:14 AM
|
 |
Throws Rocks
|
|
Join Date: Mar 2002
Location: Cincinnati, Ohio
Posts: 392
  
Time spent in forums: 6 h 31 m 7 sec
Reputation Power: 13
|
|
Quote: 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!) 
__________________
Two things have come out of Berkeley, Unix and LSD.
It is uncertain which caused the other.
|

December 28th, 2002, 06:08 PM
|
|
Contributing User
|
|
Join Date: Jul 2002
Posts: 56
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
casino rules
it's really hard to do it actually so dont depend on it.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|