Skip to content

Commit 8a509b6

Browse files
committed
Maze snapshot
1 parent 1542544 commit 8a509b6

9 files changed

Lines changed: 107 additions & 42 deletions

File tree

vol8/src/main/java/ru/mifi/practice/vol8/Machine.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ru.mifi.practice.vol8;
22

3+
import lombok.Getter;
4+
35
import java.lang.reflect.Constructor;
46
import java.time.LocalDateTime;
57
import java.time.format.DateTimeFormatter;
@@ -8,9 +10,11 @@
810
import java.util.Map;
911
import java.util.Optional;
1012

13+
@Getter
1114
public abstract class Machine {
1215
public static final Key MACHINE_CLASS = () -> "machine_class";
13-
protected final Context context;
16+
public static final Key MACHINE_HANDLER = () -> "machine_handler";
17+
private final Context context;
1418

1519
protected Machine() {
1620
this(new Context.Standard());
@@ -33,6 +37,10 @@ public static Machine of(Context context) {
3337
}
3438
}
3539

40+
public State execute() {
41+
return execute(context.get(MACHINE_HANDLER, Handler.class).orElseThrow());
42+
}
43+
3644
public State execute(Handler handler) {
3745
return execute(context, handler);
3846
}

vol8/src/main/java/ru/mifi/practice/vol8/otp/Main.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22

33
import ru.mifi.practice.vol8.Machine;
44

5-
import static ru.mifi.practice.vol8.Machine.MACHINE_CLASS;
65
import static ru.mifi.practice.vol8.otp.OTPKey.CODE;
6+
import static ru.mifi.practice.vol8.otp.OTPMachine.PERSISTENCE_CODE;
77

88
public abstract class Main {
99
public static void main(String[] args) {
10-
Machine.Context context = new Machine.Context.Standard();
11-
context.setCurrentState(OTP.INITIATE);
12-
context.set(MACHINE_CLASS, OTPMachine.class);
13-
Machine machine = Machine.of(context);
14-
OTPMachine.OTPHandler handler = new OTPMachine.OTPHandler();
15-
Machine.State state = machine.execute(handler);
16-
System.out.println(state);
10+
Machine machine = new OTPMachine();
11+
Machine.Context context = machine.getContext();
12+
Machine.State state = machine.execute();
1713
if (state.equals(OTP.WAS_SENT)) {
18-
context.set(CODE, handler.getCode());
14+
context.set(CODE, context.get(PERSISTENCE_CODE, String.class).orElseThrow());
15+
}
16+
state = machine.execute();
17+
if (!state.equals(OTP.VERIFIED)) {
18+
throw new IllegalStateException();
1919
}
20-
state = machine.execute(handler);
21-
System.out.println(state);
2220
}
2321
}

vol8/src/main/java/ru/mifi/practice/vol8/otp/OTPMachine.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,22 @@
55
import java.util.Random;
66

77
public final class OTPMachine extends Machine {
8+
public static final Key PERSISTENCE_CODE = () -> "persist_code";
9+
810
public OTPMachine() {
11+
super();
12+
Context context = getContext();
913
context.setCurrentState(OTP.INITIATE);
1014
context.set(MACHINE_CLASS, OTPMachine.class);
15+
context.set(MACHINE_HANDLER, new OTPHandler());
16+
}
17+
18+
@SuppressWarnings("unused")
19+
public OTPMachine(Context context) {
20+
super(context);
21+
context.setCurrentState(OTP.INITIATE);
22+
context.set(MACHINE_CLASS, OTPMachine.class);
23+
context.set(MACHINE_HANDLER, new OTPHandler());
1124
}
1225

1326
static final class OTPHandler implements Handler {
@@ -23,6 +36,7 @@ public void printf(String format, Object... args) {
2336
@Override
2437
public boolean sendNextCode(Context context) {
2538
code = String.valueOf(new Random().nextInt(9999) + 1000);
39+
context.set(PERSISTENCE_CODE, code);
2640
debugf("[%15s] %s%n", "Код", code);
2741
return true;
2842
}
@@ -36,9 +50,5 @@ public boolean isCodeEquals(Context context, Key codeKey) {
3650
public void persist(Context context) {
3751
this.context = context.copy();
3852
}
39-
40-
String getCode() {
41-
return code;
42-
}
4353
}
4454
}

vol_/src/main/java/ru/mifi/practice/voln/maze/Main.java renamed to vol_/src/main/java/ru/mifi/practice/voln/mazes/Main.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package ru.mifi.practice.voln.maze;
1+
package ru.mifi.practice.voln.mazes;
22

3-
import ru.mifi.practice.voln.maze.implementation.ImageRepresentation;
4-
import ru.mifi.practice.voln.maze.implementation.finder.NodeFinder;
5-
import ru.mifi.practice.voln.maze.implementation.generator.NodeGenerator;
3+
import ru.mifi.practice.voln.mazes.implementation.ImageRepresentation;
4+
import ru.mifi.practice.voln.mazes.implementation.finder.NodeFinder;
5+
import ru.mifi.practice.voln.mazes.implementation.generator.NodeGenerator;
66

77
public abstract class Main {
88
public static void main(String[] args) {
99
Maze.Generator generator = new NodeGenerator();
10-
Maze.Finder finder = new NodeFinder();
1110
Maze.Representation representation = new ImageRepresentation();
12-
for (int i = 0; i < 9; i++) {
11+
Maze.Finder finder = new NodeFinder();
12+
for (int i = 0; i < 1; i++) {
1313
generate(generator, finder, representation, 10, 10, "N" + i);
1414
}
1515
}

vol_/src/main/java/ru/mifi/practice/voln/maze/Maze.java renamed to vol_/src/main/java/ru/mifi/practice/voln/mazes/Maze.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package ru.mifi.practice.voln.maze;
1+
package ru.mifi.practice.voln.mazes;
22

3+
import java.awt.Color;
34
import java.util.Objects;
45

56
@SuppressWarnings("PMD.ConstantsInInterface")
@@ -24,9 +25,10 @@ interface Serializer {
2425
String serialize(Grid grid);
2526
}
2627

27-
@FunctionalInterface
2828
interface Representation {
2929
void representation(String name, Grid grid, Point[] path);
30+
31+
void snapshot(int index, Grid grid, Point[] points, Color color);
3032
}
3133

3234
record Grid(int cols, int rows, char[][] data) {

vol_/src/main/java/ru/mifi/practice/voln/maze/implementation/ImageRepresentation.java renamed to vol_/src/main/java/ru/mifi/practice/voln/mazes/implementation/ImageRepresentation.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package ru.mifi.practice.voln.maze.implementation;
1+
package ru.mifi.practice.voln.mazes.implementation;
22

33
import lombok.SneakyThrows;
4-
import ru.mifi.practice.voln.maze.Maze;
4+
import ru.mifi.practice.voln.mazes.Maze;
55

66
import javax.imageio.ImageIO;
77
import java.awt.BasicStroke;
@@ -97,6 +97,29 @@ private BufferedImage createImage(Maze.Grid maze, Maze.Point[] path) {
9797
return result;
9898
}
9999

100+
private BufferedImage createSnapshot(Maze.Grid maze, Maze.Point[] points, Color color) {
101+
BufferedImage result = new BufferedImage(maze.cols() * width + width, maze.rows() * width + width, BufferedImage.TYPE_INT_ARGB);
102+
Graphics2D g = result.createGraphics();
103+
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
104+
g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
105+
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
106+
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
107+
drawMaze(g, maze, lineColor, width, thickness);
108+
final Stroke lastStroke = g.getStroke();
109+
g.setStroke(new BasicStroke(thickness));
110+
final Color lastColor = g.getColor();
111+
g.setColor(color);
112+
for (Maze.Point p : points) {
113+
int halfWidth = width / 2;
114+
int xCenter = (p.x() * width) + halfWidth;
115+
int yCenter = (p.y() * width) + halfWidth;
116+
drawCenteredCircle(g, xCenter, yCenter, thickness);
117+
}
118+
g.setStroke(lastStroke);
119+
g.setColor(lastColor);
120+
return result;
121+
}
122+
100123
@SneakyThrows
101124
@Override
102125
public void representation(String name, Maze.Grid grid, Maze.Point[] path) {
@@ -105,4 +128,11 @@ public void representation(String name, Maze.Grid grid, Maze.Point[] path) {
105128
ImageIO.write(image, "PNG", output);
106129
}
107130

131+
@SneakyThrows
132+
@Override
133+
public void snapshot(int index, Maze.Grid grid, Maze.Point[] points, Color color) {
134+
BufferedImage image = createSnapshot(grid, points, color);
135+
File output = new File(String.format("%03dx%03d-snapshot-%04d.png", grid.rows(), grid.cols(), index));
136+
ImageIO.write(image, "PNG", output);
137+
}
108138
}

vol_/src/main/java/ru/mifi/practice/voln/maze/implementation/NodeCommon.java renamed to vol_/src/main/java/ru/mifi/practice/voln/mazes/implementation/NodeCommon.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package ru.mifi.practice.voln.maze.implementation;
1+
package ru.mifi.practice.voln.mazes.implementation;
22

3-
import ru.mifi.practice.voln.maze.Maze;
3+
import ru.mifi.practice.voln.mazes.Maze;
44

55
import java.util.List;
66
import java.util.Set;

vol_/src/main/java/ru/mifi/practice/voln/maze/implementation/finder/NodeFinder.java renamed to vol_/src/main/java/ru/mifi/practice/voln/mazes/implementation/finder/NodeFinder.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1-
package ru.mifi.practice.voln.maze.implementation.finder;
1+
package ru.mifi.practice.voln.mazes.implementation.finder;
22

3-
import ru.mifi.practice.voln.maze.Maze;
4-
import ru.mifi.practice.voln.maze.implementation.NodeCommon;
3+
import ru.mifi.practice.voln.mazes.Maze;
4+
import ru.mifi.practice.voln.mazes.implementation.NodeCommon;
55

6+
import java.awt.Color;
67
import java.util.ArrayList;
78
import java.util.HashSet;
89
import java.util.List;
910
import java.util.Set;
1011

1112
public final class NodeFinder extends NodeCommon implements Maze.Finder {
13+
private final Maze.Representation repr;
14+
15+
public NodeFinder(Maze.Representation representation) {
16+
this.repr = representation;
17+
}
18+
19+
public NodeFinder() {
20+
this(null);
21+
}
22+
1223
private static Node getNearest(List<Node> neighbors) {
1324
neighbors.sort((o1, o2) -> {
1425
Integer d1 = o1.distance;
1526
Integer d2 = o2.distance;
1627
return d1.compareTo(d2);
1728
});
18-
for (Node cell : neighbors) {
19-
if (cell.distance != -1) {
20-
return cell;
29+
for (Node node : neighbors) {
30+
if (node.distance != -1) {
31+
return node;
2132
}
2233
}
2334
return null;
@@ -29,23 +40,30 @@ public Maze.Point[] findPath(Maze.Grid maze) {
2940
Set<Node> nodeSet = new HashSet<>();
3041
for (int y = 0; y < maze.rows(); y++) {
3142
for (int x = 0; x < maze.cols(); x++) {
32-
Node cell = new Node(x, y, maze.data(x, y));
33-
nodes[x][y] = cell;
34-
nodeSet.add(cell);
43+
Node node = new Node(x, y, maze.data(x, y));
44+
nodes[x][y] = node;
45+
nodeSet.add(node);
3546
}
3647
}
3748

3849
Node start = nodes[0][0];
3950
start.distance = 0;
4051
Node finish = nodes[maze.rows() - 1][maze.cols() - 1];
4152
int wave = 0;
53+
int index = 0;
54+
List<Maze.Point> points = new ArrayList<>();
4255
do {
4356
for (Node node : nodeSet) {
4457
if (node.distance == wave) {
4558
List<Node> neighbors = node.neighbors(nodeSet);
4659
for (Node neighbor : neighbors) {
4760
if (neighbor.distance == -1) {
61+
points.add(new Maze.Point(neighbor.x, neighbor.y));
4862
neighbor.distance = wave + 1;
63+
if (repr != null) {
64+
repr.snapshot(index, maze, points.toArray(new Maze.Point[0]), Color.ORANGE);
65+
++index;
66+
}
4967
}
5068
}
5169
}
@@ -55,8 +73,7 @@ public Maze.Point[] findPath(Maze.Grid maze) {
5573
List<Node> path = new ArrayList<>();
5674
path.add(finish);
5775
Node node = finish;
58-
while (!path.contains(start)) {
59-
assert node != null;
76+
while (!path.contains(start) && node != null) {
6077
node = getNearest(node.neighbors(nodeSet));
6178
path.add(node);
6279
}

vol_/src/main/java/ru/mifi/practice/voln/maze/implementation/generator/NodeGenerator.java renamed to vol_/src/main/java/ru/mifi/practice/voln/mazes/implementation/generator/NodeGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package ru.mifi.practice.voln.maze.implementation.generator;
1+
package ru.mifi.practice.voln.mazes.implementation.generator;
22

3-
import ru.mifi.practice.voln.maze.Maze;
4-
import ru.mifi.practice.voln.maze.implementation.NodeCommon;
3+
import ru.mifi.practice.voln.mazes.Maze;
4+
import ru.mifi.practice.voln.mazes.implementation.NodeCommon;
55

66
import java.util.Date;
77
import java.util.Random;

0 commit comments

Comments
 (0)