Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Designing a Card Game

Reply
Thread Tools

Designing a Card Game

 
 
pek
Guest
Posts: n/a
 
      05-28-2008
I'm about to create a simple card game and I'm not sure about what
classes should I create for the cards. I know a Card has a Rank (King,
Queen, Jack, 10....) and a Suit (Spades, Clubs, Hearts and Diamonds)
but the problem comes when I want to introduce Jokers and Tramp cards.
A Joker doesn't have a Suit (or a Rank, depending on how you look at
it).

Currently I have a Card class that takes two enumerations, a Suit
enumeration and a Rank enumeration. For the Joker I simply agreed to
always be a card with a Rank of Joker and a Suit of Spades. But that
just doesn't seem right. And I can't think of a way to create a
hierarchy of card classes.

Any suggestions?

As always, thank you in advanced.
Panagiotis
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      05-28-2008
pek wrote:
> I'm about to create a simple card game and I'm not sure about what
> classes should I create for the cards. I know a Card has a Rank (King,
> Queen, Jack, 10....) and a Suit (Spades, Clubs, Hearts and Diamonds)
> but the problem comes when I want to introduce Jokers and Tramp cards.
> A Joker doesn't have a Suit (or a Rank, depending on how you look at
> it).
>
> Currently I have a Card class that takes two enumerations, a Suit
> enumeration and a Rank enumeration. For the Joker I simply agreed to
> always be a card with a Rank of Joker and a Suit of Spades. But that
> just doesn't seem right. And I can't think of a way to create a
> hierarchy of card classes.
>
> Any suggestions?
>
> As always, thank you in advanced.
> Panagiotis


How about a rank of Joker and suit of null?

--

Knute Johnson
email s/knute/nospam/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
 
 
 
pek
Guest
Posts: n/a
 
      05-28-2008
On May 29, 12:21 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> pek wrote:
> > I'm about to create a simple card game and I'm not sure about what
> > classes should I create for the cards. I know a Card has a Rank (King,
> > Queen, Jack, 10....) and a Suit (Spades, Clubs, Hearts and Diamonds)
> > but the problem comes when I want to introduce Jokers and Tramp cards.
> > A Joker doesn't have a Suit (or a Rank, depending on how you look at
> > it).

>
> > Currently I have a Card class that takes two enumerations, a Suit
> > enumeration and a Rank enumeration. For the Joker I simply agreed to
> > always be a card with a Rank of Joker and a Suit of Spades. But that
> > just doesn't seem right. And I can't think of a way to create a
> > hierarchy of card classes.

>
> > Any suggestions?

>
> > As always, thank you in advanced.
> > Panagiotis

>
> How about a rank of Joker and suit of null?
>
> --
>
> Knute Johnson
> email s/knute/nospam/
>
> --
> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
> ------->>>>>>http://www.NewsDemon.com<<<<<<------
> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


Interestingly, you cannot pass null when a method requires an
enumeration!
 
Reply With Quote
 
pek
Guest
Posts: n/a
 
      05-28-2008
On May 28, 11:49 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Wed, 28 May 2008 13:41:13 -0700, pek <kimwl...@gmail.com> wrote:
> > I'm about to create a simple card game and I'm not sure about what
> > classes should I create for the cards. I know a Card has a Rank (King,
> > Queen, Jack, 10....) and a Suit (Spades, Clubs, Hearts and Diamonds)
> > but the problem comes when I want to introduce Jokers and Tramp cards.
> > A Joker doesn't have a Suit (or a Rank, depending on how you look at
> > it).

>
> > Currently I have a Card class that takes two enumerations, a Suit
> > enumeration and a Rank enumeration. For the Joker I simply agreed to
> > always be a card with a Rank of Joker and a Suit of Spades. But that
> > just doesn't seem right. And I can't think of a way to create a
> > hierarchy of card classes.

>
> Well, you could just add another suit to your enumeration: "no suit", or
> "joker suit" or whatever you want to call it.
>
> I never heard of a "Tramp Card" so I don't know what would be most
> appropriate for that. Even a "trump card" is usually some specific card
> within the standard 52, and so is defined by the rules, not the card
> itself.
>
> Pete


