Reputation: 55
I'm a (mostly) self taught programmer with one java class under my belt so I'm still very new to this and I'm trying to make my first real app from scratch.
Basically, I'm making an app version of a card game I own (just for me! Not to sell or anything!) and in this card game you basically use your starter cards to "buy" more cards from a lineup of cards drawn from the main deck. These cards have several values associated with them that would translate well to an app like "power level" and "point value," and in addition each of these cards does something special when you play it like "draw an extra card" or "destroy a card" or "give another player a negative card" - that type of thing.
I've already made the whole UI for the game, and have that (mostly) done, and now I'm working on the meat and potatoes of it all - the code. I've made a constructor class named Card that looks like this:
public class Card
{
private int power;
private int value;
private String type;
private String cardName;
private ImageIcon image;
public Card(int power, int value, String type, String cardName, ImageIcon image)
{
this.power = power;
this.value = value;
this.type = type;
this.cardName = cardName;
this.image = image;
}
...
}
With the ellipses representing all my getter/setter methods.
So you can see that my Card class will assigns all the necessary values to them but one - the special "thing" that each of them do (draw card/destroy card..etc)
I'm not sure where/how I should put that in my code. Since the I have a type value that will represent the different types of cards you can draw (equipment/super power/hero/etc..) I'm thinking I should make a separate class for each card type..is this good coding practice?
So to conclude this extremely lengthy post, in summary - should I separate out all my different card "types" into different classes and how should I implement that special "thing" that each card does? I feel like it will be wrapped in a method, but as far as I know, I can't have a method be a part of a constructor, can I?
Upvotes: 2
Views: 2329
Reputation: 48600
You can make the Card either an interface or an abstract class. If you go with the latter, you can actually define logic sort of like what you already have above. Depending on the route you go, each "Card" would either implement the interface or extend the abstract class.
The downside to the interface is that you have to re-implement all the fields as private variables for the sub-class.
Abstract
public abstract class Card {
protected int power;
protected int value;
protected String type;
protected String cardName;
protected ImageIcon image;
// You cannot instantiate this directly, only through children.
public Card(int power, int value, String type, String cardName, ImageIcon image) {
this.power = power;
this.value = value;
this.type = type;
this.cardName = cardName;
this.image = image;
}
// Getters/Setters...
}
public class AttackCard extends Card {
public AttackCard(int power, int value, String type, String cardName, ImageIcon image);
super(power, value, type, cardName, image);
}
}
Interface
public interface Card {
int getPower();
int getValue();
String getType();
String getCardName();
ImageIcon getImage();
}
public class AttackCard implements Card {
private int power;
private int value;
private String type;
private String cardName;
private ImageIcon image;
public AttackCard(int power, int value, String type, String cardName, ImageIcon image);
this.power = power;
this.value = value;
this.type = type;
this.cardName = cardName;
this.image = image;
}
// Implement methods from Card...
}
Edit
If you are implementing the DC Comics deck-building game then you may only need to have 1 abstract class for all cards and an interface for Hero, Villain, Super Power, and Starter. Not sure what all they types are... Then all the Heroes would extend the abstract card class for all the common properties of cards and implement the methods that are Hero-specific.
You would not need a separate class for each card. Each hero would just be an instance of Hero. You would just have a Collection of Hero cards.
// Hierarchy
abstract class Card
interface Hero
interface Villain
class HeroCard extends Card implements Hero
class VillainCard extends Card implements Villain
// Example constructors definitions for reference?
Card(String name, int value, int power);
HeroCard(String name, int value, int power);
VillainCard(String name, int value, int power);
// Create all heroes
List<Hero> heroes = new ArrayList<Hero>();
heroes.add(new HeroCard("Green Arrow", 5, 2));
// Create all villains
List<Villain> villains = new ArrayList<Villains>();
villains.add(new VillainCard("Ra's Al Ghul", 8, 3));
// All all the heroes and villains to all cards
List<Card> cards = new ArrayList<Card>();
cards.addAll(heroes);
card.addAll(villains);
Upvotes: 2
Reputation: 2895
You can use inheritance
where a general class will be there the functionality
common for all Card
classes you can put there and after that in each subclasses Card
you can put specific functionality of the class and also can use common functionality of the base class.Also you can override the methods of the superclass.
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
public class SuperCardClass {
........
public void someCommonFunction() {
}
}
public class SubCardFirstClass extends SuperCardClass {
@Override
public void someCommonFunction() {
//specific to sub class
}
}
Upvotes: 1