8
8
import java .util .stream .Collectors ;
9
9
import java .util .stream .Stream ;
10
10
11
- public class Grid <T extends Comparable < T > > {
11
+ public class Grid <T > {
12
12
private final int rows ;
13
13
private final int cols ;
14
14
private final List <List <T >> grid ;
@@ -27,7 +27,7 @@ public Grid(int rows, int cols, int currentRow, int currentCol, List<List<T>> gr
27
27
this .grid = grid ;
28
28
}
29
29
30
- public static <T extends Comparable < T > > Grid <T > create (Stream <String > input , Function <String , List <T >> mapper ) {
30
+ public static <T > Grid <T > create (Stream <String > input , Function <String , List <T >> mapper ) {
31
31
final List <List <T >> grid = input .map (mapper ).collect (Collectors .toList ());
32
32
if (!grid .stream ().map (List ::size ).allMatch (size -> grid .get (0 ).size () == size )) {
33
33
throw new IllegalArgumentException ("Received grid had rows with varying columns - invalid" );
@@ -36,6 +36,18 @@ public static <T extends Comparable<T>> Grid<T> create(Stream<String> input, Fun
36
36
return new Grid <>(grid .size (), grid .get (0 ).size (), grid );
37
37
}
38
38
39
+ public static <T > Grid <T > emptyGrid (int rows , int cols , Supplier <T > initializer ) {
40
+ final List <List <T >> grid = new ArrayList <>();
41
+ for (int i = 0 ; i < rows ; i ++) {
42
+ grid .add (new ArrayList <>());
43
+ for (int j = 0 ; j < cols ; j ++) {
44
+ grid .get (i ).add (initializer .get ());
45
+ }
46
+ }
47
+
48
+ return new Grid <>(rows , cols , grid );
49
+ }
50
+
39
51
public int getRows () {
40
52
return rows ;
41
53
}
@@ -56,17 +68,44 @@ public Pair<Integer, Integer> getCurrentPos() {
56
68
return Pair .of (getCurrentRow (), getCurrentCol ());
57
69
}
58
70
59
- public T get ( ) {
60
- return grid . get (currentRow ). get ( currentCol );
71
+ public Grid < T > set ( T value ) {
72
+ return set (currentRow , currentCol , value );
61
73
}
62
74
63
- public Grid <T > set (T value ) {
64
- grid .get (currentRow ).set (currentCol , value );
75
+ public Grid <T > set (Pair <Integer , Integer > pos , T value ) {
76
+ return set (pos .first (), pos .second (), value );
77
+ }
78
+
79
+ public Grid <T > set (int row , int col , T value ) {
80
+ grid .get (row ).set (col , value );
65
81
return this ;
66
82
}
67
83
68
- public T get (int row , int col ) {
69
- return grid .get (row ).get (col );
84
+ public Pair <Integer , Integer > locateWrappedPosition (int targetRow , int targetCol ) {
85
+ int wrappedRow = targetRow ;
86
+ int wrappedCol = targetCol ;
87
+
88
+ if (wrappedRow < 0 ) {
89
+ wrappedRow = (wrappedRow % rows + rows ) % rows ;
90
+ } else if (wrappedRow >= rows ) {
91
+ wrappedRow = wrappedRow % rows ;
92
+ }
93
+
94
+ if (wrappedCol < 0 ) {
95
+ wrappedCol = (wrappedCol % cols + cols ) % cols ;
96
+ } else if (wrappedCol >= cols ) {
97
+ wrappedCol = wrappedCol % cols ;
98
+ }
99
+
100
+ return Pair .of (wrappedRow , wrappedCol );
101
+ }
102
+
103
+ public T get () {
104
+ return get (currentRow , currentCol );
105
+ }
106
+
107
+ public T getOrNull (Pair <Integer , Integer > pos ) {
108
+ return getOrNull (pos .first (), pos .second ());
70
109
}
71
110
72
111
public T getOrNull (int row , int col ) {
@@ -77,6 +116,14 @@ public T getOrNull(int row, int col) {
77
116
}
78
117
}
79
118
119
+ public T get (Pair <Integer , Integer > pos ) {
120
+ return get (pos .first (), pos .second ());
121
+ }
122
+
123
+ public T get (int row , int col ) {
124
+ return grid .get (row ).get (col );
125
+ }
126
+
80
127
public Grid <T > move (int row , int col ) {
81
128
if (row < 0 || row >= rows || col < 0 || col >= cols ) {
82
129
throw new IllegalArgumentException ("Tried to move current cell beyond boundaries" );
@@ -203,10 +250,6 @@ public <R extends Comparable<R>> Grid<R> map(Function<PositionContext<T>, R> map
203
250
return output ;
204
251
}
205
252
206
- public List <List <T >> getBackingList () {
207
- return copy ().grid ;
208
- }
209
-
210
253
public Grid <T > copy () {
211
254
List <List <T >> copy = new ArrayList <>(rows );
212
255
@@ -225,6 +268,31 @@ public boolean contains(Pair<Integer, Integer> position) {
225
268
return position .first () >= 0 && position .first () < rows && position .second () >= 0 && position .second () < cols ;
226
269
}
227
270
271
+ public List <Grid <T >> getQuadrants () {
272
+ int midRow = rows / 2 , midCol = cols / 2 ;
273
+
274
+ Grid <T > q1 = extractSubgrid (0 , midRow , 0 , midCol );
275
+ Grid <T > q2 = extractSubgrid (0 , midRow , midCol + 1 , cols );
276
+ Grid <T > q3 = extractSubgrid (midRow + 1 , rows , 0 , midCol );
277
+ Grid <T > q4 = extractSubgrid (midRow + 1 , rows , midCol + 1 , cols );
278
+
279
+ return List .of (q1 , q2 , q3 , q4 );
280
+ }
281
+
282
+ private Grid <T > extractSubgrid (int startRow , int endRow , int startCol , int endCol ) {
283
+ Grid <T > subgrid = Grid .emptyGrid (endRow - startRow , endCol - startCol , () -> null );
284
+ for (int i = startRow ; i < endRow ; i ++) {
285
+ for (int j = startCol ; j < endCol ; j ++) {
286
+ subgrid .set (i - startRow , j - startCol , get (i , j ));
287
+ }
288
+ }
289
+ return subgrid ;
290
+ }
291
+
292
+ public List <List <T >> getBackingList () {
293
+ return grid ;
294
+ }
295
+
228
296
@ Override
229
297
public String toString () {
230
298
StringBuilder builder = new StringBuilder ("Grid {\n " +
0 commit comments