1

I am a beginner, and I am building a classical NimGame. Before, I used to save the project using an array. Now, I modify it to apply the ArrayList to this time. It seems no problem, though, the functions I've made are not working without any errors. I couldn't figure out why.

For now, I tried to add the NimPlayer type into the new playerList, which is the ArrayList. I put the ArrayList in the NimModel, and use the constructor from the NimPlayer to create new players. The Nimsys is the main panel to give commands and receive user inputs. That's why I separate them into three classes.

The command is like this $addplayer userName,familyName,givenName. And the scanner should process the string and go through the constructor to be a new object.

Any help is highly appreciated, and thank you for your kindness and patience.

Here is my related code Nimsys:

public class Nimsys {

private NimModel nimModel;

public static void main(String[] args) {
    Nimsys nimsys = new Nimsys();
    nimsys.processCommands();
}

private void processCommands() {
    this.nimModel = new NimModel();
    Scanner in = new Scanner(System.in);

    System.out.println("Welcome to Nim\n");
    while (true) {
        System.out.print('$');
        String commandin = in.nextLine().trim();

        if (commandin.equalsIgnoreCase("addplayer")) {
            addplayer(in);
        }
        if (commandin.equalsIgnoreCase("removeplayer")) {
            removeplayer(in);
        }

}

private String[] splitName(String inName) {
    String[] splittedLine = inName.split(",");
    String[] name = null;
    if (splittedLine.length == 3) {
        String userName = splittedLine[0].trim();
        String familyName = splittedLine[1].trim();
        String givenName = splittedLine[2].trim();
        name = new String[3];
        name[0] = userName;
        name[1] = familyName;
        name[2] = givenName;
    }
    return name;
}
private void addplayer(Scanner in) {
    String inName = in.nextLine().trim();
    String[] name = splitName(inName);

    if (name != null && name.length == 3) {
        ArrayList<NimPlayer> playerList = nimModel.getPlayerList();
        for (NimPlayer player: playerList) {
            if (player.getUserName().contains(name[0])) {
                System.out.println("The player already exists.");
                return;
            } else {
                nimModel.createPlayer(name[0], name[1], name[2]);
                System.out.println("The player has been created.");
            }
        }        
    } 
private void removeplayer(Scanner in) {
    String removeUserName = in.nextLine().trim();
    NimPlayer player = nimModel.removePlayer(removeUserName);
    if (player == null) {
        System.out.println("The player does not exist");
    } else {
        System.out.println("Player " + player.getUserName() + 
                " removed successfully!");
    }
}

And the NimModel:

public class NimModel {

private NimPlayer nimplayer;

private ArrayList<NimPlayer> playerList = new ArrayList<>();

public void createPlayer(String userName, String familyName, String givenName) {
    NimPlayer player = new NimPlayer(userName, familyName, givenName);
    playerList.add(player);     

}

public ArrayList<NimPlayer> getPlayerList() {
    return playerList;
}
public NimPlayer removePlayer(String userName) {
    for (NimPlayer player: playerList) {
        String nameCheck = nimplayer.getUserName();
        String playerName = player.getUserName();
        if (playerName.equals(nameCheck)) {
            playerList.remove(player);
            break;
        } 
    }
    return null;

Lastly, NimPlayer class

public class NimPlayer {


private final String userName;
private String familyName;
private String givenName;

private int gamesPlayed;
private int gamesWon;
private int winRatio;



public NimPlayer(String userName, String familyName, String givenName) {

    this.userName = userName;
    this.familyName = familyName;
    this.givenName = givenName;
    this.gamesPlayed = 0;
    this.gamesWon = 0;
}
//getters and setters
}
2
  • 1
    Are there some lines missing from your posted code or is your addPlayer(Scanner in) method defined within your processCommands() method? This won't work. Secondly, for cleanliness, since your playerList is within NimModel, you should move any code to do with iterating over that list to within NimModel. Commented May 9, 2020 at 9:47
  • Thank you for reviewing my code. What should I do with the addplayer(Scanner in)? It should take user input, though. I'll move those code related to iteration of playerList@codebod Commented May 9, 2020 at 9:56

2 Answers 2

1

When you use scanner.nextLine() you are asking for a new input to the user. So if you want the format: $addplayer user,firstName,lastName you have to fetch it into a string and use this string:

while (true) {
        System.out.print('$');
        String commandin = in.nextLine().trim();

        if (commandin.split(" ")[0].equalsIgnoreCase("addplayer")) {
            addplayer(commandin);
        }
    }
}

private void addplayer(String commandin) {
    String inName = commandin.split(" ")[1];
    String[] name = splitName(inName);
      ....
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again! Have a nice day @Alex
1

in a nutschell:

private void addplayer(Scanner in) {
String inName = in.nextLine().trim();
String[] name = splitName(inName);
if (name != null && name.length == 3) {
    ArrayList<NimPlayer> playerList = nimModel.getPlayerList();
    for (NimPlayer player: playerList) {
        if (player.getUserName().contains(name[0])) {
            System.out.println("The player already exists.");
            return;
        }
    }
    nimModel.createPlayer(name[0], name[1], name[2]);
    System.out.println("The player has been created.");         
}

Furthermore, your addPlayer() given in Nimsys is defined in your While(true) but I think it's more a typing error. Personally I would also give a constructor to your model:

import java.util.ArrayList;

public class NimModel {

private NimPlayer nimplayer;

private ArrayList<NimPlayer> playerList;
public NimModel()
{
   this.playerList =  new ArrayList<NimPlayer>();
}
public void createPlayer(String userName, String familyName, String givenName) {
    NimPlayer player = new NimPlayer(userName, familyName, givenName);
    playerList.add(player);

}

public ArrayList<NimPlayer> getPlayerList() {
    return playerList;
}

public NimPlayer removePlayer(String userName) {
    for (NimPlayer player : playerList) {
        String nameCheck = nimplayer.getUserName();
        String playerName = player.getUserName();
        if (playerName.equals(nameCheck)) {
            playerList.remove(player);
            break;
        }
    }
    return null;
}
}

1 Comment

Thank you for reviewing my code. I've spotted the tying error and edited the post. My question now turns to that it seems the scanner doesn't take the user input at the same time. $addplayer user,firstName,lastName doesn't work in the processCommand.

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.