Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Game

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Game

    Hello! I have done a hangman game and now I need to implement that 2 players can play. I have a class for one player and methods for how to make one player. But if i need to add another player how do I do? My thought is that I need to do a switch case so the user can choose how many players. Then I need to make an instance of the player class so I can save the second user. But then what?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Game

    Put instances of the Players in a list and rotate through the list of players giving each player his turn.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Game

    Player player = null;//this variable contains the loaded player.
    Player player2 = null;
    List<Player> listPlayer=new ArrayList<>(); // NY

    switch (startMenu.getString()) {
    case "1":
    Scanner sc= new Scanner(System.in); //ny

    System.out.println("Hur många spelare?1-4: "); //ny
    int playerAmount= sc.nextInt(); //ny

    if (playerAmount == 1) {//player == null
    System.out.println("Du har valt 1 spelare");
    if(player == null){
    Player newPlayer = dataHandler.createPlayer();
    listPlayer.add(newPlayer);
    if(newPlayer==null){
    break;
    } else {
    player= listPlayer.get(0);
    }
    }
    else if (playerAmount==2){
    System.out.println("Du har valt 2 spelare");
    if(player == null) {
    Player newPlayer = dataHandler.createPlayer();
    Player newPlayer2 = dataHandler.createPlayer();
    listPlayer.add(newPlayer);
    listPlayer.add(newPlayer2);
    if (newPlayer==null && newPlayer2 == null){
    break;
    } else{
    player= listPlayer.get(0);
    }
    }
    }
    }


    //Start game.
    Game newGame = new Game(player,dataHandler.getWordsFromStorage());

    like this?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Game

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Can you explain in English what that code is supposed to be doing?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. LWJGL Game: Odd behavior in game. Need help.
    By vividMario52 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 2nd, 2013, 05:43 PM
  2. Replies: 3
    Last Post: March 1st, 2013, 11:01 PM
  3. Help with Snake Game Java Code: It's Impossible to Lose the Game
    By haruspex_icis in forum What's Wrong With My Code?
    Replies: 20
    Last Post: December 17th, 2012, 12:21 PM
  4. Game Maker Language and Game Maker and Zelda Classic thread
    By Fira in forum Other Programming Languages
    Replies: 3
    Last Post: April 17th, 2012, 08:59 AM
  5. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM