Dies ist ein Aquarium, in dem bestimmte Fischarten „schwimmen“ können. Sie bewegen sich horizontal und vertikal (die vertikale Bewegung ist zufällig). Ein Aspekt ist jedoch noch nicht abgedeckt. Nämlich der, wenn zwei Fische kollidieren. Ich möchte diesen Fall in der Methode moveFish oder in einer separaten Methode abdecken, Die Implementierung soll folgendermaßen aussehen, der aktuelle Fisch soll vorübergehend verschwinden solange bis die beiden fische wieder unterschiedliche Positionen haben.
Java:
import java.util.Random;
public class Aquarium {
public int tankHeight;
public int tankWidth;
public Fisch[][] fische;
public Aquarium(int tankHeight, int tankWidth) {
this.tankHeight = tankHeight;
this.tankWidth = tankWidth;
this.fische = new Fisch[this.tankHeight][this.tankWidth];
}
public int getTankHeight() {
return this.tankHeight;
}
public int getTankWidth() {
return this.tankWidth;
}
public void setFisch(Fisch fisch) {
this.fische[fisch.getSwimHeight()][fisch.getSwimWidth()] = fisch;
}
public void printFishTank() {
System.out.println();
for (int i = 0; i < this.tankHeight; i++) {
System.out.print("|");
for (int j = 0; j < this.tankWidth; j++) {
if (fische[i][j] != null) {
System.out.print(fische[i][j].getAussehen());
j += fische[i][j].getAussehen().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 void moveFische() {
randomVertikal();
Fisch[][] updatedFish = new Fisch[this.tankHeight][this.tankWidth];
for (int i = 0; i < this.tankHeight; i++) {
for (int j = 0; j < this.tankWidth; j++) {
if (this.fische[i][j] != null) {
if (this.fische[i][j].getDirection() == true)
{
if (this.fische[i][j].getSwimWidth() + this.fische[i][j].getAussehen().length() < this.tankWidth)
{
this.fische[i][j].setSwimWidth(j+1);
}
else
{
this.fische[i][j].setDirection(false);
this.fische[i][j].turnFish(); // Drehe den Fisch
}
}
else
{
if (this.fische[i][j].getSwimWidth() - 1 >= 0)
{
this.fische[i][j].setSwimWidth(j-1);
}
else
{
this.fische[i][j].setDirection(true);
this.fische[i][j].turnFish(); // Drehe den Fisch
}
}
int newHeight = fische[i][j].getSwimHeight();
int newWidth = fische[i][j].getSwimWidth();
if (this.fische[newHeight][newWidth] != null) {
handleCollision(this.fische[newHeight][newWidth]);
}
updatedFish[newHeight][newWidth] = fische[i][j];
}
}
}
fische = updatedFish;
}
public void handleCollision(Fisch fisch) {
}
public void randomVertikal() {
Random rand = new Random();
Fisch[][] updatedFish = new Fisch[this.tankHeight][this.tankWidth];
for (int i = 0; i < this.tankHeight; i++) {
for (int j = 0; j < this.tankWidth; j++) {
if (this.fische[i][j] != null) {
Fisch currentFish = this.fische[i][j];
int newSwimHeight = currentFish.getSwimHeight();
if (currentFish instanceof Fisch.Shark && rand.nextInt(4) == 0) {
if (newSwimHeight > 0 && newSwimHeight < this.tankHeight - 1) {
newSwimHeight += rand.nextBoolean() ? 1 : -1;
} else if (newSwimHeight <= 0) {
newSwimHeight += rand.nextBoolean() ? 1 : 0;
} else if (newSwimHeight >= this.tankHeight - 1) {
newSwimHeight += rand.nextBoolean() ? 0 : -1;
}
}
if (currentFish instanceof Fisch.SwordFish && rand.nextInt(5) == 0) {
if (newSwimHeight > 0 && newSwimHeight < this.tankHeight - 1) {
newSwimHeight += rand.nextBoolean() ? 1 : -1;
} else if (newSwimHeight <= 0) {
newSwimHeight += rand.nextBoolean() ? 1 : 0;
} else if (newSwimHeight >= this.tankHeight - 1) {
newSwimHeight += rand.nextBoolean() ? 0 : -1;
}
}
if (currentFish instanceof Fisch.PufferFish && rand.nextInt(10) == 0) {
if (newSwimHeight > 0 && newSwimHeight < this.tankHeight - 1) {
newSwimHeight += rand.nextBoolean() ? 1 : -1;
} else if (newSwimHeight <= 0) {
newSwimHeight += rand.nextBoolean() ? 1 : 0;
} else if (newSwimHeight >= this.tankHeight - 1) {
newSwimHeight += rand.nextBoolean() ? 0 : -1;
}
}
currentFish.setSwimHeight(newSwimHeight);
int newHeight = currentFish.getSwimHeight();
int newWidth = currentFish.getSwimWidth();
updatedFish[newHeight][newWidth] = currentFish;
}
}
}
fische = updatedFish;
}
public static void main(String[] args) throws InterruptedException {
Fisch.SwordFish fish1 = new Fisch.SwordFish(1, 3, false);
Fisch.NormalFish fish2 = new Fisch.NormalFish(1, 9, false);
Fisch.PufferFish fish3 = new Fisch.PufferFish(3, 9, false);
Fisch.Shark fish4 = new Fisch.Shark(8, 9, false);
Fisch.NormalFish fish5 = new Fisch.NormalFish(5, 9, false);
Aquarium a = new Aquarium(9, 22);
a.setFisch(fish1);
a.setFisch(fish2);
a.setFisch(fish3);
a.setFisch(fish4);
a.setFisch(fish5);
for (int i = 0; i < 100; i++) {
a.printFishTank();
a.moveFische();
Thread.sleep(800);
}
}
}
public class Fisch {
public String aussehen;
public int swimHeight;
public int swimWidth;
public boolean direction;
public Fisch(int swimHeight, int swimWidth, boolean direction) {
this.swimHeight = swimHeight;
this.swimWidth = swimWidth;
this.direction = direction;
}
public String getAussehen() {
return this.aussehen;
}
public int getSwimHeight() {
return this.swimHeight;
}
public int getSwimWidth() {
return this.swimWidth;
}
public boolean getDirection() {
return this.direction;
}
public boolean setDirection(boolean direction) {
return this.direction = direction;
}
public int setSwimHeight(int swimHeight) {
return this.swimHeight = swimHeight;
}
public int setSwimWidth(int swimWidth) {
return this.swimWidth = swimWidth;
}
public void turnFish() {
String newaussehen = "";
for (int i = this.aussehen.length(); i >= 0; i--) {
if (i >= 0 && i < this.aussehen.length()) {
char currentChar = this.aussehen.charAt(i);
if (currentChar == '>') {
newaussehen += '<';
} else if (currentChar == '<') {
newaussehen += '>';
} else if (currentChar == ')') {
newaussehen += '(';
} else if (currentChar == '(') {
newaussehen += ')';
} else {
newaussehen += currentChar;
}
}
}
this.aussehen = newaussehen;
}
public static class NormalFish extends Fisch {
public NormalFish(int swimHeight, int swimWidth, boolean direction) {
super(swimHeight, swimWidth, direction);
this.aussehen = "<><";
}
}
public static class Shark extends Fisch {
public Shark(int swimHeight, int swimWidth, boolean direction) {
super(swimHeight, swimWidth, direction);
this.aussehen = "<///====><";
}
}
public static class PufferFish extends Fisch {
public PufferFish(int swimHeight, int swimWidth, boolean direction) {
super(swimHeight, swimWidth, direction);
this.aussehen = "<()><";
}
}
public static class SwordFish extends Fisch {
public SwordFish(int swimHeight, int swimWidth, boolean direction) {
super(swimHeight, swimWidth, direction);
this.aussehen = "-<><";
}
}
}