11package logic ;
2+
23import org .jspecify .annotations .NullMarked ;
34
45import java .io .IOException ;
89import java .nio .file .Paths ;
910import java .util .ArrayList ;
1011import java .util .List ;
12+ import java .util .stream .IntStream ;
1113
1214import 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