I'm creating a game board, using an array of 2d array. During the game the players hit or miss and this has to be printed on the board. I would like to know how I can update the array without losing and that was printed previously.
My first array:
public void drawBoard() {
int[] m = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
int[] n = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
int position = 1;
System.out.println("\n" + "This is your board : " + "\n" + " '**' means hited and '~~' means missed" + "\n");
for(int i = 0; i < height+1; i++){
for(int j = 0; j < width+1; j++) {
board[i][0] = String.valueOf(m[i]);
board[0][j] = String.valueOf(n[j]);
board[i+1][j+1] = String.valueOf(position);//"&";
position++;
System.out.print(board[i][j] + "\t");
this.ship[i][j] = board[i][j];
}
System.out.println();
}
}
Player interactions:
public void guess() {
System.out.println("\n" + "Please, enter a row number: ");
Scanner input = new Scanner(System.in);
row = input.nextInt();
System.out.println("\n" + "Please, enter a col number: ");
Scanner input2 = new Scanner(System.in);
col = input2.nextInt();
boolean win = false;
String [] z = {"**","~~"};
int linha = 0;
int coluna = 0;
if (ship[row][col].equals("A")) {
hit++;
linha = row;
coluna = col;
ship[row][col] = "**";
//board[row][col] = "**";
pickedShip[linha*coluna] = ship[row][col];
guess[lin][colu] = pickedShip[linha*coluna];
System.out.println("Hey! You hit it!");
//HitBoard();
NewBoard();
}
else if (ship[row][col].equals("B")) {
hit++;
ship[row][col] = "**";
//board[row][col] = "**";
pickedShip[row*col] = ship[row][col];
System.out.println("Hey! You hit it!");
//HitBoard();
NewBoard();
}
else if (ship[row][col].equals("C")) {
hit++;
ship[row][col] = "**";
//board[row][col] = "**";
pickedShip[row*col] = ship[row][col];
//pickedShip[(row+2)*(col+2)] = ship[row][col];
System.out.println("Hey! You hit it!");
//HitBoard();
NewBoard();
}
else if (ship[row][col].equals("D")) {
hit++;
ship[row][col] = "**";
//board[row][col] = "**";
pickedShip[row*col] = ship[row][col];
System.out.println("Hey! You hit it!");
//HitBoard();
NewBoard();
}
else if (ship[row][col].equals("E")) {
hit++;
ship[row][col] = "**";
//board[row][col] = "**";
pickedShip[row*col] = ship[row][col];
System.out.println("Hey! You hit it!");
//HitBoard();
NewBoard();
}
else if (ship[row][col].equals("F")) {
hit++;
ship[row][col] = "**";
//board[row][col] = "**";
pickedShip[row*col] = ship[row][col];
System.out.println("Hey! You hit it!");
//HitBoard();
NewBoard();
}
else if(ship[row][col].equals("**") || ship[row][col].equals("~~")){
System.out.println("Sorry! It was already picked!");
}
else {
//miss++;
ship[row][col] = "~~";
//board[row][col] = "~~";
pickedShip[row*col] = ship[row][col];
System.out.println("\n" + "Sorry, you miss it!" + "\n");
//missedBoard();
NewBoard();
}
}
And finally my new matrix:
public void NewBoard() {
int[] m = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
int[] n = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
for(int i = 0; i < height+1; i++){
for(int j = 0; j < width+1; j++) {
board[i][0] = String.valueOf(m[i]);
board[0][j] = String.valueOf(n[j]);
board[i+1][j+1] = "&";
board[lin][colu] = guess[lin][colu];
System.out.print(board[i][j] + "\t");
}
System.out.println();
}
}