Updating an ArrayList

Lyra Orwell :

I am trying to update my board (consisting of three arraylists in an arraylist) whenever a player inputs a number. The number corresponds to a square on the board in the following way:

1 2 3
4 5 6
7 8 9

I am having trouble updating the grid.

Function

public static void playBoard(int choice, ArrayList<ArrayList<String>> board, boolean playerTurn) {
    String val;
    if (!playerTurn) {
        val = "| X |";
    }
    else {
        val = "| O |";
    }
    if (choice>=1 && choice<=3) {
        System.out.println("H");
        ArrayList<String> updateRow = board.get(0);
        if (choice ==3) {
            val+="\n";
        }
        updateRow.set(choice-1, val);
        System.out.println(updateRow);
        board.set(0, updateRow);
        System.out.println(display(board));
    }
    else if (choice>=4 && choice<=6) {
        System.out.println("H");
        ArrayList<String> updateRow = board.get(1);
        if (choice ==6) {
            val+="\n";
        }
        updateRow.set((choice-4), val);
        board.set(1, updateRow);
        System.out.println(display(board));
    }
    else if (choice>=7 && choice<=9) {
        System.out.println("H");
        ArrayList<String> updateRow = board.get(2);
        if (choice ==9) {
            val+="\n";
        }
        updateRow.set(choice-7, val);
        board.set(2, updateRow);
        System.out.println(display(board));
    }
    else {
        System.out.println("Input out of range");
        return;
    }
}

The problem is that when the user inputs a value, that entire column in which the value corresponds to is updated instead of the individual square.

I have checked that:

  • Only one if statement is triggered.
  • The update occurs only once
  • The update occurs upon the correct index.

Through my debugging, I believe the problem line is:

updateRow.set(choice-1, val);

When the user (Player 1) inputs 1:

Expected Output

| X || - || - |
| - || - || - |
| - || - || - |

Actual Output

| X || - || - |
| X || - || - |
| X || - || - |

Display function

Sorry, I didn't realize you guys needed to see this other function

    public static String display(ArrayList<ArrayList<String>> board) {
    StringBuilder builder = new StringBuilder();
    for (ArrayList<String> row : board) {
        for (String space: row) {
            builder.append(space);
        }
    }
    String text = builder.toString();
    return text;
}
Joop Eggen :

The problem seems in the creation: you probably used the same column ArrayList object for every row.

// Error:
ArrayList<String> row = new ArrrayList<>();
row.add("...");
row.add("...");
row.add("...");
for (int i = 0; i < 3; ++i) {
    board.add(row);
}

should have been:

for (int i = 0; i < 3; ++i) {
    ArrayList<String> row = new ArrrayList<>();
    row.add("...");
    row.add("...");
    row.add("...");
    board.add(row);
}

The same conceptual error means: it is not needed to do:

board.set(2, updateRow); // Not needed.

Changing the entry in the updateRow object held by the board is done by reference.

Some tips:

  • Here one can use String[][].
  • It is easier to separate display/view (the strings) from the data model (a char?), so maybe char[][] board = new char[3][3];

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=323168&siteId=1