Für diejenigen, die es interessiert, gibt es hier
den vollständigen Java-Quelltext:
Diesen Textteil kann man auch direkt als HTML-Quelltext lesen,
falls der Browser ihn im Fenster nicht vollständig oder korrekt darstellt.
(< und > werden möglicherweise als Tags interpretiert)
/* ** Evolution ** Anfang: 5.12.98, letzte Änderung: Januar 1999 */
// Java Version 1.0
import java.applet.*;
import java.awt.*;
import java.util.Random;
import java.util.Date;
import java.net.URL;
public class Evolution extends Applet implements Runnable {
static Fenster fe;
static Bild bi;
static Knoepfe kn;
static Thread r;
static double mr = 5; // Mutations-Rate
static int mrint;
static int mranz = 5;
static int mg = 1; // Mutations-Größe
static int pg = 4; // Populations-Größe
static int bb = 1; // es vermehrt sich (1 = der Beste)
static int wg = 8; // Wurm-Größe
static int qua = 10; // Kantenlänge des Quadrats
static int cy; // Anzahl der Zyclen
static int ze; // abgelaufene Zeit in s
static int abw; // maximale Abweichung von der Referenz
static int abwmin; // minimale Abweichung
static int kabw; // bisher erreichte kleinste Abweichung
static String f_up = "Gitter"; // Verzweigungs-Schlüssel für paint()
static Wurm w[] = new Wurm[pg]; // die gesamte Population
static Wurm wref; // Referenz-Wurm
static int zj; // Zwischen-Zähler für abw max
static int zm; // Zwischen-Zähler für abw min
static int cpai = 3; // neu zeichnen nach "cpai" Zyclen
static String status = " ";
static boolean einzel;
static long azeit; // Anfangs-Zeit eines Durchlaufs
static Date d;
static Wurm wbei1, wbei2;
static String artNeu = "neu zeichnen";
static boolean zb;
public Insets insets() { return new Insets(30,15,30,15); }
public void init() {
setLayout(new BorderLayout());
setBackground(Color.yellow);
add("Center", new Button("Fenster öffnen"));
kn = new Knoepfe();
bi = new Bild(qua);
fe = new Fenster(this);
fe.show();
fe.hide();
// fe.toFront();
status = "neuer";
bi.machRef();
/* Beispiel-Wurm */
int bx[] = {6,6,4,5,6,4,5,6,6,7,8,9,10,11,12,13,14,6,
6,6,12,12,12,5,11,7,8,9,10,11,6,12};
int by[] = {7,8,9,9,9,8,8,10,11,11,11,11,11,11,11,10,
9,12,13,14,12,13,14,15,15,12,12,12,12,12,15,15};
wbei1 = new Wurm(20, bx.length);
for (int i=0; i < bx.length; i++) {
wbei1.wu[i].x = bx[i];
wbei1.wu[i].y = by[i];
}
// start();
}
public boolean action(Event evt, Object arg) {
if (evt.target instanceof Button) {
String s = (String)arg;
if (s.equals("Fenster öffnen"))
fe.show();
start();
}
return true;
}
public void start() {
if (r == null) { r = new Thread(this); r.start(); r.suspend(); }
if (!r.isAlive()) { r.start(); r.suspend(); }
}
public void stop() {
if (r != null) { r.stop(); r=null; } }
public static void neue() {
w = new Wurm[pg];
f_up = "Gitter";
bi.buff();
if (artNeu.equals("neu zeichnen")) wref = bi.setRef();
else if (artNeu.equals("Zufall")) wref = new Wurm(qua, wg);
else if (artNeu.equals("das Gleiche")) wref = wref;
else if (artNeu.equals("Beispiel")) wref = wbei1;
for (int i=0; i < pg; i++) {
w[i] = new Wurm(qua, wg);
}
f_up = "WurmRef";
wref.drawWurm();
f_up = "Wurm";
w[0].drawWurm();
bi.repaint();
cy = 0;
mrint = (int)(Math.floor(mr/100*pg));
if (mrint > pg) mrint=pg;
if (mrint <= 0) mrint=1;
mranz = (mrint*100)/pg;
bi.ja_neu = false;
}
public void run() {
while (true) {
if (cy == 0) {
d = new Date();
azeit = d.getTime();
}
for (int ii=0; ii < cpai; ii++) {
cy = cy+1;
for (int i=0; i < pg; i++) {
int abw = w[i].rechneAbw(wref);
}
abw = 0;
abwmin = 1000000;
for (int i=0; i < pg; i++) {
if (w[i].abw > abw) {
abw = w[i].abw;
zj = i;
}
if (w[i].abw < abwmin) {
abwmin = w[i].abw;
zm = i;
}
}
int ia = (int)(Math.floor(pg*Math.random()));
if (bb == 1) w[zj] = w[zm].copyW(); // zm = der Beste
else w[zj] = w[ia].copyW(); // ia = irgendeiner
for (int i=0; i < mrint; i++) {
ia = (int)(Math.floor(pg*Math.random()));
w[ia].mutWurm(mg);
}
}
if ((status.equals("halten") || status.equals("neuer"))
&& !(einzel == true)) { Knoepfe.bu2.setLabel("s t a r t"); r.suspend(); }
f_up = "Gitter";
bi.buff();
f_up = "WurmRef";
wref.drawWurm();
f_up = "Wurm";
w[zm].drawWurm();
abwmin = w[zm].rechneAbw(wref);
bi.repaint();
d = new Date();
ze = (int)((d.getTime()-azeit)/1000);
kn.neuWert();
if (einzel == true) { Knoepfe.bu2.setLabel("s t a r t"); r.suspend(); }
try { Thread.sleep(10); }
catch (InterruptedException e) {}
}
}
}
class Fenster extends Frame {
MenuBar mb;
Menu m1, m2, m3;
MenuItem mi11, mi12, mi21, mi22, mi13;
DialogEi d_bild, d_info, d_hilfe;
DialogEi d_einst;
DialogEi d_neu;
Hilfe hil = new Hilfe();
boolean za, zb;
Label lb[] = new Label[7];
TextField tx[] = new TextField[7];
Button bu_bild, bu_rund, bu_eck, bu_einst, bu_info;
Bild bi;
static Knoepfe kn;
Evolution evo;
Label l[] = {
new Label("Hintergrund"),
new Label("Gitter"),
new Label("Vorlage"),
new Label("Wesen-Objekt")
};
Color f[] = {
new Color(000,000,000),
new Color(159,159,159),
new Color(223,223,223),
new Color(255,255,255),
new Color(255,255,000),
new Color(191,255,191),
new Color(255,200,200),
new Color(198,198,255),
new Color(255,200,000),
new Color(255,000,000),
new Color(000,000,255),
new Color(000,223,000)
};
Panel p[][] = new Panel[l.length] [f.length];
Fenster(Evolution evo) {
super("Evolutions-Experimente");
this.evo = evo;
show();
resize(500,400);
move(200,100);
setBackground(Color.gray);
mb = new MenuBar();
m1 = new Menu("Einstellungen");
mi11 = new MenuItem("Farben");
m1.add(mi11);
mi12 = new MenuItem("Mutation");
m1.add(mi12);
mb.add(m1);
m2 = new Menu(" I n f o ");
mi21 = new MenuItem("allgemein");
m2.add(mi21);
mi22 = new MenuItem("Bedienung");
m2.add(mi22);
mb.add(m2);
setMenuBar(mb);
GridBagLayout gri = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gri);
c.fill=c.BOTH;
c.weightx=1;
c.weighty=1;
c.insets=new Insets(4,4,4,4);
bi = Evolution.bi;
gri.setConstraints(bi, c);
add(bi);
c.weightx=0;
kn = Evolution.kn;
gri.setConstraints(kn, c);
add(kn);
/* Dialog-Fenster */
d_bild = new DialogEi(this, "Bild-Farben");
d_bild.show();
// d_bild.hide();
d_bild.resize(450,250);
d_bild.move(100,100);
d_bild.setBackground(new Color(239,239,239));
d_bild.setLayout(gri);
c.insets=new Insets(5,5,5,5);
c.weightx=1;
c.weighty=1;
c.fill=c.BOTH;
for (int i=0; i < l.length; i++) {
c.gridwidth = 1;
gri.setConstraints(l[i], c);
d_bild.add(l[i]);
for (int j=0; j < f.length; j++) {
if (j==f.length-1) c.gridwidth = c.REMAINDER;
else c.gridwidth = 1;
p[i][j] = new Panel();
gri.setConstraints(p[i][j], c);
p[i][j].setBackground(f[j]);
d_bild.add(p[i][j]);
}
}
c.gridwidth = 1;
c.fill = c.NONE;
bu_rund = new Button("eckig");
if(!Bild.rund) bu_rund.setLabel("rund");
gri.setConstraints(bu_rund, c);
d_bild.add(bu_rund);
bu_bild = new Button(" OK ");
c.gridwidth = c.REMAINDER;
gri.setConstraints(bu_bild, c);
d_bild.add(bu_bild);
d_bild.repaint();
d_einst = new DialogEi(this, "Mutations-Einstellungen");
d_einst.show();
// d_einst.hide();
d_einst.resize(300,330);
d_einst.move(100,100);
d_einst.setBackground(Color.white);
d_einst.setLayout(gri);
lb[1] = new Label("Mutations-Rate (in %):");
tx[1] = new TextField(Double.toString(Evolution.mr), 10);
lb[4] = new Label("neuzeichnen nach (Zyklen):");
tx[4] = new TextField(Integer.toString(Evolution.cpai), 10);
lb[3] = new Label("Populations-Größe (Stück):");
tx[3] = new TextField(Integer.toString(Evolution.pg), 10);
lb[2] = new Label("Vermehrungs-Art (1 oder 0):");
tx[2] = new TextField(Integer.toString(Evolution.bb), 10);
lb[5] = new Label("Wesen-Größe (Kästchen):");
tx[5] = new TextField(Integer.toString(Evolution.wg), 10);
lb[6] = new Label("Quadrat-Länge:");
tx[6] = new TextField(Integer.toString(Evolution.qua), 10);
c.fill=c.HORIZONTAL;
c.weightx=0;
c.weighty=1;
c.insets=new Insets(0,6,0,6);
for (int i=1; i < 7; i++) {
c.gridwidth=1;
gri.setConstraints(lb[i], c);
d_einst.add(lb[i]);
c.gridwidth=c.REMAINDER;
gri.setConstraints(tx[i], c);
d_einst.add(tx[i]);
}
c.fill=c.NONE;
c.gridwidth=2;
bu_einst = new Button(" OK ");
gri.setConstraints(bu_einst, c);
d_einst.add(bu_einst);
d_info = new DialogEi(this, "allgemeine Information zu \"Evolution\"");
// d_info.show();
// d_info.hide();
d_info.resize(300,300);
d_info.move(200,100);
d_hilfe = new DialogEi(this, "Bedienungs-Hilfe");
d_hilfe.show();
// d_hilfe.hide();
d_hilfe.resize(400,300);
d_hilfe.move(300,300);
d_hilfe.setLayout(new BorderLayout());
d_hilfe.add("Center", hil);
d_neu = new DialogEi(this, "ein neuer Durchlauf");
d_neu.show();
// d_neu.hide();
d_neu.resize(300,300);
d_neu.move(200,100);
Button but1 = new Button("das Gleiche");
Button but2 = new Button("neu zeichnen");
Button but3 = new Button("Zufall");
Button but4 = new Button("Beispiel");
d_neu.setLayout(gri);
c.fill=c.BOTH;
c.weightx=1;
c.weighty=1;
c.insets=new Insets(20,20,10,10);
c.gridwidth=1;
gri.setConstraints(but1, c);
d_neu.add(but1);
c.insets=new Insets(20,10,10,20);
c.gridwidth=c.REMAINDER;
gri.setConstraints(but2, c);
d_neu.add(but2);
c.insets=new Insets(10,20,20,10);
c.gridwidth=1;
gri.setConstraints(but3, c);
d_neu.add(but3);
c.insets=new Insets(10,10,20,20);
c.gridwidth=c.REMAINDER;
gri.setConstraints(but4, c);
d_neu.add(but4);
Warte.warte(this, evo, 1, 300);
}
void weiter1() {
hide();
d_bild.hide();
d_einst.hide();
d_info.hide();
d_neu.hide();
d_hilfe.hide();
}
public boolean handleEvent(Event evt) {
if (evt.id == Event.ACTION_EVENT) { action(evt, evt.arg); }
if (evt.id == Event.WINDOW_DESTROY && za == false) {
hide(); }
else if (evt.id == Event.WINDOW_DESTROY && za == true) {
za=false;
d_bild.hide();
d_einst.hide();
d_info.hide();
d_neu.hide();
d_hilfe.hide();
}
if (evt.id == Event.MOUSE_DOWN) {
for (int j=0; j < f.length; j++) {
if (evt.target == p[0][j]) Bild.cHin = f[j];
if (evt.target == p[1][j]) Bild.cGit = f[j];
if (evt.target == p[2][j]) Bild.cRef = f[j];
if (evt.target == p[3][j]) Bild.cWur = f[j];
}
}
return false;
}
public boolean action(Event evt, Object arg) {
if (evt.target == mi11) { d_bild.show(); za=true; }
if (evt.target == mi12) {
d_einst.show();
za=true;
tx[5].setText(Integer.toString(Evolution.wg));
tx[6].setText(Integer.toString(Evolution.qua));
}
if (evt.target == mi21 || evt.target == mi22) {
if (!evo.zb) {
d_hilfe.show();
try{ Thread.sleep(2000);}
catch(Exception ex) { }
d_hilfe.hide();
evo.zb=true;
}
hide();
String a = "bedienung";
if(evt.target == mi21) a = "allgemein";
if(evt.target == mi22) a = "bedienung";
URL theURL;
try { theURL = new URL(evo.getDocumentBase(),"Evolution.html#" + a);
evo.getAppletContext().showDocument(theURL);
}
catch (Exception ex) {System.out.println(ex+" "+ex.getMessage()); }
}
if (evt.target == bu_einst || evt.target instanceof TextField) {
Evolution.status = "halten";
try { Thread.sleep(80+5*Evolution.cpai); }
catch (InterruptedException e) {}
Evolution.mr = Double.valueOf(tx[1].getText()).doubleValue();
Evolution.cpai = Integer.parseInt(tx[4].getText());
Evolution.pg = Integer.parseInt(tx[3].getText());
Evolution.bb = Integer.parseInt(tx[2].getText());
Evolution.wg = Integer.parseInt(tx[5].getText());
Evolution.qua = Integer.parseInt(tx[6].getText());
d_einst.hide();
d_neu.show();
za = true;
}
if (evt.target == bu_rund) {
if (Bild.rund == true) { Bild.rund = false; bu_rund.setLabel("rund"); }
else { Bild.rund = true; bu_rund.setLabel("eckig"); }
}
if (evt.target == bu_bild) {
d_bild.hide();
// hide();
// show();
}
if (evt.target == Knoepfe.bu1) {
Evolution.einzel = true;
if (Evolution.status.equals("neuer") && Evolution.artNeu.equals("neu zeichnen")) {
Evolution.neue();
}
Evolution.r.resume();
Knoepfe.bu2.setLabel(" s t o p ");
Evolution.status = "halten";
}
if (evt.target == Knoepfe.bu2) {
// if(Evolution.r.isAlive() == false)
// Evolution.r.start(); Evolution.r.suspend();
Evolution.einzel = false;
if (Evolution.status.equals("neuer")) {
if (Evolution.artNeu.equals("neu zeichnen")) { Evolution.neue(); }
Evolution.status = "halten";
// Knoepfe.bu2.setLabel("s t a r t");
}
if (Evolution.status.equals("halten")) {
Evolution.status = "laufen";
Evolution.r.resume();
Knoepfe.bu2.setLabel(" s t o p ");
}
else if (Evolution.status.equals("laufen")) {
Evolution.status = "halten";
// Knoepfe.bu2.setLabel("s t a r t");
}
kn.neuWert();
}
if (evt.target == Knoepfe.bu3) {
d_neu.show();
za = true;
}
if (evt.target instanceof Button && ((String)arg).equals("neu zeichnen")) {
Evolution.artNeu = "neu zeichnen";
Evolution.status = "neuer";
try { Thread.sleep(80+5*Evolution.cpai); }
catch (InterruptedException e) {}
bi.machRef();
kn.neuWert();
d_neu.hide();
}
if (evt.target instanceof Button && ((String)arg).equals("das Gleiche")) {
Evolution.artNeu = "das Gleiche";
Evolution.status = "neuer";
try { Thread.sleep(80+5*Evolution.cpai); }
catch (InterruptedException e) {}
Evolution.neue();
kn.neuWert();
d_neu.hide();
}
if (evt.target instanceof Button && ((String)arg).equals("Zufall")) {
Evolution.artNeu = "Zufall";
Evolution.status = "neuer";
try { Thread.sleep(80+5*Evolution.cpai); }
catch (InterruptedException e) {}
Evolution.neue();
kn.neuWert();
d_neu.hide();
}
if (evt.target instanceof Button && ((String)arg).equals("Beispiel")) {
Evolution.artNeu = "Beispiel";
Evolution.status = "neuer";
try { Thread.sleep(80+5*Evolution.cpai); }
catch (InterruptedException e) {}
if (Evolution.qua < 20) Evolution.qua = 20;
Evolution.wg = Evolution.wbei1.wu.length;
Evolution.neue();
kn.neuWert();
d_neu.hide();
}
return true;
}
}
class Bild extends Canvas {
int qua;
String f_up;
int px, py;
Image off;
Graphics o;
int br, ho;
int l, x, y;
boolean gitt[][];
boolean ja_neu;
int zref;
int xg, yg;
static boolean rund = false;
static Color cHin = Color.white;
static Color cGit = Color.gray;
static Color cRef = Color.orange;
static Color cWur = Color.blue;
Bild(int qua) {
this.qua = qua;
setBackground(Color.white);
repaint();
}
public void machRef() {
// Knoepfe.bu2.setLabel("s t a r t");
Evolution.f_up = "Gitter";
buff();
repaint();
qua = Evolution.qua;
Evolution.f_up = "NeuRef";
gitt = new boolean[qua][qua];
for (int i=0; i < qua; i++) {
for (int j=0; j < qua; j++) {
gitt[i][j] = false;
} }
ja_neu = true;
zref = 0;
}
public Wurm setRef() {
if (zref == 0) zref = 1;
Wurm w = new Wurm(qua, zref);
Evolution.wg = zref;
int i = 0;
for (int j=0; j < qua; j++) {
for (int k=0; k < qua; k++) {
if (gitt[j][k] == true) {
w.wu[i].x = j;
w.wu[i].y = k;
gitt[j][k] = false;
if (i < zref-1) i = i+1;
}
}
}
ja_neu = false;
return w;
}
public boolean mouseDown(Event evt, int x, int y) {
int xgitt, ygitt;
if (ja_neu == true) {
if (x > this.x && x < this.x+l*qua+1 &&
y > this.y && y < this.y+l*qua+1 ) {
xgitt = (x-this.x-1)/l;
ygitt = (y-this.y-1)/l;
xg = xgitt;
yg = ygitt;
if (gitt[xgitt][ygitt] == true) {
gitt[xgitt][ygitt] = false;
zref--;
}
else if (gitt[xgitt][ygitt] == false) {
gitt[xgitt][ygitt] = true;
zref++;
}
drawPunkt(xgitt, ygitt);
repaint();
}
}
return true;
}
public boolean mouseDrag(Event evt, int x, int y) {
int xgitt, ygitt;
if (ja_neu == true) {
if (x > this.x && x < this.x+l*qua+1 &&
y > this.y && y < this.y+l*qua+1 ) {
xgitt = (x-this.x-1)/l;
ygitt = (y-this.y-1)/l;
if (gitt[xgitt][ygitt] == false && !(xgitt == xg && ygitt == yg)) {
gitt[xgitt][ygitt] = true;
zref++;
drawPunkt(xgitt, ygitt);
repaint();
}
}
}
return true;
}
public void drawPunkt(int px, int py) {
this.px = px;
this.py = py;
buff();
}
public void update(Graphics g) {
paint(g);
}
public void buff() {
{
this.qua = Evolution.qua;
f_up = Evolution.f_up;
Rectangle r = bounds();
if (ho != r.height || br != r.width) {
ho = r.height;
br = r.width;
off = createImage(br, ho);
o = off.getGraphics();
}
l = (Math.min(ho, br)-50)/qua;
x = (br-l*qua)/2;
y = (ho-l*qua)/2;
if (f_up.equals("Gitter")) {
o.setColor(cHin);
o.fillRect(0,0,br,ho);
o.setColor(cGit);
for (int i=0; i <= qua; i++) {
o.drawLine(x+i*l, y , x+i*l, y+qua*l);
o.drawLine(x , y+i*l, x+qua*l, y+i*l );
}
}
if (f_up.equals("Wurm")) {
o.setColor(cWur);
if(rund) o.fillOval(x+px*l+3, y+py*l+3, l-5, l-5);
else o.fillRoundRect(x+px*l+3, y+py*l+3, l-5, l-5, l/2, l/2);
}
// o.drawOval(x+px*l+2, y+py*l+2, l-4, l-4);
// o.drawOval(x+px*l+3, y+py*l+3, l-6, l-6);
// o.drawOval(x+px*l+4, y+py*l+4, l-8, l-8);
if (f_up.equals("WurmRef")) {
o.setColor(cRef);
o.fillRect(x+px*l+1, y+py*l+1, l-1, l-1);
}
if (f_up.equals("NeuRef")) {
if (gitt[px][py] == true) o.setColor(cRef);
if (gitt[px][py] == false) o.setColor(cHin);
o.fillRect(x+px*l+1, y+py*l+1, l-1, l-1);
}
}
}
public void paint(Graphics g) {
g.drawImage(off, 0, 0, this);
Rectangle r = bounds();
if (ho != r.height || br != r.width) {
Evolution.f_up = "Gitter";
buff();
Evolution.f_up = "Wurm";
Evolution.w[0].drawWurm();
repaint();
}
}
}
class Knoepfe extends Panel {
String st[] = { "Zyklen:", "Abweichung:", "Kantenlänge:", "Wesen-Größe:",
"Populations-Größe:", "Mutations-Rate:", "" };
Label te[] = new Label[st.length];
Label za[] = new Label[st.length+1];
static Button bu1 = new Button("einzel");
static Button bu2 = new Button("s t a r t");
static Button bu3 = new Button(" neu ");
Knoepfe() {
setBackground(Color.yellow);
GridBagLayout gri = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setLayout(gri);
c.insets=new Insets(0,6,0,6);
c.fill=c.HORIZONTAL;
c.gridwidth=1;
te[0] = new Label(st[0], Label.RIGHT);
gri.setConstraints(te[0], c);
add(te[0]);
za[0] = new Label("", Label.LEFT);
gri.setConstraints(za[0], c);
add(za[0]);
c.gridwidth=c.REMAINDER;
za[7] = new Label("", Label.LEFT);
gri.setConstraints(za[7], c);
add(za[7]);
for (int i=1; i < st.length; i++) {
c.gridwidth=2;
te[i] = new Label(st[i], Label.RIGHT);
gri.setConstraints(te[i], c);
add(te[i]);
c.gridwidth=c.REMAINDER;
za[i] = new Label("", Label.LEFT);
gri.setConstraints(za[i], c);
add(za[i]);
}
c.fill=c.NONE;
c.gridwidth=1;
gri.setConstraints(bu1, c);
add(bu1);
c.gridwidth=1;
gri.setConstraints(bu2, c);
add(bu2);
c.gridwidth=1;
gri.setConstraints(bu3, c);
add(bu3);
neuWert();
}
public void neuWert() {
za[0].setText(""+Evolution.cy);
za[7].setText(""+Evolution.ze+" s");
za[1].setText(""+Evolution.abwmin);
za[2].setText(""+Evolution.qua);
za[3].setText(""+Evolution.wg);
za[4].setText(""+Evolution.pg);
za[5].setText(""+Evolution.mranz+" %");
if (Evolution.bb == 1)
te[5].setText("(1) Mutations-Rate:");
else te[5].setText("(0) Mutations-Rate:");
}
}
class Wurm {
Point wu[]; // das eigentliche "Wurm"-Objekt
int abw; // die Abweichung zu einem Referenz-Wurm
int qua; // Kantenlänge des Quadrats
Wurm (int qua, int gro) {
// qua --- Kantenlänge des Quadrats
// gro --- Größe (Anzahl Kästchen) des Wurms
this.qua = qua;
wu = new Point[gro];
for (int i=0; i < gro; i++) {
wu[i] = new Point(0,0);
wu[i].x = (int)(Math.floor(qua*Math.random()));
wu[i].y = (int)(Math.floor(qua*Math.random()));
for (int j=0; j < i; j++) {
if (wu[i].x == wu[j].x && wu[i].y == wu[j].y) {
i=i-1;
j=i;
}
}
}
}
public Wurm copyW() {
Wurm w = new Wurm(qua, wu.length);
w.abw = abw;
w.qua = qua;
for (int i=0; i < wu.length; i++) {
w.wu[i].x = wu[i].x;
w.wu[i].y = wu[i].y;
}
return w;
}
public int rechneAbw (Wurm ref) {
abw = 0;
for (int i=0; i < wu.length; i++) {
abw = abw + (Math.abs(this.wu[i].x-ref.wu[i].x))
+ (Math.abs(this.wu[i].y-ref.wu[i].y));
}
return abw;
}
public int mutWurm (int mut_gr) {
int z;
int art;
art = (int)(8*Math.random());
z = (int)(wu.length*Math.random());
if (art == 0) wu[z].x = wu[z].x+1;
if (art == 1) wu[z].x = wu[z].x-1;
if (art == 2) wu[z].y = wu[z].y+1;
if (art == 3) wu[z].y = wu[z].y-1;
if (art == 4) { wu[z].x = wu[z].x+1; wu[z].y = wu[z].y+1; }
if (art == 5) { wu[z].x = wu[z].x-1; wu[z].y = wu[z].y-1; }
if (art == 6) { wu[z].x = wu[z].x+1; wu[z].y = wu[z].y-1; }
if (art == 7) { wu[z].x = wu[z].x-1; wu[z].y = wu[z].y+1; }
return z;
}
public void drawWurm() {
// System.out.println("**drawWurm**");
for (int i=0; i < wu.length; i++) {
Evolution.bi.drawPunkt(wu[i].x, wu[i].y);
}
}
}
// Wird nur Thread.sleep() aufgerufen, so wartet das Programm anstandslos sine Zeit,
// aber das Schlafen geschieht nicht in einem Thread (nebenläufig). Das heißt, dass
// während dieser Zeit das Applet keine anderen Aktivitäten ausführen kann, zum Beispiel
// Tastatur-Ereignisse oder anstehende paint-Befehle.
// Da aber in jeder Klasse nur eine run()-Methode frei ist, gibt es hier die Warte-Klasse,
// nur damit sleep() auch wirklich als Thread ausgeführt wird.
// Später kehrt die Steuerung je nach Ziel-Schlüssel wieder zurück, wo sie herkam.
class Warte extends Thread {
static Thread t;
static Fenster f;
static Evolution evo;
static int ziel;
static int dauer;
// Parameter werden übergeben, mit run() kann man das nicht
public static void warte(Fenster f_in, Evolution evo_in, int ziel_in, int dauer_in) {
f = f_in;
evo = evo_in;
ziel = ziel_in;
dauer = dauer_in;
Warte w = new Warte();
t = new Thread(w);
t.start();
}
public void run() {
try {Thread.sleep(dauer);}
catch (Exception e) { }
if (ziel==1) f.weiter1();
// if (ziel==2) evo.weiter2();
}
}
/* Die Dialogfenster sind nicht von der Standard Klasse "Dialog", sondern eigenständige
Frames "DialogEi" und geben ihre Events wohldefiniert weiter. Ich hatte sie nachträglich
umgewandelt, damit auch die Version JAVA 1.1 damit etwas anfangen kann. */
class DialogEi extends Frame {
Fenster f;
DialogEi(Fenster f, String str) {
super(str);
this.f = f;
}
public boolean handleEvent(Event evt) {
f.handleEvent(evt);
return false;
}
}
// Hilfe
class Hilfe extends TextArea
{
Hilfe() {
super(20,30);
setBackground(Color.white);
setFont(new Font("TimesRoman", Font.PLAIN, 22));
setForeground(Color.red);
insertText(hitx,0);
}
String hitx =
"\n"+
"\n"+
"\n"+
" Die Erläuterungen\n"+
"\n"+
" sind auf der Web-Seite !"
;
}