C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old December 9th, 2002, 03:52 PM
pariah pariah is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 2 pariah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy 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!

Reply With Quote
  #2  
Old December 10th, 2002, 02:16 AM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 594 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 6 m 15 sec
Reputation Power: 21
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.

Reply With Quote
  #3  
Old December 11th, 2002, 06:46 PM
pariah pariah is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 2 pariah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old December 11th, 2002, 09:33 PM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 594 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 6 m 15 sec
Reputation Power: 21
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

Reply With Quote
  #5  
Old December 16th, 2002, 02:23 PM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
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/

Reply With Quote
  #6  
Old December 28th, 2002, 06:34 AM
ShawnD ShawnD is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 56 ShawnD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to ShawnD
also remember that if you get 5 cards without going over 21, you automatically win.
__________________
PHP is fun

Reply With Quote
  #7  
Old December 28th, 2002, 10:14 AM
GNUbie's Avatar
GNUbie GNUbie is offline
Throws Rocks
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Location: Cincinnati, Ohio
Posts: 392 GNUbie User rank is Corporal (100 - 500 Reputation Level)GNUbie User rank is Corporal (100 - 500 Reputation Level)GNUbie User rank is Corporal (100 - 500 Reputation Level)GNUbie User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 6 h 31 m 7 sec
Reputation Power: 8
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.

Reply With Quote
  #8  
Old December 28th, 2002, 06:08 PM
ShawnD ShawnD is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 56 ShawnD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to ShawnD
casino rules

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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > help a newbie


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway