home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Archive Magazine 1997
/
ARCHIVE_97.iso
/
discs
/
mag_discs
/
volume_03
/
issue02
/
c2
< prev
next >
Wrap
Text File
|
1989-10-25
|
3KB
|
150 lines
#include <stdio.h>
#include <stdlib.h>
typedef int Card;
typedef int Suit;
typedef int Rank;
#define CLUBS 1
#define DIAMONDS 2
#define SPADES 3
#define HEARTS 4
#define FIRST_SUIT 1
#define LAST_SUIT 4
Card make_card( Suit suit, Rank rank )
{
return (suit << 4) + rank;
}
Suit card_suit( Card c )
{
return c >> 4;
}
Rank card_rank( Card c )
{
return c & 0xf;
}
Card pack[52];
void initialise_pack_old(void)
{
Suit s;
Rank r;
int i = 0;
for (s = FIRST_SUIT; s <= LAST_SUIT; s = s + 1)
for (r = 1; r < 14; r = r + 1)
{
pack[ i ] = make_card( s, r );
i = i + 1;
}
}
void initialise_pack(void)
{
Suit s;
Rank r;
int i = 0;
for (s = FIRST_SUIT; s <= LAST_SUIT; s++)
for (r = 1; r < 14; r++)
pack[ i++ ] = make_card( s, r );
}
void shuffle_pack(void)
{
int i;
for (i = 51; i != 0; i--)
{
int j = rand() % (i + 1);
if (i != j)
{
Card c = pack[i];
pack[i] = pack[j];
pack[j] = c;
}
}
}
Card players[4][13];
void deal_cards(void)
{
int i = 0, player, card;
for (player = 0; player < 4; player++)
for (card = 0; card < 13; card++)
players[player][card] = pack[i++];
}
char *ranks = "?A23456789TJQK";
char *suits[] =
{
"(no suit)",
"clubs",
"diamonds",
"spades",
"hearts"
};
void print_card( Card c )
{
printf
(
"%c of %s",
ranks[ card_rank( c ) ],
suits[ card_suit( c ) ]
);
}
void print_pack(void)
{
int i;
for (i = 0; i < 52; i++)
{
print_card( pack[i] );
printf( "\n" );
}
}
void print_hands(void)
{
int player, i;
for (player = 0; player < 4; player++)
{
printf( "Hand for player %d\n", player );
for (i = 0; i < 13; i++)
{
printf( " " );
print_card( players[player][i] );
printf( "\n" );
}
}
}
int main( int argc, char *argv[] )
{
initialise_pack();
shuffle_pack();
printf( "Shuffled pack:\n" );
print_pack();
deal_cards();
printf( "Dealt hands:\n" );
print_hands();
}