Beispiel #1
0
 @Override
 public void destroy() {
   String coordinates = battleField.getQuadrant(x, y);
   int separator = coordinates.indexOf("_");
   int v = Integer.parseInt(coordinates.substring(0, separator));
   int u = Integer.parseInt(coordinates.substring(separator + 1));
   battleField.updateQuadrant(v, u, "");
   x = -100;
   y = -100;
 }
Beispiel #2
0
 public int battleFieldScanner() {
   for (int fieldX = 0; fieldX < bf.getDimentionX(); fieldX++) {
     for (int fieldY = 0; fieldY < bf.getDimentionY(); fieldY++)
       if ((af.getQuadrantString(fieldX, fieldY).equals(enemyTankPosition()))
           || (bf.scanQuadrant2(fieldY, fieldX) instanceof Rock)) {
         return fieldX + 1;
       }
   }
   return 0;
 }
Beispiel #3
0
  public static void main(String[] args) {

    BattleField bf = new BattleField();

    String[][] field = bf.getBattleField();

    for (String[] str : field) {
      System.out.println(Arrays.toString(str));
    }
  }
Beispiel #4
0
  public void reappear() {
    try {
      Thread.sleep(1000);
    } catch (Exception e) {
      System.out.println("Exception");
    }
    x = new Random().nextInt(bf.getDimentionX()) * 64;
    y = new Random().nextInt(bf.getDimentionY()) * 64;

    af.repaint();
  }
Beispiel #5
0
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    int i = 0;
    Color cc;
    for (int v = 0; v < 9; v++) {
      for (int h = 0; h < 9; h++) {
        if (COLORDED_MODE) {
          if (i % 2 == 0) {
            cc = new Color(252, 241, 177);
          } else {
            cc = new Color(233, 243, 255);
          }
        } else {
          cc = new Color(180, 180, 180);
        }
        i++;
        g.setColor(cc);
        g.fillRect(h * 64, v * 64, 64, 64);
      }
    }

    for (int j = 0; j < battleField.getDimentionY(); j++) {
      for (int k = 0; k < battleField.getDimentionX(); k++) {
        if (battleField.scanQuadrant(j, k).equals("B")) {
          String coordinates = getQuadrantXY(j + 1, k + 1);
          int separator = coordinates.indexOf("_");
          int y = Integer.parseInt(coordinates.substring(0, separator));
          int x = Integer.parseInt(coordinates.substring(separator + 1));
          g.setColor(new Color(0, 0, 255));
          g.fillRect(x, y, 64, 64);
        }
      }
    }

    g.setColor(new Color(255, 0, 0));
    g.fillRect(tank.getX(), tank.getY(), 64, 64);

    g.setColor(new Color(0, 255, 0));
    if (tank.getDirection() == Direction.UP) {
      g.fillRect(tank.getX() + 20, tank.getY(), 24, 34);
    } else if (tank.getDirection() == Direction.DOWN) {
      g.fillRect(tank.getX() + 20, tank.getY() + 30, 24, 34);
    } else if (tank.getDirection() == Direction.LEFT) {
      g.fillRect(tank.getX(), tank.getY() + 20, 34, 24);
    } else {
      g.fillRect(tank.getX() + 30, tank.getY() + 20, 34, 24);
    }

    g.setColor(new Color(255, 255, 0));
    g.fillRect(bullet.getX(), bullet.getY(), 14, 14);
  }
Beispiel #6
0
  public ActionField() throws Exception {
    battleField = new BattleField();
    tank = new Tank(this, battleField);
    bullet = new Bullet(-100, -100, Direction.DOWN);

    JFrame frame = new JFrame("BATTLE FIELD, TANKs");
    frame.setLocation(750, 150);
    frame.setMinimumSize(new Dimension(battleField.getBF_WIDTH(), battleField.getBF_HEIGHT() + 22));
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(this);
    frame.pack();
    frame.setVisible(true);
  }
 public int fireDown() throws Exception {
   String str = getQuadrantXY(tank.getX(), tank.getY());
   int tankV = Integer.valueOf(str.substring(0, 1));
   int tankH = Integer.valueOf(str.substring(2, str.length()));
   tank.turn(Direction.DOWN);
   for (int idx = tankV; idx < battleField.getBattleField().length; idx++) {
     for (int j = tankH; j < tankH + 1; j++) {
       if (battleField.scanQuadrant(idx, j) == "B") {
         tank.fire();
       }
     }
   }
   return tankV;
 }
 @Test
 public void testGetXOffset() {
   BattleField bf = null;
   try {
     bf = new BattleField("es-in.txt");
   } catch (IllegalElementException e) {
     e.printStackTrace();
   } catch (IllegalPositionException e) {
     e.printStackTrace();
   }
   bf.setColumns(5);
   assertEquals("Entry in first if", 5 - (2 + 1), osa.getXOffset());
   osa.changeDirection();
   assertEquals("after changed direction", -2, osa.getXOffset());
   osa.changeDirection(); // reset back because is static
 }
Beispiel #9
0
 boolean checkRight(int checkX, int checkY) {
   for (; checkX < 9; checkX++) {
     if (bf.scanQuadrant2(checkY, checkX) instanceof Rock
         || af.getQuadrantString(checkX, checkY).equals(enemyTankPosition())) {
       return true;
     }
   }
   return false;
 }
Beispiel #10
0
  private boolean processInterception() {
    String currentCoordinate = getQuadrant(bullet.getX(), bullet.getY());

    int y = Integer.parseInt(currentCoordinate.substring(0, 1));
    int x = Integer.parseInt(currentCoordinate.substring(2, 3));

    boolean result = false;

    if (battleField.scanQuadrant(y, x) == " ") {
      result = false;
    }

    if (!battleField.scanQuadrant(y, x).trim().isEmpty()) {
      battleField.updateQuadrant(y, x, " ");
      result = true;
    }

    return result;
  }
 private boolean processInterception() {
   String str = getQuadrantXY(bullet.getX(), bullet.getY());
   int i = Integer.valueOf(str.substring(0, 1));
   int q = Integer.valueOf(str.substring(2, str.length()));
   if (i >= battleField.getBattleField().length - battleField.getBattleField().length
       && i < battleField.getBattleField().length
       && q >= battleField.getBattleField().length - battleField.getBattleField().length
       && q < battleField.getBattleField().length
       && battleField.scanQuadrant(i, q) == "B") {
     battleField.updateQuadrant(i, q, " ");
     return true;
   } else return false;
 }
  public void processFire(Bullet bullet) throws Exception {
    this.bullet = bullet;

    while ((bullet.getX() > -15 && bullet.getX() < battleField.getBF_WIDTH())
        && (bullet.getY() > -15 && bullet.getY() < battleField.getBF_HEIGHT())) {
      if (bullet.getDirection() == Direction.UP) {
        bullet.updateY(-step);
      } else if (bullet.getDirection() == Direction.DOWN) {
        bullet.updateY(step);
      } else if (bullet.getDirection() == Direction.LEFT) {
        bullet.updateX(-step);
      } else {
        bullet.updateX(step);
      }
      if (processInterception() == true) {
        bullet.destroy();
      }
      repaintFire();
    }
  }
  public ActionField() throws Exception {
    battleField = new BattleField();
    tank = new Tank(this, battleField);

    agressor =
        new Agressor(
            Integer.parseInt(position.substring(0, position.indexOf("_"))),
            Integer.parseInt(position.substring(position.lastIndexOf("_") + 1, position.length())),
            Direction.LEFT,
            this,
            battleField);
    bullet = new Bullet(-100, -100, Direction.STOP);
    JFrame frame = new JFrame("BATTLE FIELD, DAY 2");
    frame.setLocation(750, 150);
    frame.setMinimumSize(
        new Dimension(battleField.getBF_WIDTH() + 16, battleField.getBF_HEIGHT() + 39));
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(this);
    frame.pack();
    frame.setVisible(true);
  }
 public int fireLeft() throws Exception {
   String str = getQuadrantXY(tank.getX(), tank.getY());
   int tankV = Integer.valueOf(str.substring(0, 1));
   int tankH = Integer.valueOf(str.substring(2, str.length()));
   tank.turn(Direction.LEFT);
   for (int idx = tankV; idx < tankV + 1; idx++) {
     for (int j = tankH; j >= 0; j--) {
       if (battleField.scanQuadrant(idx, j) == "B") {
         tank.fire();
       }
     }
   }
   return tankH;
 }
  public int getXOffset() {

    return (BattleField.getColumns() - (this.x - 1));
  }
 public int getYOffset() {
   return (BattleField.getRows() - (this.y - 1));
 }