aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv/java/wallj/viewer/Main.java
blob: 01c621145dbc6609c28f74e0e5ef645bcfdaa023 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package fr.umlv.java.wallj.viewer;

import fr.umlv.java.wallj.board.Board;
import fr.umlv.java.wallj.board.BoardParser;
import fr.umlv.java.wallj.board.BoardValidator;
import fr.umlv.zen5.Application;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Main {

  private static final String DEFAULT_MAP_NAME = "/maps/level0.txt";
  private static java.util.List<Path> getResourcePaths(String[] args) throws URISyntaxException,IOException {
    LinkedList<Path> paths = new LinkedList<>();
    String strFolder = System.getProperty("levels");
    if (strFolder != null) {
      Path pathFolder = Paths.get(strFolder);
      Files.newDirectoryStream(pathFolder).forEach(paths::add);
    } else {
      final URI uri = Main.class.getResource(DEFAULT_MAP_NAME).toURI();
      Map<String, String> env = new HashMap<>();
      env.put("create", "true");
      FileSystem zipfs = FileSystems.newFileSystem(uri, env);
      paths.add(Paths.get(uri));
    }
    return paths;
  }

  public static void main(String[] args) {
    List<Board> boards = new LinkedList<>();
    try {
      List<Path> boardPaths = Main.getResourcePaths(args);
      for (Path path : boardPaths) {
        BoardValidator boardValidator = new BoardValidator(BoardParser.parse(path));
        boards.add(
         boardValidator.validate(BoardValidator.Constraint::hasActualReachableBlocks, "Some supposed reachable blocks are not reachable.")
          .validate(BoardValidator.Constraint::hasMandatoryBlocks, "Some mandatory blocks are missing.")
          .validate(BoardValidator.Constraint::isBounded, "The board is not correctly bounded.")
          .validate(BoardValidator.Constraint::isHollow, "The board must have a unique and simple interior.")
          .get());
      }
      Viewer viewer = new Viewer(boards);
      Application.run(Color.WHITE, viewer::eventLoop);
    } catch (URISyntaxException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(2);
    } catch (BoardValidator.ValidationException e) {
      for (Throwable throwable : e.getSuppressed()) {
        System.err.println(throwable.getMessage());
      }
      System.exit(3);
    }
  }
}