How to replace and add char value to the left of a position in an array... So I
ID: 3883679 • Letter: H
Question
How to replace and add char value to the left of a position in an array...
So I am currently trying to create a project that will create a 32 by 8 tank array that will generate a tank with 4 fish in random positions, which will look like this ><(('>. I have created this program up until generating the random positions of the fish head, > but need to find how I can replace values to the left of the position with the rest of the fish's body ><((' . One of the rows should look like this ~~~~~~~~><(('>~~~~~~. And if the fish is within 5 positions to the left of the tank, then the full body won't be seen (('>~~~~~~~~~~~. The method I need assistance with is placeFishInTank. I don't need a direct solution but would just like to understand how I can go about this since what I am doing hasn't been working.
import java.util.Random;
public class Main {
public static void main(String[] args) {
char [][] tilde = new char [8][32];
fillTank(tilde, '~');
int fish [][] = generateRandomPositions(4, 8, 32);
for (int i = 0; i < fish.length; ++i) {
placeFishInTank(tilde, fish[i][0], fish[i][1]);
}
renderTank(tilde);
}
/**
* Copies the water character into every position in the tank array. The two-dimensional tank
* array can have dimensions of any size(s).
*
* @param tank will contain all water characters after this method is called.
* @param water is the character copied into the tank.
*/
public static void fillTank(char[][] tank, char water)
{
for (int row = 0; row < tank.length; ++row) {
for (int column = 0; column < tank[row].length; ++column) {
tank [row][column] = '~';
}
System.out.println();
}
}
public static void placeFishInTank(char [][] positionInTank, int x, int y)
{
positionInTank[x][y] = '>';
for (int i = 0; i < positionInTank.length; i++) {
String.replaceChar(i - 1) = '''; //HERE IS WHERE I AM GETTING STUCK, TRYING TO PUT THE REST OF THE BODY IN
}
}
/**
* Prints the contents of the tank into the console in row major order, so that the
* smallest row indexes are on top and the smallest column indexes are on the left. For
* example:
* tank[0][0] tank[0][1] tank[0][2] ...
* tank[1][0] tank[1][1] tank[1][2] ...
* ...
* Each row is on its own line, and this method should work for two-dimensional tanks with
* dimensions of any size.
*
* @param tank contains the characters that will be printed to the console.
*/
public static void renderTank(char[][] tank)
{
for (int row = 0; row < tank.length; ++row) {
for (int column = 0; column < tank[row].length; ++column) {
System.out.print(tank[row][column]);
}
System.out.println();
}
}
public static int[][] generateRandomPositions(int number, int width, int height) //width and height different
{
int [][] fish = new int [number][2];
for (int i = 0; i < number; ++i) {
fish [i][0] = Utility.randomInt(width);
fish [i][1] = Utility.randomInt(height);
}
return fish;
}
}
}
Explanation / Answer
public static void placeFishInTanker(char[][] positionInTank, int x, int y) {
String f = "><))'>";
char[] fish = f.toCharArray();
int start = x;
int k = fish.length - 1;
while (start > -1) {
positionInTank[start][y] = fish[k];
k--;
start--;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.