• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

objects, objects, objects...

 
Kuba Zygmunt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kuba Zygmunt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic