|
8 | 8 | import java.awt.event.MouseListener; |
9 | 9 | import java.awt.event.MouseMotionListener; |
10 | 10 | import java.io.IOException; |
11 | | -import java.nio.file.StandardOpenOption; |
12 | 11 | import java.util.*; |
13 | 12 | import java.util.stream.IntStream; |
14 | 13 |
|
@@ -93,36 +92,15 @@ public Editor (int rowCount, int columnCount, String name) throws IOException { |
93 | 92 | createQuitButton(); |
94 | 93 | JButton save = createSaveButton(); |
95 | 94 |
|
96 | | - save.addActionListener(new ActionListener() { |
97 | | - @Override |
98 | | - public void actionPerformed(ActionEvent e) { |
99 | | - |
100 | | - if (isValidLevel(rowCount, columnCount)) { |
101 | | - String line = ""; |
102 | | - for (int i = 0; i < rowCount * columnCount; i++) { |
103 | | - |
104 | | - int c = i / columnCount; |
105 | | - int l = i % columnCount; |
106 | | - TileType endContent = controller.warehouse.getCase(c, l).getContent(); |
107 | | - line += endContent.getCode(); |
108 | | - |
109 | | - if (line.length() == columnCount && i+1 == columnCount) { |
110 | | - levelFile.write(line); |
111 | | - line = ""; |
112 | | - } |
113 | | - else if (line.length() == columnCount) { |
114 | | - levelFile.write(System.lineSeparator() + line, StandardOpenOption.APPEND); |
115 | | - line = ""; |
116 | | - } |
117 | | - } |
118 | | - dispose(); |
119 | | - new HomeWindow(); |
120 | | - } |
121 | | - else { |
122 | | - errorLabel.setText("Invalid level!"); |
123 | | - } |
124 | | - } |
125 | | - }); |
| 95 | + save.addActionListener(_ -> { |
| 96 | + if (isValidLevel(rowCount, columnCount)) { |
| 97 | + saveLevelToFile(rowCount, columnCount); |
| 98 | + dispose(); |
| 99 | + new HomeWindow(); |
| 100 | + } else { |
| 101 | + errorLabel.setText("Invalid level!"); |
| 102 | + } |
| 103 | + }); |
126 | 104 |
|
127 | 105 | JPanel sokobanPanel = new SokobanPanel(controller); |
128 | 106 | sokobanPanel.setName(Component.SOKOBAN_PANEL.name()); |
@@ -311,17 +289,39 @@ public void mouseExited(MouseEvent e) {} |
311 | 289 | */ |
312 | 290 | private boolean isValidLevel(int rowCount, int columnCount) { |
313 | 291 | int[] tileCounts = new int[TileType.values().length]; |
314 | | - |
| 292 | + |
315 | 293 | IntStream.range(0, rowCount * columnCount).forEach(i -> { |
316 | 294 | int c = i / columnCount; |
317 | 295 | int l = i % columnCount; |
318 | 296 | TileType tileType = controller.warehouse.getCase(c, l).getContent(); |
319 | 297 | tileCounts[tileType.ordinal()]++; |
320 | 298 | }); |
321 | | - |
| 299 | + |
322 | 300 | return isValidLevel(tileCounts); |
323 | 301 | } |
324 | | - |
| 302 | + |
| 303 | + /** |
| 304 | + * Saves the current level state to a file. |
| 305 | + * @param rowCount Number of rows in the level |
| 306 | + * @param columnCount Number of columns in the level |
| 307 | + */ |
| 308 | + private void saveLevelToFile(int rowCount, int columnCount) { |
| 309 | + StringBuilder levelContent = new StringBuilder(); |
| 310 | + |
| 311 | + IntStream.range(0, rowCount * columnCount).forEach(i -> { |
| 312 | + int c = i / columnCount; |
| 313 | + int l = i % columnCount; |
| 314 | + TileType tileType = controller.warehouse.getCase(c, l).getContent(); |
| 315 | + levelContent.append(tileType.getCode()); |
| 316 | + |
| 317 | + if ((i + 1) % columnCount == 0) { // the row is complete |
| 318 | + levelContent.append(System.lineSeparator()); |
| 319 | + } |
| 320 | + }); |
| 321 | + |
| 322 | + levelFile.write(levelContent.toString()); |
| 323 | + } |
| 324 | + |
325 | 325 | /** |
326 | 326 | * Validates the level based on tile counts. |
327 | 327 | * @param tileCounts Array containing counts of each tile type |
|
0 commit comments