Пример #1
0
 public void setAlleSchiffe() {
   List<Schiff> list =
       Arrays.asList(
           new Schlachtschiff(0, 0, 0),
           new Zerstoerer(0, 0, 0),
           new Zerstoerer(0, 0, 0),
           new Kreuzer(0, 0, 0),
           new Kreuzer(0, 0, 0),
           new Kreuzer(0, 0, 0),
           new UBoot(0, 0, 0),
           new UBoot(0, 0, 0),
           new UBoot(0, 0, 0),
           new UBoot(0, 0, 0));
   for (Schiff schiff : list) {
     setShiffAtRandomPosition(schiff);
   }
 }
Пример #2
0
  public void setShiffAtRandomPosition(Schiff schiff) {
    Random rand = new Random();
    int direction = rand.nextInt(2);
    int maxWidth = this.width;
    int maxHeight = this.height;
    if (direction == 0) {
      maxWidth -= (schiff.getPunkte().size() - 1);
    } else {
      maxHeight -= (schiff.getPunkte().size() - 1);
    }
    int col = rand.nextInt(maxWidth);
    int row = rand.nextInt(maxHeight);

    SchiffName typ = schiff.getName();
    switch (typ) {
      case KREUZER:
        schiff = new Kreuzer(row, col, direction);
        break;
      case SCHLACHTSCHIFF:
        schiff = new Schlachtschiff(row, col, direction);
        break;
      case UBOOT:
        schiff = new UBoot(row, col, direction);
        break;
      case ZERSTOERER:
        schiff = new Zerstoerer(row, col, direction);
        break;

      default:
        schiff = null;
        break;
    }
    boolean kolidiert = false;
    for (Schiff s : schiffListe) {
      if (s.kolidiertMitSchiff(schiff)) {
        kolidiert = true;
      }
    }
    if (!kolidiert) {
      addSchiff(schiff);
    } else {
      setShiffAtRandomPosition(schiff);
    }
  }