0

Musical Chairs. Musical Chairs is a children’s game where the players walk around a group of chairs while some music is playing. When the music stops, everyone must sit down. But, there is one less chair than there are people, so someone gets left out. And, indeed, that person is out of the game. A chair is removed. And the game is played again; someone else goes out. This continues until there is only one player left, the winner.

I am having problems storing the command line arguments in the player[]

here is my code

import java.util.*;
public class MusicalChairs {

    Player [] players;
    Random r = new Random();
    public static void main(String[] args){


      MusicalChairs mc = new MusicalChairs();



          mc.setUpGame(args);


        }

        public void setUpGame(String [] p){
            System.out.println("This is how we stand.......");

           for (int i = 0; i < p.length; i++){

            System.out.println(p[i]+" is "+ Player.Status.IN);

           }



        }

        public void showStatus(){


        }

        public void winner(){

            System.out.println("is the winner");
        }

    }

class Player{
    enum Status{IN,OUT};
    private String name;
    private Status status;

   public Player(String n){
       name=n;
   }

   public String getName(){
       return name;
   }

   public void setStatus(Status s){
       status=s;
   }

   public Status getStatus(){
       return status;
   }


    public String toString(){
      String ret = name;
      if(status==Status.IN){
          ret="IN ";
      }
      else{
          ret="OUT ";
      }
        return ret;
    }


}
1
  • 1
    You're not storing the arguments in your array. What's your specific problem? Commented Oct 2, 2012 at 0:25

2 Answers 2

1

You're not storing the arguments in your array. If your question is how to do it, then you should:

  • Initialize the players array.
  • For each argument, you must create a Player object and store it in the array.
  • Use the data in your program.

This could be done like this:

public void setUpGame(String [] p) {
    System.out.println("This is how we stand.......");
    //Initialize the `players` array.
    players = new Player[p.length];
    for (int i = 0; i < p.length; i++){
        System.out.println(p[i]+" is "+ Player.Status.IN);
        //For each argument, you must create a `Player` object and store it in the array.
        players[i] = new Player(p[i]);
        players[i].setStatus(Status.IN);
    }
    //Once your array is filled, use the data in your program.
    //...
}

The question is still open: What's your specific problem?

Sign up to request clarification or add additional context in comments.

2 Comments

+1 beat me to it ;) - You may also want to set the player status (but that's just me being nit picky ;))
@PollyPoll Could you do me favor, if these answers helped, could you mark Luggi's answer as accepted (the green tick next to the question), as he posted first ;)
1

I think you need to update your code to create new players and maintain the reference in your array...

public void setUpGame(String [] p){
    System.out.println("This is how we stand.......");

    // You may want to check for a 0 number of players...
    players = new Player[p.length];

    for (int i = 0; i < p.length; i++){
        players[i] = new Player(p[i]);
        players[i].setStatus(Player.Status.IN);
        System.out.println(players[i].getName()+" is "+ players[i].getStatus());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.