7 Replies - 5462 Views - Last Post: 02 February 2011 - 01:04 PM Rate Topic: -----

#1 jonmar2348  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-January 11

Simple Card Game

Posted 28 January 2011 - 03:40 PM

Hello everyone, I'm trying to write a program that deals two three-card hands, evaluates each hand, and determine which is the better hand. I problem I'm having is that I can't make two three-card hands. Can anyone help me out with that part.

Thanks

This is what I have so far:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void shuffle( int wDeck[][ 13 ] );
void deal( const int wDeck[][ 13 ], const char *wFace[],
           const char *wSuit[] );

int main( void )
{

   const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };


   const char *face[ 13 ] =
      { "50 - Ace", "2 - Deuce", "6 - Three", "10 - Four",
        "14 - Five", "18 - Six", "22 - Seven", "26 - Eight",
        "30 - Nine", "34 - Ten", "38 - Jack", "42 - Queen",
        "46 - King" };

   int deck[ 4 ][ 13 ] = { 0 };

   srand( time( 0 ) );

   shuffle( deck );
   deal( deck, face, suit );
   return 0;
}

void shuffle( int wDeck[][ 13 ] )
{
   int row;
   int column;
   int card;

   for ( card = 1; card <= 52; card++ ) {

      do {
         row = rand() % 4;
         column = rand() % 13;
      } while( wDeck[ row ][ column ] != 0 );

      wDeck[ row ][ column ] = card;
   }
}

void deal( const int wDeck[][ 13 ], const char *wFace[],
           const char *wSuit[] )
{
   int card;
   int row;
   int column;

   for ( card = 1; card <= 3; card++ ) {

      for ( row = 0; row <= 3; row++ ) {

         for ( column = 0; column <= 12; column++ ) {

            if ( wDeck[ row ][ column ] == card ) {

               printf( "%5s of %-8s%c", wFace[ column ], wSuit[ row ],
                  card %1 == 0 ? '\n' : '\t' );
            }
         }
      }
   }
}

 


Is This A Good Question/Topic? 0
  • +

Replies To: Simple Card Game

#2 xTorvos  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 271
  • Joined: 23-October 09

Re: Simple Card Game

Posted 28 January 2011 - 03:49 PM

Is there a reason that your deck is a 4x13 array? A deck is more like a stack or queue than a grid.

Edit: Sorry for being non-descriptive. The reason I ask is because, if I were you, I would "pop" a card off of the deck and "push" it onto an array that would represent a "hand".

This post has been edited by xTorvos: 28 January 2011 - 03:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 jonmar2348  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-January 11

Re: Simple Card Game

Posted 28 January 2011 - 04:22 PM

The 4X13 array is for the the cards. The 4 array holds the suit of the card and the 13 array holds the number that is on the card.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5761
  • View blog
  • Posts: 12,574
  • Joined: 16-October 07

Re: Simple Card Game

Posted 28 January 2011 - 04:32 PM

So, what are you doing here?
row = rand() % 4;
column = rand() % 13;



You understand that if your card value is between 0..51 and applying those mods will get you face and suit:
for ( card = 0; card < 52; card++ ) {
	printf("suit=%d face=%d\n", card % 4, card % 13);



What if:
#define DECK_SIZE 52
typedef int Card;
typedef Card Deck[DECK_SIZE];

int getFace(Card);
int getSuit(Card);
const char *getFaceName(Card);
const char *getSuitName(Card);
void initDeck(Deck);
void shuffleDeck(Deck);


Was This Post Helpful? 0
  • +
  • -

#5 jonmar2348  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-January 11

Re: Simple Card Game

Posted 28 January 2011 - 04:53 PM

[quote name='baavgai' date='28 January 2011 - 04:32 PM' timestamp='1296257550' post='1233890']
So, what are you doing here?
row = rand() % 4;
column = rand() % 13;



The row generates the suits at random and the columns generates the faces at random.
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1621
  • View blog
  • Posts: 3,078
  • Joined: 30-May 10

Re: Simple Card Game

Posted 29 January 2011 - 12:48 AM

> This is what I have so far:
You mean this is what you found lying around on the internet in innumerable places.
http://www.daniweb.c...read290595.html
http://cboard.cprogr...cards-deal.html
http://www.cplusplus.../beginner/9201/

How about posting some code YOU WROTE YOURSELF so that we can actually see what you're capable of, and thus provide help that you might actually be able to understand.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_dave*


Reputation:

Re: Simple Card Game

Posted 02 February 2011 - 11:08 AM

View PostSalem_c, on 29 January 2011 - 12:48 AM, said:

> This is what I have so far:
You mean this is what you found lying around on the internet in innumerable places.
http://www.daniweb.c...read290595.html
http://cboard.cprogr...cards-deal.html
http://www.cplusplus.../beginner/9201/

How about posting some code YOU WROTE YOURSELF so that we can actually see what you're capable of, and thus provide help that you might actually be able to understand.

Most of the code is from the book the task was to modify the code into a three card game, so yes, he found it somewhere, in his text book, it was a legitmate question.
Was This Post Helpful? 0

#8 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1621
  • View blog
  • Posts: 3,078
  • Joined: 30-May 10

Re: Simple Card Game

Posted 02 February 2011 - 01:04 PM

> it was a legitmate question.
Says who?
- some anonymous drive-by, who is perhaps hoping their little missive will prompt a reply with the answer?
- the forum rules which basically state there is no reward for ZERO effort. Copy/paste + "I tried...." doesn't count unless you post what you actually tried.

Now you need to understand a few more things as well.
1. Helpers here can do these kinds of problems in less time than it takes some people to get a coffee. That isn't the point (oh, it's only a few minutes for someone who knows what they're doing). We're here as guides (for the curious), not pack mules (for the lazy).

2. Most of the pro's here have at one time or another been forced to work with someone who is so clueless, as to wonder how the hell they got that job in the first place, and what a bloody marvellous ass-kisser they must be. The more lazy and inept people that can be winnowed out now, the better for everyone later on. You think homework is tough - wait till you see what you need to do to hold down a job (for sure, you won't be able to google for your code and then pretend it's yours).

3. Even if we did spoon-feed the answer, it would still have to get past the Goldilocks problem. Namely, any answer would have a 66% chance of being either "too easy" or "too hard". This is NOT productive use of OUR time. By knowing what a poster is really capable of (evidence: post your own code), we can tune the response accordingly.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1