Shuffle and Deal Cards [Archive] - C Board

PDA

View Full Version : Shuffle and Deal Cards


killmequick
03-19-2008, 01:02 PM
I need to make a program that shuffles a deck of cards and simply deals them into 4 hands
as soon as the program starts it will list something like this:
D9 H5 S10 C4 DJ C10 DA H2 D8 H10 D7 S9 HQ
and 3 more rows of 13

i have got this working correctly but is there anyway i could do it with a double array without using the constant characters. here is my code:


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

void shuffle( int wdeck[][13]);
void deal( const int wdeck[][13], const char *wface[],
const char *wsuit[]);
void pair( int a[]);


int main()
{
const char *suit[4] = {"H", "D", "C", "S"};

const char *face[13] = {"A", "2", "3", "3", "5",
"6", "7", "8", "9", "10", "J", "Q", "K"};

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 <= 52; card++ ) {

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

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

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

printf("%5s%-8s%\n", wface[column], wsuit[row],
card % 52 == 0 );


}
}
}
}_getch();
}

Aran
03-19-2008, 07:47 PM
This is C++.

Please ask a moderator to move it to the C++ forum, or delete it and repost it there.

vart
03-20-2008, 12:26 AM
Actually - this is C

On the C board I have just now answered question - how to allocated 2D dynamic array base on user input

killmequick
03-21-2008, 12:48 AM
where have you done this array

vart
03-21-2008, 01:30 AM
http://cboard.cprogramming.com/showthread.php?t=100587