'public 2d array equals something else in a different class

I'm creating the game battleship in java for my APCS A class. I have a placement class and a shoot class. There are 2 human players, so in P1Shoot it's supposed to take P2's board (board2), and if the value there equals 1, there is a ship there. When printed at the end of placement, it appears how it should. However, when I try accessing board2 in P1Shoot it just returns an empty array. Does anyone know why this is? When prompt for ship info, the user input all ships at once in the following format type rowColOrientation... Ex: A A1h B A6v D E3h S F4v P G7h (Horizontal or Vertical)

public class P2Placement {
public int[][] board2 = new int[10][10]; 
public void isValidPlacement(String p) { 
    if(p.length() != 25){
        System.out.println("Invalid Placement");
    }
    else {
        String s1 = p.substring(0, 5), s2 = p.substring(5, 10), s3 = p.substring(10, 15), s4 = p.substring(15, 20), s5 = p.substring(20, 25);
        PlacementMethods test1 = new PlacementMethods(s1), test2 = new PlacementMethods(s2), test3 = new PlacementMethods(s3), test4 = new PlacementMethods(s4), test5 = new PlacementMethods(s5);
        PlacementMethods[] pmethods = new PlacementMethods[]{test1, test2, test3, test4, test5};
        Ship[] ships = new Ship[5];
        for (int i = 0; i < pmethods.length; i++) {
            ships[i] = pmethods[i].getType();
        }
        for (int i = 0; i < pmethods.length; i++) {
            if (pmethods[i].getOrientation() == 'h') {
                for (int c = pmethods[i].getCol(); c < pmethods[i].getCol() + ships[i].getSize(); c++) {
                    if (getBoard2(pmethods[i].getRow(), c) != 0) {
                        System.out.println("Invalid Placement of " + ships[i].getName());
                        break;
                    }
                    if (c == pmethods[i].getCol() - 2 + ships[i].getSize() && getBoard2(pmethods[i].getRow(), c) == 0) {
                        for (int col = pmethods[i].getCol(); col < pmethods[i].getCol() + ships[i].getSize(); col++) {
                            setBoard2(pmethods[i].getRow(), col, 1);
                        }
                        System.out.println(ships[i].getName() + " was Successfullly Placed");
                        break;
                    }
                }
            }
            else if (pmethods[i].getOrientation() == 'v') {
                for (int r = pmethods[i].getRow(); r < pmethods[i].getRow() + ships[i].getSize(); r++) {
                    if (getBoard2(r, pmethods[i].getCol()) != 0) {
                        System.out.println("Invalid Placement of " + ships[i].getName());
                        break;
                    }
                    if (r == pmethods[i].getRow() + ships[i].getSize() - 2 && getBoard2(r, pmethods[i].getCol()) == 0) {
                        for (int row = pmethods[i].getRow(); row < pmethods[i].getRow() + ships[i].getSize(); row++) {
                            setBoard2(row, pmethods[i].getCol(), 1);
                        }
                        System.out.println(ships[i].getName() + " was Successfullly Placed");
                        break;
                    }
                }
            }
        }
    }
    for (int[] rows: board2) {
        for (int cols: rows) {
            System.out.printf("%10d", cols);
        }
        System.out.println();
    }
}
public int getBoard2(int r, int c){
    return board2[r][c];
}
public int[][] getBoard2(){
    return board2;
}
public void setBoard2(int r, int c, int x){
    board2[r][c] = x;
}
public boolean isWin(){
    for(int r = 0; r < board2.length; r++){
        for(int c = 0; c < board2[0].length; c++){
            if(board2[r][c] == 1){
                return true;
            }
        }
    }
    return false;
}
}

public class P1Shoot {
private double hits;
private double misses;
private double total;
private int r;
private int c;
private int[][] board1s = new int[10][10];
P2Placement p = new P2Placement();
public P1Shoot(){
    hits = 0;
    misses = 0;
    total = 0;
    r = 0; 
    c = 0;
}
public void isHit(String rowcol) {
    for(int[] rows: p.board2) {
        for (int cols: rows) {
            System.out.printf("%10d", cols);
        }
        System.out.println();
    }
    switch (rowcol.charAt(0)) {
        case 'A':
            r = 0;
            break;

        case 'B':
            r = 1;
            break;

        case 'C':
            r = 2;
            break;

        case 'D':
            r = 3;
            break;

        case 'E':
            r = 4;
            break;

        case 'F':
            r = 5;
            break;

        case 'G':
            r = 6;
            break;

        case 'H':
            r = 7;
            break;

        case 'I':
            r = 8;
            break;

        case 'J':
            r = 9;
            break;

        default:
            System.out.println("Invalid Row");
            break;
    }
    c = Integer.parseInt(rowcol.substring(1, 2)) - 1;
    if (c < 10) {
        System.out.println();
        if (p.board2[r][c] == 1) {
            p.board2[r][c] = 0;
            board1s[r][c] = 1;
            for (int[] rows : board1s) {
                for (int cols : rows) {
                    System.out.printf("%10d", cols);
                }
                System.out.println();
            }
            hits++;
            total++;
            System.out.println("Hit at " + r + c);
        } else {
            board1s[r][c] = -1;
            for (int[] rows : board1s) {
                for (int cols : rows) {
                    System.out.printf("%10d", cols);
                }
                System.out.println();
            }
            misses++;
            total++;
            System.out.println("Missed at " + r + c);
        }
    }
    else{
        System.out.println("Invalid Column");
    }
}
public double getHits(){
    return hits;
}
public double getMisses() {
    return misses;
}
public double getTotal() {
    return total;
}
}


Solution 1:[1]

Well, from what I can tell, inside of P1Shoot... you instantiate a new P2Placement, but never populate it.

This will create a new P2Placement...

P2Placement p = new P2Placement();

However, I cannot see anywhere in the code where that P2Placement, named, 'p', ever gets any data.

Consider adding a P2Placement to your P1Shoot's constructor so that you will be able to check Player 2's real board.

public P1Shoot(P2Placement otherPlayerBoard){
hits = 0;
misses = 0;
total = 0;
r = 0; 
c = 0;
p = otherPlayerBoard;
}

Then, wherever you call:

new P1Shoot();

you should instead call:

new P1Shoot(player2Board);

being sure to instantiate player2Board beforehand. I am unsure of whether or not I have explained this sufficiently for a student. So I will try to summarize once more using more words.

The reason your P2Placement array seems empty, is that the P2Placement is created new within P1Shoot, but is never populated. So, somewhere in your GameHandler/Driver/Controller class, you have probably populated a P2Placement object's 2D array... we just need to make sure P1Shoot is aware of this.

Another alternative, would be to implement Player2Placement's board2 array as a static variable and then p1 shoot could access it via Player2Placement.board2. I'm not so sure that I would go this route though because it could prove to be a limiting design down the road if you were to ever enhance the application.

Write back with any questions that you may have, I will be happy to elaborate and continuing to offer support!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Java-Enthusiast