I have a ship locations class which i wish to use to store the locations of each players ships, i set this sub in my client class. I then wish to read it back from a different class called game session. Im creating a new ShipLocatiopns(); object however i was under the impression that by making the array static i will be able to read it from other classes regardless
This is the code in my game session which then reads the getsub method from ship locations,when i read this out i will always get {{0,0},{0,0}} even after I have set this in battle ship
Is it actually possible to set a array from one class, then read it back in another.I believe the problem relies in the game session class(the while loop below) as this is the only place where it doesnt seem to get the correct array
this is my shipLocations class
This is the code in my game session which then reads the getsub method from ship locations,when i read this out i will always get {{0,0},{0,0}} even after I have set this in battle ship
Is it actually possible to set a array from one class, then read it back in another.I believe the problem relies in the game session class(the while loop below) as this is the only place where it doesnt seem to get the correct array
while(drawBoard){
System.out.println("While statment reached");
int row = fromPlayer1.readInt();
int column = fromPlayer1.readInt();
System.out.println("the row and col "+row + column);
char shipT=fromPlayer1.readChar();
System.out.println("the char charcter set "+shipT);//null
Thread.sleep(5000);
ShipLocations sl = new ShipLocations();
if(sl.p1sub==null)
{
System.out.println("array is null");
}
System.out.println(Arrays.deepToString(sl.getp1sub()));
}
public class BattleShipsClient implements Runnable, BattleShipConstants
{
public BattleShipsClient()
{
//panels for grid
JPanel p = new JPanel();
JPanel jp = new JPanel();
p.setLayout(new GridLayout(6,6,0,0));
for (int i=0;i<6;i++)
for(int j=0;j<6;j++)
p.add(grid[i][j] = new Grid(i,j,this));
jp.setLayout(new GridLayout(6,6,0,0));
for (int i=0;i<6;i++)
for(int j=0;j<6;j++)
jp.add(pgrid[i][j] = new BGrid(i,j,this));
/*
* a new grid is created, so from here once the ship is drawn in one
* must create a x in another grid
*/
p.setPreferredSize(new Dimension (320,320));
jp.setPreferredSize(new Dimension (320,320));
p.setBorder(new LineBorder(Color.black,1));
jlbTitle.setHorizontalAlignment(JLabel.CENTER);
jlbTitle.setFont(new Font("SansSerif", Font.BOLD, 16));
jlbTitle.setBorder(new LineBorder(Color.black, 1));
jlbStatus.setBorder(new LineBorder(Color.black, 1));
j.add(jlbTitle,BorderLayout.NORTH);
j.add(p,BorderLayout.CENTER);
j.add(jlbStatus,BorderLayout.SOUTH);
jtb.add(new JLabel("choose your attack position"),BorderLayout.NORTH);
jtb.add(jp,BorderLayout.CENTER);
jtb.add(new JLabel(jlbStatus.getText()),BorderLayout.SOUTH);
tabbedPane.addTab("chooser", j);
tabbedPane.addTab("attacker", jtb);
jf.add(tabbedPane);
jf.setSize(800,480);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//jf.pack();
jf.setVisible(true);
connectToServer();
}
private void connectToServer() {
try{
Socket sock;
sock = new Socket(host,44406);
fromServer = new DataInputStream(sock.getInputStream());
toServer = new DataOutputStream(sock.getOutputStream());
}
catch (Exception ex)
{
System.err.println(ex);
}
Thread thread = new Thread(this);
thread.start();
}
public void run(){
try {
int player = fromServer.readInt();
if (player == PLAYER1)
{
setMyToken('x');//my token is set
setLocation(true);
setStatusMessage("Waiting for player 2 to join");
placeBoard(player);
setTitleMessage("Player 1 has set battleships");
fromServer.readInt();//reads a integer
setMyTurn(true);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void placeBoard(int player) throws IOException// this is only called once and not updated when clicked again
{
while(getLocations())
{
while(isWaiting())//true
{
toServer.writeInt(player);
int row = getRowSelected();
int col = getColumnSelected();
int choice=Integer.parseInt(JOptionPane.showInputDialog("Please 1 for a sub, 2 for a battleship and 3 for a destroyer"));
clickCount ++;
if(clickCount >2)
{
setLocation(false);
continueToPlay=true;
}
if (choice ==1 &&sub.placed==false)
{
String orientation =JOptionPane.showInputDialog("please enter your orientations");
if(orientation.equalsIgnoreCase("h"))
{
//row os the same
//collum number will be sent, then plus one on the other side
sub.local = new int[2][2];
sub.local[0][0] =row;
sub.local[0][1]=col;
sub.local[1][0]=row;
sub.local[1][1] =col+1;
toServer.writeInt(row);
toServer.writeInt(col);
toServer.writeChar('s');//possible just write each char for each situation
sub.placed=true;
sl.setp1sub(new int[][]{{row,col},
{row,col+1}});
System.out.println("getsub "+Arrays.deepToString(sl.getp1sub()));
grid[row][col+1].setToken(getMyToken());
}
}
}
}
this is my shipLocations class
public class ShipLocations {
static int [][] p1sub;
public ShipLocations()
{
p1sub = new int[2][2];
}
public int[][] getp1sub()
{
return p1sub;
}
public void setp1sub(int[][] local) {
for (int i = 0;i <local.length;i++)
{
for(int j = 0;j<local.length;j++)
{
p1sub [i][j]= local[i][j];
System.out.println("set method"+p1sub[i][j]);
}
}
}
}