Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Would love some help on fixing this Java code. So my goal for this code was to create a 2D array fish tank that would

Would love some help on fixing this Java code.

So my goal for this code was to create a 2D array fish tank that would have 4 fish which would look like: ><))'>, 1 fish hook that would look like: J, and 6 pieces of fish food that would look like: *. I have my code all correct except for the moveAllObjects method at the bottom. The purpose of this method is to move the certain objects within the array. For the fish, it is supposed to move over 1 to the right, the fish hook is supposed to move up one unit, and the fish food is supposed to move over to the left one and down one. I don't need to change anything else in this code except for this method and possibly main. I just want to make sure that each time this code is run, that the tank properly moves all the objects. Thanks in advance for the help! I also wanted to post a scene of what one of the runs would look like:

~~~~~~~~~~~~~~~~~~~~~~~~*~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~J

~~~><(('>~~~~~~~~~~~~~~~*~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~><(('>~*~~~~~~~~~*~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~><(('>~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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) {

placeObjectInTank("><))'>", tilde, fish[i][0], fish[i][1]);

moveAllObjects(fish, 1, 0, 8, 32);

}

int fishHook [][] = generateRandomPositions(1, 8, 32);

for (int i = 0; i < fishHook.length; ++i) {

placeObjectInTank("J", tilde, fishHook[i][0], fishHook[i][1]);

moveAllObjects(fishHook, 0, 1, 8, 32);

}

int fishFood [][] = generateRandomPositions(6, 8, 32);

for (int i = 0; i < fishFood.length; ++i) {

placeObjectInTank("*", tilde, fishFood[i][0], fishFood[i][1]);

moveAllObjects(fishFood, -1, -1, 8, 32);

}

renderTank(tilde);

}

public static void fillTank(char[][] tank, char water) //fill the tank up

{

for (int row = 0; row < tank.length; ++row) {

for (int column = 0; column < tank[row].length; ++column) {

tank [row][column] = water;

}

System.out.println();

}

}

public static void placeObjectInTank(String object, char [][] positionInTank, int column, int row)

{

object.charAt(object.length() - 1);

int k = row;

for (int i = object.length() - 1; i >= 0; --i) {

positionInTank[column][k] = object.charAt(i);

k--;

if (k < 0) {

k = positionInTank[0].length-1; //looks at first row and finds first row

}

}

}

public static void renderTank(char[][] tank) //render our 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) //give us our positions

{

int [][] randomPositions = new int [number][2]; //number of fish with coordinates

for (int i = 0; i < number; ++i) {

randomPositions [i][0] = Utility.randomInt(width);

randomPositions [i][1] = Utility.randomInt(height);

}

return randomPositions;

}

public static void moveAllObjects(int[][] positions, int dx, int dy, int width, int height)

{

if (dx == 0 && dy == 1) {

for (int i = 0; i < positions.length; ++i) {

positions[i][0] = positions[i][0] - dy;

}

}

if (dx == 1 && dy == 1) {

for (int i = 0; i < positions.length; ++i) {

positions[i][1] = positions[i][1] - dx;

positions[i][0] = positions[i][0] + dy;

}

}

if (dx == 1 && dy == 0) {

for (int i = 0; i < positions.length; ++i) {

positions[i][1] = (positions[i][1] + dx) % width;

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

Recognize the four core purposes service environments fulfill.

Answered: 1 week ago