Пример #1
0
  /**
   * Converts the given CLUE game data XML Tags.
   *
   * @param gameData CLUE game data to convert
   * @return the root XML Tag
   */
  private static XMLTag convertToXML(ClueGameData gameData) {
    XMLTag rootTag = new XMLTag();
    XMLTag clueGameTag = new XMLTag("ClueGame");

    XMLTag roomsTag = new XMLTag("Rooms");
    roomsTag.addAttribute(
        "displayRoomPicturesOnBoard", Boolean.toString(gameData.getDisplayRoomPicturesOnBoard()));
    for (Room room : gameData.getRooms()) {
      XMLTag roomTag = new XMLTag("Room");
      roomTag.addAttribute("id", Integer.toString(room.getId()));
      roomTag.addAttribute("name", room.getName());
      String pictureName = room.getPictureName();
      if (pictureName != null && !pictureName.isEmpty())
        roomTag.addAttribute("pictureName", pictureName);
      if (room.getTransparentPictureColor() != null)
        roomTag.addAttribute(
            "transparentPictureColor",
            Integer.toString(room.getTransparentPictureColor().getRGB()));
      roomsTag.addSubTag(roomTag);
    }
    clueGameTag.addSubTag(roomsTag);

    XMLTag suspectsTag = new XMLTag("Suspects");
    for (Suspect suspect : gameData.getSuspects()) {
      XMLTag suspectTag = new XMLTag("Suspect");
      suspectTag.addAttribute("id", Integer.toString(suspect.getId()));
      suspectTag.addAttribute("name", suspect.getName());
      suspectTag.addAttribute("color", Integer.toString(suspect.getColor().getRGB()));
      String pictureName = suspect.getPictureName();
      if (pictureName != null && !pictureName.isEmpty())
        suspectTag.addAttribute("pictureName", pictureName);
      suspectsTag.addSubTag(suspectTag);
    }
    clueGameTag.addSubTag(suspectsTag);

    XMLTag weaponsTag = new XMLTag("Weapons");
    for (Weapon weapon : gameData.getWeapons()) {
      XMLTag weaponTag = new XMLTag("Weapon");
      weaponTag.addAttribute("id", Integer.toString(weapon.getId()));
      weaponTag.addAttribute("name", weapon.getName());
      String pictureName = weapon.getPictureName();
      if (pictureName != null && !pictureName.isEmpty())
        weaponTag.addAttribute("pictureName", pictureName);
      weaponsTag.addSubTag(weaponTag);
    }
    clueGameTag.addSubTag(weaponsTag);

    Board board = gameData.getBoard();
    XMLTag boardTag = new XMLTag("ClueBoard");
    boardTag.addAttribute("height", Integer.toString(board.getHeight()));
    boardTag.addAttribute("width", Integer.toString(board.getWidth()));
    if (gameData.getBackgroundImageFilename() != null)
      boardTag.addAttribute("backgroundImageFilename", gameData.getBackgroundImageFilename());
    boardTag.addAttribute(
        "backgroundColor", Integer.toString(gameData.getBackgroundColor().getRGB()));
    for (int row = 0; row < board.getHeight(); row++) {
      for (int col = 0; col < board.getWidth(); col++) {
        DisplayTile tile = board.getTile(row, col);
        if (tile.isFreeTile()) continue;

        XMLTag tileTag = new XMLTag("Tile");
        tileTag.addAttribute("row", Integer.toString(row));
        tileTag.addAttribute("col", Integer.toString(col));

        if (tile.isRoomTile())
          tileTag.addAttribute("roomId", Integer.toString(tile.getRoom().getId()));
        for (DisplayTile.Direction direction : DisplayTile.Direction.values()) {
          if (tile.hasDoor(direction)) {
            XMLTag doorTag = new XMLTag("Door");
            doorTag.addContent(direction.toString());
            tileTag.addSubTag(doorTag);
          }
        }

        if (tile.isRemovedTile()) tileTag.addAttribute("removed", "true");
        else if (tile.isPassage()) {
          XMLTag passageConnectionTag = new XMLTag("PassageConnection");
          Board.TilePosition tilePosition = board.getTilePosition(tile.getPassageConnection());
          passageConnectionTag.addAttribute("row", Integer.toString(tilePosition.row));
          passageConnectionTag.addAttribute("col", Integer.toString(tilePosition.col));
          tileTag.addSubTag(passageConnectionTag);
        } else if (tile.hasSuspect()) {
          tileTag.addAttribute("suspectId", Integer.toString(tile.getSuspect().getId()));
          tileTag.addAttribute("suspectDirection", tile.getSuspectDirection().toString());
        }

        boardTag.addSubTag(tileTag);
      }
    }
    clueGameTag.addSubTag(boardTag);

    XMLTag storyTag = new XMLTag("Story");
    storyTag.addContent(gameData.getStory());
    clueGameTag.addSubTag(storyTag);

    rootTag.addSubTag(clueGameTag);
    return rootTag;
  }
