package fhp;
import java.awt.Point;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class FHPModell {
private Map<Point, Teilchen> model;
/**
* Modelle sollen nur mit Fabrikmethoden erzeugt werden.
*/
private FHPModell() {
model = new HashMap<Point, Teilchen>();
}
/**
* Fabrik für ein 100 Knoten Modell.
*
* @return Modell mit 100 Knoten
*/
public static FHPModell create10x10Model() {
Random r = new Random();
FHPModell m = new FHPModell();
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 10; x++) {
Point p = new Point(x, y);
Teilchen t = Teilchen.createRandomTeilchen();
if (r.nextInt(4) == 0)
// Knoten ohne Teilchen
m.model.put(p, null);
else
m.model.put(p, t);
}
}
return m;
}
/**
* Fabrik für ein 16 Knoten Modell.
*
* @return Modell mit 16 Knoten
*/
public static FHPModell create4x4Model() {
Random r = new Random();
FHPModell m = new FHPModell();
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
Point p = new Point(x, y);
Teilchen t = Teilchen.createRandomTeilchen();
if (r.nextInt(4) == 0)
// Knoten ohne Teilchen
m.model.put(p, null);
else
m.model.put(p, t);
}
}
return m;
}
public static void main(String[] args) {
// FHPModell fhpModell = FHPModell.create4x4Model();
FHPModell fhpModell = FHPModell.create10x10Model();
System.out.println(fhpModell.toString());
System.out.println("Zustand wechseln");
fhpModell.wechselZustand();
System.out.println(fhpModell.toString());
System.out.println("...Ende");
}
private void bewege() {
Map<Point, Teilchen> bewegtesModell = new HashMap<Point, Teilchen>();
for (Point p : model.keySet()) {
Teilchen teilchen = model.get(p);
int x_neu = 0, y_neu = 0;
if (teilchen == null) {
x_neu = p.x;
y_neu = p.y;
} else {
int richtung = teilchen.getRichtung();
switch (richtung) {
case 0:
x_neu = p.x + 1;
y_neu = p.y;
break;
case 60:
x_neu = p.x + 1;
y_neu = p.y + 1;
break;
// u.s.w
default:
break;
}
}
Point p_neu = new Point(x_neu, y_neu);
// laut FHP Modell sollte jetzt jede Bewegung möglich sein
// deshalb wäre hier ein Test gut
if (bewegtesModell.containsKey(p_neu)) {
// Programmierfehler
}
bewegtesModell.put(p_neu, teilchen);
}
// nun noch den Zustand wechseln
model = bewegtesModell;
}
private void kollidiere() {
for (Point p : model.keySet()) {
Teilchen teilchen = model.get(p);
// Knoten ist leer
if (teilchen == null)
continue;
else {
List<Teilchen> nachbarn = getNachbarn(p);
int anzahlNachbarn = 0;
for (Teilchen nachbar : nachbarn) {
if (nachbar != null)
anzahlNachbarn++;
}
// zwei Teilchen
if (anzahlNachbarn == 1) {
Teilchen nachbar = null;
for (Teilchen nachbarInListe : nachbarn) {
if (nachbarInListe != null)
nachbar = nachbarInListe;
}
if (Teilchen.isImpulsNull(teilchen, nachbar)) {
// - Teilchen._60_GRAD kann auch sein
nachbar.drehen(Teilchen._60_GRAD);
teilchen.drehen(Teilchen._60_GRAD);
}
}
// drei Teilchen
else if (anzahlNachbarn == 2) {
Teilchen n1 = null;
Teilchen n2 = null;
for (Teilchen nachbarInListe : nachbarn) {
if (nachbarInListe != null) {
if (n1 == null)
n1 = nachbarInListe;
else
n2 = nachbarInListe;
}
}
if (Teilchen.isImpulsNull(teilchen, n1, n2)) {
teilchen.drehen(Teilchen._60_GRAD);
n1.drehen(Teilchen._60_GRAD);
n2.drehen(Teilchen._60_GRAD);
}
}
// vier Teilchen
else if (anzahlNachbarn == 3) {
Teilchen n1 = null;
Teilchen n2 = null;
Teilchen n3 = null;
for (Teilchen nachbarInListe : nachbarn) {
if (nachbarInListe != null) {
if (n1 == null)
n1 = nachbarInListe;
else if (n2 == null)
n2 = nachbarInListe;
else
n3 = nachbarInListe;
}
}
// hier fehtl noch die isImpulseNull für 4 Parameter
// - Teilchen._60_GRAD kann auch sein
teilchen.drehen(Teilchen._60_GRAD);
n1.drehen(Teilchen._60_GRAD);
n2.drehen(Teilchen._60_GRAD);
n3.drehen(Teilchen._60_GRAD);
}
// ...sonst keine Kollision
}
}
}
public List<Teilchen> getNachbarn(Point knoten) {
List<Teilchen> nachbarn = new ArrayList<Teilchen>();
int x, y;
int nx, ny;
Point nachbarKnoten;
Teilchen nachbarTeilchen = null;
x = knoten.x;
y = knoten.y;
nx = x - 1;
ny = y;
nachbarKnoten = new Point(nx, ny);
// wenn es zu dem Knoten kein Teilchen gibt ist der Rückgabewert null
nachbarTeilchen = model.get(nachbarKnoten);
nachbarn.add(nachbarTeilchen);
nx = x + 1;
ny = y;
nachbarKnoten = new Point(nx, ny);
// wenn es zu dem Knoten kein Teilchen gibt ist der Rückgabewert null
nachbarTeilchen = model.get(nachbarKnoten);
nachbarn.add(nachbarTeilchen);
nx = x - 1;
ny = y + 1;
nachbarKnoten = new Point(nx, ny);
// wenn es zu dem Knoten kein Teilchen gibt ist der Rückgabewert null
nachbarTeilchen = model.get(nachbarKnoten);
nachbarn.add(nachbarTeilchen);
nx = x + 1;
ny = y + 1;
nachbarKnoten = new Point(nx, ny);
// wenn es zu dem Knoten kein Teilchen gibt ist der Rückgabewert null
nachbarTeilchen = model.get(nachbarKnoten);
nachbarn.add(nachbarTeilchen);
nx = x - 1;
ny = y - 1;
nachbarKnoten = new Point(nx, ny);
// wenn es zu dem Knoten kein Teilchen gibt ist der Rückgabewert null
nachbarTeilchen = model.get(nachbarKnoten);
nachbarn.add(nachbarTeilchen);
nx = x + 1;
ny = y - 1;
nachbarKnoten = new Point(nx, ny);
// wenn es zu dem Knoten kein Teilchen gibt ist der Rückgabewert null
nachbarTeilchen = model.get(nachbarKnoten);
nachbarn.add(nachbarTeilchen);
return nachbarn;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
String result = "";
int y_max = 0;
int x_max = 0;
for (Point p : model.keySet()) {
y_max = Math.max(y_max, p.y);
x_max = Math.max(x_max, p.x);
}
for (int y = 0; y <= y_max; y++) {
for (int x = 0; x <= x_max; x++) {
Point p = new Point(x, y);
Teilchen t = model.get(p);
result += String.valueOf(t) + " --- ";
}
if (y % 2 == 0)
result += "\n --- ";
else
result += "\n";
}
return result;
}
public void wechselZustand() {
kollidiere();
bewege();
}
}