Thread: Card game help

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    27

    Card game help

    I am making a card game(that i invented my self)but i need to know how to make it so that i get random cards...i no how to do that but not how to make it so the same card apears again...and i am sorry but the last time i posted a card game i invented some 1 copyed it and said it was there so i can not post my actual code sorry
    compiler:dev-C++
    asr

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    rand() will give you a random number from 0 to RAND_MAX (usually about 32768). Call srand(time(0)) to seed it - oh yes, and include <time.h> too (or <ctime> for C++).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    27
    i no how to do that but how can i make it so the same card dosnt come up twice
    compiler:dev-C++
    asr

  4. #4
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    In what kind of card game do you draw a random card from the deck? o_O

    Anyway...
    I'd recommend putting all of your cards into a vector, something like:

    std::vector <Card> deck;

    Then using deck.push_back() to add on each card in the deck. To draw a card:

    Code:
    // Gets a random number within the size of deck.
    int randNum = (rand()*(RAND_MAX+1))%deck.size();
    
    // Sets your new card to the correspond card in your deck
    Card newCard = deck[randNum];
    
    // Removes that card from the deck - syntax is ugly - could probably be done cleaner.
    deck.erase(deck.begin()+(randNum-1), deck.begin()+randNum);
    Should work, but I'm not positive and I'm certainly no expert.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    27
    k ill try it and experiment
    compiler:dev-C++
    asr

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    Without using the STL. You just need to create a deck, shuffle it and take one of the top at a time.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Exactly, a stack. And to "shuffle" it you could use rand().

    Is the card game in C++?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    Quote Originally Posted by sand_man
    Without using the STL. You just need to create a deck, shuffle it and take one of the top at a time.
    I thought by "a random card" he meant any card from the deck at any time?

    Although I guess that's pretty much the same thing as shuffling it once and taking it off the top...hmm.


    I'm silly.

  9. #9
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Woah.. what's a vector and where can I find detailed information on how to use them? cause when I did my card game I just used the deck as a class.. and had bool card[56] containing 1 or 0 if the card is in place or not, then i randomized between 1 and total number of cards left, and finally through a while loop, got that card out of the deck and showed it to the user once.. had some functions like clear_deck(), get_card(), and stuff...
    what does signature stand for?

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    27
    sorry for not replying i was in calgary
    compiler:dev-C++
    asr

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector out of range program crash.
    By Shamino in forum C++ Programming
    Replies: 11
    Last Post: 01-18-2008, 05:37 PM
  2. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  3. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  4. Video card mystery
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-30-2003, 02:56 PM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM