Results 1 to 4 of 4
- 09-01-2010, 02:11 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 17
- Rep Power
- 0
Creating unspecified ammount of objects
Hey, I am making a DOS game where you can be multiple players. Therefor I have a class called "Player" that I create objects from.
In the beginning of the game I want the player to choose how many players that shall participate and then I want to create that many "Player" objects, how would I do this? I know how to ask the player how many players that shall participate but I don't know how to create unspecified ammount of objects!
Example 1:
Player says: "I want there to be 3 players in this game"
*Program creates 3 Player objects called player1, player2, player3* <- The part I need help with!
Example 2:
Player says: "I want there to be 5 players in this game"
*Program creates 5 Player objects called player1, player2, player3, player4, player5* <- The part I need help with!
I hope I made myself clear and thank you for the help.
- 09-01-2010, 05:12 PM #2
You have to understand that in your example, the # of elements is specified. Ask the client how many player would like to play and store that answer in a variable. There you go. You now know how many players are going to play. Example:
You can now make an array of Players with numbOfPlayers elements. Finally, iterate through that array and initialize a Player for every element.Java Code:Scanner cin = new Scanner(System.in); System.out.print("How many players would like to play this game? "); int numbOfPlayers = cin.nextInt(); System.out.println(numbOfPlayers + " number of players will be playing.");Last edited by Lil_Aziz1; 09-01-2010 at 05:14 PM.
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 09-01-2010, 05:43 PM #3
Is your problem with the creating of these 5 named variable at execution time?creates 5 Player objects called player1, player2, player3, player4, player5
It can't be done. All variables are given names at compile time.
You can use a Map object to give an object a name:
HashMap hp = new HashMap();
hp.put("player1", new Player(1)); // use key for name of object
- 09-01-2010, 06:46 PM #4
Member
- Join Date
- Aug 2010
- Posts
- 17
- Rep Power
- 0
I was a bit unsure about how to describe what I wanted, so good thing you corrected it. Thanks for your answer, I'll probably use arrays like you said.
Yeh I'm guessing my problem is with creating the 5 variables at execution time. My first idea was that maybe you could do something similar to how you build strings but I guess that ain't possible then. I'll just use arrays for now, but I'll be checking out HashMaps for sure.
Similar Threads
-
Creating an Array of Objects
By int80 in forum New To JavaReplies: 4Last Post: 08-09-2011, 12:40 PM -
creating dynamic objects?
By alacn in forum New To JavaReplies: 4Last Post: 06-04-2010, 03:51 AM -
creating public objects
By TaxpayersMoney in forum New To JavaReplies: 2Last Post: 05-19-2010, 06:50 PM -
HashMap with unspecified value type
By kleelof in forum New To JavaReplies: 16Last Post: 04-28-2010, 12:57 PM -
Creating custom objects
By coltragon in forum New To JavaReplies: 11Last Post: 12-29-2009, 10:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks