473,807 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing info for cards

Hi All,

I have got this task which I cant get my head around:
It asks how I would store information:
I just want to get an opinion from the group:
So here's the question:

"
You have been asked to write a program for a card playing friend. How
would you store information about playing cards? For example how would
you store the fact that a certain card is the 7 of diamonds? (You may
use more than one variable if you wish)
"

All help welcome.
Thanks in advance.

Radith

Nov 15 '05 #1
26 1975

"Radith" <ra****@xtra.co .nz> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
Hi All,

I have got this task which I cant get my head around:
It asks how I would store information:
I just want to get an opinion from the group:
So here's the question:

"
You have been asked to write a program for a card playing friend. How
would you store information about playing cards? For example how would
you store the fact that a certain card is the 7 of diamonds? (You may
use more than one variable if you wish)
"

All help welcome.
Thanks in advance.

Radith


enum suits {CLUBS, HEARTS, DIAMONDS, SPADES};
enum faces {ACE = 1, JACK = 11, QUEEN, KING};

struct card
{
unsigned int rank;
unsigned char suit;
};

card c = {7, DIAMONDS};
-Mike
Nov 15 '05 #2
Mike Wahler wrote:
"Radith" <ra****@xtra.co .nz> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
Hi All,

I have got this task which I cant get my head around:
It asks how I would store information:
I just want to get an opinion from the group:
So here's the question:

"
You have been asked to write a program for a card playing friend. How
would you store information about playing cards? For example how would
you store the fact that a certain card is the 7 of diamonds? (You may
use more than one variable if you wish)
"

All help welcome.
Thanks in advance.

Radith


enum suits {CLUBS, HEARTS, DIAMONDS, SPADES};
enum faces {ACE = 1, JACK = 11, QUEEN, KING};

struct card
{
unsigned int rank;
unsigned char suit;
};

card c = {7, DIAMONDS};


struct card c = {7, DIAMONDS);

Robert Gamble

Nov 15 '05 #3
In article <11************ **********@z14g 2000cwz.googleg roups.com>,
rg*******@gmail .com says...
Mike Wahler wrote:
"Radith" <ra****@xtra.co .nz> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
Hi All,

I have got this task which I cant get my head around:
It asks how I would store information:
I just want to get an opinion from the group:
So here's the question:

"
You have been asked to write a program for a card playing friend. How
would you store information about playing cards? For example how would
you store the fact that a certain card is the 7 of diamonds? (You may
use more than one variable if you wish)
"

All help welcome.
Thanks in advance.

Radith


enum suits {CLUBS, HEARTS, DIAMONDS, SPADES};
enum faces {ACE = 1, JACK = 11, QUEEN, KING};

struct card
{
unsigned int rank;
unsigned char suit;
};

card c = {7, DIAMONDS};


struct card c = {7, DIAMONDS);

Robert Gamble

struct card c = {7, DIAMONDS};

If you're gonna be a nit-picky smart-ass, you better be correct.
Nov 15 '05 #4
On 9 Aug 2005 18:00:41 -0700, "Radith" <ra****@xtra.co .nz> wrote:
Hi All,

I have got this task which I cant get my head around:
It asks how I would store information:
I just want to get an opinion from the group:
So here's the question:

"
You have been asked to write a program for a card playing friend. How
would you store information about playing cards? For example how would
you store the fact that a certain card is the 7 of diamonds? (You may
use more than one variable if you wish)
"


You have lots of options. Just to start you off consider a struct
with two members or a 2D array of char.
<<Remove the del for email>>
Nov 15 '05 #5
John Doe wrote:
In article <11************ **********@z14g 2000cwz.googleg roups.com>,
rg*******@gmail .com says...
Mike Wahler wrote:
"Radith" <ra****@xtra.co .nz> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
> Hi All,
>
> I have got this task which I cant get my head around:
> It asks how I would store information:
> I just want to get an opinion from the group:
> So here's the question:
>
> "
> You have been asked to write a program for a card playing friend. How
> would you store information about playing cards? For example how would
> you store the fact that a certain card is the 7 of diamonds? (You may
> use more than one variable if you wish)
> "
>
> All help welcome.
> Thanks in advance.
>
> Radith

