473,732 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

selecting and working with appropriate data type

I'm trying to figure out what data type is appropriate to represent a
card in a game. The idea that I thought was going to work was a
struct, foo, with two integer fields and two fields of char arrays:
index cardno description suit
( 1, 1,Two of clubs ,'c')
( 2, 2,Three of clubs ,'c')
( 3, 3,Four of clubs ,'c')
( 4, 4,Five of clubs ,'c')
( 5, 5,Six of clubs ,'c')
Dec 19 '06 #1
13 2201
>>>>"LS" == lane straatman <gr**********@n etzero.netwrite s:

LSI'm trying to figure out what data type is appropriate to
LSrepresent a card in a game. The idea that I thought was going
LSto work was a struct, foo, with two integer fields and two
LSfields of char arrays:

Why use a struct when an integer will suffice?

card_no / 13 will give you the suit, 0-3.

card_no % 13 will give you the number, 0-12.

Everything else in your data structure is redundant information.

Charlton
--
Charlton Wilbur
cw*****@chromat ico.net
Dec 19 '06 #2

lane straatman wrote:
I'm trying to figure out what data type is appropriate to represent a
card in a game. The idea that I thought was going to work was a
struct, foo, with two integer fields and two fields of char arrays:
index cardno description suit
( 1, 1,Two of clubs ,'c')
( 2, 2,Three of clubs ,'c')
( 3, 3,Four of clubs ,'c')
( 4, 4,Five of clubs ,'c')
( 5, 5,Six of clubs ,'c')
.
. [cardno = index % 13]
.
( 48, 9,Ten of spades ,'s')
( 49, 10,Jack of spades ,'s')
( 50, 11,Queen of spades ,'s')
( 51, 12,King of spades ,'s')
( 52, 13,Ace of spades ,'s')
Let's say a person is dealt card 5 and 48. This means foo.index = 5 &&
foo.index = 48 . How does a person test to see whether he has a pair?
I can't get my head around it and suspect that my datatype as it is
isn't as useful as I thought it was going to be. Any hints
appreciated. LS
Isn't this classically an application of enumerations - one for suit
and one for value?

Dec 19 '06 #3
"Charlton Wilbur" <cw*****@chroma tico.netwrote in message
news:87******** ****@mithril.ch romatico.net...
>>>>>"LS" == lane straatman <gr**********@n etzero.netwrite s:

LSI'm trying to figure out what data type is appropriate to
LSrepresent a card in a game. The idea that I thought was going
LSto work was a struct, foo, with two integer fields and two
LSfields of char arrays:

Why use a struct when an integer will suffice?

card_no / 13 will give you the suit, 0-3.

card_no % 13 will give you the number, 0-12.

Everything else in your data structure is redundant information.
With regard to the OP's question ...

There are several measures of the "goodness" of a representation:

a)Whether you can store and extract the information you commonly need
efficiently. (i / 13 and i %13 are VERY efficient.)

b)Whether the representation is human-interpretable (to interpret memory
dumps, debugging info, etc.).

c)Whether all of the bit patterns that may occur are valid (if not, it tends
to lead to logical errors). (Generally, the best case is a set of
enumerations where the actual space is the cartesian product of each
individual space).

http://en.wikipedia.org/wiki/Cartesian_product

(and what luck, the URL above gives a playing card example).

Mr. Wilbur's suggestion is probably the best one possible.

A perhaps equal fallback would be (number) x (suit). However, it is my
understanding that jokers have no suit, so this means the cartesian product
is larger than the actual space -- bad.

An even less desirable representation would be (number) x (suit) x
(is_a_face_card ) x (is_a_joker). The problem with that representation is
that the actual space is smaller than the cartesian product of each
individual space. In other words, (number = 9) && (is_a_face_card == T) is
a contradiction (for example).

It is unavoidable to violate (c) for a variety of reasons. However, for
this problem, an integer in the range of 0..51 (or slightly larger to handle
jokers) is probably the best, especially given the speed with which most
processors will do an integer division these days.

Dec 19 '06 #4
lane straatman wrote:
I'm trying to figure out what data type is appropriate to represent a
card in a game. The idea that I thought was going to work was a
struct, foo, with two integer fields and two fields of char arrays:
index cardno description suit
( 1, 1,Two of clubs ,'c')
( 2, 2,Three of clubs ,'c')
( 3, 3,Four of clubs ,'c')
( 4, 4,Five of clubs ,'c')
( 5, 5,Six of clubs ,'c')
.
. [cardno = index % 13]
.
( 48, 9,Ten of spades ,'s')
( 49, 10,Jack of spades ,'s')
( 50, 11,Queen of spades ,'s')
( 51, 12,King of spades ,'s')
( 52, 13,Ace of spades ,'s')
Let's say a person is dealt card 5 and 48. This means foo.index = 5 &&
foo.index = 48 . How does a person test to see whether he has a pair?
I can't get my head around it and suspect that my datatype as it is
isn't as useful as I thought it was going to be. Any hints
appreciated. LS
As well as the advice you've also been given, you might usefully
look at /how you're going to use/ your data-type and have that
guide you towards your representation.

You may find that the ID for the card isn't a significant problem
and its choice is made easier by seeing what you want to do. For
example, it seems likely that you want to print a card out, read
a card in, shuffle a packful of cards, sort cards by suit or by
rank or by honour count ...

So consider taking an abstract type approach. You can, after all,
change how you represent a card later on.

(Especially if you have tests in place to help you avoid errors.)

--
Chris "HO. HO. HO." Dollin
Scoring, bah. If I want scoring I'll go play /Age of Steam/.

Dec 19 '06 #5
lane straatman wrote:
>
I'm trying to figure out what data type is appropriate to represent a
card in a game. The idea that I thought was going to work was a
struct, foo, with two integer fields and two fields of char arrays:
index cardno description suit
( 1, 1,Two of clubs ,'c')
( 2, 2,Three of clubs ,'c')
( 3, 3,Four of clubs ,'c')
( 4, 4,Five of clubs ,'c')
( 5, 5,Six of clubs ,'c')
.
. [cardno = index % 13]
.
( 48, 9,Ten of spades ,'s')
( 49, 10,Jack of spades ,'s')
( 50, 11,Queen of spades ,'s')
( 51, 12,King of spades ,'s')
( 52, 13,Ace of spades ,'s')
Let's say a person is dealt card 5 and 48. This means foo.index = 5 &&
foo.index = 48 . How does a person test to see whether he has a pair?
Consider the hand as an array.
Sort the array, and check for equality in consecutive elements.
Paul Hsieh wrote a program that I like,
which fully evaluates poker hands and scores them.

This is one that I wrote:

/* BEGIN shuffle.c */

#include <stdio.h>
#include <time.h>
/*
** #define HANDS 1
** #define DEALS 5
** #define LU_RAND_SEED 1124021992
** 1124021815
** 1141904096
** 1141906091
** 1143411367
** 123456789LU
*/
#define LU_RAND_SEED time(NULL) /* time(NULL), 1141904096 */
#define DEALS 5
#define HAND 5
#define CARDS (SUITS * RANKS)
#define SUITS (sizeof suit / sizeof *suit)
#define RANKS (sizeof rank / sizeof *rank)
#define LU_RAND(S) ((S) * 69069 + 362437 & 0xffffffffLU)

struct poker {
int suit;
int rank;
};

int pair(struct poker *hand);
int three(struct poker *hand);
int four(struct poker *hand);
int full(struct poker *hand);
int two_pair(struct poker *hand);
int straight(struct poker *hand);
int flush(struct poker *hand);
int compar_rank(voi d const *first, void const *second);
int compar_suit(voi d const *first, void const *second);
long unsigned shuffle(int *array, int n, long unsigned seed);
void s_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));

