コード例 #1
0
ファイル: SaveLoad.java プロジェクト: uri/unicorn-shoppe
  /**
   * Creates the XML file for the game that should be used to keep track of everything.
   *
   * @throws ParserConfigurationException
   * @throws TransformerException
   * @throws IOException
   * @throws SAXException
   */
  public static void createXML(World world)
      throws ParserConfigurationException, TransformerException, SAXException, IOException {

    Document xml = getDocument(null);
    Element money, gameTime, game;
    Element store, quantity, type, item;

    game = xml.createElement("Game");
    xml.appendChild(game);

    // Money
    money = xml.createElement("money");
    money.setTextContent(String.valueOf(world.getMoney()));
    game.appendChild(money);

    gameTime = xml.createElement("gametime");
    gameTime.setTextContent(String.valueOf(System.currentTimeMillis()));
    game.appendChild(gameTime);

    // You'll need to loop through this eventually
    store = xml.createElement("store");
    game.appendChild(store);
    Attr attr = xml.createAttribute("id");
    attr.setValue("1");
    store.setAttributeNode(attr);

    // Loop through the items
    for (Item i : world.getStore().getItems()) {
      item = xml.createElement("item");
      store.appendChild(item);
      attr = xml.createAttribute("id");
      attr.setValue("" + i.getType());
      item.setAttributeNode(attr);

      quantity = xml.createElement("quantity");
      quantity.setTextContent("" + i.getAmount());
      item.appendChild(quantity);
    }
    // Write the content into xml file
    writeXML(xml, new File(FILE_NAME));
  }
コード例 #2
0
  @Override
  public Collection<Action<?>> decide(
      Car self, World world, Game game, Move move, Navigator navigator) {
    if (happened) {
      return Collections.emptySet();
    }

    if (initializing) {
      double speed = StrictMath.hypot(self.getSpeedX(), self.getSpeedY());

      if (speed > 1.0D) {
        log.print("Init\n");
        initializing = false;
      } else {
        return Collections.emptySet();
      }
    }

    Collection<Car> cars = cars(self, world, game).collect(Collectors.toList());

    log.printf(
        "Sides #%d: %s%n",
        tick, cars.stream().map(e -> new Point(e.getX(), e.getY())).collect(Collectors.toList()));

    if (cars.size() >= 2 && tick == 0) {
      tick = world.getTick();
    } else if (cars.size() >= 2 && world.getTick() - tick > THRESHOLD) {
      log.printf(
          "Let idiots pass: %s%n",
          cars.stream().map(e -> new Point(e.getX(), e.getY())).collect(Collectors.toList()));
      happened = true;
      return Collections.singleton(new SpeedAction(Priority.HIGH, 0.1D));
    } else if (cars.size() < 2) {
      tick = world.getTick();
    }

    return Collections.emptySet();
  }
コード例 #3
0
 private Stream<Car> cars(Car self, World world, Game game) {
   return Arrays.stream(world.getCars())
       .filter(e -> !e.isTeammate())
       .filter(e -> new Rectangle(self, game.getTrackTileSize()).hasCollision(new Rectangle(e)));
 }