whiteElephant
whiteElephant

Reputation: 263

creating objects for the correct object

first off apologies for the question title I wasn't to sure what to name this.

anyway I'm working on a game, where there can be a number of players who can each have a number of pets. I have developed the main structure to the game, e.g. player class, pet class and a main class .. From there I have been working on the GUI, where I ask how many players, and how many pets each player would like.. Where I am getting stuck is how to to create the pets for each player.

I have created a pretty basic form that asks for the player to choose a pet type, give it a name and then create the pet..

public  void createPets( final Player player){

//various buttons,comboBox and labels go here
//layout managers
//add it all to a frame

JButton jbCreatePet = new JButton("Create Pet");

jbCreatePet.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        String name = jtfName.toString();

        if (cbSpecies.getSelectedIndex() == 0){
            Alien alien = new Alien();
            alien.setName(name);
            player.getAllPets.add(alien);


        }
        else if(cbSpecies.getSelectedIndex() == 1){
            create other pet2
        }
        else{
            create other pet3
        }
    }
});

player is a Player object passed into the method using a for loop.. 

for (Player player: allPlayers){
    createPets(player);
}

Now I know that its NOT correct to use the for loop e.g. the form will simply skip to the last player and none of the other players will get to create a pet..

So I have a couple of questions:

  1. When I assigned the created pet to the players list of all pets, eclipse told me I had to create it final. I somewhat understand why but what I am wondering is by making the player parameter final does that mean i wont be able to create pets for other plays, only the first player..

  2. How can I show my form to each player e.g. 2 players in the game both with 2 pets, player 1 chooses a pet and gives it a name then creates it, he will then be told he needs to create another pet (form shows again) so he creates another pet, then its player 2s turn to choose and create 2 pets... I guess I am trying to figure out how to pass the right player argument into the createPet method at the right time...

Please let me know if you would like me to clarify anything else...

Big thanks to whoever can help me with this!!!!

Upvotes: 2

Views: 86

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

When I assigned the created pet to the players list of all pets, eclipse told me I had to create it final. I somewhat understand why but what I am wondering is by making the player parameter final does that mean i wont be able to create pets for other plays, only the first player..

Eclipse isn't requiring this -- Java is because you're using the Player parameter within an anonymous inner class and so it must be final. This will not prevent you using this same method for other players.

How can I show my form to each player e.g. 2 players in the game both with 2 pets, player 1 chooses a pet and gives it a name then creates it, he will then be told he needs to create another pet (form shows again) so he creates another pet, then its player 2s turn to choose and create 2 pets... I guess I am trying to figure out how to pass the right player argument into the createPet method at the right time...

The main Game object will control all of the above, correct? I suppose you could use a for loop, one that say displays a modal dialog such as a JOptionPane inside of the loop.

Another option is to create JPanel view that allows all players to enter their pets. It's all up to you, and I recommend that you experiment with different approaches.

One main thing that you'll want to be sure to do early on is to strongly separate your program's logic from the GUI. For instance your Player and Pet classes should have no knowledge about the GUI, should have no Swing code whatsoever, so that the code to logically add Pets is non-GUI (but can and will be used by the GUI).


Edit
Consider giving your Game class a registerPlayer(Player player) or editPlayer(Player player) method that any Player can call to register their name, their Pets, and any other property that may be needed to play the game. Then have this method called once when a JButton is pressed. Don't allow the game to progress unless all Players have been properly registered.

Upvotes: 4

Related Questions