I have created a Player
class that takes in String playerName
to it's constructor. The game will have human players and computer bots, each is created as Player player1 = new Player("Tom");
. After I have created each Player
instance for the human players, I would now like to create a loop that achieves the following:
for (int i = (numHumanPlayers + 1); i < totalNumPlayers; i++) {
String instanceName = "player" + i;
String playerName = "Bot " + (i - numHumanPlayers);
Player <instanceName> = new Player(playerName);
}
where <instanceName>
is the contents of String instanceName
.
For example, if there were two human players and two computer players, this would result in the following for players:
Player player1 = new Player("Tom");
Player player2 = new Player("Jerry");
Player player3 = new Player("Bot 1");
Player player4 = new Player("Bot 2");
I have seen the Class.forName()
method as well as using the Constructor.newInstance()
and I believe that these are what I need to use. However, I cannot figure out the syntax to use these methods in order to achieve the result for my custom Player
class.