How to replace elements in a 2D array? Okay so for my program, I am trying to cr
ID: 3883765 • Letter: H
Question
How to replace elements in a 2D array?
Okay so for my program, I am trying to create a fish tank. My program is to generate 4 different FISH: ><))'> in a tank of tilde (~) characters. The tank is a 2D array of 8 rows and 32 columns so it would look like ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ for one of the eight rows (before I generate the random positions of the fish in the rank). Then one row could look like ~~~~~~~~~~><))'>~~~~~~~~~~~~~~~~ after I generate random positions for my fish. It should also hide characters of the fishes body if it is more to the left like such )'>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ where there is only three parts of the fish, 2 body characters and the head character.The random position coordinate I have generated for each of the four fishes starts on the head of the fish (>) which means I have been able to successfully generate four > in the tank, which looks like ~~~~~~~~~~~~~~~~>~~~~~~~~~~~~~~~ when ran. But my problem is how I can get the rest of the fishes body after the head. I know I am going to be working backwards from the > character and replace the tilde characters with characters from the fishes body. I don't need the direct answer, rather some steps that would explain it and some suggestions. It would be a big help. Here is some of the code below.
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]); // METHOD I AM ON...
}
renderTank(tilde);
}
public static void placeFishInTank(char [][] positionInTank, int x, int y) //METHOD TO PLACE THE HEAD AND BODY
{
positionInTank[x][y] = '>'; //PLACING THE HEAD
// for (int i = 0; i < positionInTank.length; i++) {
// for (int j = 0; i < positionInTank[x].length; j++)
// if (positionInTank[][] >= 0) {
// String.charAt(i, j);
// }
// }
//OR I WAS THINKING OF DOING SOMETHING LIKE
//if (!positionInTank[row].length - 1 = 0) {
//add on next character '''
// }
//if (!positionInTank[row] - 2 = 0) {
//add on next character ')'
// }
//if (!positionInTank[row] - 3 = 0) {
//add on next character ')'
// }
//if (!positionInTank[row] - 4 = 0) {
//add on next character '<'
// }
//if (!positionInTank[row] - 5 = 0) {
//add on next character '>'
// }
}
Explanation / Answer
set fish[] = {> ,< ,), ), ', >} // place fish characters in an array
set head = 5 // head as the last index
loop if x > 0 or head > 0 // loop till reached end of the maze i.e. x > 0 or complete fish is drawn i.e. head > 0
start
place fish[head] at position[x, y]
head = head - 1;
x = x - 1;
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.