-1

I'm a first grader of a software engineering study and I have some trouble with a showcase I have to make. I give you a quick rundown of my project.

I don't know if anyone is familiar with the game Clash Royal? Its a game based on getting higher in trophies and collecting cards. And each arena unlocks new cards.

I want to make a deck builder. Where you can select 8 cards, and I calculate the average elixer cost. I want a function that can display all the cards available for your trophy range.

With that out of the way, let me explain my problem: I made a Superclass: Card, and the classes Troop, Spell, DefBuilding, and PassiveBuilding extend on that superclass. After that, I created a class: CardPool where I defined all the objects for all the "Card classes".

Then I made a Class: Arena, and in my main, I declared all the arenas. The thing I am struggling with is the fact that I don't know how I putt all a few cards in a vector of the matching Arena. Something like card 1-2-3 needs to go into a vector of Arena 1 and card 4-5-6 need to go in a vector Arena 2. Only I have 4 different Classes witch all are cards.

So the actual question is as follow: How can I make a vector list of Arena 1 (or 2,3,4) and put 2 Troops, 1 spell, 1 defBuilding into that vector list? And eventually, be able to display them.

The last ting I tried was to Cast the class as you can see in my CardPool class. That didn't work.

note: I made my Class CardPool an interface just to try things out, heard something on school about it.

Superclass: Card

public class Card {
    private String name;
    private int elixer;

    public Card(String name, int elixer) {
        this.name = name;
        this.elixer = elixer;
    }
}

Class: Troop (only showing Troop the other classes are the same)

public class Troop extends Card {

    private int dammage;
    private int dammagePerSecond;
    private int hitpoints;
    private double range;
    private double hitSpeed;
    private String speed;
    private int deathDammage;
    private int count;

    public Troop(String name, int elixer, int dammage, int dammagePerSecond, int hitpoints, double range, double hitSpeed, String speed, int deathDammage, int count) {
        super(name, elixer);
        this.dammage = dammage;
        this.dammagePerSecond = dammagePerSecond;
        this.hitpoints = hitpoints;
        this.range = range;
        this.hitSpeed = hitSpeed;
        this.speed = speed;
        this.deathDammage = deathDammage;
        this.count = count;
    }
}

class: CardPoop (now interface, I only showing 2 of each class)

public interface CardPool {

    CardPool pekka = (CardPool) new Troop("P.E.K.K.A.", 7, 984, 546, 3458, 0, 1.8, "Slow", 0, 1 );
    CardPool archer = (CardPool) new Troop ("Archer", 3,  89, 74, 252, 5, 1.2, "Medium", 0, 2 );

    CardPool bombTower = (CardPool) new DefensiveBuilding ("Bomb Tower", 4, 184, 115, 1126, 6, 1.6, 35 );
    CardPool cannon = (CardPool) new DefensiveBuilding("Cannon", 3, 127, 158, 742, 5.5, 0.8, 30);

    CardPool barbarianHut = (CardPool) new PassiveBuilding("Barbarian Hut", 7,  1936, 60);
    CardPool elixirCollector = (CardPool) new PassiveBuilding("Elixer Collector", 6, 888, 70);

    CardPool arrow = (CardPool) new Spell("Arrows", 3, 243, 86, 4);
    CardPool goblinBarrel = (CardPool) new Spell ("Goblin Barrel", 3, 300, 900, 1.5);

class: Arena

import java.util.ArrayList;

public class Arena {
    private String name;
    private int minTrophies;
    private int maxTrophies;

    private ArrayList<CardPool> cardList = new ArrayList<>();

    public ArrayList<CardPool> getCardList() {return cardList;}

    public Arena(String name, int minTrophies, int maxTrophies) {
        this.name = name;
        this.minTrophies = minTrophies;
        this.maxTrophies = maxTrophies;
    }
}

class: Main (only showing the first arena in full)

public class Main {

    public static void main(String[] args) {

        Arena trainingCamp = new Arena("Training Camp", 0, 0);

        trainingCamp.getCardList().add(CardPool.arrow);
        trainingCamp.getCardList().add(CardPool.minions);
        trainingCamp.getCardList().add(CardPool.archer);
        trainingCamp.getCardList().add(CardPool.knight);
        trainingCamp.getCardList().add(CardPool.fireBall);
        trainingCamp.getCardList().add(CardPool.miniPekka);
        trainingCamp.getCardList().add(CardPool.musketeer);
        trainingCamp.getCardList().add(CardPool.giant);
        trainingCamp.getCardList().add(CardPool.wallBreakers);
        trainingCamp.getCardList().add(CardPool.prince);
        trainingCamp.getCardList().add(CardPool.babyDragon);
        trainingCamp.getCardList().add(CardPool.skeletonArmy);

So now I am getting an error message that I am unable to cast Cardpool.

What should I do?

I hope some of you can help me out.

Thx, Pex

Andronicus
  • 25,419
  • 17
  • 47
  • 88

2 Answers2

1

Your problem is probably appearing because the cards should not be of type CardPool.

Take 'PEKKA' for example. It is a Troop which is a child class of Card and has nothing to do with CardPool (so casting to CardPool is nonsense). As a result, your CardPool could do with being a basic class with predefined, public statically accessible Card objects for all of the different cards.

Then for each Arena, you could just have an ArrayList which acts as your cardList as you have now.

As another suggestion, adding an 'addCard(Card c)' method to Arena would be better for you than having to 'getCardList().add()' every time.

chaza150
  • 11
  • 1
0

You're casting to CardPool not Card. You would have to make Card implement CardPool

public class Card implements CardPool

or Troop itself

public class Troop extends Card implements CardPool

to be able to cast it to CardPool.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
  • Wow, that worked thank you, and how would i be able to print out all the items in the vectorlist from, lets say, Trainingcamp. – Pex van Nieuwburg Apr 16 '19 at 20:12
  • @PexvanNieuwburg I'm glad. Consider marking as correct;) Iterate like here https://stackoverflow.com/questions/10168066/how-to-print-out-all-the-elements-of-a-list-in-java/10168136 – Andronicus Apr 16 '19 at 20:14