1
1
package org .bk .ass .path ;
2
2
3
- import static org .assertj . core .api .Assertions . assertThat ;
3
+ import org .junit . jupiter .api .Test ;
4
4
5
- import java . awt . Color ;
6
- import java .awt .Graphics ;
5
+ import javax . imageio . ImageIO ;
6
+ import java .awt .* ;
7
7
import java .awt .image .BufferedImage ;
8
8
import java .io .File ;
9
9
import java .io .IOException ;
10
10
import java .util .SplittableRandom ;
11
- import javax . imageio . ImageIO ;
12
- import org .junit . jupiter .api .Test ;
11
+
12
+ import static org .assertj . core .api .Assertions . assertThat ;
13
13
14
14
class JpsTest {
15
15
@@ -67,7 +67,7 @@ void shouldFindDiagonalPath() {
67
67
Jps sut =
68
68
new Jps (
69
69
Map .fromBooleanArray (
70
- new boolean [][]{{true , true , true }, {true , true , true }, {true , true , true }}));
70
+ new boolean [][]{{true , true , true }, {true , true , true }, {true , true , true }}));
71
71
72
72
// WHEN
73
73
Result result = sut .findPath (new Position (2 , 2 ), new Position (0 , 0 ));
@@ -82,7 +82,7 @@ void shouldFindPathWithObstacle() {
82
82
Jps sut =
83
83
new Jps (
84
84
Map .fromBooleanArray (
85
- new boolean [][]{{true , true , true }, {true , false , false }, {true , true , true }}));
85
+ new boolean [][]{{true , true , true }, {true , false , false }, {true , true , true }}));
86
86
87
87
// WHEN
88
88
Result result = sut .findPath (new Position (2 , 2 ), new Position (0 , 0 ));
@@ -99,12 +99,12 @@ void shouldNotFindPathWhenBlockedButWithCircle() {
99
99
Jps sut =
100
100
new Jps (
101
101
Map .fromBooleanArray (
102
- new boolean [][]{
103
- {true , true , true , true , true },
104
- {true , false , false , false , true },
105
- {true , false , true , false , true },
106
- {true , false , false , false , true },
107
- {true , true , true , true , true }
102
+ new boolean [][]{
103
+ {true , true , true , true , true },
104
+ {true , false , false , false , true },
105
+ {true , false , true , false , true },
106
+ {true , false , false , false , true },
107
+ {true , true , true , true , true }
108
108
}));
109
109
110
110
// WHEN
@@ -120,12 +120,12 @@ void shouldFindPathWhenCircleHasHole() {
120
120
Jps sut =
121
121
new Jps (
122
122
Map .fromBooleanArray (
123
- new boolean [][]{
124
- {true , true , true , true , true },
125
- {true , false , false , false , true },
126
- {true , false , true , false , true },
127
- {true , false , false , true , true },
128
- {true , true , true , true , true }
123
+ new boolean [][]{
124
+ {true , true , true , true , true },
125
+ {true , false , false , false , true },
126
+ {true , false , true , false , true },
127
+ {true , false , false , true , true },
128
+ {true , true , true , true , true }
129
129
}));
130
130
131
131
// WHEN
@@ -146,8 +146,8 @@ public boolean isWalkable(int x, int y) {
146
146
return y >= 0
147
147
&& y <= 999
148
148
&& (x == 0 && y % 4 == 1
149
- || x == 999 && y % 4 == 3
150
- || y % 2 == 0 && x >= 0 && x <= 999 );
149
+ || x == 999 && y % 4 == 3
150
+ || y % 2 == 0 && x >= 0 && x <= 999 );
151
151
}
152
152
153
153
@ Override
@@ -173,16 +173,16 @@ void shouldFindPathInDemoMap() throws IOException {
173
173
// GIVEN
174
174
BufferedImage image = ImageIO .read (JpsTest .class .getResourceAsStream ("/dungeon_map.bmp" ));
175
175
boolean [][] data = new boolean [image .getHeight ()][image .getWidth ()];
176
- for (int y = 0 ; y < image .getHeight (); y ++) {
177
- for (int x = 0 ; x < image .getWidth (); x ++) {
178
- data [y ][ x ] = image .getRGB (x , y ) == -1 ;
176
+ for (int x = 0 ; x < image .getWidth (); x ++) {
177
+ for (int y = 0 ; y < image .getHeight (); y ++) {
178
+ data [x ][ y ] = image .getRGB (x , y ) == -1 ;
179
179
}
180
180
}
181
181
Map map = Map .fromBooleanArray (data );
182
182
Jps sut = new Jps (map );
183
183
SplittableRandom rnd = new SplittableRandom (123456 );
184
184
Result result = null ;
185
- for (int i = 0 ; i < 1000 ; i ++) {
185
+ for (int i = 0 ; i < 500 ; i ++) {
186
186
Position start ;
187
187
do {
188
188
start = new Position (rnd .nextInt (image .getWidth ()), rnd .nextInt (image .getHeight ()));
@@ -201,14 +201,18 @@ void shouldFindPathInDemoMap() throws IOException {
201
201
new BufferedImage (image .getWidth (), image .getHeight (), BufferedImage .TYPE_3BYTE_BGR );
202
202
Graphics g = out .getGraphics ();
203
203
g .drawImage (image , 0 , 0 , null );
204
- g .setColor (Color .GREEN );
205
204
Position last = null ;
205
+ g .setColor (Color .GREEN );
206
206
for (Position p : result .path ) {
207
207
if (last != null ) {
208
208
g .drawLine (last .x , last .y , p .x , p .y );
209
209
}
210
210
last = p ;
211
211
}
212
+ g .setColor (Color .RED );
213
+ for (Position p : result .path ) {
214
+ g .drawLine (p .x , p .y , p .x , p .y );
215
+ }
212
216
ImageIO .write (out , "PNG" , new File ("build/map_with_path.png" ));
213
217
}
214
218
}
0 commit comments