Advertisement
atanasovetr

StarMines

Apr 1st, 2021
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. public class Table {
  2.     private int d1;
  3.     private int d2;
  4.     private double[][] table;
  5.  
  6.     public Table(int d1, int d2){
  7.         this.d1 = d1;
  8.         this.d2 = d2;
  9.         this.table = new double[this.d1][this.d2];
  10.         fillingTable();
  11.     }
  12.  
  13.     private void fillingTable() {
  14.         for (int i = 0; i < this.table.length; i++) {
  15.             for (int j = 0; j < this.table[i].length; j++) {
  16.                 this.table[i][j] = 100;
  17.             }
  18.         }
  19.     }
  20.  
  21.     public void printTable(){
  22.         for (int i = 0; i < this.table.length; i++) {
  23.             for (int j = 0; j < this.table[i].length; j++) {
  24.                 System.out.printf("%.2f ",this.table[i][j] );
  25.             }
  26.             System.out.println();
  27.         }
  28.     }
  29.  
  30.     public void attackingCell(int row, int column, double damage){
  31.         row -= 1;
  32.         column -= 1;
  33.         double neighDamage = 0;
  34.         if(damage > 1 && damage < 30){
  35.             neighDamage = 5;
  36.         }
  37.         else if(damage <= 70){
  38.             neighDamage = 10;
  39.         }
  40.         else {
  41.             neighDamage = damage * 0.2;
  42.         }
  43.  
  44.         if(row > this.d1 || column > this.d2 || row < 0 || column < 0 ){
  45.             System.out.println("Invalid cell");
  46.         }
  47.         else {
  48.             this.table[row][column] -= damage;
  49.  
  50.             for (int i = -1; i <= 1; i++) {
  51.                 try {
  52.                     if(this.table[row - 1][column + i] - neighDamage < 0){
  53.                         this.table[row - 1][column + i] = 0;
  54.                     }
  55.                     else {
  56.                         this.table[row - 1][column + i] -= neighDamage;
  57.                     }
  58.                 } catch (Exception ignored) {
  59.                 }
  60.             }
  61.             try {
  62.                 if(this.table[row][column - 1] - neighDamage < 0) {
  63.                     this.table[row][column - 1] = 0;
  64.                 }
  65.                 else{
  66.                     this.table[row][column - 1] -= neighDamage;
  67.                 }
  68.             } catch (Exception ignored) {
  69.             }
  70.  
  71.             try {
  72.                 if(this.table[row][column + 1] - neighDamage < 0){
  73.                     this.table[row][column + 1] = 0;
  74.                 }
  75.                 else{
  76.                     this.table[row][column + 1] -= neighDamage;
  77.                 }
  78.             } catch (Exception ignored) {
  79.             }
  80.  
  81.  
  82.             for (int i = -1; i <= 1; i++) {
  83.                 try {
  84.                     if(this.table[row + 1][column + i] - neighDamage < 0) {
  85.                         this.table[row + 1][column + i] = 0;
  86.                     }
  87.                     else{
  88.                         this.table[row + 1][column + i] -= neighDamage;
  89.                     }
  90.                 } catch (Exception ignored) {
  91.                 }
  92.  
  93.             }
  94.         }
  95.  
  96.     }
  97. }
  98.  
  99. ----------------------------------------------------------------------
  100.  
  101. import java.util.Scanner;
  102.  
  103. public class StarMines {
  104.     public static void main(String[] args) {
  105.         Scanner scan = new Scanner(System.in);
  106.  
  107.         String[] sizes = scan.nextLine().split(",");
  108.         Table t1 = new Table(Integer.parseInt(sizes[0]), Integer.parseInt(sizes[1]));
  109.  
  110.         while(true){
  111.             String command = scan.nextLine();
  112.             if(command.equals("GAME OVER")){
  113.                 break;
  114.             }
  115.  
  116.             String takeDamage[]= command.split(",");
  117.             t1.attackingCell(Integer.parseInt(takeDamage[0]),
  118.                              Integer.parseInt(takeDamage[1]),
  119.                              Integer.parseInt(takeDamage[2]));
  120.         }
  121.         t1.printTable();
  122.     }
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement