示例#1
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);
    }
  }
示例#2
0
  public void markiereZerstoertesSchiff(Schiff schiff) {
    for (Punkt p : schiff.getBereich()) {
      try {
        Feld feld;
        feld = map[p.getRow()][p.getCol()];

        if (!feld.isIstSchiff()) {
          feld.setIstBeschossen(true);
        } else {
          feld.setIstZerstoert(true);
        }
      } catch (IndexOutOfBoundsException e) {
      }
    }
  }
示例#3
0
 public void shoot(int row, int col) throws IndexOutOfBoundsException {
   Feld feld;
   feld = map[row][col];
   if (feld.getIstBeschossen()) {
     throw new SchiffeVersenkenException("Angegebene Position ist schon beschossen.");
   }
   feld.setIstBeschossen(true);
   feld.setIstSchiff(false);
   boolean continueGame = false;
   for (Schiff schiff : schiffListe) {
     if (schiff.istGetroffen(row, col)) {
       feld.setIstSchiff(true);
     }
     if (schiff.isIstZerstoert()) {
       markiereZerstoertesSchiff(schiff);
       continueGame = continueGame && false;
     } else {
       continueGame = true;
     }
   }
   if (!continueGame) {
     Main.setDoRun(false);
   }
 }