int main(void)
{
size_t card;
struct poker hand[HAND];
long unsigned deal;
long unsigned seed;
char *suit[] = {"Hearts","Diam onds","Clubs"," Spades"};
char *rank[] = {"Deuce","Three ","Four","Five" ,"Six",
"Seven","Eight" ,"Nine","Ten"," Jack","Queen"," King","Ace"
};
int deck[CARDS];

seed = (long unsigned)LU_RAN D_SEED;
printf("\nseed = %lu\n\n", seed);
deal = DEALS;
while (deal-- != 0) {
seed = shuffle(deck, CARDS, seed);
for (card = 0; card != HAND; ++card) {
hand[card].suit = deck[card] % SUITS;
hand[card].rank = deck[card] % RANKS;
}
if (pair(hand)) {
if (three(hand)) {
if (four(hand)) {
puts("Four of a Kind:");
} else {
if (full(hand)) {
puts("Full House:");
} else {
puts("Three of a Kind:");
}
}
} else {
if (two_pair(hand) ) {
puts("Two Pair:");
} else {
puts("Pair:");
}
}
} else {
switch (2 * flush(hand) + straight(hand)) {
case 0:
s_sort(hand, HAND, sizeof *hand, compar_rank);
printf("%s High:\n", rank[hand[4].rank]);
break;
case 1:
puts("Straight: ");
break;
case 2:
puts("Flush:");
break;
default:
puts("Straight Flush:");
break;
}
}
putchar('\n');
for (card = 0; card != HAND; ++card) {
printf("%s of %s\n",
rank[deck[card] % RANKS],
suit[deck[card] % SUITS]);
}
putchar('\n');
}
return 0;
}

int pair(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_rank);
return hand[1].rank == hand[0].rank
|| hand[2].rank == hand[1].rank
|| hand[3].rank == hand[2].rank
|| hand[4].rank == hand[3].rank;
}

int three(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_rank);
return hand[0].rank == hand[2].rank
|| hand[1].rank == hand[3].rank
|| hand[2].rank == hand[4].rank;
}

int four(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_rank);
return hand[0].rank == hand[3].rank
|| hand[1].rank == hand[4].rank;
}

int full(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_rank);
return hand[0].rank == hand[1].rank
&& hand[2].rank == hand[4].rank
|| hand[0].rank == hand[2].rank
&& hand[3].rank == hand[4].rank;
}

int two_pair(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_rank);
return hand[0].rank == hand[1].rank
&& hand[3].rank == hand[2].rank
|| hand[0].rank == hand[1].rank
&& hand[3].rank == hand[4].rank
|| hand[1].rank == hand[2].rank
&& hand[3].rank == hand[4].rank;
}

int straight(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_rank);
return (hand[0].rank == 12 && hand[1].rank == 0
|| hand[0].rank + 1 == hand[1].rank)
&& hand[1].rank + 1 == hand[2].rank
&& hand[2].rank + 1 == hand[3].rank
&& hand[3].rank + 1 == hand[4].rank;
}

int flush(struct poker *hand)
{
s_sort(hand, HAND, sizeof *hand, compar_suit);
return hand[0].suit == hand[4].suit;
}

int compar_rank(voi d const *first, void const *second)
{
int int_1 = (*(struct poker*)first).r ank;
int int_2 = (*(struct poker*)second). rank;

return int_2 int_1 ? -1 : int_2 != int_1;
}

int compar_suit(voi d const *first, void const *second)
{
int int_1 = (*(struct poker*)first).s uit;
int int_2 = (*(struct poker*)second). suit;

return int_2 int_1 ? -1 : int_2 != int_1;
}

long unsigned shuffle(int *array, int n, long unsigned seed)
{
int i, r;

array[0] = 0;
for (i = 1; n i; ++i) {
seed = LU_RAND(seed);
r = seed % (i + 1);
array[i] = 0;
array[i] = array[r];
array[r] = i;
}
return seed;
}

void s_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
size_t bytes;
unsigned char *array, *after, *i, *j, *k, *p1, *p2, *end, swap;

array = base;
after = nmemb * size + array;
if (nmemb (size_t)-1 / 4) {
nmemb /= 4;
} else {
nmemb = (nmemb * 3 + 1) / 7;
}
while (nmemb != 0) {
bytes = nmemb * size;
i = bytes + array;
do {
j = i - bytes;
if (compar(j, i) 0) {
k = i;
do {
p1 = j;
p2 = k;
end = p2 + size;
do {
swap = *p1;
*p1++ = *p2;
*p2++ = swap;
} while (p2 != end);
if (bytes + array j) {
break;
}
k = j;
j -= bytes;
} while (compar(j, k) 0);
}
i += size;
} while (i != after);
nmemb = (nmemb * 3 + 1) / 7;
}
}

