Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Tryin to create a deck of cards

  • 03-08-2011 3:05pm
    #1
    Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭


    Hey!

    Im tryin to create a deck of cards and im having a bit of trouble.

    I have a card class with a string to represent the suit and an int for the value of the card.
    I have setters and getters for both.

    Then I have a deck class with a card array to hold the deck.

    Im having trouble generating the deck of cards tho.

    my code for generating the desk is;

    public card[] generateDeck(card c){
    c=new card();
    int value=2;                                  //value of first card is two. Up to 15 for one suit
    for(int j=0;j<12;j++){                     //13 cards in a suit          
    c.setSuit("hearts");
    c.setValue(value);
    deck[j]=c;
    value++;
    }
    }
    

    My problem is that the card value is not increasing. Its just stayin the same no matter what i seem to do. Any advice would be great!!

    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Because you only have one card object. You only go "new card()" once, rather than doing it inside the loop. Thus you have an array which contains references to the same object => same value.


  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    srsly78 wrote: »
    Because you only have one card object. You only go "new card()" once, rather than doing it inside the loop. Thus you have an array which contains references to the same object => same value.

    Ahhhh... thanks very much that sorted it right out. Stupid thing to do!!

    thanks alot!!


  • Registered Users, Registered Users 2 Posts: 3,323 ✭✭✭padraig_f


    jonny666 wrote: »
    for(int j=0;j<12;j++){ //13 cards in a suit


    this will only go around 12 times.

    would probably also use an enum for suits and card values, they're more efficient than strings and have the advantage over ints that they're restricted to only valid values and are more descriptive.

    e.g..
          enum Suit
          {
             Clubs = 1,
             Diamonds,
             Hearts,
             Spades
          };
    
          enum Rank
          {
             Deuce = 2,
             Trey,
             Four,
             Five,
             Six,
             Seven,
             Eight,
             Nine,
             Ten,
             Jack,
             Queen,
             King,
             Ace
          };
    


  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    padraig_f wrote: »
    this will only go around 12 times.

    would probably also use an enum for suits and card values, they're more efficient than strings and have the advantage over ints that they're restricted to only valid values and are more descriptive.

    e.g..
          enum Suit
          {
             Clubs = 1,
             Diamonds,
             Hearts,
             Spades
          };
    
          enum Rank
          {
             Deuce = 2,
             Trey,
             Four,
             Five,
             Six,
             Seven,
             Eight,
             Nine,
             Ten,
             Jack,
             Queen,
             King,
             Ace
          };
    


    Thanks for the reply Padraig. To be honest im only learning still and I havent covered enum yet.

    The reason i only wanted my loop to go like that was i was goina do one loop for each suit


  • Registered Users, Registered Users 2 Posts: 3,323 ✭✭✭padraig_f


    jonny666 wrote: »
    Thanks for the reply Padraig. To be honest im only learning still and I havent covered enum yet.
    Fair enough.
    for(int j=0;j<12;j++){                     //13 cards in a suit     
    
    The reason i only wanted my loop to go like that was i was goina do one loop for each suit

    You'll still need to change the 12 to 13, as it is, it's only going around 12 times (0 to 11), so you'll be one card short per suit.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,042 ✭✭✭Groinshot


    What about an array from 0 to 51. one number for each card. 0-12 are one suit etc etc. That way you only need one random number to draw from the deck?


  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    Groinshot wrote: »
    What about an array from 0 to 51. one number for each card. 0-12 are one suit etc etc. That way you only need one random number to draw from the deck?

    Ya thats what i ended up doin in the end. The other way was just to get things working first.

    I thought this would be an easy thing to do but it turns out its more complicated then I had anticipated!! Ah well...good test to see how im doin!!


Advertisement