Пример #2
0
  /**
   * Converts the given XMLTag to CLUE game data.
   *
   * @param rootTag root Tag of the XML to begin parsing with
   * @return the generated CLUE game data
   * @throws XMLException
   */
  private static ClueGameData readFromXMLRootTag(XMLTag rootTag) throws XMLException {
    ClueGameData gameData = new ClueGameData();
    char passageLetter = 'A';

    XMLTag clueGameTag = rootTag.getSubTag("ClueGame");

    XMLTag roomsTag = clueGameTag.getSubTag("Rooms");
    gameData.setDisplayRoomPicturesOnBoard(
        Boolean.parseBoolean(roomsTag.getAttributeValue("displayRoomPicturesOnBoard")));
    List<Room> rooms = new ArrayList<Room>();
    for (XMLTag roomTag : roomsTag.getSubTags("Room")) {
      String roomName = roomTag.getAttributeValue("name");
      if (roomName == null)
        throw new XMLException(
            "Tag does not have a name attribute: " + XMLWriter.writeXMLToString(roomTag));
      Room room = new Room(parseAttributeAsInt(roomTag, "id"), roomName);

      String pictureName = roomTag.getAttributeValue("pictureName");
      if (pictureName != null) room.setPictureName(pictureName);

      if (roomTag.getAttributeValue("transparentPictureColor") != null)
        room.setTransparentPictureColor(
            new Color(parseAttributeAsInt(roomTag, "transparentPictureColor")));

      rooms.add(room);
    }
    gameData.setRooms(rooms);

    XMLTag suspectsTag = clueGameTag.getSubTag("Suspects");
    List<Suspect> suspects = new ArrayList<Suspect>();
    for (XMLTag suspectTag : suspectsTag.getSubTags("Suspect")) {
      String suspectName = suspectTag.getAttributeValue("name");
      if (suspectName == null)
        throw new XMLException(
            "Tag does not have a name attribute: " + XMLWriter.writeXMLToString(suspectTag));
      Color color = new Color(parseAttributeAsInt(suspectTag, "color"));
      Suspect suspect = new Suspect(parseAttributeAsInt(suspectTag, "id"), suspectName, color);

      String pictureName = suspectTag.getAttributeValue("pictureName");
      if (pictureName != null) suspect.setPictureName(pictureName);

      suspects.add(suspect);
    }
    gameData.setSuspects(suspects);

    XMLTag weaponsTag = clueGameTag.getSubTag("Weapons");
    List<Weapon> weapons = new ArrayList<Weapon>();
    for (XMLTag weaponTag : weaponsTag.getSubTags("Weapon")) {
      String weaponName = weaponTag.getAttributeValue("name");
      if (weaponName == null)
        throw new XMLException(
            "Tag does not have a name attribute: " + XMLWriter.writeXMLToString(weaponTag));
      Weapon weapon = new Weapon(parseAttributeAsInt(weaponTag, "id"), weaponName);

      String pictureName = weaponTag.getAttributeValue("pictureName");
      if (pictureName != null) weapon.setPictureName(pictureName);

      weapons.add(weapon);
    }
    gameData.setWeapons(weapons);

    XMLTag boardTag = clueGameTag.getSubTag("ClueBoard");
    Board board =
        new Board(parseAttributeAsInt(boardTag, "height"), parseAttributeAsInt(boardTag, "width"));
    gameData.setBackgroundImageFilename(boardTag.getAttributeValue("backgroundImageFilename"));
    gameData.setBackgroundColor(new Color(parseAttributeAsInt(boardTag, "backgroundColor")));
    for (XMLTag tileTag : boardTag.getSubTags("Tile")) {
      DisplayTile tile =
          board.getTile(parseAttributeAsInt(tileTag, "row"), parseAttributeAsInt(tileTag, "col"));
      if (tileTag.containsAttribute("roomId")) {
        int roomId = parseAttributeAsInt(tileTag, "roomId");
        Room room = null;
        for (Room currentRoom : rooms) {
          if (currentRoom.getId() == roomId) {
            room = currentRoom;
            break;
          }
        }
        if (room == null)
          throw new XMLException(
              "No matching room for Tile roomId: " + XMLWriter.writeXMLToString(tileTag));
        tile.setRoom(room);
      }

      for (XMLTag doorTag : tileTag.getSubTags("Door")) {
        try {
          tile.addDoor(DisplayTile.Direction.valueOf(doorTag.getContent()));
        } catch (IllegalArgumentException ex) {
          throw new XMLException(
              "Door Tag does not contain a valid direction: "
                  + XMLWriter.writeXMLToString(doorTag));
        }
      }

      if (tileTag.containsAttribute("removed")
          && tileTag.getAttributeValue("removed").equals("true")) tile.setRemoved();

      for (XMLTag passageConnectionTag : tileTag.getSubTags("PassageConnection")) {
        if (tile.isPassage()) continue;

        DisplayTile connectedTile =
            board.getTile(
                parseAttributeAsInt(passageConnectionTag, "row"),
                parseAttributeAsInt(passageConnectionTag, "col"));
        tile.setPassageConnection(connectedTile, passageLetter);
        connectedTile.setPassageConnection(tile, passageLetter);
        passageLetter++;
      }

      if (tileTag.containsAttribute("suspectId")) {
        int suspectId = parseAttributeAsInt(tileTag, "suspectId");
        Suspect suspect = null;
        for (Suspect currentSuspect : suspects) {
          if (currentSuspect.getId() == suspectId) {
            suspect = currentSuspect;
            break;
          }
        }
        if (suspect == null)
          throw new XMLException(
              "No matching suspect for Tile suspectId: " + XMLWriter.writeXMLToString(tileTag));

        DisplayTile.Direction direction =
            DisplayTile.Direction.valueOf(tileTag.getAttributeValue("suspectDirection"));
        tile.addSuspect(suspect, direction);
      }
    }
    gameData.setBoard(board);

    gameData.setStory(clueGameTag.getSubTag("Story").getContent());
    return gameData;
  }