/* END shuffle.c */
--
pete
Dec 19 '06 #6

pete wrote:
LS: fishing for hints on representing a poker game
Consider the hand as an array.
Sort the array, and check for equality in consecutive elements.
Paul Hsieh wrote a program that I like,
which fully evaluates poker hands and scores them.

This is one that I wrote:

/* BEGIN shuffle.c */

#include <stdio.h>
#include <time.h>
/*
** #define HANDS 1
** #define DEALS 5
** #define LU_RAND_SEED 1124021992
** 1124021815
** 1141904096
** 1141906091
** 1143411367
** 123456789LU
*/
#define LU_RAND_SEED time(NULL) /* time(NULL), 1141904096 */
#define DEALS 5
#define HAND 5
#define CARDS (SUITS * RANKS)
#define SUITS (sizeof suit / sizeof *suit)
#define RANKS (sizeof rank / sizeof *rank)
#define LU_RAND(S) ((S) * 69069 + 362437 & 0xffffffffLU)

struct poker {
int suit;
int rank;
};

int pair(struct poker *hand);
int three(struct poker *hand);
int four(struct poker *hand);
int full(struct poker *hand);
int two_pair(struct poker *hand);
int straight(struct poker *hand);
int flush(struct poker *hand);
int compar_rank(voi d const *first, void const *second);
int compar_suit(voi d const *first, void const *second);
long unsigned shuffle(int *array, int n, long unsigned seed);
void s_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
[main and beyond snipped for brevity]
Thanks all for replies and in particular pete for his generous post.
I'm a mile away from a compiler now, so I can't step through it, but it
looks as clean as a whistle.

One thing I want to do is to be able to calculate is how much of a
difference it makes in the odds when a card gets flipped up, which
happens fairly frequently in real life, and on which I have no numeric
handle. A card flipped up during the deal--in Western
Michigan--becomes the burn card. I'm unaware of how universal this
practice is. An upturned burn card would differ from the usual, I
should think.

Does Paul's version come up with a number for a hand that would inform
a decision? LS

Dec 20 '06 #7

lane straatman <gr**********@n etzero.netwrote in message
news:11******** **************@ 79g2000cws.goog legroups.com...
pete wrote:
LS: fishing for hints on representing a poker game
As is usually the case, it turns out he was looking to solve
Fermat's Last Theorum and asked how to add 2+2...
Consider the hand as an array.
Sort the array, and check for equality in consecutive elements.
Paul Hsieh wrote a program that I like,
which fully evaluates poker hands and scores them.

This is one that I wrote:

/* BEGIN shuffle.c */

#include <stdio.h>
[main and beyond snipped for brevity]
Thanks all for replies and in particular pete for his generous post.
I'm a mile away from a compiler now, so I can't step through it, but it
looks as clean as a whistle.
Well, as one of my "mentors" on one of my first jobs said, "if it
looks good, it must be good"...

I really do prefer my object-oriented C++ card-playing classes
myself...
One thing I want to do is to be able to calculate is how much of a
difference it makes in the odds when a card gets flipped up, which
happens fairly frequently in real life, and on which I have no numeric
handle. A card flipped up during the deal--in Western
Michigan--becomes the burn card. I'm unaware of how universal this
practice is. An upturned burn card would differ from the usual, I
should think.
It's just a card that can never be drawn to the community cards or
to a player's hand, so you remove it from the deck remainder that
you evaluate using combinatorial analysis...oh wait, are you writing
a combinatorial analyzer, or a "Monte Carlo" simulator, or
perhaps a simulator that can call in a combinatorial analysis
at any time, or what?

For my purposes (which include all of the above), I use three
arrays for cards ("deck", "discards/burns", and "table", with
hands being composed of pointers to "groups" of cards
on the "table"). A "card" will always be in one of the three
arrays...
Does Paul's version come up with a number for a hand that would inform
a decision? LS
Almost definitely not...but a poker "odds" analyzer is simpler
and more straightforward to write than for some other games
I can think of...