Yes, I agree. trump card usually depends on the rules of a game. In my
game, a trump card is a card with a Rank of Trump (a special rank) and
a specific Suit (a standard suit - either spades or clubs etc.). So
creating a Trump card wasn't difficult.

As for Jokers. I changed it to always be Joker (Suit) Joker (Rank).
But I'm not sure if this is right. A stupid question crossed my mind:
What if I wanted to create a standard deck of 52 cards with 2 Red
Jokers and 2 Black Jokers (as is the case of a usual deck of cards)?
Does this mean that I have to have a Suit of RED_JOKER and
BLACK_JOKER? Isn't this a little overkill?

Thanks for your answers
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      05-28-2008
pek wrote:
> On May 29, 12:21 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> wrote:
>> pek wrote:
>>> I'm about to create a simple card game and I'm not sure about what
>>> classes should I create for the cards. I know a Card has a Rank (King,
>>> Queen, Jack, 10....) and a Suit (Spades, Clubs, Hearts and Diamonds)
>>> but the problem comes when I want to introduce Jokers and Tramp cards.
>>> A Joker doesn't have a Suit (or a Rank, depending on how you look at
>>> it).
>>> Currently I have a Card class that takes two enumerations, a Suit
>>> enumeration and a Rank enumeration. For the Joker I simply agreed to
>>> always be a card with a Rank of Joker and a Suit of Spades. But that
>>> just doesn't seem right. And I can't think of a way to create a
>>> hierarchy of card classes.
>>> Any suggestions?
>>> As always, thank you in advanced.
>>> Panagiotis

>> How about a rank of Joker and suit of null?
>>
>> --
>>
>> Knute Johnson
>> email s/knute/nospam/
>>
>> --
>> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
>> ------->>>>>>http://www.NewsDemon.com<<<<<<------
>> Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

>
> Interestingly, you cannot pass null when a method requires an
> enumeration!


Sure you can!

public class test7 {
enum X { KING, QUEEN, JACK };

static void method(X x) {
System.out.println(x);
}

public static void main(String[] args) {
method((X)null);
}
}

C:\Documents and Settings\Knute Johnson>java test7
null

--

Knute Johnson
email s/knute/nospam/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
pek
Guest
Posts: n/a
 
      05-28-2008
On May 29, 1:13 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Wed, 28 May 2008 14:34:03 -0700, pek <kimwl...@gmail.com> wrote:
> > [...]
> > As for Jokers. I changed it to always be Joker (Suit) Joker (Rank).
> > But I'm not sure if this is right. A stupid question crossed my mind:
> > What if I wanted to create a standard deck of 52 cards with 2 Red
> > Jokers and 2 Black Jokers (as is the case of a usual deck of cards)?
> > Does this mean that I have to have a Suit of RED_JOKER and
> > BLACK_JOKER? Isn't this a little overkill?

>
> You can call the suit whatever you want. If you anticipate the need for a
> "red" and "black" suit, separate from the existing four standard suits,
> _and_ used for some kind of card other than a joker, you might just want
> to call them "red" and "black", instead of including the word "joker".
> The other suits -- hearts, diamonds, spades, clubs -- already have an
> implied color for the name, so "red" and "black" by themselves seem to be
> reasonably clear as "general purpose" as opposed to overlapping the four
> standard suits.
>
> If it really bothers you though, I certainly don't see anything wrong with
> a somewhat redundant name, like "RED_JOKER" or "RED_GENERAL" (to
> differentiate from the specific suits, for example).
>
> An alternative would be to use a bitfield for the enumeration. Keeping in
> mind that my limited attempts to use Java enumerations have resulted only
> in clumsy code when trying to do anything but the most basic
> declarations. But it seems to me that you could have an enumeration where
> one bit signified the color, while one or two other bits signified the
> specific suit (depending on how much redundancy you wanted). Then you
> could just reuse the same "red" and "black" values already in use for the
> regular suits for other cards that have a color for the suit but not a
> specific suit per se.
>
> Personally, I'd go with the simpler version. We're only talking a handful
> of constants one way or the other and context is always going to give you
> all the information you need. But if you're really worried about
> "overkill", sure...there are other ways to do it.
>
> Like so many things, "overkill" is in the eye of the beholder.
>
> Pete


