7 Replies - 2669 Views - Last Post: 18 December 2012 - 05:31 PM Rate Topic: -----

#1 JaoWolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-August 12

Card Game Problem

Posted 23 August 2012 - 01:29 PM

Hey guys, this is my first time posting here...

I would you like some ideas for the following situation. In a card game like pokémon, for example, suppose there is a class called OptionCard derived from another class Card. OptionCard is responsible for adding specific effects to the gaming scene. Instances of OptionCard obviously represent different cards with different effects, ie instructions that specify such an effect. Well, the question is: how can I do to create different instances of OptionCard where each of them have different effects? It's like a single member function of a class execute different effects according to their instances. Any idea?

Is This A Good Question/Topic? 0
  • +

Replies To: Card Game Problem

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,681
  • Joined: 25-December 09

Re: Card Game Problem

Posted 23 August 2012 - 01:47 PM

Quote

Well, the question is: how can I do to create different instances of OptionCard where each of them have different effects? It's like a single member function of a class execute different effects according to their instances. Any idea?


If you have a good understanding of class inheritance and pointers you may want to look into Polymorphism.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 JaoWolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-August 12

Re: Card Game Problem

Posted 23 August 2012 - 01:51 PM

Yes buddy, I can create derived classes of OptionCard and reimplement a virtual function effect() in each of them. But I think this is not a good idea. I don't wanna have a class for each single card, you know...
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3000
  • View blog
  • Posts: 10,390
  • Joined: 08-August 08

Re: Card Game Problem

Posted 23 August 2012 - 02:05 PM

You wouldn't unless every card has a unique effect . You'd only have classes for each specific effect. If different cards can have varying levels of different effects then you might make the levels parameters used by the method(s).
Was This Post Helpful? 0
  • +
  • -

#5 JaoWolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-August 12

Re: Card Game Problem

Posted 23 August 2012 - 02:47 PM

Thanks CTphpnwb, I think it's a good idea. I can create an abstract class Effect and then subclass other classes with trivials effects. With that in hand, all the cards can have a pointer to Effect and after beeing loaded I can define what kind of effect that card would have. Thanks. =]
Was This Post Helpful? 0
  • +
  • -

#6 antolyevich  Icon User is offline

  • New D.I.C Head

Reputation: -6
  • View blog
  • Posts: 12
  • Joined: 02-September 12

Re: Card Game Problem

Posted 13 December 2012 - 10:32 PM

I would like to add that you can also simply have a variable damage,hp, etc (whatever stats you need) and dependant on what user has, you can vary these stats without more than a few function calls, which will save memory and prevent crashes.
Was This Post Helpful? -1
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,246
  • Joined: 27-December 08

Re: Card Game Problem

Posted 14 December 2012 - 10:12 AM

antolyevich, please make sure to check the dates on the threads. There is no reason to go through and necro threads if you don't have significant content to add.
Was This Post Helpful? 0
  • +
  • -

#8 extremeblueness  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 186
  • Joined: 22-October 12

Re: Card Game Problem

Posted 18 December 2012 - 05:31 PM

For future reference for anyone who wants to know something like this, a simple solution would be to have one class which declares variables and create objects of that class. Like this:

public class Example {
   int x;
   int y;
}

public class Example2 {
   public static void main(String args[]) {
      Directions example = new Directions();

      example.x = 3;
      example.y = 4;
   }
}



And then you could declare as many objects with these variables as you need. You could use if statements to control the effects.

This is java, not C++, but from my understanding, it could easily be converted to C++.

This post has been edited by extremeblueness: 18 December 2012 - 05:33 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1