---
William Ernest Reid

Dec 20 '06 #8

Bill Reid wrote:
lane straatman <gr**********@n etzero.netwrote in message
news:11******** **************@ 79g2000cws.goog legroups.com...
pete wrote:
LS: fishing for hints on representing a poker game
As is usually the case, it turns out he was looking to solve
Fermat's Last Theorum and asked how to add 2+2...
It was only a century back that logicians proclaimed that "arithmetic
teeters."
Consider the hand as an array.
Sort the array, and check for equality in consecutive elements.
Paul Hsieh wrote a program that I like,
which fully evaluates poker hands and scores them.
>
This is one that I wrote:
>
/* BEGIN shuffle.c */
>
#include <stdio.h>
[main and beyond snipped for brevity]
Thanks all for replies and in particular pete for his generous post.
I'm a mile away from a compiler now, so I can't step through it, but it
looks as clean as a whistle.
Well, as one of my "mentors" on one of my first jobs said, "if it
looks good, it must be good"...

I really do prefer my object-oriented C++ card-playing classes
myself...
What is it that you gain from OO?
One thing I want to do is to be able to calculate is how much of a
difference it makes in the odds when a card gets flipped up, which
happens fairly frequently in real life, and on which I have no numeric
handle. A card flipped up during the deal--in Western
Michigan--becomes the burn card. I'm unaware of how universal this
practice is. An upturned burn card would differ from the usual, I
should think.
It's just a card that can never be drawn to the community cards or
to a player's hand, so you remove it from the deck remainder that
you evaluate using combinatorial analysis...oh wait, are you writing
a combinatorial analyzer, or a "Monte Carlo" simulator, or
perhaps a simulator that can call in a combinatorial analysis
at any time, or what?
Monte Carlo is the short answer. I'm only able to do combo with pen
and paper. It's nice to have a computer tell you that you've got the
correct answer. More than anything though, I like to see a scenario,
say, that you're dealt a pocket pair and see how the bidding might go
given tight/loose players, short/tall stacks and position. Right now,
I array the cards and "players" on my desk and try to do the odds
analysis. In real life, I'm having trouble playing with loose players
on tall stacks in cash games. The burning question there is usually
just when to push all in.
For my purposes (which include all of the above), I use three
arrays for cards ("deck", "discards/burns", and "table", with
hands being composed of pointers to "groups" of cards
on the "table"). A "card" will always be in one of the three
arrays...
Does Paul's version come up with a number for a hand that would inform
a decision? LS
Almost definitely not...but a poker "odds" analyzer is simpler
and more straightforward to write than for some other games
I can think of...
True. LS

Dec 20 '06 #9
lane straatman wrote:
>
pete wrote:
LS: fishing for hints on representing a poker game
Consider the hand as an array.
Sort the array, and check for equality in consecutive elements.
Paul Hsieh wrote a program that I like,
which fully evaluates poker hands and scores them.
Does Paul's version come up with a number for a hand that would inform
a decision? LS
Yes.

I deleted some unused variables and did very little reformatting.
This is pretty close to what he posted:

/* BEGIN pokeref.h */

#ifndef H_POKEREF_H
#define H_POKEREF_H

#define CLUB_SUIT (1)
#define DIAMOND_SUIT (2)
#define HEART_SUIT (4)
#define SPADE_SUIT (8)

#define RANK_SHL (27)
#define SUBR_SHL (13)

#define STRAIGHT_FLUSH_ SCORE (8 << RANK_SHL)
#define FOUR_KIND_SCORE (7 << RANK_SHL)
#define FULL_HOUSE_SCOR E (6 << RANK_SHL)
#define FLUSH_SCORE (5 << RANK_SHL)
#define STRAIGHT_SCORE (4 << RANK_SHL)
#define THREE_KIND_SCOR E (3 << RANK_SHL)
#define TWO_PAIR_SCORE (2 << RANK_SHL)
#define TWO_KIND_SCORE (1 << RANK_SHL)

#define ONE_PAIR_SCORE (TWO_KIND_SCORE )

#ifndef DWORD
typedef unsigned long DWORD;
#endif

#ifndef BYTE
typedef unsigned long BYTE;
#endif

#ifndef WORD
typedef unsigned long WORD;
#endif

typedef struct {
int len;
BYTE entry[52];
} CardPileType;

#endif

/* END pokeref.h */

/* BEGIN pokeref.c */

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

#include "pokeref.h"

DWORD CardValue[52] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
};

DWORD CardMask[52] = {
1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800,
0x1000,
1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800,
0x1000,
1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800,
0x1000,
1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800,
0x1000
};

DWORD CardSuit[52] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
};

DWORD PokerScore( BYTE * h )
{
DWORD u,y,z;
DWORD c0,c1,c2,c3,c4;
DWORD m1,m2,m3,m4;
/*
// Make suits powers of two.
*/
u = CardSuit[h[0]];
u |= CardSuit[h[1]];
u |= CardSuit[h[2]];
u |= CardSuit[h[3]];
u |= CardSuit[h[4]];
/*
// Test for single suitedness
*/
u = u&(u-1);
/*
// Make cards powers of two.
*/
c0 = CardMask[h[0]];
c1 = CardMask[h[1]];
c2 = CardMask[h[2]];
c3 = CardMask[h[3]];
c4 = CardMask[h[4]];
/*
// Build masks of 1, 2, 3, and 4 of a kind.
*/
m1 = c0 | c1;
m2 = c1 & c0;
m2 |= c2 & m1;
m1 |= c2;
m2 |= c3 & m1;
m1 |= c3;
m2 |= c4 & m1;
m1 |= c4;

if( m2==0 ) { /*// No pairs?*/
/*
// Is the mask a sequence of 1 bits?
*/
z = m1&(m1-1);
z ^= m1;
y = (z<<5) - z;
/*
// Deal with the bicycle/wheel 5,4,3,2,Ace straight
*/
if ( m1 == 0x100F ) {
if( u!=0 ) return STRAIGHT_SCORE + 0xF;

return STRAIGHT_FLUSH_ SCORE + 0xF;
}

if( y==m1 ) {
if( u!=0 ) return STRAIGHT_SCORE + m1;

return STRAIGHT_FLUSH_ SCORE + m1;
}

if( u!=0 ) return m1; // Nothing

return FLUSH_SCORE + m1;
}
/*
// m1 = c0 | ... | c4
// m2 = (c0 & c1) | ((c0|c1) & c2)
| ((c0|c1|c2) & c3) | ((c0|c1|c2|c3) & c4)
// m3 = mask of 3 of a kind.
// m4 = mask of 4 of a kind.
*/
m1 = c0 | c1;
m2 = c1 & c0;
m3 = c2 & m2;
m2 |= c2 & m1;
m1 |= c2;
m4 = c3 & m3;
m3 |= c3 & m2;
m2 |= c3 & m1;
m1 |= c3;
m4 |= c4 & m3;
m3 |= c4 & m2;
m2 |= c4 & m1;
m1 |= c4;

m1 &= ~m2;

if(m3 == 0) {
if( (m2&(m2 - 1))==0 )
return TWO_KIND_SCORE + (m2 << SUBR_SHL) + m1;

return TWO_PAIR_SCORE + (m2 << SUBR_SHL) + m1;
}

m2 &= ~m3;

if( m4==0 ) {
if( m2==0 )
return THREE_KIND_SCOR E + (m3 << SUBR_SHL) + m1;

return FULL_HOUSE_SCOR E + (m3 << SUBR_SHL) + m2;
}

return FOUR_KIND_SCORE + (m4 << SUBR_SHL) + m1;
}

//char suitdisp[9] = { 0, 5, 4, 0, 3, 0, 0, 0, 6 };

char suitdisp[9] = { 0, 'c', 'd', 0, 'h', 0, 0, 0, 's' };

void DisplayCard( BYTE c ) {
char s[4];

s[0] = " 1 "[CardValue[c]];
s[1] = "234567890J QKA"[CardValue[c]];
s[2] = suitdisp[CardSuit[c]];
s[3] = '\0';

printf(" %s ",s);

}

