示例#1
0
  public TextPoint initFromConfig(String filename)
      throws IOException, FieldInitException, OutOfFieldException, SnakeOnWallException,
          SnakeCollisionException, TeleportInitException {
    Charset ENCODING = StandardCharsets.UTF_8;
    Path path = Paths.get(filename);
    List<String> tmpField = Files.readAllLines(path, ENCODING);

    HashMap<String, String> gameParams = new HashMap<String, String>();
    for (String line : tmpField) {
      String param, value = null;
      String[] tmp = line.split("=");
      param = tmp[0].trim();
      if (tmp.length > 1) {
        value = tmp[1].trim();
      }
      gameParams.put(param, value);
    }

    String type = gameParams.get("type");
    if ("MULTI".equals(type)) {
      setType(GameType.MULTI);
    } else if ("MULTI_BATTLE".equals(type)) {
      setType(GameType.MULTI_BATTLE);
    }

    TextPoint fieldSize = initField(gameParams.get("field_path"));

    String[] snake = gameParams.get("snake_1").split(",");
    TextPoint head = new TextPoint(Integer.valueOf(snake[0]), Integer.valueOf(snake[1]));
    Direction dir = textToDirection(snake[1]);
    int size = Integer.valueOf(snake[3]);
    initSnake(1, head, dir, size);
    if (gameType != GameType.SINGLE) {
      snake = gameParams.get("snake_2").split(",");
      head = new TextPoint(Integer.valueOf(snake[0]), Integer.valueOf(snake[1]));
      dir = textToDirection(snake[2]);
      size = Integer.valueOf(snake[3]);
      initSnake(2, head, dir, size);
    }

    String[] teleports = gameParams.get("teleports").split("\\|");
    for (int i = 0; i < teleports.length; i++) {
      String[] ports = teleports[i].split(";");
      HashMap<TextPoint, Direction> p = new HashMap<TextPoint, Direction>();
      for (int j = 0; j < ports.length; j++) {
        String[] port = ports[j].split(",");
        p.put(
            new TextPoint(Integer.valueOf(port[0]), Integer.valueOf(port[1])),
            textToDirection(port[2]));
      }
      initPorts(p);
    }

    addStars(Integer.valueOf(gameParams.get("stars")));

    return fieldSize;
  }
示例#2
0
  public void move()
      throws SnakeOnWallException, OutOfFieldException, SnakeAddException, SnakeCollisionException {
    // TODO: re-factor, too messy
    for (int j = 0; j < snake.length; j++) {
      if (gameType == GameType.SINGLE && j >= 1) {
        break;
      }
      try {
        snake[j].move();
      } catch (SnakeCollisionException e) {
        if (gameType == GameType.MULTI_BATTLE) {
          // TODO: choose direction randomly
          initSnake(j + 1, getEmptyPoint(), Direction.RIGHT, 1);
          continue;
        } else {
          throw e;
        }
      }
      TextPoint head = snake[j].getHead();
      if (field.isWall(head) && gameType != GameType.MULTI_BATTLE) {
        throw new SnakeOnWallException("Snake Head is on the Wall");
      } else if (field.isWall(head) && gameType == GameType.MULTI_BATTLE) {
        initSnake(j + 1, getEmptyPoint(), Direction.RIGHT, 1);
        continue;
      }
      if (stars.isStar(head)) {
        snake[j].add();
        stars.remove(head);
        score[j]++;
      }
      for (int i = 0; i < ports.length; i++) {
        if (ports[i] != null && ports[i].isPort(head)) {
          ports[i].updateHead(snake[j]);
          break;
        }
      }
    }

    if (gameType != GameType.SINGLE) {
      boolean isClash =
          snake[0].isOnSnake(snake[1].getHead()) || snake[1].isOnSnake(snake[0].getHead());
      boolean isNotBattle = gameType == GameType.MULTI;
      if (isClash) {
        // eat other snake tail or restart smaller snake on clash (if
        // battle)
        if (isNotBattle) {
          throw new SnakeCollisionException("Snakes clash");
        } else if (snake[0].getSize() == 1 && snake[1].getSize() == 1) {
          initSnake(1, getEmptyPoint(), Direction.RIGHT, 1);
          initSnake(2, getEmptyPoint(), Direction.RIGHT, 1);
        } else if (snake[0].isTail(snake[1].getHead())) {
          if (snake[0].getSize() == 1) {
            initSnake(1, getEmptyPoint(), Direction.RIGHT, 1);
          } else {
            snake[0].cutTail();
          }
          score[1]++;
          snake[1].add();
        } else if (snake[1].isTail(snake[0].getHead())) {
          if (snake[1].getSize() == 1) {
            initSnake(2, getEmptyPoint(), Direction.RIGHT, 1);
          } else {
            snake[1].cutTail();
          }
          score[0]++;
          snake[0].add();
        } else if (snake[0].getSize() > snake[1].getSize()) {
          initSnake(2, getEmptyPoint(), Direction.RIGHT, 1);
        } else if (snake[0].getSize() < snake[1].getSize()) {
          initSnake(1, getEmptyPoint(), Direction.RIGHT, 1);
        } else if (snake[0].getSize() == snake[1].getSize()) {
          initSnake(1, getEmptyPoint(), Direction.RIGHT, 1);
          initSnake(2, getEmptyPoint(), Direction.RIGHT, 1);
        }
      }
    }
  }