0

I am building a classical Nim game for practicing. This project only uses an array to deal with the player data object. For removing the existing player, I created a method called removeplayer and assign the specific object to a null value.

So far, I am able to:

  1. Create a non-null array for saving players' data that I can add (assign) new players.
  2. During the test, the null value will be assigned a non-null value in the array. For example, I have an array length for 5 space, but only 2 players' data being put in the array. The rest of the place will be filtered with a non-null object.

I thought I could just assign the object I wanted to remove by assigning it to null, and when returning the array, it should be filtered with non-null object. However, it turned out to be malfunction that I can't remove the player data.

Therefore, is there any way to replace the existing data with a non-null object or any way to remove the object in the array?

Here is part of my code Nimsys:

public static void searchAndRemovePlayer(String user) {

    for (int i = 0; i < NimPlayer.getCounter(); i++) {
        String userName = NimPlayer.getPlayer()[i].getUserName().trim();
        if (userName.equals(user)) {
            NimPlayer.getPlayer()[i] = null;
            System.out.println("Remove successfully!");// A test to see if the code runs
            return;
        }
    }
    System.out.println("The player does not exist.\n");
}
public static void main(String[] args) {
    System.out.println("Welcome to Nim\n");
    Scanner in = new Scanner(System.in);
    while (true) {
        System.out.print('$');
        String commandin = in.next();


if (commandin.equals("removeplayer")) {
            String user = in.nextLine().trim();

            if (user.equals("")) {
                System.out.println("Are you sure you want to remove all players? (y/n) \n");

                commandin = in.next();
                if (commandin.equals("y")) {
                    for (int i = 0; i < NimPlayer.getCounter(); i++) {
                        NimPlayer.getPlayer()[i] = null;
                    }
                    System.out.println("Remove all the players");
                }
            }
            if (!user.equals("")) {
                searchAndRemovePlayer(user);
            }
        }
        }

And below is the part of the NimPlayer:

public class NimPlayer {
private String userName;
private String familyName;
private String givenName;
private int score;
private int gamePlayed;

private static int counter;
private static final int SIZE = 5;
static NimPlayer[] playerList = new NimPlayer[SIZE]; // set an array here


//define NimPlayer data type
public NimPlayer(String userName, String surName, String givenName) {
    this.userName = userName;
    this.familyName = surName;
    this.givenName = givenName;

}
// create new data using NimPlayer data type
public static void createPlayer(String userName, String familyName, String givenName) {
    if (counter<SIZE) {
        playerList[counter++] = new NimPlayer(userName, familyName, givenName);
    } else {
        System.out.println("Cannot add more players.");
    }
}
public static int getCounter() {
    return counter;
}
public static NimPlayer [] getPlayer() {   
    NimPlayer[] nimPlayers = Arrays.stream(playerList).filter(Objects::nonNull).toArray(NimPlayer[]::new);
    counter = nimPlayers.length;  //update the counter
    return nimPlayers; }

//getters and setters
}
2
  • 1
    getPlayer() returns a new array has nothing to do with playerList array. So no mattter what you do with it, the playerList won't change. Commented Apr 27, 2020 at 3:12
  • Thanks for pointing out. I am trying to create a setter for it. If you know how to deal with it, could you please illustrate to me as I am quite new to java. @meng Commented Apr 27, 2020 at 4:20

2 Answers 2

1

What you are doing in NimSys is getting the player list array from NimPlayer, and then setting it to null in NimSys. It still exists in NimPlayer, so the next time you call getPlayer() it'll still return the complete array with no null values, as it was never modified in NimPlayer. What you should do instead is this:

in NimPlayer

create a new method called setPlayerList that takes array as a parameter. Then set the playerList in NimPlayer equal to this new array.

And in NimSys, create a new array and set it to getPlayerList, then set the value you want to null. Then call setPlayer() and parse the new array as parameter so that it will be modified in NimPlayer.

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

1 Comment

Logically understood. No offense, could you please show me how to do it with this problem. @Ghost
0

Based on @Ghost's statement, I figured out myself for doing this.

First in NimPlayer, create a setter for the playerList array. If the original array is static, then, the setter should be static as well.

public static void setPlayerList(NimPlayer [] newplayerList) {
    NimPlayer.playerList = newplayerList; }

Second, to use the setter, I need to get the original array for replacing. And pick the element that I want to delete.

Third, call the setter to put the new array to replace the previous one.

NimPlayer [] playerList = NimPlayer.getPlayer();
for (int i = 0; i < playerList.length; i++) {
        String userName =playerList[i].getUserName().trim();
        if (userName.equals(user)) {
            playerList[i] = null;
            System.out.println("Remove successfully!");
            NimPlayer.setPlayerList(playerList);//call the setter to replace older list
            return;
        }
    }

Finally, wrap these lines of codes to a function in Nimsys.

public static void searchAndRemovePlayer(String user) {
    NimPlayer [] playerList = NimPlayer.getPlayer();
    for (int i = 0; i < playerList.length; i++) {
        String userName =playerList[i].getUserName().trim();
        if (userName.equals(user)) {
            playerList[i] = null;
            System.out.println("Remove successfully!");
            NimPlayer.setPlayerList(playerList);
            return;
        }
    }
    System.out.println("The player does not exist.\n");
}

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.