This is a Fish Tank, where certain variants of fish can "swim" in. They move horizontal and vertikal (the vertikal movement is random). But one aspect is not covered yet. When two fish collide one fish just dissapears and never gets shown again. I would like to cover this case in the moveFish method or in a sepperat method, where the larger fish covers up the smaller one, until they have different positions again.
````
public class FishTank {
public int tankHeight;
public int tankWidth;
public Fish[][] fish;
public FishTank(int tankHeight,int tankWidth){
this.tankHeight=tankHeight;
this.tankWidth=tankWidth;
this.fish= new Fish[this.tankHeight][this.tankWidth];
}
public void setFish(Fish fish) {
this.fish[fish.getSwimHeight()][fish.getSwimWidth()] = fish;
}
public void moveFish() {
Fish[][] updatedFish = new Fish[this.tankHeight][this.tankWidth];
for (int i = 0; i < this.tankHeight; i++) {
for (int j = 0; j < this.tankWidth; j++) {
if (fish[i][j] != null) {
fish[i][j].swim(this);;
int newHeight = fish[i][j].getSwimHeight();
int newWidth = fish[i][j].getSwimWidth();
updatedFish[newHeight][newWidth] = fish[i][j];
}
}
}
fish = updatedFish;
}
private void handlePotentialCollision() {
}
public void printAquarium() {
System.out.println();
for (int i = 0; i < this.tankHeight; i++) {
System.out.print("|");
for (int j = 0; j < this.tankWidth; j++) {
if (fish[i][j] != null ) {
System.out.print(fish[i][j].getLook());
j += fish[i][j].getLook().length() -1;
} else {
System.out.print(" ");
}
}
System.out.println("|");
}
System.out.print("+");
for (int i = 0; i < this.tankWidth; i++) {
System.out.print("-");
}
System.out.println("+");
System.out.println();
}
public int getTankHeight() {
return tankHeight;
}
public int getTankWidth() {
return tankWidth;
}
public static void main(String[] args) {
Fish.NormalFish fish = new Fish.NormalFish(1, 10, false);
Fish.Pufferfish fish2= new Fish.Pufferfish(1, 1, false);
FishTank ft= new FishTank(4,20);
ft.setFish(fish);
ft.setFish(fish2);
ft.printAquarium();
for (int i = 0; i < 100; i++) {
ft.moveFish();
ft.printAquarium();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
```
import java.util.Random;
public class Fish {
public String look;
public int SwimHeight;
public int SwimWidth;
public boolean direction;
public Fish(int SwimHeight,int SwimWidth, boolean direction) {
this.SwimHeight=SwimHeight;
this.SwimWidth=SwimWidth;
this.direction=direction;
}
public String toString() {
return this.look;
}
public void swim(FishTank fishTank) {
Random rand = new Random();
if (direction==true) {
if (SwimWidth + this.look.length() < fishTank.tankWidth) {
SwimWidth += 1;
} else {
direction = !direction;
turnFish();
}
} else {
if (SwimWidth - 1 >= 0) {
SwimWidth -= 1;
} else {
direction = !direction;
turnFish();
}
}
if (this instanceof Shark && rand.nextInt(4) == 0) {
if (SwimHeight - 1 >= 0 && SwimHeight + 1 < fishTank.tankHeight) {
SwimHeight += rand.nextBoolean() ? 1 : -1;
} else if(SwimHeight - 1 < 0){
SwimHeight += rand.nextBoolean() ? 1 : 0;
}
else {
SwimHeight += rand.nextBoolean() ? -1 : 0;
}
} else if (this instanceof Pufferfish && rand.nextInt(10) == 0) {
if (SwimHeight + 1 < fishTank.tankHeight && SwimHeight - 1 >= 0) {
SwimHeight += rand.nextBoolean() ? 1 : -1;
} else if(SwimHeight - 1 < 0){
SwimHeight += rand.nextBoolean() ? 1 : 0;
}
else {
SwimHeight += rand.nextBoolean() ? -1 : 0;
}
} else if (this instanceof Swordfish && rand.nextInt(5) == 0) {
if (SwimHeight + 1 < fishTank.tankHeight && SwimHeight - 1 >= 0) {
SwimHeight += rand.nextBoolean() ? 1 : -1;
} else if(SwimHeight - 1 < 0){
SwimHeight += rand.nextBoolean() ? 1 : 0;
}
else {
SwimHeight += rand.nextBoolean() ? -1 : 0;
}
}
}
public void turnFish() {
String newLook = "";
for (int i = this.look.length() - 1; i >= 0; i--) {
if (i >= 0 && i < this.look.length()) {
char currentChar = this.look.charAt(i);
if (currentChar == '>') {
newLook += '<';
} else if (currentChar == '<') {
newLook += '>';
} else if (currentChar == ')') {
newLook += '(';
} else if (currentChar == '(') {
newLook += ')';
} else {
newLook += currentChar;
}
}
}
this.look = newLook;
}
public String getLook() {
return look;
}
public int getSwimHeight() {
return SwimHeight;
}
public void setSwimHeight(int swimHeight) {
SwimHeight = swimHeight;
}
public int getSwimWidth() {
return SwimWidth;
}
public void setSwimWidth(int swimWidth) {
SwimWidth = swimWidth;
}
public boolean getDirection() {
return direction;
}
public void changeDirection() {
this.direction=!direction;
}
public static class NormalFish extends Fish{
public NormalFish(int SwimHeight, int SwimWidth, boolean direction) {
super(SwimHeight, SwimWidth, direction);
this.look="<><";
}
}
public static class Shark extends Fish {
public Shark(int SwimHeight, int SwimWidth, boolean direction) {
super(SwimHeight, SwimWidth, direction);
this.look="<///====><";
}
}
public static class Pufferfish extends Fish {
public Pufferfish(int SwimHeight, int SwimWidth, boolean direction) {
super(SwimHeight, SwimWidth, direction);
this.look="<()><";
}
}
public static class Swordfish extends Fish {
public Swordfish(int SwimHeight, int SwimWidth, boolean direction) {
super(SwimHeight, SwimWidth, direction);
this.look="-<><";
}
}
}
```
What I have tried:
I tried making a method, where a other fish gets searched and then the two fish get compared, but the next step is unclear for me