예제 #1
0
  private void preprocessMap() {
    // werkt enkel voor de 'entrance'-map
    // -> 20 clusters met vaste hoogte en breedte, behalve onderste rij,
    // clusters zijn genummerd van links naar rechts en boven naar onder (=>
    // eerst op 1 lijn)
    // de openingen worden wel bepaald door het programma
    int clusterHeight = 10;
    int clusterWidth = 15;
    int coordinate;
    Tile current;
    Node tmp;
    String tileType;
    ArrayList<Integer> processedOpenings = new ArrayList<Integer>();
    ArrayList<Integer> openings = new ArrayList<Integer>();

    allNodes.add(cluster1Nodes);
    allNodes.add(cluster2Nodes);
    allNodes.add(cluster3Nodes);
    allNodes.add(cluster4Nodes);
    allNodes.add(cluster5Nodes);
    allNodes.add(cluster6Nodes);
    allNodes.add(cluster7Nodes);
    allNodes.add(cluster8Nodes);
    allNodes.add(cluster9Nodes);
    allNodes.add(cluster10Nodes);
    allNodes.add(cluster11Nodes);
    allNodes.add(cluster12Nodes);
    allNodes.add(cluster13Nodes);
    allNodes.add(cluster14Nodes);
    allNodes.add(cluster15Nodes);
    allNodes.add(cluster16Nodes);
    allNodes.add(cluster17Nodes);
    allNodes.add(cluster18Nodes);
    allNodes.add(cluster19Nodes);
    allNodes.add(cluster20Nodes);

    // hulpvariabelen
    int hor_begin = 0, hor_end = 0, ver_begin = 0, ver_end = 0, ver_x = 0, hor_y = 0;

    for (int j = 1; j < 16; j++) {
      switch (j) {
        case 1:
          hor_begin = 0;
          ver_begin = 0;
          hor_end = hor_begin + clusterWidth - 1;
          ver_end = ver_begin + clusterHeight;
          ver_x = 14;
          hor_y = 9;
          break;
        case 2:
          hor_begin = 15;
          ver_begin = 0;
          hor_end = hor_begin + clusterWidth - 1;
          ver_end = ver_begin + clusterHeight;
          ver_x = 29;
          hor_y = 9;
          break;
        case 3:
          hor_begin = 30;
          ver_begin = 0;
          hor_end = hor_begin + clusterWidth - 1;
          ver_end = ver_begin + clusterHeight;
          ver_x = 44;
          hor_y = 9;
          break;
        case 4:
          hor_begin = 45;
          ver_begin = 0;
          hor_end = hor_begin + clusterWidth - 1;
          ver_end = ver_begin + clusterHeight;
          ver_x = 59;
          hor_y = 9;
          break;
        case 5:
          hor_begin = 60;
          ver_begin = 0;
          hor_end = hor_begin + clusterWidth;
          ver_end = ver_begin;
          ver_x = 0;
          hor_y = 9;
          break;
        case 6:
          hor_begin = 0;
          ver_begin = 10;
          hor_end = hor_begin + clusterWidth - 1;
          ver_end = ver_begin + clusterHeight;
          ver_x = 14;
          hor_y = 19;
          break;
        case 7:
          hor_begin = 15;
          ver_begin = 10;
          hor_end = hor_begin + clusterWidth - 1;
          ver_end = ver_begin + clusterHeight;
          ver_x = 29;
          hor_y = 19;
          break;
        case 8:
          hor_begin = 30;
          ver_begin = 10;
          hor_end = hor_begin + clusterWidth - 1;
          ver_end = ver_begin + clusterHeight;
          ver_x = 44;
          hor_y = 19;
          break;
        case 9:
          hor_begin = 45;
          ver_begin = 10;
          hor_end = hor_begin + clusterWidth - 1;
          ver_end = ver_begin + clusterHeight;
          ver_x = 59;
          hor_y = 19;
          break;
        case 10:
          hor_begin = 60;
          ver_begin = 0;
          hor_end = hor_begin + clusterWidth;
          ver_end = ver_begin;
          ver_x = 0;
          hor_y = 19;
          break;
        case 11:
          hor_begin = 0;
          ver_begin = 20;
          hor_end = hor_begin + clusterWidth - 1;
          ver_end = ver_begin + clusterHeight;
          ver_x = 14;
          hor_y = 29;
          break;
        case 12:
          hor_begin = 15;
          ver_begin = 20;
          hor_end = hor_begin + clusterWidth - 1;
          ver_end = ver_begin + clusterHeight;
          ver_x = 29;
          hor_y = 29;
          break;
        case 13:
          hor_begin = 30;
          ver_begin = 20;
          hor_end = hor_begin + clusterWidth - 1;
          ver_end = ver_begin + clusterHeight;
          ver_x = 44;
          hor_y = 29;
          break;
        case 14:
          hor_begin = 45;
          ver_begin = 20;
          hor_end = hor_begin + clusterWidth - 1;
          ver_end = ver_begin + clusterHeight;
          ver_x = 59;
          hor_y = 29;
          break;
        case 15:
          hor_begin = 60;
          ver_begin = 0;
          hor_end = hor_begin + clusterWidth;
          ver_end = ver_begin;
          ver_x = 0;
          hor_y = 29;
          break;
      }

      for (int i = ver_begin; i < ver_end; i++) {
        coordinate = returnCoorFromXY(ver_x, i);
        current = getTile(coordinate);
        tileType = current.getIdentifier();
        if (tileType.equals(Tile.GRASS_TILE)) {
          openings.add(coordinate);
        }
      }
      if (openings.size() > 0) processedOpenings = processOpenings(openings);

      for (int i = 0; i < processedOpenings.size(); i++) {
        tmp = new Node(j, j + 1, processedOpenings.get(i));
        allNodes.get(j - 1).add(tmp);
        allNodes.get(j).add(tmp);
      }

      openings.clear();
      processedOpenings.clear();
      for (int i = hor_begin; i < hor_end; i++) {
        coordinate = returnCoorFromXY(i, hor_y);
        current = getTile(coordinate);
        tileType = current.getIdentifier();
        if (tileType.equals(Tile.GRASS_TILE)) {
          openings.add(coordinate);
        }
      }
      if (openings.size() > 0) processedOpenings = processOpenings(openings);

      for (int i = 0; i < processedOpenings.size(); i++) {
        tmp = new Node(j, j + 5, processedOpenings.get(i));
        allNodes.get(j - 1).add(tmp);
        allNodes.get(j + 4).add(tmp);
      }

      openings.clear();
      processedOpenings.clear();
    }
    // the last 4 clusters are handled seperatly for convenience
    for (int j = 0; j < 4; j++) {
      for (int i = 30; i < 42; i++) {
        coordinate = returnCoorFromXY(14 + 15 * j, i);
        current = getTile(coordinate);
        tileType = current.getIdentifier();
        if (tileType.equals(Tile.GRASS_TILE)) {
          openings.add(coordinate);
        }
      }
      if (openings.size() > 0) processedOpenings = processOpenings(openings);

      for (int i = 0; i < processedOpenings.size(); i++) {
        tmp = new Node(16 + j, 17 + j, processedOpenings.get(i));
        allNodes.get(15 + j).add(tmp);
        allNodes.get(16 + j).add(tmp);
      }

      openings.clear();
      processedOpenings.clear();
    }
  }