void DisplayHand( CardPileType * h ) {
int v=0;
int i;

for(i=0;i<5;i++ ) DisplayCard( h->entry[i] );

printf(" =%08X\n",PokerS core(&h->entry[0]));
}

CardPileType Deck, Hand;

void Shuffle( CardPileType * c ) {
int i = c->len,j;

for(;i>1;) {
BYTE t;
j = rand() % i;
i--;
t = c->entry[i];
c->entry[i] = c->entry[j];
c->entry[j] = t;
}
}

int Deal( CardPileType * h, CardPileType * d, int n )
{
int i;
for( i=0; i<n && d->len>0; i++ ) {
d->len--;
h->entry[ h->len ] = d->entry[ d->len ];
h->len++;
}
return i;
}

void InitCards()
{
int i;

Deck.len = 52;
for(i=0;i<52;i+ +)
Deck.entry[i] = i;
Shuffle(&Deck);
Hand.len = 0;
}

#define ARCHIVE_SHL 6
#define ARCHIVE_NUM (1<<(ARCHIVE_SH L))

CardPileType HandArchive[ARCHIVE_NUM];

int main(void)
{
int i,j;
int c;

srand( (unsigned int)time((void *)0) );
InitCards();
printf("STRAIGH T_FLUSH_SCORE %08x\n",STRAIGH T_FLUSH_SCORE );
printf("FOUR_KI ND_SCORE %08x\n",FOUR_KI ND_SCORE );
printf("FULL_HO USE_SCORE %08x\n",FULL_HO USE_SCORE );
printf("FLUSH_S CORE %08x\n",FLUSH_S CORE );
printf("STRAIGH T_SCORE %08x\n",STRAIGH T_SCORE );
printf("THREE_K IND_SCORE %08x\n",THREE_K IND_SCORE );
printf("TWO_PAI R_SCORE %08x\n",TWO_PAI R_SCORE );
printf("TWO_KIN D_SCORE %08x\n",TWO_KIN D_SCORE );

for(i=0;i<ARCHI VE_NUM;i++) {
Deal(&Hand,&Dec k,5);
if( i<32 ) DisplayHand(&Ha nd);
HandArchive[i] = Hand;
Deal(&Deck,&Han d,5); // Add hand back into Deck.
Shuffle(&Deck); // Reshuffle Deck.
}

for(c=0;c<1024; c++) {
for(j=0,i=0;i<A RCHIVE_NUM;i++) {
j += PokerScore(&Han dArchive[i].entry[0]);
}
}

printf("\nRigge d hands\n\n");
Hand.len = 5;
Hand.entry[0] = 12;
Hand.entry[1] = 12+13;
Hand.entry[2] = 12+26;
Hand.entry[3] = 12+39;
Hand.entry[4] = 11;
DisplayHand(&Ha nd);

Hand.entry[0] = 12;
Hand.entry[1] = 12+13;
Hand.entry[2] = 12+26;
Hand.entry[3] = 11+39;
Hand.entry[4] = 11;
DisplayHand(&Ha nd);

Hand.entry[0] = 12;
Hand.entry[1] = 11+13;
Hand.entry[2] = 10+26;
Hand.entry[3] = 9+39;
Hand.entry[4] = 8;
DisplayHand(&Ha nd);

Hand.entry[0] = 12;
Hand.entry[1] = 12+13;
Hand.entry[2] = 2+26;
Hand.entry[3] = 3+39;
Hand.entry[4] = 4;
DisplayHand(&Ha nd);

Hand.entry[0] = 2;
Hand.entry[1] = 2+13;
Hand.entry[2] = 3+26;
Hand.entry[3] = 3+39;
Hand.entry[4] = 4;
DisplayHand(&Ha nd);

Hand.entry[0] = 0;
Hand.entry[1] = 0+13;
Hand.entry[2] = 1+26;
Hand.entry[3] = 2+39;
Hand.entry[4] = 3;
DisplayHand(&Ha nd);

Hand.entry[0] = 0;
Hand.entry[1] = 0+13;
Hand.entry[2] = 1+26;
Hand.entry[3] = 1+39;
Hand.entry[4] = 2;
DisplayHand(&Ha nd);

Hand.entry[0] = 3;
Hand.entry[1] = 2+13;
Hand.entry[2] = 1+26;
Hand.entry[3] = 0+39;
Hand.entry[4] = 12;
DisplayHand(&Ha nd);

Hand.entry[0] = 4;
Hand.entry[1] = 3+13;
Hand.entry[2] = 2+26;
Hand.entry[3] = 1+39;
Hand.entry[4] = 0;
DisplayHand(&Ha nd);

Hand.entry[0] = 12;
Hand.entry[1] = 12+13;
Hand.entry[2] = 11+26;
Hand.entry[3] = 11+39;
Hand.entry[4] = 10;
DisplayHand(&Ha nd);

Hand.entry[0] = 12+ 26;
Hand.entry[1] = 12+13;
Hand.entry[2] = 11+26;
Hand.entry[3] = 11;
Hand.entry[4] = 10+39;
DisplayHand(&Ha nd);

Hand.entry[0] = 12;
Hand.entry[1] = 0;
Hand.entry[2] = 1;
Hand.entry[3] = 2;
Hand.entry[4] = 3;
DisplayHand(&Ha nd);

Hand.entry[0] = 11 + 26;
Hand.entry[1] = 0;
Hand.entry[2] = 1;
Hand.entry[3] = 2;
Hand.entry[4] = 3;
DisplayHand(&Ha nd);

Hand.entry[0] = 11 + 26;
Hand.entry[1] = 4;
Hand.entry[2] = 1;
Hand.entry[3] = 2;
Hand.entry[4] = 3;
DisplayHand(&Ha nd);

return 0;
}

