예제 #1
0
 private boolean getRestTankCells(Tank t, Random rand_num) {
   while (t.getCells().size() != 4) {
     int next_row = t.getCells().getLast().getCurrent_row();
     int next_col = t.getCells().getLast().getCurrent_column();
     /*chose direction, 0 = north, 1 = east, 2 south, 3 =west */
     int next_cell = rand_num.nextInt(4);
     if (next_cell == 0) { // north
       /*check if the next cell is occupied by another tank or out of boundary*/
       int north = next_row - 1;
       if ((north >= 0 && north < number_row) && !(cells[north][next_col].getIsTank())) {
         t.addCells(cells[north][next_col]);
         cells[north][next_col].setIsTank(true);
       }
     } else if (next_cell == 1) {
       int east = next_col + 1;
       if ((east >= 0 && east < number_column) && !cells[next_row][east].getIsTank()) {
         t.addCells(cells[next_row][east]);
         cells[next_row][east].setIsTank(true);
       }
     } else if (next_cell == 2) {
       int south = next_row + 1;
       if ((south >= 0 && south < number_row) && !cells[south][next_col].getIsTank()) {
         t.addCells(cells[south][next_col]);
         cells[south][next_col].setIsTank(true);
       }
     } else if (next_cell == 3) {
       int west = next_col - 1;
       if ((west >= 0 && west < number_column) && !cells[next_row][west].getIsTank()) {
         t.addCells(cells[next_row][west]);
         cells[next_row][west].setIsTank(true);
       }
     }
   }
   if (t.getCells().size() == 4) {
     return true;
   } else {
     return false;
   }
 }
예제 #2
0
  public int fireCannons(Cell cell, Tank[] tanks) {
    if (!cell.getHasBeenAttacked()) { // he cell has not been attacked, check if it is tank
      cell.setHasBeenAttacked(true);
      if (cell.getIsTank()) { // it is part of tank
        cell.setTankHit(true);
        for (Tank t : tanks) {
          if (t.getCells().contains(cell)) {
            t.setNumber_undamaged_cell(t.getNumber_undamaged_cell() - 1);
          }
        }
        return 1; // hit
      } else {

        return -1; // miss
      }

    } else { // the cell has been attacked, next check if it is tank and
      if (cell.isTankHit()) {
        return 0; // hit, but do nothing
      } else {
        return -1;
      }
    }
  }