Results 1 to 4 of 4
  1. #1
    plm-pusik is offline Member
    Join Date
    Aug 2010
    Posts
    17
    Rep Power
    0

    Default 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.

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    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:

    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.");
    You can now make an array of Players with numbOfPlayers elements. Finally, iterate through that array and initialize a Player for every element.
    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)

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    creates 5 Player objects called player1, player2, player3, player4, player5
    Is your problem with the creating of these 5 named variable at execution time?
    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

  4. #4
    plm-pusik is offline Member
    Join Date
    Aug 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    You have to understand that in your example, the # of elements is specified.

    ...
    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.

    Quote Originally Posted by Norm View Post
    Is your problem with the creating of these 5 named variable at execution time?
    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
    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

  1. Creating an Array of Objects
    By int80 in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 12:40 PM
  2. creating dynamic objects?
    By alacn in forum New To Java
    Replies: 4
    Last Post: 06-04-2010, 03:51 AM
  3. creating public objects
    By TaxpayersMoney in forum New To Java
    Replies: 2
    Last Post: 05-19-2010, 06:50 PM
  4. HashMap with unspecified value type
    By kleelof in forum New To Java
    Replies: 16
    Last Post: 04-28-2010, 12:57 PM
  5. Creating custom objects
    By coltragon in forum New To Java
    Replies: 11
    Last Post: 12-29-2009, 10:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •