Last active
December 8, 2017 14:17
-
-
Save alexanderbazo/a43901c174e5e0c51e08d29ed1b5ec1f to your computer and use it in GitHub Desktop.
Ein Lösungsvorschlag für die AntColony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import de.ur.mi.graphicsapp.GraphicsApp; | |
import simulator.ColonySimulator; | |
import world.ColonyView; | |
public class AntColony extends GraphicsApp { | |
private static final int WIDTH = 300; | |
private static final int HEIGHT = 300; | |
private ColonySimulator simulator; | |
private ColonyView view; | |
public void setup() { | |
size(WIDTH, HEIGHT); | |
simulator = new ColonySimulator(WIDTH, HEIGHT); | |
view = new ColonyView(); | |
simulator.addObserver(view); | |
simulator.start(); | |
} | |
public void draw() { | |
simulator.tick(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package simulator.ants; | |
import de.ur.mi.geom.Point; | |
import simulator.World; | |
import java.util.ArrayList; | |
public class AntFactory { | |
private World world; | |
public AntFactory(World world) { | |
this.world = world; | |
} | |
public Ant cloneAnt(Ant ant) { | |
if (ant instanceof Queen) { | |
return new Queen(ant); | |
} | |
if (ant instanceof Warrior) { | |
return new Warrior(ant); | |
} | |
if (ant instanceof Worker) { | |
return new Worker(ant); | |
} | |
return null; | |
} | |
public Ant getAnt(Ants type) { | |
Point position = getRandomStartPosition(); | |
if(position == null) { | |
return null; | |
} | |
return getAnt(type, (int) position.getX(), (int) position.getY()); | |
} | |
public Ant getAnt(Ants type, int x, int y) { | |
switch (type) { | |
case QUEEN: | |
return new Queen(x, y); | |
case WARRIOR: | |
return new Warrior(x, y); | |
case WORKER: | |
return new Worker(x, y); | |
default: | |
return null; | |
} | |
} | |
private Point getRandomStartPosition() { | |
ArrayList<Ant> ants = world.getAnts(); | |
Point startPosition; | |
for (Ant ant : ants) { | |
startPosition = findFreeNeighbourForAnt(ant); | |
if(startPosition != null) { | |
return startPosition; | |
} | |
} | |
return null; | |
} | |
private Point findFreeNeighbourForAnt(Ant ant) { | |
int xPos, yPos; | |
for (int xx = -1; xx <= 1; xx++) { | |
for (int yy = -1; yy <= 1; yy++) { | |
xPos = ant.getX() + xx; | |
yPos = ant.getY() + yy; | |
if(xPos == 0 && yPos == 0) { | |
continue; | |
} | |
if(xPos < 0 || xPos > world.getWorldWidth()) { | |
continue; | |
} | |
if(yPos < 0 || yPos > world.getWorldHeight()) { | |
continue; | |
} | |
if(world.getAntAtPosition(xPos,yPos) != null) { | |
continue; | |
} | |
return new Point(xPos, yPos); | |
} | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package simulator; | |
import de.ur.mi.util.RandomGenerator; | |
import simulator.ants.*; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Observable; | |
public class ColonySimulator extends Observable implements World { | |
private RandomGenerator random; | |
private int width; | |
private int height; | |
private ArrayList<Ant> ants; | |
private AntFactory factory; | |
public ColonySimulator(int width, int height) { | |
this.width = width; | |
this.height = height; | |
} | |
public void start() { | |
random = RandomGenerator.getInstance(); | |
ants = new ArrayList<Ant>(); | |
factory = new AntFactory(this); | |
placeInitalQueen(); | |
} | |
public void tick() { | |
double probability = random.nextDouble(0.0,1.0); | |
if(probability > 0.5) { | |
return; | |
} | |
if(probability > 0.1) { | |
Ant ant = factory.getAnt(Ants.WORKER); | |
placeAnt(ant); | |
return; | |
} | |
if(probability > 0.05) { | |
Ant ant = factory.getAnt(Ants.WARRIOR); | |
placeAnt(ant); | |
return; | |
} | |
Ant ant = factory.getAnt(Ants.QUEEN); | |
placeAnt(ant); | |
return; | |
} | |
private void placeInitalQueen() { | |
int xPos = random.nextInt(0, width); | |
int yPos = random.nextInt(0, height); | |
Ant ant = factory.getAnt(Ants.QUEEN, xPos, yPos); | |
placeAnt(ant); | |
} | |
private void placeAnt(Ant ant) { | |
if(ant == null) { | |
return; | |
} | |
ants.add(ant); | |
setChanged(); | |
notifyObservers(ant); | |
} | |
public ArrayList<Ant> getAnts() { | |
ArrayList<Ant> ants = new ArrayList<Ant>(); | |
for(Ant ant: this.ants) { | |
ants.add(factory.cloneAnt(ant)); | |
} | |
Collections.shuffle(ants); | |
return ants; | |
} | |
public Ant getAntAtPosition(int x, int y) { | |
for(Ant ant: ants) { | |
if(ant.getX() == x && ant.getY() == y) { | |
return ant; | |
} | |
} | |
return null; | |
} | |
public int getWorldWidth() { | |
return width; | |
} | |
public int getWorldHeight() { | |
return height; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package world; | |
import de.ur.mi.graphics.Rect; | |
import simulator.ants.Ant; | |
import java.util.Observable; | |
import java.util.Observer; | |
public class ColonyView implements Observer { | |
public void update(Observable subject, Object obj) { | |
if(obj instanceof Ant) { | |
drawAnt((Ant) obj); | |
} | |
} | |
private void drawAnt(Ant ant) { | |
Rect rect = new Rect(ant.getX(), ant.getY(), 1, 1, ant.getColor()); | |
rect.draw(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package simulator; | |
import simulator.ants.Ant; | |
import java.util.ArrayList; | |
public interface World { | |
public ArrayList<Ant> getAnts(); | |
public Ant getAntAtPosition(int x, int y); | |
public int getWorldWidth(); | |
public int getWorldHeight(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment