@@ -22,7 +22,6 @@ public class Computer {
2222
2323 private Set <Coordinates > possibleKick = new HashSet <>();
2424 private Set <Coordinates > possibleMoves = new HashSet <>();
25- private Set <Coordinates > possibleKickAndNotIsEnemyKickMe = new HashSet <>();
2625
2726 private BoardPoint boardPoint = new BoardPoint ();
2827
@@ -82,7 +81,6 @@ public void getGameData() {
8281
8382 possibleMoves .clear ();
8483 possibleKick .clear ();
85- possibleKickAndNotIsEnemyKickMe .clear ();
8684
8785 for (Map .Entry <Coordinates , PawnClass > entry : cacheBoard .entrySet ()) {
8886 if (entry .getValue ().getColor ().isBlack ()) {
@@ -107,28 +105,23 @@ public Coordinates choosePawn() {
107105 }
108106
109107 private Coordinates choosePawnEasy () {
110- Object [] object = null ;
111-
112108 if (possibleMoves .size () > 0 ) {
113- object = possibleMoves . toArray ( );
109+ return selectRandom ( possibleMoves );
114110 } else if (possibleKick .size () > 0 ) {
115- object = possibleKick .toArray ();
116-
111+ return selectRandom (possibleKick );
117112 }
118113
119- return ( Coordinates ) object [ random . nextInt ( object . length )] ;
114+ return null ;
120115 }
121116
122117 private Coordinates choosePawnNormal () {
123- Object [] object = null ;
124-
125118 if (possibleKick .size () > 0 ) {
126- object = possibleKick . toArray ( );
119+ return selectRandom ( possibleKick );
127120 } else if (possibleMoves .size () > 0 ) {
128- object = possibleMoves . toArray ( );
121+ return selectRandom ( possibleMoves );
129122 }
130123
131- return ( Coordinates ) object [ random . nextInt ( object . length )] ;
124+ return null ;
132125 }
133126
134127 private Coordinates choosePawnHard () {
@@ -141,47 +134,30 @@ private Coordinates choosePawnHard() {
141134 Set <Coordinates > cachePossiblePawn = new HashSet <>();
142135
143136 for (Coordinates coordinates : cachePawn ) {
144-
145137 PawnMoves moves = new PawnMoves (Board .getPawn (coordinates ), coordinates );
146138
147139 Set <Coordinates > cacheMoves = new HashSet <>();
148140 cacheMoves .addAll (moves .getPossibleKick ());
149141 cacheMoves .addAll (moves .getPossibleMoves ());
150142
151- for (Coordinates moveCoordinates : cacheMoves ) {
152- PawnClass oldPawn = Board .addPawnWithoutDesign (moveCoordinates , Board .getPawn (coordinates ));
153-
154- int point = boardPoint .calculateBoard ();
155-
156- if (point > minNumber ) {
157- minNumber = point ;
158- }
159-
160- Board .removePawnWithoutDesign (moveCoordinates );
143+ int point = getMinNumber (cacheMoves , Board .getPawn (coordinates ));
161144
162- if (oldPawn != null ) {
163- Board .addPawnWithoutDesign (moveCoordinates , oldPawn );
164- }
145+ if (point > minNumber ) {
146+ minNumber = point ;
165147 }
148+ }
166149
167- for (Coordinates moveCoordinates : cacheMoves ) {
168- PawnClass oldPawn = Board .addPawnWithoutDesign (moveCoordinates , Board .getPawn (coordinates ));
169-
170- int point = boardPoint .calculateBoard ();
171-
172- if (point == minNumber ) {
173- cachePossiblePawn .add (coordinates );
174- }
150+ for (Coordinates coordinates : cachePawn ) {
151+ PawnMoves moves = new PawnMoves (Board .getPawn (coordinates ), coordinates );
175152
176- Board .removePawnWithoutDesign (moveCoordinates );
153+ Set <Coordinates > cacheMoves = new HashSet <>();
154+ cacheMoves .addAll (moves .getPossibleKick ());
155+ cacheMoves .addAll (moves .getPossibleMoves ());
177156
178- if (oldPawn != null ) {
179- Board .addPawnWithoutDesign (moveCoordinates , oldPawn );
180- }
181- }
157+ cachePossiblePawn .addAll (getListWithOnlyMinNumber (cacheMoves , Board .getPawn (coordinates ), minNumber ));
182158 }
183159
184- return ( Coordinates ) cachePossiblePawn . toArray ()[ random . nextInt ( cachePossiblePawn . size ())] ;
160+ return selectRandom ( cachePossiblePawn ) ;
185161 }
186162
187163 public Coordinates chooseMove (Coordinates coordinates ) {
@@ -197,11 +173,9 @@ private Coordinates chooseMoveEasy(Coordinates coordinates) {
197173 PawnMoves moves = new PawnMoves (pawn , coordinates );
198174
199175 if (moves .getPossibleMoves ().size () > 0 ) {
200- Object [] object = moves .getPossibleMoves ().toArray ();
201- return (Coordinates ) object [random .nextInt (object .length )];
176+ return selectRandom (moves .getPossibleMoves ());
202177 } else if (moves .getPossibleKick ().size () > 0 ) {
203- Object [] object = moves .getPossibleKick ().toArray ();
204- return (Coordinates ) object [random .nextInt (object .length )];
178+ return selectRandom (moves .getPossibleKick ());
205179 }
206180
207181 return null ;
@@ -212,11 +186,9 @@ private Coordinates chooseMoveNormal(Coordinates coordinates) {
212186 PawnMoves moves = new PawnMoves (pawn , coordinates );
213187
214188 if (moves .getPossibleKick ().size () > 0 ) {
215- Object [] object = moves .getPossibleKick ().toArray ();
216- return (Coordinates ) object [random .nextInt (object .length )];
189+ return selectRandom (moves .getPossibleKick ());
217190 } else if (moves .getPossibleMoves ().size () > 0 ) {
218- Object [] object = moves .getPossibleMoves ().toArray ();
219- return (Coordinates ) object [random .nextInt (object .length )];
191+ return selectRandom (moves .getPossibleMoves ());
220192 }
221193
222194 return null ;
@@ -226,51 +198,63 @@ private Coordinates chooseMoveHard(Coordinates coordinates) {
226198 PawnClass pawn = Board .getPawn (coordinates );
227199 PawnMoves moves = new PawnMoves (pawn , coordinates );
228200
229- Object [] object = null ;
201+ Set <Coordinates > possibleMove = new HashSet <>();
202+ possibleMove .addAll (moves .getPossibleMoves ());
203+ possibleMove .addAll (moves .getPossibleKick ());
204+
205+ int minNumber = getMinNumber (possibleMove , pawn );
206+
207+ Set <Coordinates > test = getListWithOnlyMinNumber (possibleMove , pawn , minNumber );
208+
209+ return selectRandom (test );
210+ }
211+
212+ private int getMinNumber (Set <Coordinates > list , PawnClass actualPawn ) {
213+ int minNumber = -10000 ;
230214
231- if ( moves . getPossibleKick (). size () > 0 ) {
232- moves . getPossibleKick (). forEach ( entry -> checkEnemyKickField ( entry , pawn ) );
215+ for ( Coordinates coordinates : list ) {
216+ PawnClass oldPawn = Board . addPawnWithoutDesign ( coordinates , actualPawn );
233217
234- if ( possibleKickAndNotIsEnemyKickMe . size () > 0 ) {
235- object = possibleKickAndNotIsEnemyKickMe . toArray ();
236- } else {
237- object = moves . getPossibleKick (). toArray () ;
218+ int point = boardPoint . calculateBoard ();
219+
220+ if ( point > minNumber ) {
221+ minNumber = point ;
238222 }
239223
240- return (Coordinates ) object [random .nextInt (object .length )];
241- } else if (moves .getPossibleMoves ().size () > 0 ) {
242- object = moves .getPossibleMoves ().toArray ();
243- return (Coordinates ) object [random .nextInt (object .length )];
224+ Board .removePawnWithoutDesign (coordinates );
225+
226+ if (oldPawn != null ) {
227+ Board .addPawnWithoutDesign (coordinates , oldPawn );
228+ }
244229 }
245230
246- return null ;
231+ return minNumber ;
247232 }
248233
249- public Coordinates selectRandom (Set <Coordinates > list ) {
250- Object [] object = list .toArray ();
251- return (Coordinates ) object [random .nextInt (object .length )];
252- }
234+ private Set <Coordinates > getListWithOnlyMinNumber (Set <Coordinates > list , PawnClass actualPawn , int minNumber ) {
235+ Set <Coordinates > returnList = new HashSet <>();
253236
254- private void checkEnemyKickField (Coordinates coordinates , PawnClass actualPawn ) {
255- PawnClass oldPawn = Board .addPawnWithoutDesign (coordinates , actualPawn );
237+ for (Coordinates coordinates : list ) {
238+ PawnClass oldPawn = Board .addPawnWithoutDesign (coordinates , actualPawn );
256239
257- Set < Coordinates > possibleEnemyKick = new HashSet <> ();
240+ int point = boardPoint . calculateBoard ();
258241
259- for (Map .Entry <Coordinates , PawnClass > entry : Board .getBoard ().entrySet ()) {
260- if (!Board .isThisSameColor (entry .getKey (), actualPawn .getColor ()) && !entry .getValue ().getPawn ().isKing ()) {
261- PawnMoves moves = new PawnMoves (entry .getValue (), entry .getKey ());
262- possibleEnemyKick .addAll (moves .getPossibleKick ());
242+ if (point == minNumber ) {
243+ returnList .add (coordinates );
263244 }
264- }
265245
266- Board .removePawnWithoutDesign (coordinates );
246+ Board .removePawnWithoutDesign (coordinates );
267247
268- if (oldPawn != null ) {
269- Board .addPawnWithoutDesign (coordinates , oldPawn );
248+ if (oldPawn != null ) {
249+ Board .addPawnWithoutDesign (coordinates , oldPawn );
250+ }
270251 }
271252
272- if (!possibleEnemyKick .contains (coordinates )) {
273- possibleKickAndNotIsEnemyKickMe .add (coordinates );
274- }
253+ return returnList ;
254+ }
255+
256+ public Coordinates selectRandom (Set <Coordinates > list ) {
257+ Object [] object = list .toArray ();
258+ return (Coordinates ) object [random .nextInt (object .length )];
275259 }
276260}
0 commit comments