Hahahhaa.. I totally agree with your last statement.
I finally decided to keep it a simple as possible. I didn't add the
color of a joker since a red or a black joker doesn't have any
difference other than graphically.

Thanks again.
 
Reply With Quote
 
Ed Kirwan
Guest
Posts: n/a
 
      05-29-2008
pek wrote:

> I'm about to create a simple card game and I'm not sure about what
> classes should I create for the cards. I know a Card has a Rank (King,
> Queen, Jack, 10....) and a Suit (Spades, Clubs, Hearts and Diamonds)
> but the problem comes when I want to introduce Jokers and Tramp cards.
> A Joker doesn't have a Suit (or a Rank, depending on how you look at
> it).
>
> Currently I have a Card class that takes two enumerations, a Suit
> enumeration and a Rank enumeration. For the Joker I simply agreed to
> always be a card with a Rank of Joker and a Suit of Spades. But that
> just doesn't seem right. And I can't think of a way to create a
> hierarchy of card classes.
>
> Any suggestions?


Perhaps it doesn't seem right to have a Joker defined with a suit (or a
null-suit of some kind, as Peter and Knute suggest), but the alternative is
to introduce a class-based distinction between cards with suits and cards
without suits and that would surely raise the complexity of the program for
little gain.

The simplicity of the class you describe sounds good, especially as it may
be re-used across games:
http://www.edmundkirwan.com/servlet/...ackage132.html

That the Joker will involve rules different from other cards will not
trigger behaviour of the Joker itself, but of some evaluation of a hand of
cards:
http://www.edmundkirwan.com/servlet/...ackage101.html

Though, admittedly, cards should be able to at least compare themselves with
other cards, yet even this comparison may be game-dependent. That will be
an interesting problem for you.

--
..ed


 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      05-29-2008
On Thu, 29 May 2008, Ed Kirwan wrote:

> pek wrote:
>
>> I'm about to create a simple card game and I'm not sure about what
>> classes should I create for the cards. I know a Card has a Rank (King,
>> Queen, Jack, 10....) and a Suit (Spades, Clubs, Hearts and Diamonds)
>> but the problem comes when I want to introduce Jokers and Tramp cards.
>> A Joker doesn't have a Suit (or a Rank, depending on how you look at
>> it).
>>
>> Currently I have a Card class that takes two enumerations, a Suit
>> enumeration and a Rank enumeration. For the Joker I simply agreed to
>> always be a card with a Rank of Joker and a Suit of Spades. But that
>> just doesn't seem right. And I can't think of a way to create a
>> hierarchy of card classes.
>>
>> Any suggestions?

>
> Perhaps it doesn't seem right to have a Joker defined with a suit (or a
> null-suit of some kind, as Peter and Knute suggest), but the alternative is
> to introduce a class-based distinction between cards with suits and cards
> without suits and that would surely raise the complexity of the program for
> little gain.


I think the null suit is the way to go. Either an actual 'physical' null,
or a Suit object that means 'no suit'. The latter might be more sensible,
as you avoid having to deal with physical nulls - this is actually a
design pattern called 'Null Object' (or 'Active Nothing' if you're my
dad). Another one for Pek to tick off!

I'd put some behaviour in Rank that can be used to ensure that Cards are
in valid combinations. Say:

// WARNING: i haven't tried to compile or run any of this code!

public enum Suit {SPADES, HEARTS, CLUBS, DIAMONDS, NONE}

public enum Rank {

KING (true), QUEEN (true), JACK (true), TEN (true), NINE (true),
EIGHT (true), SEVEN (true), SIX (true), FIVE (true), FOUR (true),
THREE (true), TWO (true), ACE (true),
JOKER (false) ;

private boolean hasSuit ;

private Rank(boolean hasSuit) {
this.hasSuit = hasSuit ;
}

public boolean canBeOfSuit(Suit suit) {
return hasSuit ^ (suit == Suit.NONE) ;
}

}

Alternatively, a constant-specific method might work:

