Package settlers.game.map

Source Code of settlers.game.map.DefaultMap

package settlers.game.map;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import settlers.game.DefaultGameEnums.TileTypes;
import settlers.game.data.MapCoord;

public class DefaultMap extends SettlerMap {

  private boolean[] isLand;
  private final List<TileTypes> tileMap;

  /**
   * Generates a map according to the original Catan rules
   */
  public DefaultMap() {
    height = 5;
    width = 5;

    isOriginDown = false;

    chance = new int[width][height];

    isLand = new boolean[] { false, true, true, true, false, true, true,
        true, true, false, true, true, true, true, true, true, true,
        true, true, false, false, true, true, true, false, };

    NUM_OF_CHANCE_TOKENS = new int[][] {
        { 2, 3, 4, 5, 6, 8, 9, 10, 11, 12 },// chance tiles
        { 1, 2, 2, 2, 2, 2, 2, 2, 2, 1 } // number of them
    };

    // make and fill the map of tiles
    tileMap = new ArrayList<TileTypes>(makeTileMap(requiredResources(),
        isLand));

    Random generator = new Random();

    int[][] chanceTokens = { { 2, 3, 4, 5, 6, 8, 9, 10, 11, 12 },// chance
                                    // tiles
        { 1, 2, 2, 2, 2, 2, 2, 2, 2, 1 },// number of them
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };// min number allowed

    int current;

    // TODO make more efficient
    for (int x = 0; x < chance.length; x++) {
      for (int y = 0; y < chance[0].length; y++) {
        if (isLand[x * width + y]
            && !Arrays.equals(chanceTokens[1], chanceTokens[2])) {
          while (true) {
            if (tileMap.get(x * width + y) == TileTypes.DESERT) {
              break; // do not build on dessert
            }
            current = generator.nextInt(chanceTokens[0].length);
            if (chanceTokens[1][current] > 0) {
              chance[x][y] = chanceTokens[0][current];
              chanceTokens[1][current]--;
              break;
            }
          }
        }
      }
    }
  }

  private Map<TileTypes, Integer> requiredResources() {

    // the type and number of tiles to be randomly placed on the map
    Map<TileTypes, Integer> randomTiles = new HashMap<TileTypes, Integer>();
    randomTiles.put(TileTypes.LUMBER, 4);
    randomTiles.put(TileTypes.WHEAT, 4);
    randomTiles.put(TileTypes.WOOL, 4);
    randomTiles.put(TileTypes.BRICK, 3);
    randomTiles.put(TileTypes.ORE, 3);
    randomTiles.put(TileTypes.DESERT, 1);
    return randomTiles;
  }

  private static List<TileTypes> makeTileMap(
      Map<TileTypes, Integer> randomTiles, boolean[] isLand) {

    Random generator = new Random();
    TileTypes currentResource;

    List<TileTypes> tiles = new ArrayList<TileTypes>();

    for (int x = 0; x < isLand.length; x++) {
      if (isLand[x]) {
        while (true) {

          /*
           * randomly pick one of the remaining resources available in
           * randomTiles
           */
          currentResource = (TileTypes) randomTiles.keySet()
              .toArray()[generator.nextInt(randomTiles.size())];

          tiles.add(currentResource);

          /*
           * maintain that only Resources that can still be placed are
           * in randomTiles
           */
          if (randomTiles.get(currentResource) > 1)
            randomTiles.put(currentResource, randomTiles
                .get(currentResource) - 1);
          else
            randomTiles.remove(currentResource);
          break;
        }
      } else {
        tiles.add(TileTypes.NONE);
      }
    }
    return tiles;
  }

  public TileTypes getTileAt(MapCoord coord) {
    return tileMap.get(coord.getX() * width + coord.getY());
  }

  public TileTypes getTileAt(int x, int y) {
    return tileMap.get(x * width + y);
  }

  public boolean isLand(int x, int y) {
    return isLand[x * width + y];
  }

  public String tileMapAsString() {
    return tileMap.toString();
  }
}
TOP

Related Classes of settlers.game.map.DefaultMap

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.