0

I'm practicing making the game Battleship. I made the game grid for the player here, and tried to copy the grid for the computer player. However, when I change the AI's grid, it changes the player's grid. I read this is because they are referenced together, not individual copies.

    static String[] Row1 = new String[] {" ", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
    static String[] Row2 = new String[] {"A", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row3 = new String[] {"B", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row4 = new String[] {"C", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row5 = new String[] {"D", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row6 = new String[] {"E", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row7 = new String[] {"F", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row8 = new String[] {"G", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row9 = new String[] {"H", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row10 = new String[] {"I", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
    static String[] Row11 = new String[] {"J", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};

    static String[][] playerGrid = {Row1,Row2,Row3,Row4,Row5,Row6,Row7,Row8,Row9,Row10,Row11};

    static String[][] aiGrid = playerGrid;

I read about using clone(), but I'm not sure where that goes in the code.

I also tried

    static String[][] aiGrid = new String[][](playerGrid.getText());

but I got an error stating I was trying to store a String [] into a String[][]

Any advice?

Ahmad Al-Kurdi
  • 2,248
  • 3
  • 23
  • 39
Bill
  • 1
  • 2
  • 2
    Possible duplicate of [copy a 2d array in java](http://stackoverflow.com/questions/1686425/copy-a-2d-array-in-java) – resueman Jan 03 '16 at 20:39

4 Answers4

2

Try Arrays.copyOf(...) inside a loop:

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

Example:

    String[][] source = {{"a","b","c"},{"d","e","f"},{"g","h","i"}};
    String[][] copy = new String[source.length][];
    for(int i = 0 ; i < source.length ; i++) {
        copy[i] = Arrays.copyOf(source[i],source[i].length);
    }

The clone() clones the enclosing array, but not the inner arrays. For example:

    String[][] source = {{"a","b","c"},{"d","e","f"},{"g","h","i"}};
    String[][] copy = source.clone();
    boolean areTheSame = true;
    for(int i = 0 ; i < source.length ; i++) {
        areTheSame = areTheSame && (source[i] == copy[i]);
    }
    System.out.println("areTheSame = " + areTheSame);

The output is areTheSame = true

Mario
  • 1,661
  • 13
  • 22
0

clone would only copy the first dimension.

You can try this:

static String[][] playerGrid = {Row1.clone,Row2.clone,Row3.clone,Row4.clone,Row5.clone,Row6.clone,Row7.clone,Row8.clone,Row9.clone,Row10.clone,Row11.clone};
Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
0

The way I would go about setting up the two arrays is by passing them both through a method to initialize the grid.

public String[][] playerGrid;
public String[][] aiGrid;

public static void main(String[] args){

    initBoard(playerBoard);
    initBoard(aiBoard);

}

public static void initBoard(String[][] board){

    String[] digits=new String[]{"1","2","3","4","5","6","7","8","9","10"};
    String[] chars=new String[]{"A","B","C","D","E","F","G","H","I","J"};
    board = new String[11][11];
    for(int y=0;y<11;y++){
        for(int x=0;x<11;x++){
            if(x == 0 && y == 0){
                board[y][x] = " ";
            } else if(x == 0){
                board[y][x] = chars[y-1];
            } else if(y == 0){
                board[y][x] = digits[x-1];
            } else {
                board[y][x] = "-";
            }
        }
    }
}

You could also use this method later if you add the ability to restart the game. Just pass the grids through the method once more and they'll be good as new.

JereTheJuggler
  • 127
  • 1
  • 2
  • 9
-1

This will do it:

static String[][] aiGrid = playerGrid.clone();

Here you use to method clone of Object to clone the String[][] playerGrid. This will copy the variable playerGrid to aiGrid

Extremelyd1
  • 110
  • 7