@@ -21,7 +21,8 @@ public static class IllegalActionException extends Exception {
21
21
/**
22
22
* Construct an IllegalActionException, with no message.
23
23
*/
24
- public IllegalActionException () {}
24
+ public IllegalActionException () {
25
+ }
25
26
}
26
27
27
28
/**
@@ -63,27 +64,30 @@ public IllegalActionException() {}
63
64
/**
64
65
* This class can not be instantiated.
65
66
*/
66
- private ActionEncoder () {}
67
+ private ActionEncoder () {
68
+ }
67
69
68
70
/**
69
71
* Get the fringe indexes of the given game state's board, in a list sorted by x and then y.
72
+ *
70
73
* @param gameState the game state to get the fringe indexes from
71
74
* @return the list of ordered (x-precedence) fringe indexes of the game state
72
75
*/
73
76
private static List <Pos > fringeIndexes (GameState gameState ) {
74
77
Board board = gameState .board ();
75
78
return board .insertionPositions ().stream ()
76
- .sorted (Comparator .comparing (Pos ::x ).thenComparing (Pos ::y ))
77
- .toList ();
79
+ .sorted (Comparator .comparing (Pos ::x ).thenComparing (Pos ::y ))
80
+ .toList ();
78
81
}
79
82
80
83
/**
81
84
* Encode an action where the given tile is placed on the board of the given game state.
85
+ *
82
86
* @param gameState the initial game state
83
- * @param tile the tile to place
87
+ * @param tile the tile to place
84
88
* @return the new game state resulting from the action and the encoded action
85
89
*/
86
- public static StateAction withPlacedTile (GameState gameState , PlacedTile tile ){
90
+ public static StateAction withPlacedTile (GameState gameState , PlacedTile tile ) {
87
91
List <Pos > fringeIndexes = fringeIndexes (gameState );
88
92
//todo: check if the tile is in the fringe?
89
93
int indexToEncode = fringeIndexes .indexOf (tile .pos ()); // a number between 0 and 189
@@ -95,13 +99,15 @@ public static StateAction withPlacedTile(GameState gameState, PlacedTile tile){
95
99
96
100
/**
97
101
* Encode an action where the given occupant is placed on the board of the given game state.
102
+ *
98
103
* @param gameState the initial game state
99
- * @param occupant the occupant to place
104
+ * @param occupant the occupant to place
100
105
* @return the new game state resulting from the action and the encoded action
101
106
*/
102
107
public static StateAction withNewOccupant (GameState gameState , Occupant occupant ) {
103
108
// if the occupant is null, we encode 11111, which means that there is no occupant to place
104
- if (occupant == null ) return new StateAction (gameState .withNewOccupant (null ), Base32 .encodeBits5 (WITH_NO_OCCUPANT ));
109
+ if (occupant == null )
110
+ return new StateAction (gameState .withNewOccupant (null ), Base32 .encodeBits5 (WITH_NO_OCCUPANT ));
105
111
int kindToEncode = occupant .kind ().ordinal (); // a number between 0 and 1
106
112
int zoneToEncode = Zone .localId (occupant .zoneId ());
107
113
int toEncode = kindToEncode << WITH_NEW_OCCUPANT_KIND_SHIFT | zoneToEncode ;
@@ -110,14 +116,16 @@ public static StateAction withNewOccupant(GameState gameState, Occupant occupant
110
116
111
117
/**
112
118
* Encode an action where the given occupant is removed from the board of the given game state.
119
+ *
113
120
* @param gameState the initial game state
114
- * @param occupant the occupant to remove
121
+ * @param occupant the occupant to remove
115
122
* @return the new game state resulting from the action and the encoded action
116
123
*/
117
124
public static StateAction withOccupantRemoved (GameState gameState , Occupant occupant ) {
118
- if (occupant == null ) return new StateAction (gameState .withOccupantRemoved (null ), Base32 .encodeBits5 (WITH_NO_OCCUPANT ));
125
+ if (occupant == null )
126
+ return new StateAction (gameState .withOccupantRemoved (null ), Base32 .encodeBits5 (WITH_NO_OCCUPANT ));
119
127
List <Occupant > occupants = gameState .board ().occupants ().stream ()
120
- .sorted (Comparator .comparingInt (Occupant ::zoneId )).toList ();
128
+ .sorted (Comparator .comparingInt (Occupant ::zoneId )).toList ();
121
129
int indexToEncode = occupants .indexOf (occupant ); // a number between 0 and 24
122
130
return new StateAction (gameState .withOccupantRemoved (occupant ), Base32 .encodeBits5 (indexToEncode ));
123
131
}
@@ -126,8 +134,9 @@ public static StateAction withOccupantRemoved(GameState gameState, Occupant occu
126
134
* Decode and apply the given action encoded in Base32 to the given game state,
127
135
* throwing an IllegalActionException if the action is invalid.
128
136
* This method lets the caller handle the exception, to choose what to do in case of an invalid action.
137
+ *
129
138
* @param gameState the initial game state
130
- * @param action the Base32-code for the action to decode and apply
139
+ * @param action the Base32-code for the action to decode and apply
131
140
* @return the new game state resulting from the action and the decoded action
132
141
* @throws IllegalActionException if the action is invalid for the given game state
133
142
*/
@@ -147,7 +156,7 @@ private static StateAction decodeAndApplyWithException(GameState gameState, Stri
147
156
Pos pos = fringeIndexes .get (tileInFringeIdx );
148
157
Tile tile = gameState .tileToPlace ();
149
158
PlacedTile placedTile = new PlacedTile (
150
- tile , gameState .currentPlayer (), Rotation .ALL .get (rotationIdx ), pos
159
+ tile , gameState .currentPlayer (), Rotation .ALL .get (rotationIdx ), pos
151
160
);
152
161
Preconditions .checkValidAction (gameState .board ().canAddTile (placedTile ));
153
162
yield new StateAction (gameState .withPlacedTile (placedTile ), action );
@@ -160,19 +169,19 @@ private static StateAction decodeAndApplyWithException(GameState gameState, Stri
160
169
int localId = decoded & WITH_NEW_OCCUPANT_ZONE_MASK ;
161
170
Occupant .Kind kind = Occupant .Kind .ALL .get (kindIdx );
162
171
Occupant occupant = gameState .lastTilePotentialOccupants ().stream ()
163
- .filter (occ -> occ .kind () == kind && Zone .localId (occ .zoneId ()) == localId )
164
- .findFirst ()
165
- .orElseThrow (IllegalActionException ::new );
172
+ .filter (occ -> occ .kind () == kind && Zone .localId (occ .zoneId ()) == localId )
173
+ .findFirst ()
174
+ .orElseThrow (IllegalActionException ::new );
166
175
yield new StateAction (gameState .withNewOccupant (occupant ), action );
167
176
}
168
177
case RETAKE_PAWN -> {
169
178
Preconditions .checkValidAction (action .length () == WITH_OCCUPANT_REMOVED_ACTION_LENGTH );
170
179
int decoded = Base32 .decode (action );
171
180
if (decoded == WITH_NO_OCCUPANT ) yield new StateAction (gameState .withOccupantRemoved (null ), action );
172
181
List <Occupant > occupants = gameState .board ().occupants ()
173
- .stream ()
174
- .sorted (Comparator .comparingInt (Occupant ::zoneId ))
175
- .toList ();
182
+ .stream ()
183
+ .sorted (Comparator .comparingInt (Occupant ::zoneId ))
184
+ .toList ();
176
185
Preconditions .checkValidAction (occupants .size () > decoded );
177
186
Occupant occupant = occupants .get (decoded );
178
187
Preconditions .checkValidAction (occupant .kind () == Occupant .Kind .PAWN );
@@ -187,10 +196,11 @@ private static StateAction decodeAndApplyWithException(GameState gameState, Stri
187
196
188
197
/**
189
198
* Decode and apply the given action encoded in Base32 to the given game state.
199
+ *
190
200
* @param gameState the initial game state
191
- * @param action the Base32-code for the action to decode and apply
201
+ * @param action the Base32-code for the action to decode and apply
192
202
* @return the new game state resulting from the action and the decoded action,
193
- * or null if the action is invalid
203
+ * or null if the action is invalid
194
204
*/
195
205
public static StateAction decodeAndApply (GameState gameState , String action ) {
196
206
// we catch the exception and return null if the action is invalid
@@ -204,8 +214,10 @@ public static StateAction decodeAndApply(GameState gameState, String action) {
204
214
/**
205
215
* A record to represent a pair of a game state and an action, used in this program
206
216
* to return a game state resulting from an action and the Base32 encoded action itself.
217
+ *
207
218
* @param gameState the game state
208
- * @param action the action
219
+ * @param action the action
209
220
*/
210
- public record StateAction (GameState gameState , String action ) {}
221
+ public record StateAction (GameState gameState , String action ) {
222
+ }
211
223
}
0 commit comments