How to call 200 functions with 1 variable?



  • Hello I need to write 200 different functions. More importantly I need to name or mark them properly so I can call them with 1 variable (i).

    For example, if i has a value of 100 then I should call #100 function.

    I tried

    void (*a)[200]();
    

    but it won't compile. Is it possible to use Vector or something?

    Thanks for the help!


  • Lifetime Qt Champion

    Hi,

    What are these 200 functions ?


  • Moderators

    @MasterBlade Why do you need 200(!) functions? Or do you mean you want to call one using an index?
    If so then do:

    void f1() {}
    typedef void(*Function)();
    Function functions[200];
    functions[0] = &f1;
    ...
    // call one
    functions[i]();
    


  • @SGaist said in How to call 200 functions with 1 variable?:

    Hi,

    What are these 200 functions ?

    @jsulm said in How to call 200 functions with 1 variable?:

    @MasterBlade Why do you need 200(!) functions? Or do you mean you want to call one using an index?

    I'm designing a card game. It currently has about 200 cards. There needs to be 1 function for each of them to do something.

    I think it should be possible to call the corresponding function with only the card #


  • Lifetime Qt Champion

    Are these 200 functions really doing something very different ?



  • @SGaist said in How to call 200 functions with 1 variable?:

    Are these 200 functions really doing something very different ?

    Of course. Every card is unique. They share something in common but there are always differences. I may even need more than 1 functions for a card.


  • Moderators

    @MasterBlade said in How to call 200 functions with 1 variable?:

    I think it should be possible to call the corresponding function with only the card #

    See the code I posted above



  • @jsulm said in How to call 200 functions with 1 variable?:

    @MasterBlade said in How to call 200 functions with 1 variable?:

    I think it should be possible to call the corresponding function with only the card #

    See the code I posted above

    Yeah sure that works. Thank you so much.

    One more question please,

    Is it possible to store those function to an INI file on a hard disc? So that I don't need to store all 200 functions to my program. And that I can take only what I need without bothering other functions.


  • Moderators

    @MasterBlade How do you want to store C++ functions in a text file?
    And what is the problem you want to solve with this? You would still need to store 200 functions somewhere, right?



  • @jsulm said in How to call 200 functions with 1 variable?:

    @MasterBlade How do you want to store C++ functions in a text file?
    And what is the problem you want to solve with this? You would still need to store 200 functions somewhere, right?

    Well I want to store them in a text file so that I only read what I need from it.

    Maybe I can pick up some key elements from them and make a universal function to solve all problems once and for all.


  • Moderators

    @MasterBlade Yes, you should rather store the data you need for all these cards, instead of functions. Then use one function with these different data.



  • hi @MasterBlade ,

    I have the feeling you're approching this subject from the wrong angle.

    What you should do is create a base class Cards that covers all functions / attributes / properties that all cards have (e.g color: ["check","cross","hearts"..], [played/not played] etc, and than derive individual classes from base class Joker, Ace, King etc.

    You know, object oriented, the way it's thaught :-)



  • @J.Hilk said in How to call 200 functions with 1 variable?:

    hi @MasterBlade ,

    I have the feeling you're approching this subject from the wrong angle.

    What you should do is create a base class Cards that covers all functions / attributes / properties that all cards have (e.g color: ["check","cross","hearts"..], [played/not played] etc, and than derive individual classes from base class Joker, Ace, King etc.

    You know, object oriented, the way it's thaught :-)

    Thanks for the suggestion. You are absolutely correct. That's what I'm doing now.

    I already have a big class containing all the basic properties of those cards. I stored these properties in a text file on my hard disk.

    But as for those functions, I don't have an idea on how to deal with them, since every card has unique functions, (some would allow you to draw more cards, while others deal damage to other cards) and it would be difficult to store functions directly on a hard disk.

    I am now trying to disassemble those functions to some basic elements. That's good for normalization, but it proves to be hard labor work. Maybe I should just add those functions to the program manually.


  • Qt Champions 2017

    Hi
    Its sounds like a very odd design.
    Are you using virtual functions ?
    Polymorphic behavior is the trademark of c++/OOP.
    http://www.cplusplus.com/doc/tutorial/polymorphism/

    So you base class should define the basic features of a card, and you would subclass
    the base class to override the functions that changes what should happen pr card type.


  • Qt Champions 2017

    @MasterBlade said in How to call 200 functions with 1 variable?:

    But as for those functions, I don't have an idea on how to deal with them, since every card has unique functions, (some would allow you to draw more cards, while others deal damage to other cards) and it would be difficult to store functions directly on a hard disk.

    Apply once more what mr. Hilk wrote up there, you need to design first and then go to implementing. Meaning that what you describe is a prime candidate for composition tied together with polymorphism. Every action would be a class, and all actions a card can do would derive from the same (abstract) base class. Then the actions would be composed into the cards, or in other words a card will keep a list of actions that it'd do when called upon. Then you can write generic code for the card that iterates all the actions from the list through a base pointer and invokes them.



  • @mrjj said in How to call 200 functions with 1 variable?:

    Hi
    Its sounds like a very odd design.
    Are you using virtual functions ?
    Polymorphic behavior is the trademark of c++/OOP.
    http://www.cplusplus.com/doc/tutorial/polymorphism/

    So you base class should define the basic features of a card, and you would subclass
    the base class to override the functions that changes what should happen pr card type.

    Thank you so much for the advice. I do appreciate it.

    My program is very basic. I used one object for the cards and another for players.

    All cards share something in common, like their properties or attributes which can be stored as variables. But their abilities are unique for each card and I am not sure if I can store them with variables. If I can't I have to use functions for each of them.

    For now I'm storing all card info (with variables) in a text file. And I only initialize cards necessary for the game (usually 30-40 unique cards in a 'Deck'). If I have to use functions, since I can't store functions in a text file I have to initialize all cards in my program which cost memory and time. I will expand the card pool later and likely to have a 3,000 card pool in total. That would be insane.



  • @MasterBlade

    • It would be insane to have 3,000 different functions/sub-classes!
    • You cannot store C++ source code in a file to be "read in and acted upon" at runtime.
    • You want to create whatever common code for all cards in your Card class in C++ (compiled) code.
    • You want to store whatever information about each individual card as data (not code) --- a description of its attributes, effects, its image, etc. --- in some kind of external file. It could be XML, JSON or whatever, or it could even be stored in a database.
    • You want to read/access that at runtime as required, and your C++ code should be generic enough that it can look at that data and have code which acts upon it to produce the desired effects.


  • @JonB said in How to call 200 functions with 1 variable?:

    @MasterBlade

    • It would be insane to have 3,000 different functions/sub-classes!
    • You cannot store C++ source code in a file to be "read in and acted upon" at runtime.
    • You want to create whatever common code for all cards in your Card class in C++ (compiled) code.
    • You want to store whatever information about each individual card as data (not code) --- a description of its attributes, effects, its image, etc. --- in some kind of external file. It could be XML, JSON or whatever, or it could even be stored in a database.
    • You want to read/access that at runtime as required, and your C++ code should be generic enough that it can look at that data and have code which acts upon it to produce the desired effects.

    Thanks for the suggestion. Sorry I didn't read the post yesterday.

    I totally agree that 3,000 functions are insane. But 200 is rather acceptable. I want to make life easier by writing 200 functions for now and make a running program. I'll disassemble those functions and pick out those basic elements to make a universal function that covers all card functionalities for future implements.


  • Qt Champions 2017

    @MasterBlade
    So its really 200 different function with nothing in common ?
    like
    Card 1
    Burn()
    Card 2
    Freeze()
    Card 3
    ExtraTurn()
    ....


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.