0

For our project, we have made a Players class. The idea behind this was to make an instance of this class for each player. This class contains some variables that can be retrieved with different functions.

The question is how do we call this class multiple (variable) times in a loop, but each time with a different name and different input variables. We want the number of players to be variable and different variables for each player.

Here is the class declaration from the .h file:

class Players {
  private:
    String _UID; 
    boolean _Thief;
    String _color;

  public:
    Players(String UID, String color, boolean Thief);
    String getUID();
    boolean isThief();
};

And I want to call this class in a function that would look something like this:

for (int i = 0; i <= AMOUNT OF PLAYERS; i++) {
    //Some functions to read the right input for this player
    Read UID ();
    Get color ();
    Get thiefstate ();

    Players player1 ("UID in string here", "color here", True of False here) //Player1 should become player 2 the second time etc
}

How can we change the name of the class instance each time? And whould this approach work or is there a better way to do this?

Thanks in advance

Share a link to this question (includes your user id)
| edit | | close | delete |
  • have a question about your code. What does this line mean? Player * players[NUM_PLAYERS]; Because I keep getting the error message on line" if (players[i] == NULL) " which says 'players' was not declared in this scope' So I thought it might be because of a problem with that – Tim Woertman Jun 12 at 11:44    
0

I made an example Arduino project based on assignment 4, with a class Player which can be a base for this.

In Userinterface::checkSensors() it simulates the addition of scanned uid's to the list of the controller.

Share a link to this answer (includes your user id)
| edit | delete |
  • Thank you, that helped a lot – Tim Woertman Jun 11 at 9:15    
  • I have a question about your code. What does this line mean? Player * players[NUM_PLAYERS]; – Tim Woertman Jun 12 at 11:02    
  • Because I keep getting the error message on line" if (players[i] == NULL) " which says 'players' was not declared in this scope' So I thought it might be because of a problem with that – Tim Woertman Jun 12 at 11:13    
0

I do not have time tonight for a long answer. Assignment 4a uses a class Product and maintains a list of products also, so you can have a look at how this class is organised. Thats the main reason we had this assignment...

Share a link to this answer (includes your user id)
| edit | delete |

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.