wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes objects, objects, objects... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "objects, objects, objects..." Watch "objects, objects, objects..." New topic

objects, objects, objects...

Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37

I'm trying to code some type of game where I have to types of card.
So I have base class

abstract class Card {
string name;

class ActionCard extends Card {
void doAction() { ... }

class NobleCard extends Card {
int value;
string cat;

In game we have two decks ( one for each type of card)
so I created base class

class Deck {
private List<? extends Card> deck
public Card dealCard() { }

then is a class of Player who holds only Action cards in hands

class Player {
ActionCard hand;

void addActionCard(ActionCard card) { }

and now my question is:
Should I crate children like ActionDeck, NobleDeck, to ensure that I got a proper type card from the Deck

or use base one, operate on Card, and then
in Player class I should change a method
addActionCard(Card card) and then use casting to ActionCard class ?

What's the better solution for this ?
Francesco Bianchi
Ranch Hand

Joined: Jun 22, 2007
Posts: 62
I'm not so sure if I got "the rules" of your game.. try to be more clear about them if you want to get the maximum from the forum...

Anyway, probably what you need is writing a generic class, something like this:

of course you will be likely make your Deck work only on objects which extend Card... You can do it that in the class declaration, when you define E, similarly to with the only difference the you use E as a placeholder instead of the wildcard ?.

You should be able to put the pieces together on your own now. Try it and if not, no problem, ask

SCJP 5 & 6, SCWCD 5, SCBCD 5
Kuba Zygmunt
Ranch Hand

Joined: Jul 17, 2007
Posts: 37
Ah yes!! Deck<E extends Card> works perfectly!! thank you

the rules are:
* on the table is line of 12 noble cards ( we take them from Noble's Deck)

* player holds in hands only Action cards which do action
* player collects nobles and puts them in his pile.
* player receive action card from Action Deck.

so I created

public class TestGame {
Deck<NobleCard> nobleDeck;
Deck<ActionCard> actionDeck;

public class Player {
String name;
int score;

List<ActionCard> hand;
List<NobleCard> collected;

thank you again
Francesco Bianchi
Ranch Hand

Joined: Jun 22, 2007
Posts: 62
You are welcome
I agree. Here's the link: http://aspose.com/file-tools
subject: objects, objects, objects...
Similar Threads
Best way to represent cards?
blackjack java
Constructor problem.
can't split card shuffle method into a separate class
access objects with same name but different attributes