/* END pokeref.c */
--
pete
Dec 20 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1679
by: budgie | last post by:
Hi, I have a table with availability of rooms which has the htlrm_id: hotel room id avl_date: date this room is available avl_qty: quantity of this room available avl_rate: rate at which these rooms can be booked an example of the data in the table is:
5
3357
by: Axial | last post by:
Question: How to select columns from Excel-generated XML when some cells are empty. I've found examples where rows are to be selected, but I can't seem to extrapolate from that to selecting columns when some cells are empty. Is there a way to use the ss:Index to account for the missing <Cell elements? Thank you for any suggestions. ====================
4
2682
by: Chris Jones | last post by:
Does anyone know of a C#/ASP.NET Open File dialog that I can use in an aspx page that allows multiselect? I need to be able to select and upload numerous files. Is there a reason why MS has made this so difficult? I am willing to pay for such a control.
6
2056
by: aaj | last post by:
Hi all I use a data adapter to read numerous tables in to a dataset. The dataset holds tables which in turn holds full details of the records i.e. keys, extra colums etc.. In some cases I need to use parts of the tables in datagrids, and here is where my problem lies
5
1798
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that manipulate (and report on) members of the class. Interface consists of: - 5 private variables char author; char title; char code;
1
1549
by: jdhcards | last post by:
Hello, I've been banging my head against a problem all day without a solution, and I'm hoping you all can help. I've got a piece of XML that defines a set of elements in a flat list. Each of these elements has an attribute "Id" which has a unique value. Some of these elements have a parent-child relationship, specified with a "ResultId" attribute in one of the child nodes. <element id=1>
4
26704
by: Tomasz Jastrzebski | last post by:
Hello Everyone, I have a GridView control bound to a plain DataTable object. AutoGenerateEditButton is set to true, Edit button gets displayed, and RowEditing event fires as expected.
4
2106
by: darrel | last post by:
I have a DDL list along these lines: item value="1" text="a" item value="2" text="b" item value="3" text="c" item value="2" text="d" item value="2" text="e" item value="1" text="f" item value="1" text="g"
1
2053
by: Mark B | last post by:
This is my first try at using AJAX. I want the calendars to be enabled if the user checks CheckBox1. It works OK for a normal all page refresh but once I introduced the AJAX code it stopped working. Any ideas? <%@ Page Language="VB" AutoEventWireup="false" CodeFile="default-ajax.aspx.vb" Inherits="pages_verify_groups_Default" Debug="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.