Click here to Skip to main content
15,888,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();

		//linker Rand wird ausgegeben 
		for (int i = 0; i < this.tankHeight; i++) {
			System.out.print("|");
	
			//Breite wird Stück für Stück ausgegeben mit den Fischen
			  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++) {  // Führen Sie die Bewegung fünfmal durch (als Beispiel)
	        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; // Ändere die Richtung
	                turnFish(); // Drehe den Fisch
	            }
	        } else {
	            if (SwimWidth - 1 >= 0) {
	                SwimWidth -= 1;
	            } else {
	                direction = !direction; // Ändere die Richtung
	                turnFish(); // Drehe den Fisch
	            }
	        }
		 
		 if (this instanceof Shark && rand.nextInt(4) == 0) {
		        // Ändere Schwimmtiefe nur, wenn nicht am unteren Rand
		        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) {
		        // Ändere Schwimmtiefe nur, wenn nicht am oberen oder unteren Rand
		        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) {
		        // Ändere Schwimmtiefe nur, wenn nicht am oberen Rand
		        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
Posted

1 solution

With your design, you can't handle "collisions", because you're using "buckets". You can only "avoid" collisions by checking the bucket you "plan" to move to by first checking the bucket to see if it is "empty".

You could wind up with a situation where a fish is trapped by other fish.

If you used x,y,z coordinates, the fish can also go up and down. Or anywhere else. Then collisions are detected by comparing "center point separation" (for example).
 
Share this answer
 
Comments
fs7schmitzii 25-Dec-23 8:23am    
i fixed it like this, but theres just one "error" left. When they collide, for 1 output only the small fish gets shown, but it should be the larger one. HEres an example:| |
| ><><()><
|
|
+--------------------+


|
| ><>
|
|
+--------------------+


|
| <()><
|
|
+--------------------+


|
| <()><
|
|
+--------------------+


|
|<()><><>
|
|
+--------------------+


|
|><()> ><>
|
|
+--------------------+ 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 (this.fish[i][j] != null) {
this.fish[i][j].swim(this);

int newHeight = this.fish[i][j].getSwimHeight();
int newWidth = this.fish[i][j].getSwimWidth();

if (updatedFish[newHeight][newWidth] == null) {
updatedFish[newHeight][newWidth] = this.fish[i][j];


} else {

Fish smallerFish;
Fish largerFish;

if (this.fish[i][j].getLook().length() <= updatedFish[newHeight][newWidth].getLook().length()) {
largerFish = updatedFish[newHeight][newWidth];
smallerFish = this.fish[i][j];

} else {
largerFish = this.fish[i][j];
smallerFish = updatedFish[newHeight][newWidth];

}

int tempHeight = newHeight;
int tempWidth = newWidth;

// Finde eine leere Position für den kleineren Fisch
while (updatedFish[tempHeight][tempWidth] != null) {
updatedFish[tempHeight][tempWidth].swim(this);
}

// Aktualisiere die temporäre Position des kleineren Fisches
smallerFish.setSwimHeight(tempHeight);
smallerFish.setSwimWidth(tempWidth);

// Setze den kleineren Fisch an die temporäre Position
updatedFish[tempHeight][tempWidth] = smallerFish;
updatedFish[newHeight][newWidth] = largerFish;
}
}
}
}

this.fish = updatedFish;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900