Thread: stuck on card game in C. any suggestions?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    9

    stuck on card game in C. any suggestions?

    I'm not asking for the answer, I'm just asking to be pointed in the right direction!

    I'm not going to post all the code because i just need help in one section of it:

    Code:
    void filldeck(card deck[52])
    
    {
    
       /*fill the deck here*/
    	
    
       return;
    
    }
    The deck needs to be saved in hex format into the array, i understand that hex value format is 0x[hex number]

    the way the card will be saved is:

    01000011

    first 2 bits (11) will be the suit (11 = heart, 01 = spade, 10 = club, 00 = diamond)
    next 4 bits (0000) will be the card number
    next 1 bit (1) wil be the colour (1 = red, 0 = black)
    last 1 bit (0) is blank

    im aware i need the 01000011 converted to hex to be saved into the array.
    based on this i need to generate a deck of 52 cards, i can write the cards hex values out on a paper (by hand), but how would i go about making the code generate it into the array?

    if someone can please guide me to the right resource maybe i'd really appreicate it, ive looked everywhere can't find anything that will help

    edit: I also think i should get rid of the ++ in my username :P
    Last edited by ihatec++; 03-18-2010 at 08:20 AM.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    4 for loops?

    Why are you saving the color? Every card deck I've ever seen had hearts and diamonds as RED, spades and puppy-dog-feet (clubs) are black.

    Not the real answer, so I don't mind posting:
    Code:
    j = 0;
    for (i = 1; i <=13 ; i++) {  // Working with Hearts
            deck[j++] = (1 << 6) | (i < 2) | 3;
    }
    for (i = 1 ; i <= 13; i++){ // Working with clubs
      ETC.
    EDIT: Crap! I guess it was the real answer after all, sorry about that. I meant to change it up a bit.

    EDIT 2: MK27's post showed me I had miscounted 7 -> 6. I really hate DST.
    Last edited by Kennedy; 03-18-2010 at 09:03 AM.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ihatec++ View Post
    edit: I also think i should get rid of the ++ in my username :P
    No.

    I'm not sure why you want to use hex values with the bit masking here, but go ahead. Here's some ideas (using decimal numbers, you can convert them yourself if you get a kick out of hex):
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void getsuit(int n, char *suit) {
    	switch (n) {
    		case (0): 
    			strcpy(suit,"diamonds");
    			break;
    		case (1): 
    			strcpy(suit,"spades");
    			break;
    		case (2): 
    			strcpy(suit,"clubs");
    			break;
    		case (3): 
    			strcpy(suit,"hearts");
    			break;
    		default: break;
    	}
    }
    
    
    int main() {
    	unsigned bitflags = 0, 
    		suitmask, valuemask, colorbit = 1<<6;
    	char suit[16];
    
    	suitmask = ~(~0U<<2);
    	valuemask = ~((~0U<<6)+3);
    
    	/* set hearts */
    	bitflags ^= 3;
    	bitflags ^= (1<<6);
    
    	/* set jack */
    	bitflags ^= (11<<2);
    
    	/* show */
    	getsuit(bitflags&suitmask,suit);
    	printf ("Suit: %s\nValue: %d\nColor: %s\n",
    		suit, (bitflags&valuemask)>>2, bitflags&colorbit ? "red" : "black");
    
    	return 0;
    }
    I'm presuming you understand bitshifting which is why you want to do this. Anyway, ~ is "complement of", 0U is all zero, so ~0U is all ones...
    Last edited by MK27; 03-18-2010 at 08:38 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's an easier way to do this:

    1. Generate a number between 1 and 52.
    2. Face = number % 13.
    3. Suite = number / 13.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by quzah View Post
    There's an easier way to do this:

    1. Generate a number between 1 and 52.
    2. Face = number % 13.
    3. Suite = number / 13.


    Quzah.
    Still no good way to get the color, though.

    > first 2 bits (11) will be the suit (11 = heart, 01 = spade, 10 = club, 00 = diamond)

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    9
    Thanks for the feedback, I'm nearly done with this project it was MUCH simpler then i thought

    edit: again thanks everyone!!

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Kennedy View Post
    Still no good way to get the color, though.

    > first 2 bits (11) will be the suit (11 = heart, 01 = spade, 10 = club, 00 = diamond)
    Code:
    if( card / 13 < 2 ) red else black
    Gee that was tough. Or you could just do:
    Code:
    cardcolor = card > 26 ? black : red;

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > > first 2 bits (11) will be the suit (11 = heart, 01 = spade, 10 = club, 00 = diamond)
    What colours are suits in your decks Quzah?

    Their "friend" has a much better ordering of suits (for extracting colour)
    C Bit Manipulation
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I don't understand why there's a need to keep track of the color separately? Just do the suit bits correctly and you get the color too? Or am I missing something?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. Best communication choice for two-person card game.
    By nucleon in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-17-2008, 08:16 PM
  3. Design suggestions: interacting with game items
    By anon in forum C++ Programming
    Replies: 3
    Last Post: 09-19-2008, 08:03 AM
  4. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  5. Sign up: The Card Game Contest
    By ygfperson in forum Contests Board
    Replies: 40
    Last Post: 09-24-2002, 05:43 PM