Like Tree2Likes

Random suites for black jack game..

This is a discussion on Random suites for black jack game.. within the C Programming forums, part of the General Programming Boards category; Ok I am trying to add card suits to my game. I completed the game simulation but just trying to ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    17

    Question Random suites for black jack game..

    Ok I am trying to add card suits to my game. I completed the game simulation but just trying to add some more to it. currently the game will generate random cards ie(numbers) include k for king, Q for queen, j for jack, and A for ace. Now I want to add random suits to this. I created the function below but i keep getting. the following error message when I compile.


    342 conflicting types for 'dealFace'
    44 previous declaration of 'dealFace' was here

    currently the program will print out all four suits accross the top like a logo. Now I would like to add suits to my random card numbers.

    Also how do I add color to background. Like change it to a green, or have the screen flash when player wins. or some kinds of chime.

    which ever is simplist I will try to use. I'm just a beginner so if it is complicated for me to follow right now. I will not use, but appreciate any suggestions.
    Thanks.. Code below.



    Code:
     const char HEART = (char)259;  
      const char DIAMOND = (char)260;
      const char SPADES = (char)262;
      const char CLUBS = (char)261;
    
    
     char dealFace( char face[] );
      char face [4] = { HEART, DIAMOND, SPADES, CLUBS};
    
     dealFace( char face[] ) char dealFace( char face[] );
    {
      
        int value = 0;
        int random = 0; 
    
        random = rand() % 52 + 1;    //Random number generation
        
        while( face[random] == 0 ) {
          random = rand() % 52 + 1;
                     
        }
      
        value = face[random];
        face[random] = 0;
      
        return value; 
      
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    card = 1 + (rand() % 52);
    suit = card / 13;
    face = card % 13;
    1-13 = hearts, 14-26 = diamonds, 27-39 = spades, 40-52 = clubs

    Or whatever order you prefer.


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

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Darius Dempsey View Post

    Code:
     char dealFace( char face[] );
     
     dealFace( char face[] ) char dealFace( char face[] );
    {
    // ...
    }
    You have a double line there, is that in your actual code?

    All your global variables are overflowing.

    face is an array of size 4, but you address it as if it where size 52.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    actually it should be 4, guess depends on how cde is written, but according to another suggestion maybe keep it at 52.

    Also should I declare function as a char, or int?
    Last edited by Darius Dempsey; 04-02-2011 at 05:19 PM.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Ok, actually it needs to be 53 given how an array is indexed and the code you have posted so far.

    Since your random value can be up to 52, when you address your array: face[random] you are highly likely to be out of bounds since the highest index you can use is face[3].

    Quote Originally Posted by Darius Dempsey View Post
    Also should I declare function as a char, or int?
    Since the variable you are returning is declared as an int, return an int.
    Last edited by Subsonics; 04-02-2011 at 05:26 PM.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    Well I originally had it declard as Char since it is an actually symbol/charther I am trying to display. So should it be Char or int?

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Either way, declare value as char and return char, or declare value as int and return int. In the code you are showing here you aren't using characters.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    ok so is this how I should declare it? because i am getting syntax error of invalid type.
    Code:
    char dealFace( char face[] );
      char face [52] = {1 - 13 = HEART, 14 - 26 = DIAMOND, 27 - 40 = SPADES, 41 - 52 = CLUBS};

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    No that wont work, you need to initialize the entire array. Although you could use that approach, for practical reasons quazah's suggestions is probably better.

    My comments so far is related to the code you have posted, not the best way to solve what you are trying to achieve.

    Code:
    suit = card / 13;
    Will give you a value in the range of 0 - 3, that you then can use to index your array. suit needs to be an integer type, then truncation will take care of the rest.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Simple trick... (although it does require a bit of out of order thinking)...

    Code:
    int deck[52]; 
    
    suite = deck[i] % 4;
    
    card = deck[i] / 4;
    This means your aces are numbered 0, 1, 2, 3
    The deuces are numbered 4, 5, 6, 7
    etc.

    And while I'm at it here's a better card shuffle that gurantees no duplicates
    Code:
    // at top of program
    int deck[52];
    srand(time(NULL));
    
    // fill deck
    for (x = 0; x < 52, x++)
       deck[x] = x;
    
    // shuffle
    int y, t;  // card and temp
    for (x = 51; x > 0; x++)
      { y = rand() % x;
         t = deck[y];
          deck[y] = deck[x];
           deck[x] = t; }
    Last edited by CommonTater; 04-02-2011 at 07:52 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Simple trick... (although it does require a bit of out of order thinking)...

    Code:
    int deck[52]; 
    
    suite = deck[i] % 4;
    
    card = deck[i] / 4;
    The difference between this and what I've already shown is that this group all of the 1s together, and all of the 2s together, etc., instead of grouping by suit.


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

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    The difference between this and what I've already shown is that this group all of the 1s together, and all of the 2s together, etc., instead of grouping by suit.


    Quzah.
    Yes, it's a different organization of the deck. But it's one I've found convenient in code...
    Nobody said your was was wrong. I just thought mine would make an interesting alternative.

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    Thanks guys I finally got this to work.. but believe or not, I still can't get characters to work in my program...although when I did them outside of the prog they did work.. I checked declarations type, I do have variable declared as char, and the scanf set up to get %c.. but wont work in program... anyways that was a different post.. just saying you would think get Chars would be easier than setting up arrays and functions...

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Yes, it's a different organization of the deck. But it's one I've found convenient in code...
    Nobody said your was was wrong. I just thought mine would make an interesting alternative.
    I was mainly explaining the difference for the OP, not saying either way was better. They're both better than maintaining a struct with the suit and face value in my opinion.


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

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    I was mainly explaining the difference for the OP, not saying either way was better. They're both better than maintaining a struct with the suit and face value in my opinion.
    Agreed. Some things just don't lend themselves well to structs... others do.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  3. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-26-2006, 12:14 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 08:41 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21