enum suits {CLUBS, HEARTS, DIAMONDS, SPADES};
enum faces {ACE = 1, JACK = 11, QUEEN, KING};

struct card
{
unsigned int rank;
unsigned char suit;
};

card c = {7, DIAMONDS};


struct card c = {7, DIAMONDS);

Robert Gamble

struct card c = {7, DIAMONDS};


Obviously that was an attempt to keep true to the tradition that any
post correcting a grammatical (or syntactical) error must introduce one
of it's own ;)

Robert Gamble

Nov 15 '05 #6
In article <vv************ ****@newsread1. news.pas.earthl ink.net>,
Mike Wahler <mk******@mkwah ler.net> wrote:
enum suits {CLUBS, HEARTS, DIAMONDS, SPADES};
enum faces {ACE = 1, JACK = 11, QUEEN, KING}; struct card
{
unsigned int rank;
unsigned char suit;
}; card c = {7, DIAMONDS};


Mike, is there any particular reason you used int for rank when
you only used char for suit? The minimum size for char has
enough range to cover the largest possible card rank, which would
tend to suggest something like using char for each of
the elements.
For that matter, if space is more important than efficiency, then

struct card
{
unsigned char rank: 5;
unsigned char suit: 3;
}

or perhaps
#define rank_of(card) (((card)&0x1e)> >1)
#define suit_of(card) (((card)&0xe1)> >5)
#define is_face_up(card ) ((card)&1)
#define build_card(rank ,suit,faceup) ((suit)<<5 | ((rank)<<1) | (faceup))

typedef unsigned char card;
Mind you, I wouldn't recommend this latter implementation to anyone who
doesn't understand why the symmetry of the bitmasks is important
in rank_of() and suit_of().
--
"This was a Golden Age, a time of high adventure, rich living and
hard dying... but nobody thought so." -- Alfred Bester, TSMD
Nov 15 '05 #7
"Robert Gamble" <rg*******@gmai l.com> writes:
[...]
Obviously that was an attempt to keep true to the tradition that any
post correcting a grammatical (or syntactical) error must introduce one
of it's own ;)


Shouldn't that be "its'"?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8
Walter Roberson wrote:
In article <vv************ ****@newsread1. news.pas.earthl ink.net>,
#define rank_of(card) (((card)&0x1e)> >1)
#define suit_of(card) (((card)&0xe1)> >5)
#define is_face_up(card ) ((card)&1)
#define build_card(rank ,suit,faceup) ((suit)<<5 | ((rank)<<1) | (faceup))

typedef unsigned char card;
Mind you, I wouldn't recommend this latter implementation to anyone who
doesn't understand why the symmetry of the bitmasks is important
in rank_of() and suit_of().


Ok, you wouldn't recommend this implementation to me, cause I can't see
why the symmetry of the two bitmasks is so important.

Correct me if I'm wrong, but

#define suit_of(card) (((card)&0xe0)> >5)

would produce exactly the same result in every situation.

Nov 15 '05 #9
Radith wrote:

Hi All,

I have got this task which I cant get my head around:
It asks how I would store information:
I just want to get an opinion from the group:
So here's the question:

"
You have been asked to write a program for a card playing friend. How
would you store information about playing cards? For example how would
you store the fact that a certain card is the 7 of diamonds? (You may
use more than one variable if you wish)


/* BEGIN shuffle.c */

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

#define LU_RAND_SEED 123456789LU
#define LU_RAND(S) ((S) * 69069 + 362437 & 0xffffffffLU)
#define SUITS (sizeof suit / sizeof *suit)
#define RANKS (sizeof rank / sizeof *rank)
#define CARDS (SUITS * RANKS)
#define HAND 5
#define DEALS 5

struct poker {
int suit;
int rank;
};

long unsigned shuffle(int *, int, long unsigned);
int compar_rank(voi d const*, void const*);
int compar_suit(voi d const*, void const*);
int straight(struct poker *);
int pair(struct poker *);
int flush(struct poker *);
int three(struct poker *);
int four(struct poker *);
int full(struct poker *);
int two_pair(struct poker *);
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 = LU_RAND_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];

putchar('\n');
/**/
seed = (long unsigned)time(0 );
/*//**/
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[1].rank == hand[0].rank
&& hand[4].rank == hand[3].rank
&&(hand[2].rank == hand[1].rank
|| hand[3].rank == hand[2].rank);
}

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

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

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 / 3 - 1) {
nmemb = nmemb / 3 - 1;
} 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
Nov 15 '05 #10

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

Similar topics

2
1773
by: Francisco | last post by:
I have this problem: I have a database with information about games, and users are able to vote for them. Everytime a user votes for a game I store the unique game name into a session variable (an array). So if they are in a page were they already voted, they won't have the option to do so. The idea is that the session cookie lasts "forever", I don't want them voting for the same game everytime they get to the site. This isn't soo strict,...
2
2177
by: kelvSYC | last post by:
I'm trying to program something along the lines of a "trading card" idea: I have a single copy of the "card" in the program, yet there may be multiple "instances" of the "card", with differing information (such as the owner of the "card") in each instance. struct Person; Person Bob; Person Joe;
5
2148
by: Don Vaillancourt | last post by:
I'm building a system when one can upload a document to the website. I will be storing the document on the hard-drive for quick/easy access, but I was also thinking of storing it in an existing database since most of the sites information is all stored there. As well there would be only one place to worry about backing up. And if the file on the hard-drive was ever missing or became corrupted, I could restore it form tha database. Is...
9
1881
by: elyob | last post by:
Hi, I'm looking at storing snippets of details in MySQL about what credit cards a business excepts. Rather than have a whole column for Visa, another for Amex etc ... I am looking at having a column called payment types and inserting multiple codes ... e.g. ViAmBcCa Is this a good way of doing things? To me it'd be a lot cleaner and limit amount of Db work to be done. Is this a sensible way in your opinion? What's the best way of...
6
1736
by: Kieran Benton | last post by:
Hi, I have quite a lot of metadata in a WinForms app that I'm currently storing within a hashtable, which is fine as long as I know the unique ID of the track (Im storing info on media files). Up until now I've been sending the track info to a seperate server app using serialization/sockets which stores the metadata in a mysql db so that I can query on Artist/Album etc as well. However, I'm looking to remove the server and move into a more...
12
4985
by: Alex D. | last post by:
is it possible? pros and cons?
10
4968
by: Arun Nair | last post by:
Can any one help me with this im not getting it even after reading books because there is not much of discussion anywhere a> Implement a calss that represents a playing card. The class should implement the following methods: _ _ init _ _ (self, rank, suit) Creates a card. rank is an integer in range of 1 to 13 (Ace:1, King 13), suit is a character in set {'h','d','c','s'} getRank(self) Returns the rank of the card
11
10155
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I have worked with application settings in VS2005 and C# for awhile, but usually with standard types. I have been trying to store a custom container/class/type in an application setting and I have seen erratic results. I am aware of one known defect where user classes do not show up in the list of types on the Property/Settings page in the visual designer and I am wondering if I am encountering some other peculiar issue, or if there are...
8
29172
by: garyrowell | last post by:
I have been at this programme for hours trying to work out what is wrong. Any help would be very much appricated. Here is the breif I received. The program This week you are going to write three classes: Card.java, Deck.java and DeckTester.java. The specification for each class is given below. Card.Java This is a simple class that represents a playing card. Card has two attributes: • rank which is a String that represents the value...
0
9720
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
9599
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10626
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
10112
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
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6879
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
5546
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
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3854
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.