1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
package de.lmu.tcs;
import java.awt.*;
/**
* View
*
* Created by jost on 24.11.15.
*/
public class Ansicht {
private final GraphicsWindow fenster;
private final int max_x; //Breite
private final int max_y; //Höhe
private final int skalierung;
public Ansicht(int x, int y, int skalierung) {
this.max_x = x;
this.max_y = y;
this.skalierung = skalierung;
Point furthestCenter = centerByIndex(x, y);
this.fenster = new GraphicsWindow(
(int) (furthestCenter.getX() + Math.sqrt(3) * 1/2 * (double) skalierung + (double) (y % 2) * Math.sqrt(3) * 1/2 * (double) skalierung)
, (int) (furthestCenter.getY() + (double) skalierung / 2)
);
}
public void zeichneZelle(Zelle zelle) {
Position pos = zelle.getPosition();
// Rectangle box = new Rectangle(pos.getX() * skalierung, pos.getY() * skalierung, skalierung - 1, skalierung - 1);
Polygon box = new Hexagon(centerByIndex(pos.getX(), pos.getY()), skalierung).asPolygon();
fenster.setColor(Color.GRAY);
fenster.draw(box);
if (zelle.istTot()) {
fenster.setColor(Param.ZELLENFARBE[0]);
} else {
fenster.setColor(Param.ZELLENFARBE[Math.min(zelle.alter() + 1, Param.ZELLENFARBE.length - 1)]);
}
fenster.fill(box);
}
public void zeichneSpielfeld(Zelle[][] feld) {
fenster.clear();
// for (int x = 0; x < max_x; x++) {
// for (int y = 0; y < max_y; y++) {
// zeichenZelle(feld[x][y]);
// }
// Äquivalente Alternative ohne explizite Indizes:
for (Zelle[] zeile : feld) {
for (Zelle zelle : zeile) {
if (zelle != null)
zeichneZelle(zelle);
}
}
}
public Position getClick() {
Point point = fenster.mouseClick();
Point firstCenter = centerByIndex(0,0);
Position testStart = new Position(
(point.x - firstCenter.x) / ((int) (Math.sqrt(3) * (double) skalierung))
, (point.y - firstCenter.y) / ((int) (1.5 * (double) skalierung))
); // bad guess -- P.S.: actually, now it's a pretty good guess
for (int d = 0; d < Math.max(max_x, max_y); d++) // and starting there we test everything (also, another extremely bad guess)
for (int dx = -d; dx <= d; dx++)
for (int dy = -d; dy <= d; dy++)
{
if (Math.abs(dx) < d && Math.abs(dy) < d)
continue;
int x = testStart.getX() + dx;
int y = testStart.getY() + dy;
// zeichneZelle(new Zelle(new Position(x, y), Zelle.LEBENDIG));
// try {Thread.sleep(100);} catch (Exception e){}
Hexagon test = new Hexagon(centerByIndex(x, y), skalierung);
if (test.contains(point))
return new Position(x, y);
}
return new Position(-1, -1);
}
public void sleep(long delay) {
fenster.sleep(delay);
}
public void setText(String message) {
fenster.setText(message);
}
public Point centerByIndex(int x, int y)
{
return new Point(
(int) (Math.sqrt(3) * ((double) skalierung) * (((double) x) + 1 + ((double) (y % 2)) / 2))
, (int) (((double) skalierung) * (1 + 1.5 * ((double) y)))
);
}
}
|