Skip to content

Commit 6d835ef

Browse files
author
Arun Prasaad
committed
Make new level initial state more user-friendly
1 parent 182785f commit 6d835ef

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

src/main/java/ihm/Editor.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import java.awt.event.MouseMotionListener;
1010
import java.io.IOException;
1111
import java.nio.file.StandardOpenOption;
12+
import java.util.ArrayList;
13+
import java.util.Collections;
14+
import java.util.List;
15+
import java.util.stream.IntStream;
1216

1317
import com.google.common.annotations.VisibleForTesting;
1418
import javax.swing.*;
@@ -153,12 +157,18 @@ else if (line.length() == columnCount) {
153157
}
154158

155159
private void initializeEmptyLevel(int rowCount, int columnCount) {
156-
String tileCode = "" + TileType.OUTSIDE.getCode();
157-
String content = IntStreamEx
158-
.range(rowCount)
159-
.mapToObj(_ -> tileCode.repeat(columnCount))
160-
.joining(System.lineSeparator());
161-
levelFile.write(content);
160+
final var wall = TileType.WALL.codeAsString();
161+
String wallRow = wall.repeat(columnCount);
162+
String middleRow = wall
163+
+ TileType.FLOOR.codeAsString().repeat(columnCount - 2)
164+
+ wall;
165+
166+
List<String> lines = new ArrayList<>();
167+
lines.add(wallRow);
168+
lines.addAll(Collections.nCopies(rowCount - 2, middleRow));
169+
lines.add(wallRow);
170+
171+
levelFile.write(String.join(System.lineSeparator(), lines));
162172
}
163173

164174
private void createBackButton() {

src/test/java/ihm/EditorTest.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.file.Path;
1717
import java.util.Arrays;
1818
import java.util.List;
19+
import java.util.stream.IntStream;
1920

2021
import static java.nio.file.Files.deleteIfExists;
2122
import static org.assertj.core.api.BDDAssertions.then;
@@ -313,31 +314,35 @@ ExitHandler defaultExitHandler() {
313314
* Represents expected positions in the test grid.
314315
*/
315316
@Test
316-
void a_new_level_is_created_with_all_tiles_set_to_outside() throws Exception {
317+
void a_new_level_is_created_with_walls_and_floor_spaces() throws Exception {
317318
// Given
318319
int testRows = 7;
319-
int testCols = 10;
320+
int testCols = 7;
320321
String testLevelName = "emptyGridTest";
321322
Path testLevelPath = Path.of("levels", testLevelName + ".txt");
322323

323324
try {
324325
// When
325-
Editor emptyEditor = new Editor(testRows, testCols, testLevelName);
326+
Editor newEditor = new Editor(testRows, testCols, testLevelName);
326327

327-
// Then - Verify the file was created with correct dimensions and content
328328
then(testLevelPath).exists();
329329
List<String> lines = Files.readAllLines(testLevelPath);
330330

331331
then(lines).hasSize(testRows);
332-
then(lines).allSatisfy(line -> {
333-
then(line.length()).isEqualTo(testCols);
334-
then(line).isEqualTo(TileType.OUTSIDE.codeAsString().repeat(testCols));
335-
});
336-
337-
// Clean up
338-
emptyEditor.dispose();
339-
} finally {
340-
// Ensure cleanup even if test fails
332+
333+
// First and last row should be all walls
334+
String wallRow = TileType.WALL.codeAsString().repeat(testCols);
335+
then(lines.getFirst()).isEqualTo(wallRow);
336+
then(lines.getLast()).isEqualTo(wallRow);
337+
338+
// Middle rows should have walls on the sides
339+
String middleRow = "%s%s%s".formatted(
340+
TileType.WALL.codeAsString(),
341+
TileType.FLOOR.codeAsString().repeat(testCols - 2),
342+
TileType.WALL.codeAsString());
343+
then(lines.subList(1, testRows - 1)).allSatisfy(line -> then(line).isEqualTo(middleRow));
344+
}
345+
finally {
341346
Files.deleteIfExists(testLevelPath);
342347
}
343348
}

0 commit comments

Comments
 (0)