Skip to content

Commit 1288685

Browse files
author
Arun Prasaad
committed
Simplify level grid saving to file
1 parent 570701b commit 1288685

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

src/main/java/ihm/Editor.java

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.awt.event.MouseListener;
99
import java.awt.event.MouseMotionListener;
1010
import java.io.IOException;
11-
import java.nio.file.StandardOpenOption;
1211
import java.util.*;
1312
import java.util.stream.IntStream;
1413

@@ -93,36 +92,15 @@ public Editor (int rowCount, int columnCount, String name) throws IOException {
9392
createQuitButton();
9493
JButton save = createSaveButton();
9594

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+
});
126104

127105
JPanel sokobanPanel = new SokobanPanel(controller);
128106
sokobanPanel.setName(Component.SOKOBAN_PANEL.name());
@@ -311,17 +289,39 @@ public void mouseExited(MouseEvent e) {}
311289
*/
312290
private boolean isValidLevel(int rowCount, int columnCount) {
313291
int[] tileCounts = new int[TileType.values().length];
314-
292+
315293
IntStream.range(0, rowCount * columnCount).forEach(i -> {
316294
int c = i / columnCount;
317295
int l = i % columnCount;
318296
TileType tileType = controller.warehouse.getCase(c, l).getContent();
319297
tileCounts[tileType.ordinal()]++;
320298
});
321-
299+
322300
return isValidLevel(tileCounts);
323301
}
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+
325325
/**
326326
* Validates the level based on tile counts.
327327
* @param tileCounts Array containing counts of each tile type

0 commit comments

Comments
 (0)