예제 #2
0
  private void readFromFile(Tile[] tbs, String file) {

    int height = squaresY;
    int width = squaresX;
    int countSprites = 0;

    BufferedReader reader = null;
    int i = 0;
    int lines = 0;
    try {
      reader = new BufferedReader(new FileReader(file));
      String line = reader.readLine();
      lines++;

      while (lines <= height) {
        String[] tokens = line.split("\t");
        if (tokens.length != width) {
          throw new IOException();
        }

        for (String x : tokens) {

          Tile tile = mapper.get(x);

          // if the tile did not map, it should be an int value (and
          // as such a coin)
          if (tile == null) {
            int value = Integer.parseInt(x);
            this.coinLocations.put(i, value);
            tile = mapper.get(Tile.COIN_TILE);
          } else if (tile.getIdentifier().equals(Tile.BLUE_CAMP_TILE)) {
            this.blueCamp = i;
          } else if (tile.getIdentifier().equals(Tile.RED_CAMP_TILE)) {
            this.redCamp = i;
          } else if (tile.getIdentifier().equals(Tile.BLUE_SPRITE_TILE)) {
            countSprites++;
            Agent a = null;
            if (this.blueTeamPathStrategy == AGENT_STRATEGY.RANDOM_WALKER) {
              a = new RandomWalker("" + countSprites, false, this, pathCredits);
            } else if (this.blueTeamPathStrategy == AGENT_STRATEGY.ASTARWALKER) {
              a = new AStarWalker("" + countSprites, false, this, pathCredits);
            }
            // ADD OTHER STRATEGIES FOR BLUE HERE
            else if (this.blueTeamPathStrategy == AGENT_STRATEGY.HIERARCHICALWALKER) {
              a = new HierarchicalWalker("" + countSprites, false, this, pathCredits);
            }

            // *****
            else {
              System.err.println("Illegal walker type");
              System.exit(-1);
            }
            a.setLocation(i);
            this.blueTeam.add(a);
            this.allSprites.add(a);
          } else if (tile.getIdentifier().equals(Tile.RED_SPRITE_TILE)) {
            countSprites++;
            Agent a = null;
            if (this.redTeamPathStrategy == AGENT_STRATEGY.RANDOM_WALKER) {
              a = new RandomWalker("" + countSprites, true, this, pathCredits);
            } else if (this.redTeamPathStrategy == AGENT_STRATEGY.ASTARWALKER) {
              a = new AStarWalker("" + countSprites, true, this, pathCredits);
            }
            // add other strategies for red team here

            // *****
            else {
              System.err.println("Illegal walker type");
              System.exit(-1);
            }
            this.redTeam.add(a);
            a.setLocation(i);
            this.allSprites.add(a);
          }
          tbs[i] = tile;
          i++;
        }
        line = reader.readLine();
        lines++;
      }

    } catch (IOException e) {
      System.err.println("Error reading  map file");
      e.printStackTrace();
      System.exit(-1);
    } finally {
      try {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }