I built a virtual aquarium. The problem on the one side is, that my moveFishes() method is really complex structured to the point, where I personally don't know how to structure it into different methods, while justifying, the program is still working. I'm asking if someone is able to structure my class and organize it into more simple methods.
On the other hand the collisions of fish are not properly working, and I cant figure out how to?
```
import java.util.Random;
public class Aquarium {
public int depth;
public int width;
public Fish[][] fishes;
public Aquarium(int depth, int width) {
this.depth = depth;
this.width = width;
this.fishes = new Fish[depth][width];
}
public void printAquarium() {
System.out.println();
for (int i = 0; i < this.depth; i++) {
System.out.print("|");
for (int j = 0; j < this.width; j++) {
if (fishes[i][j] != null) {
System.out.print(fishes[i][j].getAppearance());
j += fishes[i][j].getAppearance().length() - 1;
} else {
System.out.print(" ");
}
}
System.out.println("|");
}
System.out.print("+");
for (int i = 0; i < this.width; i++) {
System.out.print("-");
}
System.out.println("+");
System.out.println();
}
public void setFish(int row, int column, Fish fish) {
if (row >= 0 && row < depth && column >= 0 && column < width) {
fishes[row][column] = fish;
}
}
public void moveFishes() {
for (int i = 0; i < this.depth; i++) {
for (int j = 0; j < this.width - 1; j++) {
if (fishes[i][j] != null && fishes[i][j + 1] != null) {
Fish currentFish = fishes[i][j];
Fish nextFish = fishes[i][j + 1];
if (currentFish.getDirection() && !nextFish.getDirection()) {
if (j + currentFish.getAppearance().length() > j + 1) {
handleCollision(i, j);
}
} else if (!currentFish.getDirection() && nextFish.getDirection()) {
if (j + 1 + nextFish.getAppearance().length() > j) {
handleCollision(i, j);
}
}
}
}
}
for (int i = 0; i < this.depth; i++) {
for (int j = 0; j < this.width; j++) {
if (fishes[i][j] != null) {
Fish currentFish = fishes[i][j];
boolean direction = currentFish.getDirection();
if (!direction) {
if (j + 1 < this.width) {
if (fishes[i][j + 1] == null) {
fishes[i][j + 1] = currentFish;
fishes[i][j] = null;
j++;
if (j == this.width - currentFish.getAppearance().length()) {
currentFish.turnFish();
currentFish.setDirection(!direction);
}
}
}
} else {
if (j - 1 >= 0) {
if (fishes[i][j - 1] == null) {
fishes[i][j - 1] = currentFish;
fishes[i][j] = null;
j--;
if (j == 0) {
currentFish.turnFish();
currentFish.setDirection(!direction);
}
}
changeDepth(currentFish, i, j);
}
}
}
}
}
}
private void handleCollision(int i, int j) {
Fish currentFish = fishes[i][j];
Fish nextFish = fishes[i][j + 1];
int currentFishLength = currentFish.getAppearance().length();
int nextFishLength = nextFish.getAppearance().length();
if (j + currentFishLength > j + 1 && j + 1 + nextFishLength > j) {
if (currentFish.getDirection()) {
fishes[i][j + 1] = currentFish;
fishes[i][j] = nextFish;
} else {
fishes[i][j] = nextFish;
fishes[i][j + 1] = currentFish;
}
}
}
private void changeDepth(Fish currentFish, int i, int j) {
Random random = new Random();
if (currentFish instanceof Fish.Shark && random.nextInt(4) == 0) {
int newDepth = i + (random.nextBoolean() ? 1 : -1);
if (newDepth >= 0 && newDepth < this.depth) {
fishes[i][j] = null;
fishes[newDepth][j] = currentFish;
}
} else if (currentFish instanceof Fish.PufferFish && random.nextInt(10) == 0) {
int newDepth = i + (random.nextBoolean() ? 1 : -1);
if (newDepth >= 0 && newDepth < this.depth) {
fishes[i][j] = null;
fishes[newDepth][j] = currentFish;
}
} else if (currentFish instanceof Fish.Swordfish && random.nextInt(5) == 0) {
int newDepth = i + (random.nextBoolean() ? 1 : -1);
if (newDepth >= 0 && newDepth < this.depth) {
fishes[i][j] = null;
fishes[newDepth][j] = currentFish;
}
}
}
public static void main(String[] args) {
Aquarium a = new Aquarium(8, 30);
Fish one = new Fish.Swordfish();
Fish two = new Fish.Shark();
Fish three = new Fish.PufferFish();
Fish four = new Fish.NormalFish();
a.setFish(2, 8, one);
a.setFish(3, 3, two);
a.setFish(5, 20, three);
a.setFish(5, 8, four);
for (int i = 0; i < 100; i++) {
a.moveFishes();
a.printAquarium();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
```
public class Fish {
public String appearance;
public boolean direction;
public Fish(String appearance, boolean direction) {
this.appearance = appearance;
this.direction = direction;
}
public String getAppearance() {
return appearance;
}
public void setAppearance(String appearance) {
this.appearance = appearance;
}
public void turnFish() {
String newAppearance = "";
for (int i = this.appearance.length() - 1; i >= 0; i--) {
if (i >= 0 && i < this.appearance.length()) {
char currentChar = this.appearance.charAt(i);
if (currentChar == '>') {
newAppearance += '<';
} else if (currentChar == '<') {
newAppearance += '>';
} else if (currentChar == ')') {
newAppearance += '(';
} else if (currentChar == '(') {
newAppearance += ')';
} else {
newAppearance += currentChar;
}
}
}
this.appearance = newAppearance;
}
public boolean getDirection() {
return direction;
}
public void setDirection(boolean direction) {
this.direction = direction;
}
public static class NormalFish extends Fish {
public NormalFish() {
super("<><", true);
}
}
public static class Shark extends Fish {
public Shark() {
super("<///====><", true);
}
}
public static class PufferFish extends Fish {
public PufferFish() {
super("<()><", true);
}
}
public static class Swordfish extends Fish {
public Swordfish() {
super("-<><", true);
}
}
}
```
<pre lang="Java"><pre lang="Java">
What I have tried:
I tried making a moveLeft and right method but they didn't work. Also I wanted to make a handleCollision method, but that didn't work either