Skip to content

Commit f7ec0a8

Browse files
author
Arun Prasaad
committed
Simplify level reading logic
1 parent 8d0d9d6 commit f7ec0a8

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

src/main/java/logic/TileType.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package logic;
22

3+
import java.util.Arrays;
4+
import java.util.Optional;
5+
36
public enum TileType {
47
FLOOR('#'),
58
WALL('M'),
@@ -32,4 +35,18 @@ public String codeAsString() {
3235
return String.valueOf(code);
3336
}
3437

38+
/**
39+
* Returns an Optional containing the TileType corresponding to the given character code.
40+
* The character code should match one of the tile type codes defined in this enum.
41+
*
42+
* @param code The character code to look up
43+
* @return An Optional containing the matching TileType if found, or an empty Optional if no match is found
44+
*/
45+
public static Optional<TileType> fromCode(char code) {
46+
return Arrays
47+
.stream(TileType.values())
48+
.filter(type -> type.getCode() == code)
49+
.findFirst();
50+
}
51+
3552
}

src/main/java/logic/Warehouse.java

Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package logic;
2+
23
import org.jspecify.annotations.NullMarked;
34

45
import java.io.IOException;
@@ -8,6 +9,7 @@
89
import java.nio.file.Paths;
910
import java.util.ArrayList;
1011
import java.util.List;
12+
import java.util.stream.IntStream;
1113

1214
import static com.google.common.base.Preconditions.checkState;
1315

@@ -18,9 +20,9 @@ public class Warehouse {
1820

1921
private final List<Cell> cells = new ArrayList<>();
2022

21-
public Warehouse(String path_to_level, Worker worker) {
23+
public Warehouse(String path_to_level, Worker worker) {
2224

23-
Path file = Paths.get(path_to_level);
25+
Path file = Paths.get(path_to_level);
2426
List<String> linesFromFile;
2527
try {
2628
linesFromFile = Files.readAllLines(file, StandardCharsets.UTF_8);
@@ -34,45 +36,31 @@ public Warehouse(String path_to_level, Worker worker) {
3436
this.columns = linesFromFile.getFirst().length();
3537
checkState(this.columns >= 5);
3638

37-
parseLevel(worker, linesFromFile);
38-
}
39-
40-
private void parseLevel(Worker worker, List<String> linesFromFile) {
41-
for (int i = 0; i<this.lines; i++) {
42-
for(int j = 0; j<this.columns; j++) {
43-
44-
switch (Character.toString(linesFromFile.get(i).charAt(j))) {
45-
case "_" ->
46-
cells.add(new Cell(i, j, TileType.OUTSIDE, this));
47-
case "M" ->
48-
cells.add(new Cell(i, j, TileType.WALL, this));
49-
case "#" ->
50-
cells.add(new Cell(i, j, TileType.FLOOR, this));
51-
case "T" ->
52-
cells.add(new Cell(i, j, TileType.STORAGE_AREA, this));
53-
case "G" -> {
54-
cells.add(new Cell(i, j, TileType.WORKER_ON_FLOOR, this));
55-
worker.moveTo(i, j);
56-
}
57-
case "C" ->
58-
cells.add(new Cell(i, j, TileType.UNSTORED_BOX, this));
59-
case "B" -> {
60-
cells.add(new Cell(i, j, TileType.WORKER_IN_STORAGE_AREA, this));
61-
worker.moveTo(i, j);
62-
}
63-
case "V" ->
64-
cells.add(new Cell(i, j, TileType.STORED_BOX, this));
65-
66-
default -> throw new RuntimeException("Invalid tile type: " + linesFromFile.get(i).charAt(j));
67-
}
68-
69-
}
70-
}
71-
}
72-
73-
74-
public Cell getCell(int l, int c) {
75-
return cells.get(l*this.columns + c);
39+
parseLevel(worker, linesFromFile);
40+
}
41+
42+
private void parseLevel(Worker worker, List<String> linesFromFile) {
43+
IntStream.range(0, this.lines)
44+
.forEach(i -> IntStream.range(0, this.columns)
45+
.forEach(j -> {
46+
char cellChar = linesFromFile.get(i).charAt(j);
47+
48+
TileType.fromCode(cellChar).ifPresentOrElse(
49+
type -> {
50+
cells.add(new Cell(i, j, type, this));
51+
if (type == TileType.WORKER_ON_FLOOR || type == TileType.WORKER_IN_STORAGE_AREA) {
52+
worker.moveTo(i, j);
53+
}
54+
},
55+
() -> { throw new RuntimeException("Invalid tile type: " + cellChar); }
56+
);
57+
})
58+
);
59+
}
60+
61+
62+
public Cell getCell(int l, int c) {
63+
return cells.get(l * this.columns + c);
7664
}
7765

7866

@@ -81,11 +69,11 @@ public boolean checkVictory() {
8169
}
8270

8371
public int getLines() {
84-
return lines;
72+
return lines;
8573
}
8674

8775
public int getColumns() {
88-
return columns;
76+
return columns;
8977
}
9078

9179
}

0 commit comments

Comments
 (0)