99import java .awt .event .MouseMotionListener ;
1010import java .io .IOException ;
1111import java .nio .file .StandardOpenOption ;
12- import java .util .ArrayList ;
13- import java .util .Collections ;
14- import java .util .List ;
12+ import java .util .*;
1513import java .util .stream .IntStream ;
1614
1715import com .google .common .annotations .VisibleForTesting ;
2018import logic .TileType ;
2119import logic .Controller ;
2220import logic .LevelFile ;
23- import one .util .streamex .IntStreamEx ;
2421
2522public class Editor extends JFrame implements MouseListener , MouseMotionListener {
2623
@@ -100,25 +97,15 @@ public Editor (int rowCount, int columnCount, String name) throws IOException {
10097 @ Override
10198 public void actionPerformed (ActionEvent e ) {
10299
103- int workerCount = 0 ;
104- int boxCount = 0 ;
105- int targetCount = 0 ;
106- for (int i = 0 ; i < rowCount * columnCount ; i ++) {
107- int c = i / columnCount ;
108- int l = i % columnCount ;
109- TileType end_content = controller .warehouse .getCase (c , l ).getContent ();
110- if (end_content == TileType .WORKER_ON_FLOOR || end_content == TileType .WORKER_IN_STORAGE_AREA ) {
111- workerCount ++;
112- }
113- if (end_content == TileType .UNSTORED_BOX || end_content == TileType .STORED_BOX ) {
114- boxCount ++;
115- }
116- if (end_content == TileType .WORKER_IN_STORAGE_AREA || end_content == TileType .STORAGE_AREA || end_content == TileType .STORED_BOX ) {
117- targetCount ++;
118- }
100+ int [] tileCounts = new int [TileType .values ().length ];
119101
120- }
121- if (workerCount == 1 && targetCount >= boxCount && boxCount > 0 ) {
102+ IntStream .range (0 , rowCount * columnCount ).forEach (i -> {
103+ int c = i / columnCount ;
104+ int l = i % columnCount ;
105+ TileType tileType = controller .warehouse .getCase (c , l ).getContent ();
106+ tileCounts [tileType .ordinal ()]++;
107+ });
108+ if (isValidLevel (tileCounts )) {
122109 String line = "" ;
123110 for (int i = 0 ; i < rowCount * columnCount ; i ++) {
124111
@@ -323,4 +310,25 @@ public void mouseEntered(MouseEvent e) {}
323310
324311 @ Override
325312 public void mouseExited (MouseEvent e ) {}
313+
314+ private boolean isValidLevel (int [] tileCounts ) {
315+ int workersFound = tileCounts [TileType .WORKER_ON_FLOOR .ordinal ()]
316+ + tileCounts [TileType .WORKER_IN_STORAGE_AREA .ordinal ()];
317+ if (workersFound != 1 ) {
318+ return false ;
319+ }
320+
321+ int boxesFound = tileCounts [TileType .UNSTORED_BOX .ordinal ()]
322+ + tileCounts [TileType .STORED_BOX .ordinal ()];
323+ if (boxesFound == 0 ) {
324+ return false ;
325+ }
326+
327+ // check if there are enough targets for all boxes
328+ int targetsFound = tileCounts [TileType .WORKER_IN_STORAGE_AREA .ordinal ()]
329+ + tileCounts [TileType .STORAGE_AREA .ordinal ()]
330+ + tileCounts [TileType .STORED_BOX .ordinal ()];
331+
332+ return targetsFound >= boxesFound ;
333+ }
326334}
0 commit comments