public enum Rank {

KING, QUEEN, JACK, TEN, NINE, EIGHT, SEVEN, SIX, FIVE, FOUR,
THREE, TWO, ACE,
JOKER {public boolean canBeOfSuit(Suit suit) {return suit == Suit.NONE}} ;

public boolean canBeOfSuit(Suit suit) {
return suit != Suit.NONE ;
}
}

Either way, Card looks like this:

public class Card {
private Suit suit ;
private Rank rank ;
public Card(Suit suit, Rank rank) {
if (!rank.canBeOfSuit(suit)) throw new IllegalArgumentException("etc") ;
this.rank = rank ;
this.suit = suit ;
}
}

> Though, admittedly, cards should be able to at least compare themselves
> with other cards, yet even this comparison may be game-dependent. That
> will be an interesting problem for you.


The thing i can't get my head round at all is how you deal with aces in
games where they can be both high and low (or either high or low, or
something). You can have methods for comparison and getting a point value,
but they'll need to know about the context around them to figure out which
value the card should take. Do you pass in that context? Messy! Do you let
the method somehow magically read it? Naughty! Do you have a toggle on the
card instance to tell it whether to be high or low? Ugly! Do you have two
rank values, ACE_HIGH and ACE_LOW? Kludgey!

tom

--
It's almost over now.
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      05-30-2008
Tom Anderson wrote:
> public enum Rank {
>
> KING (true), QUEEN (true), JACK (true), TEN (true), NINE (true),
> EIGHT (true), SEVEN (true), SIX (true), FIVE (true), FOUR (true),
> THREE (true), TWO (true), ACE (true),


ick!

> JOKER (false) ;
>
> private boolean hasSuit ;
>
> private Rank(boolean hasSuit) {
> this.hasSuit = hasSuit ;
> }
>
> public boolean canBeOfSuit(Suit suit) {
> return hasSuit ^ (suit == Suit.NONE) ;
> }
>
> }
>


public enum Rank {

KING, QUEEN, JACK, TEN, NINE, EIGHT, SEVEN,
SIX, FIVE, FOUR, THREE, TWO, ACE, JOKER (false) ;

private boolean hasSuit ;

private Rank() {
hasSuit = true;
}

private Rank(boolean hasSuit) {
this.hasSuit = hasSuit ;
}

--
RGB
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      05-30-2008
On Fri, 30 May 2008, RedGrittyBrick wrote:

> Tom Anderson wrote:
>> public enum Rank {
>>
>> KING (true), QUEEN (true), JACK (true), TEN (true), NINE (true),
>> EIGHT (true), SEVEN (true), SIX (true), FIVE (true), FOUR (true),
>> THREE (true), TWO (true), ACE (true),

>
> ick!
>
>> JOKER (false) ;
>>
>> private boolean hasSuit ;
>>
>> private Rank(boolean hasSuit) {
>> this.hasSuit = hasSuit ;
>> }
>>
>> public boolean canBeOfSuit(Suit suit) {
>> return hasSuit ^ (suit == Suit.NONE) ;
>> }
>>
>> }

>
> public enum Rank {
>
> KING, QUEEN, JACK, TEN, NINE, EIGHT, SEVEN,
> SIX, FIVE, FOUR, THREE, TWO, ACE, JOKER (false) ;
>
> private boolean hasSuit ;
>
> private Rank() {
> hasSuit = true;
> }
>
> private Rank(boolean hasSuit) {
> this.hasSuit = hasSuit ;
> }


Aha! I didn't realise you could do that. This enum stuff is all new to me!

tom

--
Oh, well of course *everything* looks bad if you remember it
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(Game) - Homeworld 1999 Game of the Year johnnoblepi Computer Support 3 02-23-2008 08:16 PM
(Game) - Homeworld 1999 Game of the Year johnnoblepi Computer Support 1 02-23-2008 08:16 PM
Game programming for kids: looking for open source 2D game developmentkit Max Kubierschky Python 10 03-31-2007 07:18 PM
I need help with card game game of war in GUI enviroment judith Java 0 11-01-2006 06:11 AM
Java Game- Game can't find it's graphics jar file BlackHawke Java 12 01-26-2004 06:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57