'How to change element in array?
Array is 2 dimensional 10x10 gameBoard
Code I made for array:
int gameBoardLength = gameBoard.length;
System.out.print(" "); //two spaces to align the text
for (int i = 0; i < gameBoardLength; i++){ //gameboard.lenght = 10
System.out.print(i + 1 + " ");
}
Code for changing 1 or more random positions in array:
private static char[][] placeShips(char[][] gameBoard, char water,char destroyer1, char ship, char destroyer2 ) {
int x = 0;
int y = 1;
int gameBoardLenght = gameBoard.length;
int placedDestroyers1 = 0;
while (placedDestroyers1 < 4){
int[] location = generateShipLocation(gameBoardLenght);
char possibilityOfPlacement = gameBoard[location[x]][location[y]];
if (possibilityOfPlacement == water){ // water is char '~' and at the begining array is filled up with that.
gameBoard[location[x]][location[y]] = destroyer1; //destroyer1 is a char (char destroyer1 = 'D'; )
}placedDestroyers1++;
} return gameBoard;
}
What i need is to change 2/3/4 char in a row.
I'v already tried to change destroyer1 into Char [] of few elements, but that didn't made the job.
Edit:
I edited code like that:
private static char[][] placeShips(char[][] gameBoard, char water,char destroyer1, char ship, char destroyer2 ) {
int x = 0;
int y = 1;
int gameBoardLenght = gameBoard.length;
int placedDestroyers1 = 0;
int[] location = generateShipLocation(gameBoardLenght); // that part is taking 1 random point from net method
while (placedDestroyers1 < 4){
char possibilityOfPlacement = gameBoard[location[x]][location[y]];
if (possibilityOfPlacement == water){
gameBoard[location[x]][location[y++]] = destroyer1; //That part should placing first random point and next ones next to that one.
}placedDestroyers1++;
} return gameBoard;
}
//Part below is making random point in location
private static int[] generateShipLocation(int gameBoardLenght) {
int [] location = new int [5];
for (int i = 0; i < location.length; i++){
location[i] = new Random().nextInt(gameBoardLenght);
} return location;
}
And it is kinda working. Output is lining in line, but it is not in 1 line it's in parts.
The way i want them to be:
X X X O
O O O O
O O O O
But they going like that:
X O X O
O O O O
O O O O
Or that:
X O O X
O O O O
O O O O
SEMI SOLUTION:
private static char[][] placingDestroyer1(char[][] gameBoard, char water,char destroyer1, char destroyer2) {
int gameBoardLenght = gameBoard.length;
int[] location = generateDestroyer1Location(gameBoardLenght);
int placedDestroyers1 = 0;
while (placedDestroyers1 < 4){
char possibilityOfPlacement = gameBoard[location[0]][location[1]];
if (possibilityOfPlacement == water && possibilityOfPlacement != destroyer1 && possibilityOfPlacement != destroyer2){
gameBoard[location[0]][location[1]] = destroyer1;
}
placedDestroyers1++;
location[1]++; //thanks to Kemper Lee, I was able to track right solution. However that solution sometimes is out of lenght of array, so it is exiting with erro
} return gameBoard;
}
Solution 1:[1]
If I'm understanding your question, the best place to start would be to create some grids for visualization:
10cols EX 1 EX 2
~~~~~~~~~~ X~~~~~~~~~ ~~~~~~~~~~
~~~~~~~~~~ X~~~~~~~~~ ~~~~~~~~~~
~~~~~~~~~~ ~~~~~~~~~~ ~XXXX~~~~~
~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~
/*10*/ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~X~~~
rows ~~~~~~~~~~ ~~~~~XXXX~ ~~~~~~X~~~
~~~~~~~~~~ ~~~X~~~~~~ ~~~~~~~~~~
~~~~~~~~~~ ~~~X~~~~~~ ~~~~~~~~~~
~~~~~~~~~~ ~~~X~~~~~~ ~XXX~~~~~~
~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~
In Example 1 when index[0][0] is filled then either index[1][0] or index[0][1] has to filled in.
Likewise, in Example 2 when index[2][2]
is filled then index[2][1]
, index[1][2]
, index[3][2]
, or index[2][3]
need to be filled
NOTE -- this is NOT a complete solution
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 |